lightweight webservices with sinatra and restclient
DESCRIPTION
Talk presented by Adam Wiggins & Blake Mizerany at Rubyconf 2008TRANSCRIPT
![Page 1: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/1.jpg)
Lightweight web serviceswith Sinatra and RestClient
Adam Wiggins &Blake MizeranyRubyconf 2008
![Page 2: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/2.jpg)
What for?
![Page 3: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/3.jpg)
app lifecycle
![Page 4: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/4.jpg)
Birth: small and beautiful
![Page 5: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/5.jpg)
Fate: sprawling mess
![Page 6: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/6.jpg)
web services
![Page 7: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/7.jpg)
Rails?
![Page 8: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/8.jpg)
Rails?
Too much.
![Page 9: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/9.jpg)
Bare mongrel handler or Rack app?
![Page 10: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/10.jpg)
Too little.
Bare mongrel handler or Rack app?
![Page 11: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/11.jpg)
Sinatra - the classy microframework for Ruby
![Page 12: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/12.jpg)
require 'rubygems'require 'sinatra'
get '/' do "Hello, whirled"end
![Page 13: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/13.jpg)
$ ruby hello.rb
![Page 14: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/15.jpg)
‣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](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/16.jpg)
A commitment to small
![Page 17: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/17.jpg)
A commitment to smallRails 87,990Merb-core 12,417Ramaze 11,796Camping 1,704Sinatra 1,576
![Page 18: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/18.jpg)
Sinatra is a proud Ruby citizen
![Page 19: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/19.jpg)
examples
![Page 20: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/20.jpg)
git-wikihttp://github.com/sr/git-wiki
355 linesof Ruby
![Page 21: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/21.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/22.jpg)
rifgrafhttp://github.com/adamwiggins/rifgraf
61 linesof Ruby
![Page 23: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/23.jpg)
github-serviceshttp://github.com/pjhyett/github-services
423 linesof Ruby
![Page 24: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/24.jpg)
scantyhttp://github.com/adamwiggins/scanty
194 linesof Ruby
![Page 25: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/25.jpg)
client side
![Page 26: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/26.jpg)
ActiveResource?
![Page 27: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/27.jpg)
ActiveResource?
Too much.
![Page 28: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/28.jpg)
Net::HTTP?
![Page 29: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/29.jpg)
Net::HTTP?
Too little.
![Page 30: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/30.jpg)
RestClient -the Sinatra-inspired microclient for Ruby
![Page 31: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/31.jpg)
require 'rubygems'require 'rest_client'
RestClient.get 'http://localhost:4567/posts'
![Page 32: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/32.jpg)
require 'rubygems'require 'rest_client'
RestClient.post 'http://localhost:4567/posts', :author => 'Me', :title => 'First Post'
![Page 33: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/33.jpg)
$ restclient http://example.com>> post '/resource', :value => 42=> "result"
Console
![Page 34: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/34.jpg)
$ 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](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/35.jpg)
$ 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](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/36.jpg)
examples
![Page 37: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/37.jpg)
heroku-clienthttp://github.com/adamwiggins/heroku-client
![Page 38: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/38.jpg)
couchresthttp://github.com/jchris/couchrest
![Page 39: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/39.jpg)
principles
![Page 40: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/40.jpg)
“lagom”
![Page 41: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/41.jpg)
“lagom”
just the right amount
![Page 42: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/42.jpg)
Fewer classes,less inheritance
![Page 43: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/43.jpg)
Controller objectmapping & routes
URLs
vs.
![Page 44: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/44.jpg)
Don’t fe
ar the U
RLs!Controller objectmapping & routes
URLs
vs.
![Page 45: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/45.jpg)
Exposed simplicity instead ofhidden complexity
![Page 46: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/46.jpg)
Small things,loosely joined,written fast
- Justin Gehtland @ Relevance
![Page 47: Lightweight Webservices with Sinatra and RestClient](https://reader034.vdocuments.us/reader034/viewer/2022052505/5555d323d8b42a711f8b4b2d/html5/thumbnails/47.jpg)
http://github.com/bmizerany/sinatra
Adam Wiggins &Blake MizeranyRubyconf 2008
http://rest-client.heroku.com/
http://adam.blog.heroku.com/