The Grapes of Rapid (RubyConf 2010)

4,558 views
4,417 views

Published on

Talk about Grape, a rapid API development framework for Ruby. Presented at RubyConf 2010.

Published in: Technology
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,558
On SlideShare
0
From Embeds
0
Number of Embeds
314
Actions
Shares
0
Downloads
43
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

The Grapes of Rapid (RubyConf 2010)

  1. 1. The Grapes of Rapid Michael Bleigh, RubyConf 2010
  2. 2. @mbleigh @intridea
  3. 3. confask.heroku.com
  4. 4. This is not the talk I wanted.
  5. 5. CDD
  6. 6. Conference Driven Development
  7. 7. Deliver Promise Me
  8. 8. Motivation
  9. 9. Ruby makes hard things easy
  10. 10. But APIs still aren’t easy
  11. 11. APIs in Rails are too entangled
  12. 12. APIs in Sinatra are too manual
  13. 13. Why can’t APIs have their own framework?
  14. 14. #newtwitter
  15. 15. Grape
  16. 16. Generalized Rapid API Erector
  17. 17. Something that sounds like API... ape grapegravy
  18. 18. High Level
  19. 19. Built for ease of development.
  20. 20. Sinatra-inspired.
  21. 21. Just works.
  22. 22. What does it do?
  23. 23. What does it do now?
  24. 24. Basics
  25. 25. require 'grape' class MyAPI < Grape::API get 'hello' do {:hello => 'world'} end end GET /hello {“hello”:”world”}
  26. 26. JSON Serialization
  27. 27. •Look for #serializable_hash •Look for #to_json •Then tries to encode the object directly •Other formats coming soon With Returned Value...
  28. 28. Prefixing
  29. 29. class MyAPI < Grape::API prefix 'api' get 'hello' do {:hello => 'world'} end end GET /hello 404 Not Found GET /api/hello {“hello”:”world”}
  30. 30. Versioning
  31. 31. class MyAPI < Grape::API prefix 'api' version 'v1' get 'hello' do {:version => version} end end GET /api/v1/hello {“version”:”v1”} GET /api/v2/hello 404 API Version Not Found
  32. 32. Namespacing
  33. 33. class MyAPI < Grape::API namespace :admin do namespace 'metrics' do get do {:clicks => Click.count} end get '/:date' do {:clicks => Click.for_date(params[:date]).count} end end end end GET /admin/metrics {“clicks”:235343} GET /admin/metrics/2010-11-13 {“clicks”:5392}
  34. 34. Stackable Configuration
  35. 35. Basic Auth
  36. 36. class MyAPI < Grape::API get 'open' do "Hello." end namespace :admin do http_basic do |u,p| u == 'admin' && p == ENV['ADMIN_PASSWORD'] end namespace 'metrics' do get do {:clicks => Click.count} end end end end GET /admin/metrics 401 Unauthorized admin:somepassword GET /admin/metrics {“clicks”:235343}
  37. 37. Helpers
  38. 38. class MyAPI < Grape::API helpers do def current_user User.find_by_token(params[:token]) end end get '/me' do current_user end end GET /me?token=12ab312df {“screen_name”:”mbleigh”}
  39. 39. Erroring
  40. 40. class MyAPI < Grape::API helpers do def current_user @current_user ||= User.find_by_token(params[:token]) end end get '/me' do error!("401 Unauthorized", 401) unless current_user current_user end end GET /me?token=invalidtoken 401 Unauthorized
  41. 41. What will it do soon?
  42. 42. Documentation Generation
  43. 43. Content Negotiation
  44. 44. OAuth 1.0/2.0
  45. 45. Multiple Files
  46. 46. Plugin System
  47. 47. Vaporware
  48. 48. Streaming APIs
  49. 49. PubSubHubBub
  50. 50. Presenters
  51. 51. Rate Limiting
  52. 52. Internal API
  53. 53. ConfAsk: Grape In Action
  54. 54. Questions? gem install grape github.com/intridea/grape

×