Skip to content

Instantly share code, notes, and snippets.

@ozgun
Created January 16, 2014 08:22
Show Gist options
  • Select an option

  • Save ozgun/8451496 to your computer and use it in GitHub Desktop.

Select an option

Save ozgun/8451496 to your computer and use it in GitHub Desktop.
json response example (API)
def create
respond_to do |format|
resp = { }
resp_status = :unprocessable_entity # 422
if (user = OtpAuth::UserAuthenticator.authenticate(params[:email].to_s, params[:password].to_s))
if (profile = OtpAuth::Profile.first_or_create(user_id: user.id))
device = profile.devices.build(push_service_type_id: OtpAuth.get_service_provider_id(params[:push_service_type].to_s.upcase),
registration_token: params[:registration_token].to_s)
if device.save
resp[:user_identifier] = user.get_or_create_user_identifier
resp_status = :created
else
resp[:errors] = device.errors.full_messages.to_sentence
end
else
resp[:errors] = "Unable to find or create profile."
end
else
resp_status = :unauthorized
resp[:errors] = "Authentication failed."
end
format.json { render json: resp.to_json, status: resp_status }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment