faraday 0.11.0 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 88d17f843f96e641d4cb8b87b360b99adfee6c53
4
- data.tar.gz: 3c0031b2fa042939cc5e7f27aefa00f8ebe6a97f
3
+ metadata.gz: d6ed59b880f0d605e4efc8ce513ced0b4688c4ef
4
+ data.tar.gz: 21d3a0326416db1e19add553b8d5c0ef1c5a3b50
5
5
  SHA512:
6
- metadata.gz: 03fa51e8008930909fb2e5f40afe8f3d59d176574a084e364569d256b0c073bd06796bb0fe96f79c42cfacda2ee8417fecb1b3fc13e5b46bbb76f960136d116c
7
- data.tar.gz: 16432e693c7c666ac40de8a03ca890f08ee1cba705477afd31d64346282f018abf9ee617e1996330bbe2b2c3352021683e386522d700c49d08246b77d36fb65a
6
+ metadata.gz: 9e882a5072cc36a986e8fc2f23f0563755bae2d0c4e6fed4adc51a6fd46b449ea6be3dd189662ea0aa4f427c5402969bfb0ad626c837e9bad68d06aa0a3cc6d3
7
+ data.tar.gz: 63856c35cbcceda6abeb3dc076574d7e880343e8ee7af5e80971c08637a647a079433b1220161c24871d3718c268d018ebdce009ca46d89c4da7c059fdaeaf72
data/README.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Faraday
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/faraday.svg)](https://rubygems.org/gems/faraday)
4
+ [![Build Status](https://travis-ci.org/lostisland/faraday.svg)](https://travis-ci.org/lostisland/faraday)
5
+ [![Gitter](https://badges.gitter.im/lostisland/faraday.svg)](https://gitter.im/lostisland/faraday?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
6
+
7
+
3
8
  Faraday is an HTTP client lib that provides a common interface over many
4
9
  adapters (such as Net::HTTP) and embraces the concept of Rack middleware when
5
10
  processing the request/response cycle.
@@ -9,7 +14,6 @@ Faraday supports these adapters:
9
14
  * [Net::HTTP][net_http] _(default)_
10
15
  * [Net::HTTP::Persistent][persistent]
11
16
  * [Excon][]
12
- * [Typhoeus][]
13
17
  * [Patron][]
14
18
  * [EventMachine][]
15
19
  * [HTTPClient][]
@@ -23,6 +27,23 @@ Available at [rubydoc.info](http://www.rubydoc.info/gems/faraday).
23
27
 
24
28
  ## Usage
25
29
 
30
+ ### Basic Use
31
+
32
+ ```ruby
33
+ response = Faraday.get 'http://sushi.com/nigiri/sake.json'
34
+ ```
35
+ A simple `get` request can be performed by using the syntax described above. This works if you don't need to set up anything; you can roll with just the default middleware
36
+ stack and default adapter (see [Faraday::RackBuilder#initialize](https://github.com/lostisland/faraday/blob/master/lib/faraday/rack_builder.rb)).
37
+
38
+ A more flexible way to use Faraday is to start with a Connection object. If you want to keep the same defaults, you can use this syntax:
39
+
40
+ ```ruby
41
+ conn = Faraday.new(:url => 'http://www.example.com')
42
+ response = conn.get '/users' # GET http://www.example.com/users'
43
+ ```
44
+
45
+ Connections can also take an options hash as a parameter or be configured by using a block. Checkout the section called [Advanced middleware usage](#advanced-middleware-usage) for more details about how to use this block for configurations.
46
+
26
47
  ```ruby
27
48
  conn = Faraday.new(:url => 'http://sushi.com') do |faraday|
28
49
  faraday.request :url_encoded # form-encode POST params
@@ -36,8 +57,13 @@ conn = Faraday.new(:url => 'http://sushi.com/api_key=s3cr3t') do |faraday|
36
57
  faraday.response :logger do | logger |
37
58
  logger.filter(/(api_key=)(\w+)/,'\1[REMOVED]')
38
59
  end
60
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
39
61
  end
62
+ ```
63
+
64
+ Once you have the connection object, use it to make HTTP requests. You can pass paramters to it in a few different ways:
40
65
 
66
+ ```ruby
41
67
  ## GET ##
42
68
 
43
69
  response = conn.get '/nigiri/sake.json' # GET http://sushi.com/nigiri/sake.json
@@ -45,7 +71,7 @@ response.body
45
71
 
46
72
  conn.get '/nigiri', { :name => 'Maguro' } # GET http://sushi.com/nigiri?name=Maguro
47
73
 
48
- conn.get do |req| # GET http://sushi.com/search?page=2&limit=100
74
+ conn.get do |req| # GET http://sushi.com/search?page=2&limit=100
49
75
  req.url '/search', :page => 2
50
76
  req.params['limit'] = 100
51
77
  end
@@ -53,7 +79,11 @@ end
53
79
  ## POST ##
54
80
 
55
81
  conn.post '/nigiri', { :name => 'Maguro' } # POST "name=maguro" to http://sushi.com/nigiri
82
+ ```
83
+
84
+ Some configuration options can be adjusted per request:
56
85
 
86
+ ```ruby
57
87
  # post payload as JSON instead of "www-form-urlencoded" encoding:
58
88
  conn.post do |req|
59
89
  req.url '/nigiri'
@@ -70,11 +100,18 @@ conn.get do |req|
70
100
  end
71
101
  ```
72
102
 
73
- If you don't need to set up anything, you can roll with just the default middleware
74
- stack and default adapter (see [Faraday::RackBuilder#initialize](https://github.com/lostisland/faraday/blob/master/lib/faraday/rack_builder.rb)):
103
+ And you can inject arbitrary data into the request using the `context` option:
75
104
 
76
105
  ```ruby
77
- response = Faraday.get 'http://sushi.com/nigiri/sake.json'
106
+ # Anything you inject using context option will be available in the env on all middlewares
107
+
108
+ conn.get do |req|
109
+ req.url '/search'
110
+ req.options.context = {
111
+ foo: 'foo',
112
+ bar: 'bar'
113
+ }
114
+ end
78
115
  ```
79
116
 
80
117
  ### Changing how parameters are serialized
@@ -177,6 +214,53 @@ later, response. Some keys are:
177
214
  :response_headers
178
215
  ```
179
216
 
217
+ ## Ad-hoc adapters customization
218
+
219
+ Faraday is intended to be a generic interface between your code and the adapter. However, sometimes you need to access a feature specific to one of the adapters that is not covered in Faraday's interface.
220
+
221
+ When that happens, you can pass a block when specifying the adapter to customize it. The block parameter will change based on the adapter you're using. See below for some examples.
222
+
223
+ ### NetHttp
224
+ ```ruby
225
+ conn = Faraday.new(...) do |f|
226
+ f.adapter :net_http do |http| # yields Net::HTTP
227
+ http.idle_timeout = 100
228
+ http.verify_callback = lambda do | preverify_ok, cert_store |
229
+ # do something here...
230
+ end
231
+ end
232
+ end
233
+ ```
234
+
235
+ ### NetHttpPersistent
236
+ ```ruby
237
+ conn = Faraday.new(...) do |f|
238
+ f.adapter :net_http_persistent do |http| # yields Net::HTTP::Persistent
239
+ http.idle_timeout = 100
240
+ http.retry_change_requests = true
241
+ end
242
+ end
243
+ ```
244
+
245
+ ### Patron
246
+ ```ruby
247
+ conn = Faraday.new(...) do |f|
248
+ f.adapter :patron do |session| # yields Patron::Session
249
+ session.max_redirects = 10
250
+ end
251
+ end
252
+ ```
253
+
254
+ ### HTTPClient
255
+ ```ruby
256
+ conn = Faraday.new(...) do |f|
257
+ f.adapter :httpclient do |client| # yields HTTPClient
258
+ client.keep_alive_timeout = 20
259
+ client.ssl_config.timeout = 25
260
+ end
261
+ end
262
+ ```
263
+
180
264
  ## Using Faraday for testing
181
265
 
182
266
  ```ruby
@@ -248,7 +332,6 @@ See [LICENSE][] for details.
248
332
  [persistent]: https://github.com/drbrain/net-http-persistent
249
333
  [travis]: https://travis-ci.org/lostisland/faraday
250
334
  [excon]: https://github.com/excon/excon#readme
251
- [typhoeus]: https://github.com/typhoeus/typhoeus#readme
252
335
  [patron]: http://toland.github.io/patron/
253
336
  [eventmachine]: https://github.com/igrigorik/em-http-request#readme
254
337
  [httpclient]: https://github.com/nahi/httpclient
data/lib/faraday.rb CHANGED
@@ -14,7 +14,7 @@ require 'forwardable'
14
14
  # conn.get '/'
15
15
  #
16
16
  module Faraday
17
- VERSION = "0.11.0"
17
+ VERSION = "0.12.0"
18
18
 
19
19
  class << self
20
20
  # Public: Gets or sets the root path that Faraday is being loaded from.
@@ -63,7 +63,7 @@ module Faraday
63
63
  # Returns a Faraday::Connection.
64
64
  def new(url = nil, options = nil)
65
65
  block = block_given? ? Proc.new : nil
66
- options = options ? default_connection_options.merge(options) : default_connection_options.dup
66
+ options = options ? default_connection_options.merge(options) : default_connection_options
67
67
  Faraday::Connection.new(url, options, &block)
68
68
  end
69
69
 
@@ -55,7 +55,7 @@ module Faraday
55
55
  # :user - String (optional)
56
56
  # :password - String (optional)
57
57
  def initialize(url = nil, options = nil)
58
- options = ConnectionOptions.from(options) unless options and options.is_a?(ConnectionOptions)
58
+ options = ConnectionOptions.from(options)
59
59
 
60
60
  if url.is_a?(Hash)
61
61
  options = options.merge(url)
@@ -81,11 +81,20 @@ module Faraday
81
81
 
82
82
  @proxy = nil
83
83
  proxy(options.fetch(:proxy) {
84
- uri = ENV['http_proxy']
85
- if uri && !uri.empty?
86
- uri = 'http://' + uri if uri !~ /^http/i
87
- uri
84
+ uri = nil
85
+ if URI.parse("").respond_to?(:find_proxy)
86
+ case url
87
+ when String
88
+ uri = URI.parse(url).find_proxy
89
+ when URI
90
+ uri = url.find_proxy
91
+ when nil
92
+ uri = find_default_proxy
93
+ end
94
+ else
95
+ uri = find_default_proxy
88
96
  end
97
+ uri
89
98
  })
90
99
 
91
100
  yield(self) if block_given?
@@ -433,5 +442,14 @@ module Faraday
433
442
  header(*args)
434
443
  headers[Faraday::Request::Authorization::KEY] = header
435
444
  end
445
+
446
+ def find_default_proxy
447
+ warn 'no_proxy is unsupported' if ENV['no_proxy'] || ENV['NO_PROXY']
448
+ uri = ENV['http_proxy']
449
+ if uri && !uri.empty?
450
+ uri = 'http://' + uri if uri !~ /^http/i
451
+ uri
452
+ end
453
+ end
436
454
  end
437
455
  end
@@ -18,23 +18,20 @@ module Faraday
18
18
  # Public
19
19
  def update(obj)
20
20
  obj.each do |key, value|
21
- if sub_options = self.class.options_for(key)
22
- value = sub_options.from(value) if value
23
- elsif Hash === value
24
- hash = {}
25
- value.each do |hash_key, hash_value|
26
- hash[hash_key] = hash_value
27
- end
28
- value = hash
21
+ sub_options = self.class.options_for(key)
22
+ if sub_options
23
+ new_value = sub_options.from(value) if value
24
+ elsif value.is_a?(Hash)
25
+ new_value = value.dup
26
+ else
27
+ new_value = value
29
28
  end
30
29
 
31
- self.send("#{key}=", value) unless value.nil?
30
+ self.send("#{key}=", new_value) unless new_value.nil?
32
31
  end
33
32
  self
34
33
  end
35
34
 
36
- alias merge! update
37
-
38
35
  # Public
39
36
  def delete(key)
40
37
  value = send(key)
@@ -48,17 +45,26 @@ module Faraday
48
45
  end
49
46
 
50
47
  # Public
51
- def merge(value)
52
- dup.update(value)
48
+ def merge!(other)
49
+ other.each do |key, other_value|
50
+ self_value = self.send(key)
51
+ sub_options = self.class.options_for(key)
52
+ new_value = sub_options ? self_value.merge(other_value) : other_value
53
+ self.send("#{key}=", new_value) unless new_value.nil?
54
+ end
55
+ self
56
+ end
57
+
58
+ # Public
59
+ def merge(other)
60
+ dup.merge!(other)
53
61
  end
54
-
62
+
55
63
  # Public
56
- def dup
64
+ def deep_dup
57
65
  self.class.from(self)
58
66
  end
59
67
 
60
- alias clone dup
61
-
62
68
  # Public
63
69
  def fetch(key, *args)
64
70
  unless symbolized_key_set.include?(key.to_sym)
@@ -196,8 +202,7 @@ module Faraday
196
202
  end
197
203
 
198
204
  class RequestOptions < Options.new(:params_encoder, :proxy, :bind,
199
- :timeout, :open_timeout, :boundary,
200
- :oauth)
205
+ :timeout, :open_timeout, :boundary, :oauth, :context)
201
206
 
202
207
  def []=(key, value)
203
208
  if key && key.to_sym == :proxy
@@ -1,13 +1,14 @@
1
1
  require File.expand_path("../url_encoded", __FILE__)
2
+ require 'securerandom'
2
3
 
3
4
  module Faraday
4
5
  class Request::Multipart < Request::UrlEncoded
5
6
  self.mime_type = 'multipart/form-data'.freeze
6
- DEFAULT_BOUNDARY = "-----------RubyMultipartPost".freeze unless defined? DEFAULT_BOUNDARY
7
+ DEFAULT_BOUNDARY_PREFIX = "-----------RubyMultipartPost".freeze unless defined? DEFAULT_BOUNDARY_PREFIX
7
8
 
8
9
  def call(env)
9
10
  match_content_type(env) do |params|
10
- env.request.boundary ||= DEFAULT_BOUNDARY
11
+ env.request.boundary ||= unique_boundary
11
12
  env.request_headers[CONTENT_TYPE] += "; boundary=#{env.request.boundary}"
12
13
  env.body = create_multipart(env, params)
13
14
  end
@@ -44,6 +45,10 @@ module Faraday
44
45
  return body
45
46
  end
46
47
 
48
+ def unique_boundary
49
+ "#{DEFAULT_BOUNDARY_PREFIX}-#{SecureRandom.hex}"
50
+ end
51
+
47
52
  def process_params(params, prefix = nil, pieces = nil, &block)
48
53
  params.inject(pieces || []) do |all, (key, value)|
49
54
  key = "#{prefix}[#{key}]" if prefix
@@ -10,7 +10,7 @@ module Faraday
10
10
  #
11
11
  # Faraday.new do |conn|
12
12
  # conn.request :retry, max: 2, interval: 0.05,
13
- # interval_randomness: 0.5, backoff_factor: 2
13
+ # interval_randomness: 0.5, backoff_factor: 2,
14
14
  # exceptions: [CustomException, 'Timeout::Error']
15
15
  # conn.adapter ...
16
16
  # end
@@ -117,6 +117,7 @@ module Faraday
117
117
  rescue @errmatch => exception
118
118
  if retries > 0 && retry_request?(env, exception)
119
119
  retries -= 1
120
+ rewind_files(request_body)
120
121
  sleep sleep_amount(retries + 1)
121
122
  retry
122
123
  end
@@ -150,5 +151,13 @@ module Faraday
150
151
  @options.methods.include?(env[:method]) || @options.retry_if.call(env, exception)
151
152
  end
152
153
 
154
+ def rewind_files(env)
155
+ env && env.each do |_, value|
156
+ if value.is_a? UploadIO
157
+ value.rewind
158
+ end
159
+ end
160
+ end
161
+
153
162
  end
154
163
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rick Olson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-13 00:00:00.000000000 Z
11
+ date: 2017-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multipart-post