A HTTP client, greedy with JSON content, GETting conditionally.
Uses Patron (github.com/toland/patron) or em-http-request (github.com/igrigorik/em-http-request) and Yajl-ruby (github.com/brianmario/yajl-ruby) whenever possible.
This library also contains some CouchDB helpers.
To select a HTTP transport layer for rufus-jig, just make sure you have loaded the library before loading rufus-jig.
For Patron:
require 'patron' require 'rufus/jig'
For em-http-request:
require 'em-http' require 'rufus/jig'
Please not that em-http-request-0.2.2 and earlier don’t support the PUT and DELETE verbs. Until a newer gem is released you’ll need to build the em-http-request gem from source.
examples
HTTP client
Let’s say we have an hypothetical document server.
getting...
require 'patron'
require 'yajl'
require 'rufus/jig'
h = Rufus::Jig::Http.new('127.0.0.1', 4567)
p @h.get('/document/xyz.json')
#
# => { "type" => "letter", "title" => "four birds" }
#
# if it's JSON, decodes it immediately
r = @h.get('/document/xyz.txt')
p [ r.status, r.body ]
#
# => [ 200, "letter:\n\nfour birds" ]
#
# else returns a Response object (currently the one of Patron)
j = @h.get('/document', :content_type => 'application/json')
j = @h.get('/document', :content_type => :json)
#
# 'Accepts' JSON
posting...
r = @h.post(
'/documents', '<doc>four birds</doc>', :content_type => 'application/xml')
p [ r.status, r.body ]
#
# => [ 201, "created." ]
Couch helpers
For the real thing : github.com/couchrest/couchrest There is also the excellent : github.com/langalex/couch_potato
Warning : rufus-jig >= 0.1.18 is meant for CouchDB >= 0.11.
The class Rufus::Jig::Couch provides a get/put/delete trio that is couch-oriented. Json encoding/decoding is automatically handled as well as
put and delete return nil in case of success and true (conflict) or and exception else.
require 'patron'
require 'yajl'
require 'rufus/jig'
c = Rufus::Jig::Couch.new('127.0.0.1', 5984, 'my_couch_db')
c.put('_id' => 'coffee0', 'category' => 'espresso')
c.put('_id' => 'coffee1', 'category' => 'instantaneous')
doc0 = c.get('coffee0')
doc1 = c.get('coffee1')
c.delete(doc0)
doc1['category'] => 'instant'
c.put(doc1)
c.delete(doc1['_id'], doc1['_rev'])
# ...
c.attach(
doc0, 'picture', File.read('espresso.jpg'), :content_type => 'image/jpeg')
# or
c.attach(
doc0['_id'], doc0['_rev'], 'picture', File.read('espresso.jpg'), :content_type => 'image/jpeg')
picture = c.get('coffe0/picture')
c.detach('coffee0', '1-x-newrevision-whatever', 'picture')
…
Warning : Patron, as of 0.4.5 is timing out when PUTting images.
on_change
Continuous feed with a 20s heartbeat :
db.on_change do |doc_id, deleted|
puts "doc #{doc_id} has been #{deleted ? 'deleted' : 'changed'}"
end
db.on_change do |doc_id, deleted, doc|
puts "doc #{doc_id} has been #{deleted ? 'deleted' : 'changed'}"
p doc
end
Please note the 3 param block, it includes the changed document (Note : this only work with CouchDB >= 0.11).
This is a blocking method (one may want to wrap it in a thread).
rdoc
rufus.rubyforge.org/rufus-jig/
testing
At first, make sure to start the tiny sinatra test server :
ruby test/server.rb
then do
ruby test/test.rb
To test the CouchDB helpers, make you have a running Couch on 127.0.0.1:5984 then do
ruby test/test.rb --couch
To test the em-http-request HTTP transport, make sure the sinatra server is running and then do:
ruby test/test.rb -- --em
mailing list
On the rufus-ruby list :
groups.google.com/group/rufus-ruby
issue tracker
github.com/jmettraux/rufus-jig/issues
irc
irc.freenode.net #ruote
the rest of Rufus
authors
John Mettraux, jmettraux.wordpress.com/
Kenneth Kalmer, www.opensourcery.co.za/
license
MIT