Created
March 28, 2013 11:00
-
-
Save hasham2/5262360 to your computer and use it in GitHub Desktop.
API sample for mobile app: Here the mobile app is sending a registration token to Rails app so that it can send it messages and push notifications and in events controller the mobile app is creating events on server so that it can be stored and analyzed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class DevicesController < ApplicationController | |
| before_filter :authenticate_user! | |
| def create | |
| @device = current_user.app.devices.new(registration_id: params[:registration_id]) | |
| if params[:platform] && params[:platform].upcase == 'IOS' | |
| @device.os = :ios | |
| end | |
| if @device.save | |
| respond_to do |format| | |
| format.json { render json: @device, status: :ok } | |
| end | |
| else | |
| if current_user.app.devices.where(registration_id: params[:registration_id]).exists? | |
| @device = current_user.app.devices.find_by(registration_id: params[:registration_id]) | |
| @device.update_attribute(:last_registered_at, Time.now) | |
| respond_to do |format| | |
| format.json { render json: @device, status: :ok } | |
| end | |
| else | |
| respond_to do |format| | |
| format.json { render json: @device.errors, status: :unprocessable_entity } | |
| end | |
| end | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class EventsController < ApplicationController | |
| before_filter :authenticate_user!, :load_event_type, :load_device | |
| def create | |
| raise ArgumentError if @device.blank? || @event_type.blank? | |
| @event = @device.events.new(parameters: params[:values]) | |
| @event.event_type = @event_type | |
| if @event.save | |
| respond_to do |format| | |
| format.json {render json: @event, status: :created} | |
| end | |
| else | |
| respond_to do |format| | |
| format.json {render json: @event.errors, status: :unprocessable_entity} | |
| end | |
| end | |
| rescue ArgumentError | |
| respond_to do |format| | |
| format.json {render json: {Error: 'Unknown Device ID or Event Type'}, status: :internal_server_error} | |
| end | |
| end | |
| def load_event_type | |
| if params && params[:event_type] | |
| @event_type = current_user.app.event_types.where(name: params[:event_type].downcase.to_sym).first | |
| end | |
| end | |
| def load_device | |
| if params && params[:device_id] | |
| @device = current_user.app.devices.find(params[:device_id]) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment