SlideShare a Scribd company logo
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 'grape'

class Bar < Grape::API
  get 'hello' do
    {:hello =>'world'}
  end
end
Hello World
   require 'grape'

class Bar < Grape::API
  get 'hello' do
    {:hello =>'world'}
  end
end




                     >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 '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
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
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”}
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”}
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”}
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
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
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”}]
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...
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>
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
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}
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}
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}
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
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
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
Demo


• Extractinator

More Related Content

PDF
Bringing the open web and APIs to mobile devices with Firefox OS - Whisky W...
PDF
Web APIs & Apps - Mozilla
PDF
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
PDF
Developing Modern Java Web Applications with Java EE 7 and AngularJS
PDF
Connect.js - Exploring React.Native
PPTX
React native by example by Vadim Ruban
PDF
Browser-level testing
PDF
Connect.js 2015 - Building Native Mobile Applications with Javascript
Bringing the open web and APIs to mobile devices with Firefox OS - Whisky W...
Web APIs & Apps - Mozilla
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
Developing Modern Java Web Applications with Java EE 7 and AngularJS
Connect.js - Exploring React.Native
React native by example by Vadim Ruban
Browser-level testing
Connect.js 2015 - Building Native Mobile Applications with Javascript

What's hot (20)

PDF
Empowering the "mobile web"
PDF
Modern JavaScript, without giving up on Rails
PDF
Do you want a SDK with that API? (Nordic APIS April 2014)
PDF
Web versus Native: round 1!
PDF
APIs, now and in the future
PPTX
Rapid prototyping and easy testing with ember cli mirage
PDF
Introduction to Usergrid - ApacheCon EU 2014
PDF
Bootiful Development with Spring Boot and React - UberConf 2018
PDF
Developing, Testing and Scaling with Apache Camel - UberConf 2015
PDF
Symfony tips and tricks
PDF
Vuejs testing
PDF
AtlasCamp 2015: Connect everywhere - Cloud and Server
PDF
Primefaces Nextgen Lju
PDF
Cross-browser testing in the real world
PDF
Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...
PDF
Webpack Encore Symfony Live 2017 San Francisco
PDF
APIs for modern web apps
PDF
How to React Native
PDF
Building Universal Web Apps with React ForwardJS 2017
PDF
React Native - Workshop
Empowering the "mobile web"
Modern JavaScript, without giving up on Rails
Do you want a SDK with that API? (Nordic APIS April 2014)
Web versus Native: round 1!
APIs, now and in the future
Rapid prototyping and easy testing with ember cli mirage
Introduction to Usergrid - ApacheCon EU 2014
Bootiful Development with Spring Boot and React - UberConf 2018
Developing, Testing and Scaling with Apache Camel - UberConf 2015
Symfony tips and tricks
Vuejs testing
AtlasCamp 2015: Connect everywhere - Cloud and Server
Primefaces Nextgen Lju
Cross-browser testing in the real world
Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...
Webpack Encore Symfony Live 2017 San Francisco
APIs for modern web apps
How to React Native
Building Universal Web Apps with React ForwardJS 2017
React Native - Workshop
Ad

Similar to 2011 a grape odyssey (20)

PPTX
Building RESTful APIs w/ Grape
PPTX
Ruby On Grape
PDF
Building Mobile Friendly APIs in Rails
PDF
Workshop: Building Vaadin add-ons
PDF
Serverless - Developers.IO 2019
PDF
Effectively Testing Services - Burlington Ruby Conf
KEY
Api development with rails
PDF
Building Better Web APIs with Rails
PPTX
Araport Workshop Tutorial 2: Authentication and the Agave Profiles Service
KEY
Dancing with websocket
ODP
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
KEY
Socket applications
PDF
FiNCのWeb API開発事情
PDF
OpenWhisk Under the Hood -- London Oct 16 2016
PDF
Burn down the silos! Helping dev and ops gel on high availability websites
PDF
Google Cloud Endpointsによる API構築
ZIP
Warden Introduction
PDF
Webauthn Tutorial
KEY
Rails web api 开发
PDF
BugBounty Tips.pdf
Building RESTful APIs w/ Grape
Ruby On Grape
Building Mobile Friendly APIs in Rails
Workshop: Building Vaadin add-ons
Serverless - Developers.IO 2019
Effectively Testing Services - Burlington Ruby Conf
Api development with rails
Building Better Web APIs with Rails
Araport Workshop Tutorial 2: Authentication and the Agave Profiles Service
Dancing with websocket
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Socket applications
FiNCのWeb API開発事情
OpenWhisk Under the Hood -- London Oct 16 2016
Burn down the silos! Helping dev and ops gel on high availability websites
Google Cloud Endpointsによる API構築
Warden Introduction
Webauthn Tutorial
Rails web api 开发
BugBounty Tips.pdf
Ad

More from Mike Hagedorn (6)

PDF
Experienced Cloud Engineer Looking for New Roles
PDF
Couchbase Talk
PDF
Hacking the Internet of Things
PDF
Using OpenStack With Fog
PPTX
Exploring the Internet of Things Using Ruby
KEY
Playing With Fire - An Introduction to Node.js
Experienced Cloud Engineer Looking for New Roles
Couchbase Talk
Hacking the Internet of Things
Using OpenStack With Fog
Exploring the Internet of Things Using Ruby
Playing With Fire - An Introduction to Node.js

Recently uploaded (20)

PPTX
Modernising the Digital Integration Hub
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PDF
Enhancing emotion recognition model for a student engagement use case through...
PPTX
Chapter 5: Probability Theory and Statistics
PDF
project resource management chapter-09.pdf
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
Getting started with AI Agents and Multi-Agent Systems
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
Web App vs Mobile App What Should You Build First.pdf
PPTX
observCloud-Native Containerability and monitoring.pptx
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PPT
Module 1.ppt Iot fundamentals and Architecture
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PDF
STKI Israel Market Study 2025 version august
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PPT
What is a Computer? Input Devices /output devices
PPTX
TLE Review Electricity (Electricity).pptx
Modernising the Digital Integration Hub
NewMind AI Weekly Chronicles - August'25-Week II
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
Enhancing emotion recognition model for a student engagement use case through...
Chapter 5: Probability Theory and Statistics
project resource management chapter-09.pdf
WOOl fibre morphology and structure.pdf for textiles
Getting started with AI Agents and Multi-Agent Systems
A contest of sentiment analysis: k-nearest neighbor versus neural network
Web App vs Mobile App What Should You Build First.pdf
observCloud-Native Containerability and monitoring.pptx
Zenith AI: Advanced Artificial Intelligence
Univ-Connecticut-ChatGPT-Presentaion.pdf
Module 1.ppt Iot fundamentals and Architecture
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
STKI Israel Market Study 2025 version august
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
What is a Computer? Input Devices /output devices
TLE Review Electricity (Electricity).pptx

2011 a grape odyssey

  • 1. 2011:A Grape Odyssey (easy api’s using Grape)
  • 2. Introduction Mike Hagedorn @mwhagedorn
  • 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
  • 10. Rails? • Too Much • Overlapping WebUI and “data” responsibilities
  • 12. Rack App? • Even MORE manual
  • 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
  • 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
  • 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”}
  • 27. JSON Serialization • Automatically invokes #to_json on returns
  • 28. JSON Serialization • Automatically invokes #to_json on returns • Other formats soon
  • 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

Editor's Notes