Rails API

83f7b66c4dee7effbb3717027f99282b?s=47 Sayanee
April 30, 2014

Rails API

Basics of creating an API=only server with Rails with AngularJS in the frontend. Code: https://github.com/sayanee/Talks/tree/master/013-rails-api

83f7b66c4dee7effbb3717027f99282b?s=128

Sayanee

April 30, 2014
Tweet

Transcript

  1. Rails API ruby meetup 29 apr 2014

  2. api only version json cors auth mock server testing

  3. backend frontend logic view rails angular puma nginx localhost:3000 localhost:8888

    domainapi.com domain.com
  4. api only

  5. api only simpler Gemfile remove /app/views folder

  6. api only /app/controllers/application_controller.rb

  7. api only /app/controllers/pages_controller.rb /config/routes.rb

  8. api only demo git checkout step1

  9. version

  10. version /app/controllers/api/v1/api_controller.rb /app/controllers/api/v1/meetups_controller.rb

  11. version /config/routes.rb $ rails g model Meetup name:string event:datetime $

    rake db:migrate $ rails c > Meetup.create(name: 'April meetup', event: Time.now)
  12. version demo git checkout step2

  13. json gem ‘active_model_serializers' rails g serializer meetup

  14. json /app/serializers/meetup_serializer.rb

  15. json /app/controllers/api/v1/meetups_controller.rb

  16. json demo git checkout step3

  17. cors

  18. cors frontend markup to display data

  19. cors frontend script to query api

  20. cors gem ‘rack-cors'

  21. cors /config/application.rb

  22. auth

  23. auth /db/migrate/xxx_add_auth_token_to_users.rb /app/models/user.rb

  24. auth /app/controllers/registrations_controller.rb backend - token sent as part of headers

  25. auth javascript frontend - token received and stored

  26. auth demo token stored in local storage

  27. auth /app/controllers/api/v1/meetups_controller.rb check token before every api request

  28. auth /app/controllers/api/v1/api_controller.rb

  29. auth demo git checkout step5

  30. mock server

  31. mock server /mock_server/server.rb with sinatra - start a mock server

  32. mock server /config/secrets.yml /app/controllers/api/v1/questions_controller.rb using mock server in test &

    dev environments
  33. mock server demo git checkout step6

  34. testing

  35. testing /spec/controllers/meetups_controller_spec.rb test for json response

  36. testing demo git checkout step7

  37. other resources demo code: github.com/ sayanee/talks/013-rails-api