2011 a grape odyssey

1,421 views

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,421
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
18
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
  • 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

    ×