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 |builder|
  builder.use Alice::Request::Yajl     # convert body to json with Yajl lib
  builder.use Alice::Adapter::Logger   # log the request somewhere?
  builder.use Alice::Adapter::Typhoeus # make http request with typhoeus
  builder.use Alice::Response::Yajl    # # parse body with yajl

  # or use shortcuts
  builder.request  :yajl     # Alice::Request::Yajl
  builder.adapter  :logger   # Alice::Adapter::Logger
  builder.adapter  :typhoeus # Alice::Adapter::Typhoeus
  builder.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

# It's possible to define stubbed request outside a test adapter block.
stubs = Alice::Test::Stubs.new do |stub|
  stub.get('/tamago') { [200, 'egg', {} }
end

# You can pass stubbed request to the test adapter or define them in a block
# or a combination of the two.
test = Alice::Connection.new do |builder|
  builder.adapter :test, stubs do |stub|
    stub.get('/ebi') {[ 200, 'shrimp', {} ]}
  end
end

# It's also possible to stub additional requests after the connection has
# been initialized. This is useful for testing.
stubs.get('/uni') {[ 200, 'urchin', {} ]}

resp = test.get '/tamago'
resp.body # => 'egg'
resp = test.get '/ebi'
resp.body # => 'shrimp'
resp = test.get '/uni'
resp.body # => 'urchin'
resp = test.get '/else' #=> raises "no such stub" error

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.