Publicidad
Publicidad

Más contenido relacionado

Publicidad
Publicidad

2011 a grape odyssey

  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 'grape' class Bar < Grape::API get 'hello' do {:hello =>'world'} end end
  25. Hello World require 'grape' class Bar < Grape::API get 'hello' do {:hello =>'world'} end end >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 'grape' module TestApp class Bar < Grape::API prefix "api" resource "widgets" do get do Widget.all end get ":id" do Widget.find(params[:id]) end end end end
  31. Prefixing require 'grape' module TestApp class Bar < Grape::API prefix "api" resource "widgets" do get do Widget.all end get ":id" do Widget.find(params[:id]) end end end end >GET /api/widgets/1
  32. Prefixing require 'grape' module TestApp class Bar < Grape::API prefix "api" resource "widgets" do get do Widget.all end get ":id" do Widget.find(params[:id]) end end end end >GET /api/widgets/1 {“name”:”widget1”}
  33. Prefixing require 'grape' module TestApp class Bar < Grape::API prefix "api" resource "widgets" do get do Widget.all end get ":id" do Widget.find(params[:id]) end end end end >GET /api/widgets >GET /api/widgets/1 {“name”:”widget1”}
  34. Prefixing require 'grape' module TestApp class Bar < Grape::API prefix "api" resource "widgets" do get do Widget.all end get ":id" do Widget.find(params[:id]) end end end end >GET /api/widgets >GET /api/widgets/1 [{“name”:”widget1”}] {“name”:”widget1”}
  35. Versioning require 'grape' module 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 'grape' module 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 'grape' module 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 'grape' module 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 end end OAuth soon...
  39. Basic Authentication require 'grape' module 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 end end OAuth soon... admin:<password>
  40. Basic Authentication require 'grape' module 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 end end OAuth soon... admin:<password> >GET /api/v2/admin
  41. Basic Authentication require 'grape' module 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 end end OAuth soon... admin:<password> >GET /api/v2/admin {“clicks”:1234}
  42. Basic Authentication require 'grape' module 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 end end OAuth soon... admin:<password> >GET /api/v2/admin >GET /api/v2/admin {“clicks”:1234}
  43. Basic Authentication require 'grape' module 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 end end 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 end end
  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 end end >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 end end >GET /self?auth_token=BAD 401 Unauthorized
  47. Demo • Extractinator

Notas del editor

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
Publicidad