2011 a grape odyssey

  • 1,012 views
Uploaded 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.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,012
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
15
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