alice

Use a Rack app as an HTTP client library. This is another exploration in the same vein as Faraday: github.com/technoweenie/faraday

Let me think: was I the same when I got up this morning? I almost think I can remember feeling a little different. But if I’m not the same, the next question is, Who in the world am I?

Usage

conn = Alice::Connection.new(:url => 'http://sushi.com') do
  use Alice::Request::Yajl     # convert body to json with Yajl lib
  use Alice::Adapter::Logger   # log the request somewhere?
  use Alice::Adapter::Typhoeus # make http request with typhoeus
  use Alice::Response::Yajl    # # parse body with yajl

  # or use shortcuts
  request  :yajl     # Alice::Request::Yajl
  adapter  :logger   # Alice::Adapter::Logger
  adapter  :typhoeus # Alice::Adapter::Typhoeus
  response :yajl     # Alice::Response::Yajl
end

resp1 = conn.get '/nigiri/sake.json'
resp2 = conn.post do |req|
  req.url  "/nigiri.json", :page => 2
  req[:content_type] = 'application/json'
  req.body = {:name => 'Unagi'}
end

Testing

test = Alice::Connection.new do
  adapter :test do |stub|
    stub.get '/nigiri/sake.json' do
      [200, {}, 'hi world']
    end
  end
end

resp = test.get '/nigiri/sake.json'
resp.body # => 'hi world'

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Running Tests

  • Yajl is needed for tests :(

  • Pass a LIVE env var to run it against a live server.

    > ruby test/live_server.rb # start the server > rake # no live tests > LIVE=1 rake # run with localhost:4567 > LIVE=foobar.dev:4567 rake # run with foobar.dev:4567

TODO

  • Add curb/em-http support

  • Add xml parsing

  • Support timeouts, proxy servers, ssl options

  • Add streaming requests and responses

  • Add default middleware load out for common cases

  • Add symbol => string index for mime types (:json => ‘application/json’)

Copyright © 2010 rick. See LICENSE for details.