Enter the app era with ruby on rails

6,194 views

Published on

Published in: Technology

Enter the app era with ruby on rails

  1. 1. Enter the App Erawith Ruby on Rails @matteocollina
  2. 2. RubyDay.it15 June 2012 www.rubyday.it
  3. 3. Matteo CollinaSoftware Engineer@matteocollinamatteocollina.com
  4. 4. www.mavigex.com www.wemobi.it
  5. 5. How is built an App? http://www.flickr.com/photos/dschulian/3173331821/
  6. 6. Code Icons by Fasticon
  7. 7. Code Run Icons by Fasticon
  8. 8. Code Run Server Icons by Fasticon
  9. 9. Code Run Server Icons by Fasticon
  10. 10. Code Run Server Icons by Fasticon
  11. 11. Code I need to serve Run Server my data toWeb and Mobile Apps Icons by Fasticon
  12. 12. We need an API
  13. 13. We need an APIWho has APIs?
  14. 14. Who has APIs?
  15. 15. Who has APIs?
  16. 16. Who has APIs?And many many others..
  17. 17. We will develop an API
  18. 18. We need to be fast! http://www.flickr.com/photos/oneaustin/1261907803
  19. 19. We have justtwenty minutes http://www.flickr.com/photos/oneaustin/1261907803
  20. 20. What do wewant to build? http://www.flickr.com/photos/oberazzi/318947873/
  21. 21. Another tool for nerds? http://www.flickr.com/photos/eyesontheroad/2260731457/
  22. 22. Another Todo List? http://www.flickr.com/photos/eyesontheroad/2260731457/
  23. 23. Enter MCDo.http://github.com/mcollina/mcdo
  24. 24. Enter MCDo.The First Todo List delivered as a REST API
  25. 25. The First Todo List delivered as a REST API
  26. 26. Signup APIhttp://github.com/mcollina/mcdo
  27. 27. Signup APIFeature: Signup API As a MCDO developer In order to develop apps I want to register new users Scenario: Succesful signup When I call "/users.json" in POST with: """ { "user": { "email": "hello@abc.org", "password": "abcde", "password_confirmation": "abcde" } } """ Then the JSON should be: """ { "id": 1, "email": "hello@abc.org" } """
  28. 28. Signup APIScenario: signup fails with a wrong password_confirmation When I call "/users.json" in POST with: """ { "user": { "email": "hello@abc.org", "password": "abcde", "password_confirmation": "abcde1" } } """ Then the JSON should be: """ { "errors": { "password": ["doesnt match confirmation"] } } """
  29. 29. How?
  30. 30. How?Ruby on RailsCucumberRSpecJSON-spec
  31. 31. How? Ruby on RailsLet’s see some code! Cucumber RSpec JSON-spec
  32. 32. Login APIhttp://github.com/mcollina/mcdo
  33. 33. Login APIFeature: Login API As a MCDO developer In order to develop apps I want to login with an existing user Background: Given there is the following user: | email | password | password_confirmation | | abcd@org.it | aa | aa | Scenario: Succesful login When I call "/session.json" in POST with: """ { "email": "abcd@org.it", "password": "aa" } """ Then the JSON should be: """ { "status": "authenticated" } """
  34. 34. Login APIScenario: Failed login When I call "/session.json" in POST with: """ { "email": "abcd@org.it", "password": "bb" } """ Then the JSON should be: """ { "status": "not authenticated" } """
  35. 35. Login APIScenario: Validating an active session Given I call "/session.json" in POST with: """ { "email": "abcd@org.it", "password": "aa" } """ When I call "/session.json" in GET Then the JSON should be: """ { "status": "authenticated" } """
  36. 36. Login API Scenario: Validating an active session Given I call "/session.json" in POST with: """ { "email": "abcd@org.it", "password": "aa" }Let’s see some code! """ When I call "/session.json" in GET Then the JSON should be: """ { "status": "authenticated" } """
  37. 37. Lists APIhttp://github.com/mcollina/mcdo
  38. 38. Lists APIFeature: Lists API As a MCDO developer In order to develop apps I want to manage a users lists Background: Given I login succesfully with user "aaa@abc.org" Scenario: Default lists When I call "/lists.json" in GET Then the JSON should be: """ { "lists": [{ "id": 1, "name": "Personal", "link": "http://www.example.com/lists/1", "items_link": "http://www.example.com/lists/1/items" }] } """
  39. 39. Lists APIScenario: Creating a list When I call "/lists.json" in POST with: """ { "list": { "name": "foobar" } } """ Then the JSON should be: """ { "name": "foobar", "items_link": "http://www.example.com/lists/1/items" } """
  40. 40. Lists APIScenario: Creating a list should add it to the index Given I call "/lists.json" in POST with: """ { "list": { "name": "foobar" } } """ When I call "/lists.json" in GET Then the JSON should be: """ { "lists": [{ "id": 2, "name": "foobar", "link": "http://www.example.com/lists/2", "items_link": "http://www.example.com/lists/2/items" }, { "id": 1, "name": "Personal", "link": "http://www.example.com/lists/1", "items_link": "http://www.example.com/lists/1/items" }] } """
  41. 41. Lists APIScenario: Removing a list (and the index is empty) Given I call "/lists/1.json" in DELETE When I call "/lists.json" Then the JSON should be: """ { "lists": [] } """Scenario: Updating a lists name When I call "/lists/1.json" in PUT with: """ { "list": { "name": "foobar" } } """ Then the JSON should be: """ { "name": "foobar", "items_link": "http://www.example.com/lists/1/items" } """
  42. 42. Lists API Scenario: Removing a list (and the index is empty) Given I call "/lists/1.json" in DELETE When I call "/lists.json" Then the JSON should be: """ { "lists": [] } """Let’s see some code! Scenario: Updating a lists name When I call "/lists/1.json" in PUT with: """ { "list": { "name": "foobar" } } """ Then the JSON should be: """ { "name": "foobar", "items_link": "http://www.example.com/lists/1/items" } """
  43. 43. Items APIhttp://github.com/mcollina/mcdo
  44. 44. Items APIFeature: Manage a lists items As a developer In order to manipulate the lists item I want to access them through APIs Background: Given I login succesfully with user "aaa@abc.org" Scenario: Default items When I call "/lists/1/items.json" in GET Then the JSON should be: """ { "items": [{ "name": "Insert your items!", "position": 0 }], "list_link": "http://www.example.com/lists/1" } """
  45. 45. Items APIScenario: Moving an element to the top Given I call "/lists/1/items.json" in POST with: ... And I call "/lists/1/items.json" in POST with: ... When I call "/lists/1/items/2/move.json" in PUT with: """ { "position": 0 } """ Then the JSON should be: """ { "items": [{ "name": "b", "position": 0 }, { "name": "Insert your items!", "position": 1 }, { "name": "c", "position": 2 }], "list_link": "http://www.example.com/lists/1" } """
  46. 46. Items API Scenario: Moving an element to the top Given I call "/lists/1/items.json" in POST with: ... And I call "/lists/1/items.json" in POST with: ... When I call "/lists/1/items/2/move.json" in PUT with: """ { "position": 0 }Let’s see some code! """ Then the JSON should be: """ { "items": [{ "name": "b", "position": 0 }, { "name": "Insert your items!", "position": 1 }, { "name": "c", "position": 2 }], "list_link": "http://www.example.com/lists/1" } """
  47. 47. Do we needan admin panel?
  48. 48. Do we needan admin panel?
  49. 49. Do we need an admin panel?Put in your Gemfile: gem activeadmin gem meta_search, >= 1.1.0.preThen run: $ bundle install $ rails g active_admin:install $ rails g active_admin:resource users $ rails g active_admin:resource lists $ rails g active_admin:resource items $ rake db:migrate
  50. 50. Do we need an admin panel?Put in your Gemfile:Let’s see some code! gem activeadmin gem meta_search, >= 1.1.0.preThen run: $ bundle install $ rails g active_admin:install $ rails g active_admin:resource users $ rails g active_admin:resource lists $ rails g active_admin:resource items $ rake db:migrate
  51. 51. Build a JS App!
  52. 52. Build a JS App! Backbone.js: MVC in the browser Rails asset pipeline concatenateand minifies our JS automatically We can even write our app inCoffeeScript: it works out of the box.
  53. 53. Build a JS App! Backbone.js: MVC in the browserto Rails asset pipeline concatenate the code.. again? and minifies our JS automatically We can even write our app in CoffeeScript: it works out of the box.
  54. 54. http://www.flickr.com/photos/oneaustin/1261907803
  55. 55. We are late, the #codemotioncrew are kicking me out http://www.flickr.com/photos/oneaustin/1261907803
  56. 56. TL;DR http://www.flickr.com/photos/evilaugust/3307382858
  57. 57. TL;DR Mobile Apps need an API Ruby on Rails is good for writing APIs You can build nice admin interfaceswith ActiveAdmin You can craft Javascript Apps easilyusing the asset pipeline.
  58. 58. RubyDay.it15 June 2012 www.rubyday.it
  59. 59. Any Questions?
  60. 60. Thank You!

×