Skip to content

Commit

Permalink
Adds support for context request options, including test coverage a…
Browse files Browse the repository at this point in the history
…nd Readme section.

Version bump 0.12.0
  • Loading branch information
iMacTia committed Mar 21, 2017
1 parent 3268aca commit 617c909
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -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
Expand All @@ -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|
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/faraday.rb
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions lib/faraday/options.rb
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/env_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 617c909

Please sign in to comment.