47
Lightweight web services with Sinatra and RestClient Adam Wiggins & Blake Mizerany Rubyconf 2008

Lightweight Webservices with Sinatra and RestClient

Embed Size (px)

DESCRIPTION

Talk presented by Adam Wiggins & Blake Mizerany at Rubyconf 2008

Citation preview

Page 1: Lightweight Webservices with Sinatra and RestClient

Lightweight web serviceswith Sinatra and RestClient

Adam Wiggins &Blake MizeranyRubyconf 2008

Page 2: Lightweight Webservices with Sinatra and RestClient

What for?

Page 3: Lightweight Webservices with Sinatra and RestClient

app lifecycle

Page 4: Lightweight Webservices with Sinatra and RestClient

Birth: small and beautiful

Page 5: Lightweight Webservices with Sinatra and RestClient

Fate: sprawling mess

Page 6: Lightweight Webservices with Sinatra and RestClient

web services

Page 7: Lightweight Webservices with Sinatra and RestClient

Rails?

Page 8: Lightweight Webservices with Sinatra and RestClient

Rails?

Too much.

Page 9: Lightweight Webservices with Sinatra and RestClient

Bare mongrel handler or Rack app?

Page 10: Lightweight Webservices with Sinatra and RestClient

Too little.

Bare mongrel handler or Rack app?

Page 11: Lightweight Webservices with Sinatra and RestClient

Sinatra - the classy microframework for Ruby

Page 12: Lightweight Webservices with Sinatra and RestClient

require 'rubygems'require 'sinatra'

get '/' do "Hello, whirled"end

Page 13: Lightweight Webservices with Sinatra and RestClient

$ ruby hello.rb

Page 14: Lightweight Webservices with Sinatra and RestClient

require 'rubygems'require 'sinatra'require 'lib/posts'

post '/posts' post = Post.create! params redirect "/posts/#{post.id}"do

get '/posts/:id' do @post = Post.find(params[:id]) erb :postend

Page 15: Lightweight Webservices with Sinatra and RestClient

‣Templating (erb, haml, builder)

‣Tests/specs (test/spec, rspec, test::unit)

‣Before filters‣Helpers‣Error handlers‣Inline templates‣Code reloading‣HTTP caching (etag, last-modified)

‣Rack / inline middleware

Page 16: Lightweight Webservices with Sinatra and RestClient

A commitment to small

Page 17: Lightweight Webservices with Sinatra and RestClient

A commitment to smallRails 87,990Merb-core 12,417Ramaze 11,796Camping 1,704Sinatra 1,576

Page 18: Lightweight Webservices with Sinatra and RestClient

Sinatra is a proud Ruby citizen

Page 19: Lightweight Webservices with Sinatra and RestClient

examples

Page 20: Lightweight Webservices with Sinatra and RestClient

git-wikihttp://github.com/sr/git-wiki

355 linesof Ruby

Page 21: Lightweight Webservices with Sinatra and RestClient

get '/:page' do @page = Page.find(params[:page]) haml :showend

__END__@@ layout%html %body %content=yield

@@ show%h1= title#page_content= @page.to_html

Page 22: Lightweight Webservices with Sinatra and RestClient

rifgrafhttp://github.com/adamwiggins/rifgraf

61 linesof Ruby

Page 23: Lightweight Webservices with Sinatra and RestClient

github-serviceshttp://github.com/pjhyett/github-services

423 linesof Ruby

Page 24: Lightweight Webservices with Sinatra and RestClient

scantyhttp://github.com/adamwiggins/scanty

194 linesof Ruby

Page 25: Lightweight Webservices with Sinatra and RestClient

client side

Page 26: Lightweight Webservices with Sinatra and RestClient

ActiveResource?

Page 27: Lightweight Webservices with Sinatra and RestClient

ActiveResource?

Too much.

Page 28: Lightweight Webservices with Sinatra and RestClient

Net::HTTP?

Page 29: Lightweight Webservices with Sinatra and RestClient

Net::HTTP?

Too little.

Page 30: Lightweight Webservices with Sinatra and RestClient

RestClient -the Sinatra-inspired microclient for Ruby

Page 31: Lightweight Webservices with Sinatra and RestClient

require 'rubygems'require 'rest_client'

RestClient.get 'http://localhost:4567/posts'

Page 32: Lightweight Webservices with Sinatra and RestClient

require 'rubygems'require 'rest_client'

RestClient.post 'http://localhost:4567/posts', :author => 'Me', :title => 'First Post'

Page 33: Lightweight Webservices with Sinatra and RestClient

$ restclient http://example.com>> post '/resource', :value => 42=> "result"

Console

Page 34: Lightweight Webservices with Sinatra and RestClient

$ restclient get http://example.com/posts > posts.xml

$ restclient post http://example.com/posts < post.xml

$ restclient put http://example.com/posts/1 < post.xml

$ restclient delete http://example.com/posts/1

A better curl

Page 35: Lightweight Webservices with Sinatra and RestClient

$ RESTCLIENT_LOG=stdout ruby myscript.rb

RestClient.get "http://example.com/posts"# => 200 OK | text/html 3781 bytes

RestClient.post "http://example.com/posts", "title=First+post"# => 200 OK | text/html 40 bytes

Logging & replay

Page 36: Lightweight Webservices with Sinatra and RestClient

examples

Page 37: Lightweight Webservices with Sinatra and RestClient

heroku-clienthttp://github.com/adamwiggins/heroku-client

Page 38: Lightweight Webservices with Sinatra and RestClient

couchresthttp://github.com/jchris/couchrest

Page 39: Lightweight Webservices with Sinatra and RestClient

principles

Page 40: Lightweight Webservices with Sinatra and RestClient

“lagom”

Page 41: Lightweight Webservices with Sinatra and RestClient

“lagom”

just the right amount

Page 42: Lightweight Webservices with Sinatra and RestClient

Fewer classes,less inheritance

Page 43: Lightweight Webservices with Sinatra and RestClient

Controller objectmapping & routes

URLs

vs.

Page 44: Lightweight Webservices with Sinatra and RestClient

Don’t fe

ar the U

RLs!Controller objectmapping & routes

URLs

vs.

Page 45: Lightweight Webservices with Sinatra and RestClient

Exposed simplicity instead ofhidden complexity

Page 46: Lightweight Webservices with Sinatra and RestClient

Small things,loosely joined,written fast

- Justin Gehtland @ Relevance

Page 47: Lightweight Webservices with Sinatra and RestClient

http://github.com/bmizerany/sinatra

Adam Wiggins &Blake MizeranyRubyconf 2008

http://rest-client.heroku.com/

http://adam.blog.heroku.com/