2011   a grape odyssey
Upcoming SlideShare
Loading in...5
×
 

2011 a grape odyssey

on

  • 1,207 views

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.

Statistics

Views

Total Views
1,207
Views on SlideShare
1,207
Embed Views
0

Actions

Likes
1
Downloads
13
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \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 2011 a grape odyssey Presentation Transcript

  • 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 of concerns
  • “Lagom”• Swedish for “just the right amount”• “Small things, loosely joined, written fast” - Justin Gehtland• Separation of concerns• Testability, Scalability
  • 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 /hello {“hello”:”world”}
  • 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 Widget.all end get ":id" do Widget.find(params[:id]) end end endend
  • 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
  • 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”}
  • 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”}
  • 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”}
  • 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
  • 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
  • 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”}]
  • 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...
  • 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>
  • 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
  • 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}
  • 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}
  • 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}
  • 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
  • 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
  • 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
  • Demo• Extractinator