Your SlideShare is downloading. ×
2011   a grape odyssey
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

2011 a grape odyssey

1,065

Published on

my presentation at houston.rb on Grape, the API builder for rack based systems using a friendly ruby/sinatra like interface. It makes separating out your API from you web app logic really easy.

my presentation at houston.rb on Grape, the API builder for rack based systems using a friendly ruby/sinatra like interface. It makes separating out your API from you web app logic really easy.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,065
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
16
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript

    • 1. 2011:A Grape Odyssey (easy api’s using Grape)
    • 2. Introduction Mike Hagedorn @mwhagedorn
    • 3. In the beginning
    • 4. In the beginning
    • 5. In the end.. Ends up a mess... So how do people deal with this? lead into next
    • 6. In the end.. Ends up a mess... So how do people deal with this? lead into next
    • 7. In the end.. Ends up a mess... So how do people deal with this? lead into next
    • 8. Rails?
    • 9. Rails?• Too Much
    • 10. Rails?• Too Much• Overlapping WebUI and “data” responsibilities
    • 11. Sinatra?• Too manual
    • 12. Rack App?• Even MORE manual
    • 13. “Lagom”
    • 14. “Lagom”• Swedish for “just the right amount”
    • 15. “Lagom”• Swedish for “just the right amount”• “Small things, loosely joined, written fast” - Justin Gehtland
    • 16. “Lagom”• Swedish for “just the right amount”• “Small things, loosely joined, written fast” - Justin Gehtland• Separation of concerns
    • 17. “Lagom”• Swedish for “just the right amount”• “Small things, loosely joined, written fast” - Justin Gehtland• Separation of concerns• Testability, Scalability
    • 18. Grape
    • 19. Grape• Generalized Rapid API Erector
    • 20. Grape• Generalized Rapid API Erector• Grape is a REST-like API micro framework
    • 21. Grape• Generalized Rapid API Erector• Grape is a REST-like API micro framework• Heavily influenced by Sinatra
    • 22. Grape• Generalized Rapid API Erector• Grape is a REST-like API micro framework• Heavily influenced by Sinatra• Ruby based
    • 23. Hello World
    • 24. Hello World require grapeclass Bar < Grape::API get hello do {:hello =>world} endend
    • 25. Hello World require grapeclass Bar < Grape::API get hello do {:hello =>world} endend >GET /hello {“hello”:”world”}
    • 26. JSON Serialization
    • 27. JSON Serialization• Automatically invokes #to_json on returns
    • 28. JSON Serialization• Automatically invokes #to_json on returns• Other formats soon
    • 29. Prefixing
    • 30. Prefixing require grapemodule TestApp class Bar < Grape::API prefix "api" resource "widgets" do get do Widget.all end get ":id" do Widget.find(params[:id]) end end endend
    • 31. Prefixing require grapemodule TestApp class Bar < Grape::API prefix "api" resource "widgets" do get do Widget.all end get ":id" do Widget.find(params[:id]) end end endend >GET /api/widgets/1
    • 32. Prefixing require grapemodule TestApp class Bar < Grape::API prefix "api" resource "widgets" do get do Widget.all end get ":id" do Widget.find(params[:id]) end end endend >GET /api/widgets/1 {“name”:”widget1”}
    • 33. Prefixing require grapemodule TestApp class Bar < Grape::API prefix "api" resource "widgets" do get do Widget.all end get ":id" do Widget.find(params[:id]) end end endend >GET /api/widgets >GET /api/widgets/1 {“name”:”widget1”}
    • 34. Prefixing require grapemodule TestApp class Bar < Grape::API prefix "api" resource "widgets" do get do Widget.all end get ":id" do Widget.find(params[:id]) end end endend >GET /api/widgets >GET /api/widgets/1 [{“name”:”widget1”}] {“name”:”widget1”}
    • 35. Versioning require grapemodule TestApp class Bar < Grape::API resource, namespace, prefix “api” group all synonyms here version "v2" resource "widgets" do get do Widget.all end get ":id" do Widget.find(params[:id]) end end end
    • 36. Versioning require grapemodule TestApp class Bar < Grape::API resource, namespace, prefix “api” group all synonyms here version "v2" resource "widgets" do get do Widget.all end get ":id" do Widget.find(params[:id]) end end end >GET /api/v2/widgets
    • 37. Versioning require grapemodule TestApp class Bar < Grape::API resource, namespace, prefix “api” group all synonyms here version "v2" resource "widgets" do get do Widget.all end get ":id" do Widget.find(params[:id]) end end end >GET /api/v2/widgets [{“name”:”widget1”}]
    • 38. Basic Authentication require grapemodule TestApp class Bar < Grape::API prefix “api” version "v2" resource "widgets" do get do Widget.all end end namespace :admin do http_basic do |u,p| u == admin && p == YOURPASSWORD end namespace metrics do get do {:clicks => Click.count} end end end endend OAuth soon...
    • 39. Basic Authentication require grapemodule TestApp class Bar < Grape::API prefix “api” version "v2" resource "widgets" do get do Widget.all end end namespace :admin do http_basic do |u,p| u == admin && p == YOURPASSWORD end namespace metrics do get do {:clicks => Click.count} end end end endend OAuth soon... admin:<password>
    • 40. Basic Authentication require grapemodule TestApp class Bar < Grape::API prefix “api” version "v2" resource "widgets" do get do Widget.all end end namespace :admin do http_basic do |u,p| u == admin && p == YOURPASSWORD end namespace metrics do get do {:clicks => Click.count} end end end endend OAuth soon... admin:<password> >GET /api/v2/admin
    • 41. Basic Authentication require grapemodule TestApp class Bar < Grape::API prefix “api” version "v2" resource "widgets" do get do Widget.all end end namespace :admin do http_basic do |u,p| u == admin && p == YOURPASSWORD end namespace metrics do get do {:clicks => Click.count} end end end endend OAuth soon... admin:<password> >GET /api/v2/admin {“clicks”:1234}
    • 42. Basic Authentication require grapemodule TestApp class Bar < Grape::API prefix “api” version "v2" resource "widgets" do get do Widget.all end end namespace :admin do http_basic do |u,p| u == admin && p == YOURPASSWORD end namespace metrics do get do {:clicks => Click.count} end end end endend OAuth soon... admin:<password> >GET /api/v2/admin >GET /api/v2/admin {“clicks”:1234}
    • 43. Basic Authentication require grapemodule TestApp class Bar < Grape::API prefix “api” version "v2" resource "widgets" do get do Widget.all end end namespace :admin do http_basic do |u,p| u == admin && p == YOURPASSWORD end namespace metrics do get do {:clicks => Click.count} end end end endend OAuth soon... admin:<password> >GET /api/v2/admin >GET /api/v2/admin 401 Unauthorized {“clicks”:1234}
    • 44. Status/Error Codes class MyAPI < Grape::API helpers do def current_user @current_user ||= User.find_by_single_use_token(params[:auth_token]) end end get /self do error!("401 Unauthorized", 401) unless current_user current_user endend
    • 45. Status/Error Codes class MyAPI < Grape::API helpers do def current_user @current_user ||= User.find_by_single_use_token(params[:auth_token]) end end get /self do error!("401 Unauthorized", 401) unless current_user current_user endend >GET /self?auth_token=BAD
    • 46. Status/Error Codes class MyAPI < Grape::API helpers do def current_user @current_user ||= User.find_by_single_use_token(params[:auth_token]) end end get /self do error!("401 Unauthorized", 401) unless current_user current_user endend >GET /self?auth_token=BAD 401 Unauthorized
    • 47. Demo• Extractinator

    ×