Examples

Basic Http Calls

GET

Basic API calls

'http://twitter.com/statuses/public_timeline.json'.to_uri.get.deserialise # works with json and xml out of the box
'http://twitter.com/statuses/public_timeline.xml'.to_uri.get.deserialise # see lib/wrest/components/translators to add other formats

Caching

Wrest::Caching.default_to_hash! # Hash should NEVER be used in a production environment. It is unbounded and will keep increasing in size.
c42 = "http://c42.in".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.

Wrest::Caching.default_to_memcached!

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

Per request callbacks

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

"http://google.com".to_uri.get do |callback|
callback.on_ok do |response|
Wrest.logger.info "Ok."
end
callback.on(202) do |response|
Wrest.logger.info "Accepted."
end
callback.on(200..206) do |response|
Wrest.logger.info "Successful."
end
end

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.

Asynchronous requests

Asynchronous requests are non-blocking. They do not return a response and
the request is executed on a separate thread. The only way to access the
response while using asynchronous request is through callbacks.

Asynchronous requests support pluggable backends. The default backend used
for asynchronous requests is ruby threads.

"http://c42.in".to_uri.get_async do |callback|
callback.on_ok do |response|
Wrest.logger.info "Ok."
end
end
sleep 1 # Causes thread created by get_async to execute

You can change the default to eventmachine.

Wrest::AsyncRequest.default_to_em!

You can also override the default on Uri objects.

"http://c42.in".to_uri.using_em.get_async do |callback|
callback.on_ok do |response|
Wrest.logger.info "Ok."
end
end

Note: The current implementation of asynchronous requests is currently in
alpha and should not be used in production.

Other useful stuff

Hash container with ActiveResource-like semantics

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.

Opt-out of core extensions

Logging

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 = Rails.logger

Json Backend

Wrest uses the multi_json gem to manage Json backends, allowing it to play
nice with Rails 3.1. To change the backend used, you can do the following:

Build

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.