diff --git a/README.md b/README.md index 0d943e257..1aeea30ec 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,7 @@ conn = Faraday.new(:url => 'http://sushi.com/api_key=s3cr3t') do |faraday| faraday.adapter Faraday.default_adapter # make requests with Net::HTTP end ``` + Once you have the connection object, use it to make HTTP requests. You can pass paramters to it in a few different ways: ```ruby @@ -79,7 +80,9 @@ end conn.post '/nigiri', { :name => 'Maguro' } # POST "name=maguro" to http://sushi.com/nigiri ``` + Some configuration options can be adjusted per request: + ```ruby # post payload as JSON instead of "www-form-urlencoded" encoding: conn.post do |req| @@ -97,6 +100,20 @@ conn.get do |req| end ``` +And you can inject arbitrary data into the request using the `context` option: + +```ruby +# Anything you inject using context option will be available in the env on all middlewares + +conn.get do |req| + req.url '/search' + req.options.context = { + foo: 'foo', + bar: 'bar' + } +end +``` + ### Changing how parameters are serialized Sometimes you need to send the same URL parameter multiple times with different diff --git a/lib/faraday.rb b/lib/faraday.rb index 3926a66b6..4ea194bb0 100644 --- a/lib/faraday.rb +++ b/lib/faraday.rb @@ -14,7 +14,7 @@ # conn.get '/' # module Faraday - VERSION = "0.11.0" + VERSION = "0.12.0" class << self # Public: Gets or sets the root path that Faraday is being loaded from. diff --git a/lib/faraday/options.rb b/lib/faraday/options.rb index b79db637e..c41550602 100644 --- a/lib/faraday/options.rb +++ b/lib/faraday/options.rb @@ -202,8 +202,7 @@ def self.fetch_error_class end class RequestOptions < Options.new(:params_encoder, :proxy, :bind, - :timeout, :open_timeout, :boundary, - :oauth) + :timeout, :open_timeout, :boundary, :oauth, :context) def []=(key, value) if key && key.to_sym == :proxy diff --git a/test/env_test.rb b/test/env_test.rb index d25eb73be..146bc3307 100644 --- a/test/env_test.rb +++ b/test/env_test.rb @@ -58,10 +58,16 @@ def test_per_request_options req.options.timeout = 10 req.options.boundary = 'boo' req.options.oauth[:consumer_secret] = 'xyz' + req.options.context = { + foo: 'foo', + bar: 'bar' + } end + assert_equal 10, env.request.timeout assert_equal 5, env.request.open_timeout assert_equal 'boo', env.request.boundary + assert_equal env.request.context, { foo: 'foo', bar: 'bar' } oauth_expected = {:consumer_secret => 'xyz', :consumer_key => 'anonymous'} assert_equal oauth_expected, env.request.oauth