Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails as a Backend for Mobile Applications

Rails as a Backend for Mobile Applications

Whether sharing or backing up custom data between users and devices or connecting to an existing web service, mobile apps commonly rely on web-based backends to extend their functionality. In this talk, we'll delve into the features Rails provides to build a RESTful API specifically for mobile apps, present some common techniques and best-practices, and point out some areas of caution.

Mike Bradford

May 23, 2012
Tweet

Other Decks in Programming

Transcript

  1. RAILS AS A BACKEND FOR MOBILE APPLICATIONS Mike Bradford Lone

    Star Ruby Conference - Friday, August 10, 2012
  2. No Assets No JavaScript RSpec JBuilder Rails’ Built-in Authentication No

    HTML REST API Custom Route Constraints Custom Rack Responder
  3. Status Code Response Headers JSON Body Request Headers JSON Body

    validation Local data store ORM validation Database Model
  4. $ rails new my_fit_log -S -J -T $ cd my_fit_log

    $ rm -rf app/assets vendor/assets app/views/layouts
  5. $ rails generate model User name email password_digest api_key $

    rails generate model Exercise user_id:integer name instructions $
  6. $ rails generate model User name email password_digest api_key $

    rails generate model Exercise user_id:integer name instructions $ rails generate model Workout user_id:integer \ completed_at:timestamp summary $
  7. $ rails generate model User name email password_digest api_key $

    rails generate model Exercise user_id:integer name instructions $ rails generate model Workout user_id:integer \ completed_at:timestamp summary $ rails generate model Routine workout_id:integer \ exercise_id:integer sets:integer reps:integer
  8. Versioning Version 1 Version 2 sign in with URL query

    params HTTP authentication api key as URL parameter
  9. Versioning Version 1 Version 2 sign in with URL query

    params HTTP authentication api key as URL parameter HTTP From Header
  10. Versioning Version 1 Version 2 sign in with URL query

    params HTTP authentication api key as URL parameter HTTP From Header Enforce additional HTTP request headers
  11. $ rails g controller api/v1/sessions create --no-assets --no-helper create app/controllers/api/v1/sessions_controller.rb

    route get "sessions/create" invoke erb create app/views/api/v1/sessions create app/views/api/v1/sessions/create.html.erb $ mv app/views/api/v1/sessions/create.html.erb \ app/views/api/v1/sessions/create.json.jbuilder $ rails g controller api/v2/sessions create --no-assets --no-helper
  12. $ rails g controller api/v1/users create update --no-assets \ --no-helper

    $ rails g controller api/v2/users create update --no-assets \ --no-helper $ rails g controller api/v1/exercises index create show update \ destroy --no-assets --no-helper $ rails g controller api/v2/exercises index create show update \ destroy --no-assets --no-helper $ rails g controller api/v1/workouts index create show update \ destroy --no-assets --no-helper $ rails g controller api/v2/workouts index create show update \ destroy --no-assets --no-helper $ rails g controller api/v1/routines index create show update \ destroy --no-assets --no-helper $ rails g controller api/v2/routines index create show update \ destroy --no-assets --no-helper
  13. $ curl -H 'Content-Type: application/json' -H 'User-Agent: MyFitLog iOS' \

    -H 'Accept: application/vnd.my_fit_log.v2' -X POST \ -d '{"email":"[email protected]", "password":"secret", \ "password_confirmation":"secret"}' http://localhost:3000/user.json Curl