Last Update: 2011-02-03 19:38:34 +0530

Wrest 1.2.0

© Copyright 2009-2011 Sidu Ponnappa. All Rights Reserved.

Wrest is a ruby REST/HTTP client library which

  • Allows you to quickly build object oriented wrappers around any web service
  • Designed to be used as a library, not just a command line REST client (fewer class/static methods, more object oriented)
  • Is spec driven, strongly favours immutable objects and avoids class methods and setters making it better suited for use as a library, especially in multi-threaded environments
  • Runs on Ruby 1.8, Ruby 1.9.2, JRuby and Rubinius with MacRuby and IronRuby support on the way (Continuous Integration server)
  • Provides convenient HTTP wrappers, redirect handling, serialisation, deserialisation and response caching
  • Supports using either Net::HTTP or libcurl as the underlying HTTP library

To receive notifications whenever new features are added to Wrest, please subscribe to my twitter feed.


For Facebook, Twitter, Delicious, GitHub and other API examples, see

Basic Http Calls


  • Basic API calls
    ''.to_uri.get.deserialise  # works with json and xml out of the box
    ''.to_uri.get.deserialise   # see lib/wrest/components/translators to add other formats
  • Timeout support
    ''.to_uri.get(:timeout => 5).body
  • Redirect support
    ''.to_uri(:follow_redirects => false).get
    ''.to_uri(:follow_redirects_limit => 1).get

    :follow_redirects_limit defaults to 5 if not specified.

  • Deserialise with XPath filtering
    ActiveSupport::XmlMini.backend = 'REXML'
                                                  :xpath => '//user/name/text()'
  • More complex request with parameters and a custom deserialiser
                  :appid  => 'YahooDemo',
                  :output => 'xml',
                  :query  => 'India',
                  :results=> '3',
                  :start  => '1'
  • Basic HTTP auth and URI extensions using Wrest::Uri#[]
    base_uri = ''.to_uri(:username => 'kaiwren', :password => 'fupupp1es')
    bookmarks = base_uri['/posts/get'].get.deserialise


  • Regular, vanilla Post with a body and headers
    '''YAML encoded body', 'Content-Type' => 'text/x-yaml')
  • Form encoded post
             :username => 'kaiwren', :password => 'fupupp1es'
             :url => '',
             :description => 'The Ruby related posts on my blog!',
             :extended => "All posts tagged with 'ruby'",
             :tags => 'ruby hacking'
  • Multipart posts
      :image =>, "image/png", file_path),
      :key => imgur_key


To delete a resource:

                                             :username => 'kaiwren',
                                             :password => 'fupupp1es'
                                             :url => ''


Wrest supports caching with pluggable back-ends.

Wrest.always_cache_using_hash!     # Hash should NEVER be used in a production environment. It is unbounded and will keep increasing in size.
c42 = "".to_uri.get

A Memcached based caching back-end is available in Wrest. You can get instructions on how to install Memcached on your system here. The Dalli gem is used by Wrest to interface with Memcached. Install dalli using ‘gem install dalli’.

Use the following method to enable caching for all requests, and set Memcached as the default back-end.


For fine-grained control over the cache store (or to use multiple cache stores in the same codebase), you can use this API:

memcache_store ="localhost:11211")
hash_store = {}

r1 = "".to_uri(:cache_store => memcache_store).get
r2 = "".to_uri(:cache_store => hash_store).get

A detailed writeup regarding caching as defined by RFC 2616, and how Wrest implements caching is at Wrest Caching Doc

You can create your own back-ends for Wrest caching by implementing the interface implemented in

To explicitly disable caching for specific requests:

"".to_uri(:disable_cache => true).get


Uri level callbacks

You can define a set of callbacks that are invoked based on the http codes of the responses to any requests on a given uri.

"".to_uri(:callback => {
            200      => lambda {|response| "Ok." },
            400..499 => lambda {|response| Wrest.logger.error "Invalid. #{response.body}"},
            300..302 => lambda {|response| Wrest.logger.debug "Redirected. #{response.message}" }
Per request callbacks

You can also define callbacks that are invoked based on the http code of the response to a particular request.

"".to_uri.get do |callback|
  callback.on_ok do |response| "Ok."

  callback.on(202) do |response| "Accepted."

  callback.on(200..206) do |response| "Successful."

Please note that Wrest is a synchronous library. All requests are blocking, and will not return till the request is completed and appropriate callbacks executed.

Other useful stuff

Allows any class to hold an attributes hash, somewhat like ActiveResource. It also supports several extensions to this base fuctionality such as support for typecasting attribute values. See examples/twitter.rb and examples/wow_realm_status.rb for more samples.


class Demon
  include Wrest::Components::Container

  always_has       :id
  typecast         :age          =>  as_integer,
                   :chi          =>  lambda{|chi|}

  alias_accessors  :chi => :energy

kai_wren ='id' => '1', 'age' => '1500', 'chi' => '1024', 'teacher' => 'Viss')       # => '1'
kai_wren.age      # => 1500
kai_wren.chi      # => #<Chi:0x113af8c @count="1024">   # => #<Chi:0x113af8c @count="1024">
kai_wren.teacher  # => 'Viss'


The Wrest logger can be set and accessed through Wrest.logger and is configured by default to log to STDOUT. If you’re using Wrest in a Rails application, you can configure logging by adding a config/initializers/wrest.rb file with the following contents :

Wrest.logger = ActiveRecord::Base.logger


Standard options are available and can be listed using rake -T. Use rake:rcov for coverage and rake:rdoc to generate documentation. The link to the continuous integration build is over at the C42 Engineering open source page.


Wrest RDocs can be found at


Features that are planned, in progress or already implemented are documented in the CHANGELOG starting from version 0.0.8.


The source is available at git://

To install the Wrest gem, do (sudo) gem install wrest.

Wrest is currently available as a gem for for Ruby and JRuby.


You can launch the interactive Wrest shell by running bin/wrest if you have the source or invoking wrest from your prompt if you’ve installed the gem.

$ wrest
>> y ''.to_uri(:timeout => 5).get.deserialise


Start the Sinatra test server for functional test.

rake -f spec/sample_app/Rakefile

Run the tests in a different terminal:

# Run the normal test suite.

# Runs the functional test suite.
rake rspec:functional