Your SlideShare is downloading. ×
0
2011:A Grape Odyssey     (easy api’s using Grape)
Introduction  Mike Hagedorn  @mwhagedorn
In the beginning
In the beginning
In the end..   Ends up a mess...   So how do people deal   with this? lead into next
In the end..   Ends up a mess...   So how do people deal   with this? lead into next
In the end..   Ends up a mess...   So how do people deal   with this? lead into next
Rails?
Rails?• Too Much
Rails?• Too Much• Overlapping WebUI and “data”  responsibilities
Sinatra?• Too manual
Rack App?• Even MORE manual
“Lagom”
“Lagom”• Swedish for “just the right amount”
“Lagom”• Swedish for “just the right amount”• “Small things, loosely joined, written fast”  - Justin Gehtland
“Lagom”• Swedish for “just the right amount”• “Small things, loosely joined, written fast”  - Justin Gehtland• Separation ...
“Lagom”• Swedish for “just the right amount”• “Small things, loosely joined, written fast”  - Justin Gehtland• Separation ...
Grape
Grape• Generalized Rapid API Erector
Grape• Generalized Rapid API Erector• Grape is a REST-like API micro framework
Grape• Generalized Rapid API Erector• Grape is a REST-like API micro framework• Heavily influenced by Sinatra
Grape• Generalized Rapid API Erector• Grape is a REST-like API micro framework• Heavily influenced by Sinatra• Ruby based
Hello World
Hello World   require grapeclass Bar < Grape::API  get hello do    {:hello =>world}  endend
Hello World   require grapeclass Bar < Grape::API  get hello do    {:hello =>world}  endend                     >GET /hell...
JSON Serialization
JSON Serialization• Automatically invokes #to_json on returns
JSON Serialization• Automatically invokes #to_json on returns• Other formats soon
Prefixing
Prefixing    require grapemodule TestApp  class Bar < Grape::API    prefix "api"    resource "widgets" do      get do      ...
Prefixing    require grapemodule TestApp  class Bar < Grape::API    prefix "api"    resource "widgets" do      get do      ...
Prefixing    require grapemodule TestApp  class Bar < Grape::API    prefix "api"    resource "widgets" do      get do      ...
Prefixing    require grapemodule TestApp  class Bar < Grape::API    prefix "api"    resource "widgets" do      get do      ...
Prefixing    require grapemodule TestApp  class Bar < Grape::API    prefix "api"    resource "widgets" do      get do      ...
Versioning   require grapemodule TestApp  class Bar < Grape::API                                   resource, namespace,   ...
Versioning   require grapemodule TestApp  class Bar < Grape::API                                   resource, namespace,   ...
Versioning   require grapemodule TestApp  class Bar < Grape::API                                   resource, namespace,   ...
Basic Authentication    require grapemodule TestApp  class Bar < Grape::API    prefix “api”    version "v2"    resource "w...
Basic Authentication    require grapemodule TestApp  class Bar < Grape::API    prefix “api”    version "v2"    resource "w...
Basic Authentication    require grapemodule TestApp  class Bar < Grape::API    prefix “api”    version "v2"    resource "w...
Basic Authentication    require grapemodule TestApp  class Bar < Grape::API    prefix “api”    version "v2"    resource "w...
Basic Authentication    require grapemodule TestApp  class Bar < Grape::API    prefix “api”    version "v2"    resource "w...
Basic Authentication    require grapemodule TestApp  class Bar < Grape::API    prefix “api”    version "v2"    resource "w...
Status/Error Codes     class MyAPI < Grape::API  helpers do     def current_user        @current_user ||= User.find_by_sin...
Status/Error Codes     class MyAPI < Grape::API  helpers do     def current_user        @current_user ||= User.find_by_sin...
Status/Error Codes     class MyAPI < Grape::API  helpers do     def current_user        @current_user ||= User.find_by_sin...
Demo• Extractinator
Upcoming SlideShare
Loading in...5
×

2011 a grape odyssey

1,089

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.

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

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

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 of "2011 a grape odyssey"

    1. 1. 2011:A Grape Odyssey (easy api’s using Grape)
    2. 2. Introduction Mike Hagedorn @mwhagedorn
    3. 3. In the beginning
    4. 4. In the beginning
    5. 5. In the end.. Ends up a mess... So how do people deal with this? lead into next
    6. 6. In the end.. Ends up a mess... So how do people deal with this? lead into next
    7. 7. In the end.. Ends up a mess... So how do people deal with this? lead into next
    8. 8. Rails?
    9. 9. Rails?• Too Much
    10. 10. Rails?• Too Much• Overlapping WebUI and “data” responsibilities
    11. 11. Sinatra?• Too manual
    12. 12. Rack App?• Even MORE manual
    13. 13. “Lagom”
    14. 14. “Lagom”• Swedish for “just the right amount”
    15. 15. “Lagom”• Swedish for “just the right amount”• “Small things, loosely joined, written fast” - Justin Gehtland
    16. 16. “Lagom”• Swedish for “just the right amount”• “Small things, loosely joined, written fast” - Justin Gehtland• Separation of concerns
    17. 17. “Lagom”• Swedish for “just the right amount”• “Small things, loosely joined, written fast” - Justin Gehtland• Separation of concerns• Testability, Scalability
    18. 18. Grape
    19. 19. Grape• Generalized Rapid API Erector
    20. 20. Grape• Generalized Rapid API Erector• Grape is a REST-like API micro framework
    21. 21. Grape• Generalized Rapid API Erector• Grape is a REST-like API micro framework• Heavily influenced by Sinatra
    22. 22. Grape• Generalized Rapid API Erector• Grape is a REST-like API micro framework• Heavily influenced by Sinatra• Ruby based
    23. 23. Hello World
    24. 24. Hello World require grapeclass Bar < Grape::API get hello do {:hello =>world} endend
    25. 25. Hello World require grapeclass Bar < Grape::API get hello do {:hello =>world} endend >GET /hello {“hello”:”world”}
    26. 26. JSON Serialization
    27. 27. JSON Serialization• Automatically invokes #to_json on returns
    28. 28. JSON Serialization• Automatically invokes #to_json on returns• Other formats soon
    29. 29. Prefixing
    30. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 47. Demo• Extractinator
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×