the grapes of rapid (rubyconf 2010)

54
The Grapes of Rapid Michael Bleigh, RubyConf 2010

Upload: michael-bleigh

Post on 11-May-2015

3.959 views

Category:

Technology


2 download

DESCRIPTION

Talk about Grape, a rapid API development framework for Ruby. Presented at RubyConf 2010.

TRANSCRIPT

Page 1: The Grapes of Rapid (RubyConf 2010)

The Grapes of RapidMichael Bleigh, RubyConf 2010

Page 2: The Grapes of Rapid (RubyConf 2010)

@mbleigh

@intridea

Page 4: The Grapes of Rapid (RubyConf 2010)

This is not the talk I wanted.

Page 5: The Grapes of Rapid (RubyConf 2010)

CDD

Page 6: The Grapes of Rapid (RubyConf 2010)

ConferenceDrivenDevelopment

Page 7: The Grapes of Rapid (RubyConf 2010)

Deliver

PromiseMe

Page 8: The Grapes of Rapid (RubyConf 2010)

Motivation

Page 9: The Grapes of Rapid (RubyConf 2010)

Ruby makes hard things easy

Page 10: The Grapes of Rapid (RubyConf 2010)

But APIs still aren’t easy

Page 11: The Grapes of Rapid (RubyConf 2010)

APIs in Rails are too entangled

Page 12: The Grapes of Rapid (RubyConf 2010)

APIs in Sinatra are too manual

Page 13: The Grapes of Rapid (RubyConf 2010)

Why can’t APIs have their own

framework?

Page 14: The Grapes of Rapid (RubyConf 2010)

#newtwitter

Page 15: The Grapes of Rapid (RubyConf 2010)

Grape

Page 16: The Grapes of Rapid (RubyConf 2010)

GeneralizedRapidAPIErector

Page 17: The Grapes of Rapid (RubyConf 2010)

Something that sounds like API...

ape grapegravy

Page 18: The Grapes of Rapid (RubyConf 2010)

High Level

Page 19: The Grapes of Rapid (RubyConf 2010)

Built for ease of development.

Page 20: The Grapes of Rapid (RubyConf 2010)

Sinatra-inspired.

Page 21: The Grapes of Rapid (RubyConf 2010)

Just works.

Page 22: The Grapes of Rapid (RubyConf 2010)

What does it do?

Page 23: The Grapes of Rapid (RubyConf 2010)

What does it do now?

Page 24: The Grapes of Rapid (RubyConf 2010)

Basics

Page 25: The Grapes of Rapid (RubyConf 2010)

require 'grape'

class MyAPI < Grape::API get 'hello' do {:hello => 'world'} endend

GET /hello{“hello”:”world”}

Page 26: The Grapes of Rapid (RubyConf 2010)

JSONSerialization

Page 27: The Grapes of Rapid (RubyConf 2010)

• Look for #serializable_hash

• Look for #to_json

• Then tries to encode the object directly

• Other formats coming soon

With Returned Value...

Page 28: The Grapes of Rapid (RubyConf 2010)

Prefixing

Page 29: The Grapes of Rapid (RubyConf 2010)

class MyAPI < Grape::API prefix 'api' get 'hello' do {:hello => 'world'} endend

GET /hello404 Not Found

GET /api/hello{“hello”:”world”}

Page 30: The Grapes of Rapid (RubyConf 2010)

Versioning

Page 31: The Grapes of Rapid (RubyConf 2010)

class MyAPI < Grape::API prefix 'api' version 'v1' get 'hello' do {:version => version} endend

GET /api/v1/hello{“version”:”v1”}

GET /api/v2/hello404 API Version Not Found

Page 32: The Grapes of Rapid (RubyConf 2010)

Namespacing

Page 33: The Grapes of Rapid (RubyConf 2010)

class MyAPI < Grape::API namespace :admin do namespace 'metrics' do get do {:clicks => Click.count} end get '/:date' do {:clicks => Click.for_date(params[:date]).count} end end endend

GET /admin/metrics{“clicks”:235343}GET /admin/metrics/2010-11-13{“clicks”:5392}

Page 34: The Grapes of Rapid (RubyConf 2010)

Stackable Configuration

Page 35: The Grapes of Rapid (RubyConf 2010)

Basic Auth

Page 36: The Grapes of Rapid (RubyConf 2010)

class MyAPI < Grape::API get 'open' do "Hello." end namespace :admin do http_basic do |u,p| u == 'admin' && p == ENV['ADMIN_PASSWORD'] end namespace 'metrics' do get do {:clicks => Click.count} end end endend

GET /admin/metrics401 Unauthorized

admin:somepasswordGET /admin/metrics{“clicks”:235343}

Page 37: The Grapes of Rapid (RubyConf 2010)

Helpers

Page 38: The Grapes of Rapid (RubyConf 2010)

class MyAPI < Grape::API helpers do def current_user User.find_by_token(params[:token]) end end get '/me' do current_user endend

GET /me?token=12ab312df{“screen_name”:”mbleigh”}

Page 39: The Grapes of Rapid (RubyConf 2010)

Erroring

Page 40: The Grapes of Rapid (RubyConf 2010)

class MyAPI < Grape::API helpers do def current_user @current_user ||= User.find_by_token(params[:token]) end end get '/me' do error!("401 Unauthorized", 401) unless current_user current_user endend

GET /me?token=invalidtoken401 Unauthorized

Page 41: The Grapes of Rapid (RubyConf 2010)

What will it do soon?

Page 42: The Grapes of Rapid (RubyConf 2010)

Documentation Generation

Page 43: The Grapes of Rapid (RubyConf 2010)

Content Negotiation

Page 44: The Grapes of Rapid (RubyConf 2010)

OAuth 1.0/2.0

Page 45: The Grapes of Rapid (RubyConf 2010)

Multiple Files

Page 46: The Grapes of Rapid (RubyConf 2010)

Plugin System

Page 47: The Grapes of Rapid (RubyConf 2010)

Vaporware

Page 48: The Grapes of Rapid (RubyConf 2010)

Streaming APIs

Page 49: The Grapes of Rapid (RubyConf 2010)

PubSubHubBub

Page 50: The Grapes of Rapid (RubyConf 2010)

Presenters

Page 51: The Grapes of Rapid (RubyConf 2010)

Rate Limiting

Page 52: The Grapes of Rapid (RubyConf 2010)

Internal API

Page 53: The Grapes of Rapid (RubyConf 2010)

ConfAsk: Grape In Action

Page 54: The Grapes of Rapid (RubyConf 2010)

Questions?

gem install grapegithub.com/intridea/grape