RESTful Api practices Rails 3

9,372 views

Published on

0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
9,372
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
55
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • RESTful Api practices Rails 3

    1. 1. RESTful API practices
    2. 2. One Backend Multiple Clients Web Front-end Mobile (backbone.js, (Android, iOS,javascriptmvc.js etc.) BlackBerry, Symbian, WP7?) Third party applications
    3. 3. API planning• RESTful - no-brainer for Rails people• Minimum viable API - Released features must be maintained and probably supported in next versions• Versioning - URI design• Formats - Json ... (xml?)• Authentication
    4. 4. VersioningFrom the client side • URI design • www.myapp.com/api/v1 • api.myapp.com/v1 • Custom header X-SMTH-Api-ver: 1
    5. 5. VersioningFrom the back-end • Sub-controllers • app/controllers/api/v1/ • app/controllers/api/v2/ • Routes • Models - mixins
    6. 6. Request and response formats respond_with method in Rails 3
    7. 7. Authentication• Session based - Devise• Request based - client provides his ID and a signature
    8. 8. Testing     describe "post create" doCreate Post       describe "when post is valid" do[POST] /api/v1/post.json         it "should respond with created"[PROPERTIES]         it "should respond with created post" • post: this object specifies the data to be created as a post (required) • third_party_id (required)         it "should have only allowed attributes in response object" • content (required)       end[EXAMPLE REQUEST]       describe "when post is invalid" doPOST http://cannedapps.com:3838/api/post.json          "post": {         it "should respond with unprocessable entity" "third_party_id": 142, "content": "Hi Guys!"       end }     endOn Success:Http status code 201 is returned, with a response body including the createdparticipant id.[EXAMPLE RESPONSE]{ "id": 767, "third_party_id": 142, "content": "Hi Guys!", "updated_at": "2011-04-14 07:18:43"}If a create or update action failed, HTTP status code 422 Unprocessable Entityand a JSON-encoded body of localized error messages will be returned.
    9. 9. Examplehttps://github.com/naru/API-practices-demo
    10. 10. Thank you! Anton Narusberg Cannedapps github.com/naruanton@cannedapps.com @antonnarusberg

    ×