faraday 0.16.0 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.md +1 -1
  3. data/README.md +347 -18
  4. data/lib/faraday/adapter/em_http.rb +99 -142
  5. data/lib/faraday/adapter/em_http_ssl_patch.rb +17 -23
  6. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +15 -18
  7. data/lib/faraday/adapter/em_synchrony.rb +60 -104
  8. data/lib/faraday/adapter/excon.rb +55 -100
  9. data/lib/faraday/adapter/httpclient.rb +39 -61
  10. data/lib/faraday/adapter/net_http.rb +51 -104
  11. data/lib/faraday/adapter/net_http_persistent.rb +27 -48
  12. data/lib/faraday/adapter/patron.rb +35 -54
  13. data/lib/faraday/adapter/rack.rb +12 -28
  14. data/lib/faraday/adapter/test.rb +53 -86
  15. data/lib/faraday/adapter/typhoeus.rb +1 -4
  16. data/lib/faraday/adapter.rb +22 -36
  17. data/lib/faraday/autoload.rb +36 -47
  18. data/lib/faraday/connection.rb +179 -321
  19. data/lib/faraday/error.rb +33 -67
  20. data/lib/faraday/middleware.rb +28 -4
  21. data/lib/faraday/options.rb +186 -35
  22. data/lib/faraday/parameters.rb +197 -4
  23. data/lib/faraday/rack_builder.rb +56 -67
  24. data/lib/faraday/request/authorization.rb +30 -42
  25. data/lib/faraday/request/basic_authentication.rb +7 -14
  26. data/lib/faraday/request/instrumentation.rb +27 -45
  27. data/lib/faraday/request/multipart.rb +48 -79
  28. data/lib/faraday/request/retry.rb +170 -197
  29. data/lib/faraday/request/token_authentication.rb +10 -15
  30. data/lib/faraday/request/url_encoded.rb +23 -41
  31. data/lib/faraday/request.rb +36 -68
  32. data/lib/faraday/response/logger.rb +69 -22
  33. data/lib/faraday/response/raise_error.rb +14 -36
  34. data/lib/faraday/response.rb +16 -23
  35. data/lib/faraday/upload_io.rb +67 -0
  36. data/lib/faraday/utils.rb +245 -28
  37. data/lib/faraday.rb +175 -93
  38. metadata +5 -22
  39. data/lib/faraday/adapter_registry.rb +0 -28
  40. data/lib/faraday/dependency_loader.rb +0 -37
  41. data/lib/faraday/encoders/flat_params_encoder.rb +0 -94
  42. data/lib/faraday/encoders/nested_params_encoder.rb +0 -171
  43. data/lib/faraday/file_part.rb +0 -128
  44. data/lib/faraday/logging/formatter.rb +0 -92
  45. data/lib/faraday/middleware_registry.rb +0 -129
  46. data/lib/faraday/options/connection_options.rb +0 -22
  47. data/lib/faraday/options/env.rb +0 -181
  48. data/lib/faraday/options/proxy_options.rb +0 -28
  49. data/lib/faraday/options/request_options.rb +0 -21
  50. data/lib/faraday/options/ssl_options.rb +0 -59
  51. data/lib/faraday/param_part.rb +0 -53
  52. data/lib/faraday/utils/headers.rb +0 -139
  53. data/lib/faraday/utils/params_hash.rb +0 -61
  54. data/spec/external_adapters/faraday_specs_setup.rb +0 -14
@@ -1,10 +1,8 @@
1
- # frozen_string_literal: true
2
-
3
1
  module Faraday
4
- # Connection objects manage the default properties and the middleware
2
+ # Public: Connection objects manage the default properties and the middleware
5
3
  # stack for fulfilling an HTTP request.
6
4
  #
7
- # @example
5
+ # Examples
8
6
  #
9
7
  # conn = Faraday::Connection.new 'http://sushi.com'
10
8
  #
@@ -14,51 +12,51 @@ module Faraday
14
12
  #
15
13
  class Connection
16
14
  # A Set of allowed HTTP verbs.
17
- METHODS = Set.new %i[get post put delete head patch options trace connect]
15
+ METHODS = Set.new [:get, :post, :put, :delete, :head, :patch, :options]
18
16
 
19
- # @return [Hash] URI query unencoded key/value pairs.
17
+ # Public: Returns a Hash of URI query unencoded key/value pairs.
20
18
  attr_reader :params
21
19
 
22
- # @return [Hash] unencoded HTTP header key/value pairs.
20
+ # Public: Returns a Hash of unencoded HTTP header key/value pairs.
23
21
  attr_reader :headers
24
22
 
25
- # @return [String] a URI with the prefix used for all requests from this
26
- # Connection. This includes a default host name, scheme, port, and path.
23
+ # Public: Returns a URI with the prefix used for all requests from this
24
+ # Connection. This includes a default host name, scheme, port, and path.
27
25
  attr_reader :url_prefix
28
26
 
29
- # @return [Faraday::Builder] Builder for this Connection.
27
+ # Public: Returns the Faraday::Builder for this Connection.
30
28
  attr_reader :builder
31
29
 
32
- # @return [Hash] SSL options.
30
+ # Public: Returns a Hash of the request options.
31
+ attr_reader :options
32
+
33
+ # Public: Returns a Hash of the SSL options.
33
34
  attr_reader :ssl
34
35
 
35
- # @return [Object] the parallel manager for this Connection.
36
+ # Public: Returns the parallel manager for this Connection.
36
37
  attr_reader :parallel_manager
37
38
 
38
- # Sets the default parallel manager for this connection.
39
+ # Public: Sets the default parallel manager for this connection.
39
40
  attr_writer :default_parallel_manager
40
41
 
41
- # @return [Hash] proxy options.
42
- attr_reader :proxy
42
+ # Public: Gets or Sets the Hash proxy options.
43
+ # attr_reader :proxy
43
44
 
44
- # Initializes a new Faraday::Connection.
45
+ # Public: Initializes a new Faraday::Connection.
45
46
  #
46
- # @param url [URI, String] URI or String base URL to use as a prefix for all
47
+ # url - URI or String base URL to use as a prefix for all
47
48
  # requests (optional).
48
- # @param options [Hash, Faraday::ConnectionOptions]
49
- # @option options [URI, String] :url ('http:/') URI or String base URL
50
- # @option options [Hash<String => String>] :params URI query unencoded
51
- # key/value pairs.
52
- # @option options [Hash<String => String>] :headers Hash of unencoded HTTP
53
- # header key/value pairs.
54
- # @option options [Hash] :request Hash of request options.
55
- # @option options [Hash] :ssl Hash of SSL options.
56
- # @option options [Hash, URI, String] :proxy proxy options, either as a URL
57
- # or as a Hash
58
- # @option options [URI, String] :proxy[:uri]
59
- # @option options [String] :proxy[:user]
60
- # @option options [String] :proxy[:password]
61
- # @yield [self] after all setup has been done
49
+ # options - Hash or Faraday::ConnectionOptions.
50
+ # :url - URI or String base URL (default: "http:/").
51
+ # :params - Hash of URI query unencoded key/value pairs.
52
+ # :headers - Hash of unencoded HTTP header key/value pairs.
53
+ # :request - Hash of request options.
54
+ # :ssl - Hash of SSL options.
55
+ # :proxy - URI, String or Hash of HTTP proxy options
56
+ # (default: "http_proxy" environment variable).
57
+ # :uri - URI or String
58
+ # :user - String (optional)
59
+ # :password - String (optional)
62
60
  def initialize(url = nil, options = nil)
63
61
  options = ConnectionOptions.from(options)
64
62
 
@@ -76,7 +74,7 @@ module Faraday
76
74
 
77
75
  @builder = options.builder || begin
78
76
  # pass an empty block to Builder so it doesn't assume default middleware
79
- options.new_builder(block_given? ? proc { |b| } : nil)
77
+ options.new_builder(block_given? ? Proc.new { |b| } : nil)
80
78
  end
81
79
 
82
80
  self.url_prefix = url || 'http:/'
@@ -84,32 +82,21 @@ module Faraday
84
82
  @params.update(options.params) if options.params
85
83
  @headers.update(options.headers) if options.headers
86
84
 
87
- initialize_proxy(url, options)
85
+ @manual_proxy = !!options.proxy
86
+ @proxy = options.proxy ? ProxyOptions.from(options.proxy) : proxy_from_env(url)
87
+ @temp_proxy = @proxy
88
88
 
89
89
  yield(self) if block_given?
90
90
 
91
91
  @headers[:user_agent] ||= "Faraday v#{VERSION}"
92
92
  end
93
93
 
94
- def initialize_proxy(url, options)
95
- @manual_proxy = !!options.proxy
96
- @proxy =
97
- if options.proxy
98
- ProxyOptions.from(options.proxy)
99
- else
100
- proxy_from_env(url)
101
- end
102
- @temp_proxy = @proxy
103
- end
104
-
105
- # Sets the Hash of URI query unencoded key/value pairs.
106
- # @param hash [Hash]
94
+ # Public: Sets the Hash of URI query unencoded key/value pairs.
107
95
  def params=(hash)
108
96
  @params.replace hash
109
97
  end
110
98
 
111
- # Sets the Hash of unencoded HTTP header key/value pairs.
112
- # @param hash [Hash]
99
+ # Public: Sets the Hash of unencoded HTTP header key/value pairs.
113
100
  def headers=(hash)
114
101
  @headers.replace hash
115
102
  end
@@ -118,171 +105,71 @@ module Faraday
118
105
 
119
106
  def_delegators :builder, :build, :use, :request, :response, :adapter, :app
120
107
 
121
- # @!method get(url = nil, params = nil, headers = nil)
122
- # Makes a GET HTTP request without a body.
123
- # @!scope class
108
+ # Public: Makes an HTTP request without a body.
124
109
  #
125
- # @param url [String] The optional String base URL to use as a prefix for
126
- # all requests. Can also be the options Hash.
127
- # @param params [Hash] Hash of URI query unencoded key/value pairs.
128
- # @param headers [Hash] unencoded HTTP header key/value pairs.
110
+ # url - The optional String base URL to use as a prefix for all
111
+ # requests. Can also be the options Hash.
112
+ # params - Hash of URI query unencoded key/value pairs.
113
+ # headers - Hash of unencoded HTTP header key/value pairs.
129
114
  #
130
- # @example
131
- # conn.get '/items', { page: 1 }, :accept => 'application/json'
115
+ # Examples
116
+ #
117
+ # conn.get '/items', {:page => 1}, :accept => 'application/json'
118
+ # conn.head '/items/1'
132
119
  #
133
120
  # # ElasticSearch example sending a body with GET.
134
121
  # conn.get '/twitter/tweet/_search' do |req|
135
122
  # req.headers[:content_type] = 'application/json'
136
123
  # req.params[:routing] = 'kimchy'
137
- # req.body = JSON.generate(query: {...})
124
+ # req.body = JSON.generate(:query => {...})
138
125
  # end
139
126
  #
140
- # @yield [Faraday::Request] for further request customizations
141
- # @return [Faraday::Response]
142
-
143
- # @!method head(url = nil, params = nil, headers = nil)
144
- # Makes a HEAD HTTP request without a body.
145
- # @!scope class
146
- #
147
- # @param url [String] The optional String base URL to use as a prefix for
148
- # all requests. Can also be the options Hash.
149
- # @param params [Hash] Hash of URI query unencoded key/value pairs.
150
- # @param headers [Hash] unencoded HTTP header key/value pairs.
151
- #
152
- # @example
153
- # conn.head '/items/1'
154
- #
155
- # @yield [Faraday::Request] for further request customizations
156
- # @return [Faraday::Response]
157
-
158
- # @!method delete(url = nil, params = nil, headers = nil)
159
- # Makes a DELETE HTTP request without a body.
160
- # @!scope class
161
- #
162
- # @param url [String] The optional String base URL to use as a prefix for
163
- # all requests. Can also be the options Hash.
164
- # @param params [Hash] Hash of URI query unencoded key/value pairs.
165
- # @param headers [Hash] unencoded HTTP header key/value pairs.
166
- #
167
- # @example
168
- # conn.delete '/items/1'
127
+ # Yields a Faraday::Request for further request customizations.
128
+ # Returns a Faraday::Response.
169
129
  #
170
- # @yield [Faraday::Request] for further request customizations
171
- # @return [Faraday::Response]
172
-
173
- # @!method connect(url = nil, params = nil, headers = nil)
174
- # Makes a CONNECT HTTP request without a body.
175
- # @!scope class
130
+ # Signature
176
131
  #
177
- # @param url [String] The optional String base URL to use as a prefix for
178
- # all requests. Can also be the options Hash.
179
- # @param params [Hash] Hash of URI query unencoded key/value pairs.
180
- # @param headers [Hash] unencoded HTTP header key/value pairs.
132
+ # <verb>(url = nil, params = nil, headers = nil)
181
133
  #
182
- # @example
183
- # conn.connect '/items/1'
184
- #
185
- # @yield [Faraday::Request] for further request customizations
186
- # @return [Faraday::Response]
187
-
188
- # @!method trace(url = nil, params = nil, headers = nil)
189
- # Makes a TRACE HTTP request without a body.
190
- # @!scope class
191
- #
192
- # @param url [String] The optional String base URL to use as a prefix for
193
- # all requests. Can also be the options Hash.
194
- # @param params [Hash] Hash of URI query unencoded key/value pairs.
195
- # @param headers [Hash] unencoded HTTP header key/value pairs.
196
- #
197
- # @example
198
- # conn.connect '/items/1'
199
- #
200
- # @yield [Faraday::Request] for further request customizations
201
- # @return [Faraday::Response]
202
-
203
- # @!visibility private
204
- METHODS_WITH_QUERY.each do |method|
134
+ # verb - An HTTP verb: get, head, or delete.
135
+ %w[get head delete].each do |method|
205
136
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
206
137
  def #{method}(url = nil, params = nil, headers = nil)
207
- run_request(:#{method}, url, nil, headers) do |request|
138
+ run_request(:#{method}, url, nil, headers) { |request|
208
139
  request.params.update(params) if params
209
- yield request if block_given?
210
- end
140
+ yield(request) if block_given?
141
+ }
211
142
  end
212
143
  RUBY
213
144
  end
214
145
 
215
- # @overload options()
216
- # Returns current Connection options.
146
+ # Public: Makes an HTTP request with a body.
217
147
  #
218
- # @overload options(url, params = nil, headers = nil)
219
- # Makes an OPTIONS HTTP request to the given URL.
220
- # @param url [String] String base URL to sue as a prefix for all requests.
221
- # @param params [Hash] Hash of URI query unencoded key/value pairs.
222
- # @param headers [Hash] unencoded HTTP header key/value pairs.
148
+ # url - The optional String base URL to use as a prefix for all
149
+ # requests. Can also be the options Hash.
150
+ # body - The String body for the request.
151
+ # headers - Hash of unencoded HTTP header key/value pairs.
223
152
  #
224
- # @example
225
- # conn.options '/items/1'
153
+ # Examples
226
154
  #
227
- # @yield [Faraday::Request] for further request customizations
228
- # @return [Faraday::Response]
229
- def options(*args)
230
- return @options if args.size.zero?
231
-
232
- url, params, headers = *args
233
- run_request(:options, url, nil, headers) do |request|
234
- request.params.update(params) if params
235
- yield request if block_given?
236
- end
237
- end
238
-
239
- # @!method post(url = nil, body = nil, headers = nil)
240
- # Makes a POST HTTP request with a body.
241
- # @!scope class
242
- #
243
- # @param url [String] The optional String base URL to use as a prefix for
244
- # all requests. Can also be the options Hash.
245
- # @param body [String] body for the request.
246
- # @param headers [Hash] unencoded HTTP header key/value pairs.
247
- #
248
- # @example
249
- # conn.post '/items', data, content_type: 'application/json'
155
+ # conn.post '/items', data, :content_type => 'application/json'
250
156
  #
251
157
  # # Simple ElasticSearch indexing sample.
252
158
  # conn.post '/twitter/tweet' do |req|
253
159
  # req.headers[:content_type] = 'application/json'
254
160
  # req.params[:routing] = 'kimchy'
255
- # req.body = JSON.generate(user: 'kimchy', ...)
161
+ # req.body = JSON.generate(:user => 'kimchy', ...)
256
162
  # end
257
163
  #
258
- # @yield [Faraday::Request] for further request customizations
259
- # @return [Faraday::Response]
260
-
261
- # @!method put(url = nil, body = nil, headers = nil)
262
- # Makes a PUT HTTP request with a body.
263
- # @!scope class
164
+ # Yields a Faraday::Request for further request customizations.
165
+ # Returns a Faraday::Response.
264
166
  #
265
- # @param url [String] The optional String base URL to use as a prefix for
266
- # all requests. Can also be the options Hash.
267
- # @param body [String] body for the request.
268
- # @param headers [Hash] unencoded HTTP header key/value pairs.
167
+ # Signature
269
168
  #
270
- # @example
271
- # # TODO: Make it a PUT example
272
- # conn.post '/items', data, content_type: 'application/json'
169
+ # <verb>(url = nil, body = nil, headers = nil)
273
170
  #
274
- # # Simple ElasticSearch indexing sample.
275
- # conn.post '/twitter/tweet' do |req|
276
- # req.headers[:content_type] = 'application/json'
277
- # req.params[:routing] = 'kimchy'
278
- # req.body = JSON.generate(user: 'kimchy', ...)
279
- # end
280
- #
281
- # @yield [Faraday::Request] for further request customizations
282
- # @return [Faraday::Response]
283
-
284
- # @!visibility private
285
- METHODS_WITH_BODY.each do |method|
171
+ # verb - An HTTP verb: post, put, or patch.
172
+ %w[post put patch].each do |method|
286
173
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
287
174
  def #{method}(url = nil, body = nil, headers = nil, &block)
288
175
  run_request(:#{method}, url, body, headers, &block)
@@ -290,110 +177,116 @@ module Faraday
290
177
  RUBY
291
178
  end
292
179
 
293
- # Sets up the Authorization header with these credentials, encoded
180
+ # Public: Sets up the Authorization header with these credentials, encoded
294
181
  # with base64.
295
182
  #
296
- # @param login [String] The authentication login.
297
- # @param pass [String] The authentication password.
183
+ # login - The authentication login.
184
+ # pass - The authentication password.
298
185
  #
299
- # @example
186
+ # Examples
300
187
  #
301
188
  # conn.basic_auth 'Aladdin', 'open sesame'
302
189
  # conn.headers['Authorization']
303
190
  # # => "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
304
191
  #
305
- # @return [void]
192
+ # Returns nothing.
306
193
  def basic_auth(login, pass)
307
194
  set_authorization_header(:basic_auth, login, pass)
308
195
  end
309
196
 
310
- # Sets up the Authorization header with the given token.
197
+ # Public: Sets up the Authorization header with the given token.
311
198
  #
312
- # @param token [String]
313
- # @param options [Hash] extra token options.
199
+ # token - The String token.
200
+ # options - Optional Hash of extra token options.
314
201
  #
315
- # @example
202
+ # Examples
316
203
  #
317
- # conn.token_auth 'abcdef', foo: 'bar'
204
+ # conn.token_auth 'abcdef', :foo => 'bar'
318
205
  # conn.headers['Authorization']
319
206
  # # => "Token token=\"abcdef\",
320
207
  # foo=\"bar\""
321
208
  #
322
- # @return [void]
209
+ # Returns nothing.
323
210
  def token_auth(token, options = nil)
324
211
  set_authorization_header(:token_auth, token, options)
325
212
  end
326
213
 
327
- # Sets up a custom Authorization header.
214
+ # Public: Sets up a custom Authorization header.
328
215
  #
329
- # @param type [String] authorization type
330
- # @param token [String, Hash] token. A String value is taken literally, and
331
- # a Hash is encoded into comma-separated key/value pairs.
216
+ # type - The String authorization type.
217
+ # token - The String or Hash token. A String value is taken literally, and
218
+ # a Hash is encoded into comma separated key/value pairs.
332
219
  #
333
- # @example
220
+ # Examples
334
221
  #
335
222
  # conn.authorization :Bearer, 'mF_9.B5f-4.1JqM'
336
223
  # conn.headers['Authorization']
337
224
  # # => "Bearer mF_9.B5f-4.1JqM"
338
225
  #
339
- # conn.authorization :Token, token: 'abcdef', foo: 'bar'
226
+ # conn.authorization :Token, :token => 'abcdef', :foo => 'bar'
340
227
  # conn.headers['Authorization']
341
228
  # # => "Token token=\"abcdef\",
342
229
  # foo=\"bar\""
343
230
  #
344
- # @return [void]
231
+ # Returns nothing.
345
232
  def authorization(type, token)
346
233
  set_authorization_header(:authorization, type, token)
347
234
  end
348
235
 
349
- # Check if the adapter is parallel-capable.
236
+ # Internal: Traverse the middleware stack in search of a
237
+ # parallel-capable adapter.
350
238
  #
351
- # @yield if the adapter isn't parallel-capable, or if no adapter is set yet.
239
+ # Yields in case of not found.
352
240
  #
353
- # @return [Object, nil] a parallel manager or nil if yielded
354
- # @api private
241
+ # Returns a parallel manager or nil if not found.
355
242
  def default_parallel_manager
356
243
  @default_parallel_manager ||= begin
357
- adapter = @builder.adapter.klass if @builder.adapter
244
+ handler = @builder.handlers.detect do |h|
245
+ h.klass.respond_to?(:supports_parallel?) and h.klass.supports_parallel?
246
+ end
358
247
 
359
- if support_parallel?(adapter)
360
- adapter.setup_parallel_manager
248
+ if handler
249
+ handler.klass.setup_parallel_manager
361
250
  elsif block_given?
362
251
  yield
363
252
  end
364
253
  end
365
254
  end
366
255
 
367
- # Determine if this Faraday::Connection can make parallel requests.
256
+ # Public: Determine if this Faraday::Connection can make parallel requests.
368
257
  #
369
- # @return [Boolean]
258
+ # Returns true or false.
370
259
  def in_parallel?
371
260
  !!@parallel_manager
372
261
  end
373
262
 
374
- # Sets up the parallel manager to make a set of requests.
263
+ # Public: Sets up the parallel manager to make a set of requests.
375
264
  #
376
- # @param manager [Object] The parallel manager that this Connection's
377
- # Adapter uses.
265
+ # manager - The parallel manager that this Connection's Adapter uses.
378
266
  #
379
- # @yield a block to execute multiple requests.
380
- # @return [void]
267
+ # Yields a block to execute multiple requests.
268
+ # Returns nothing.
381
269
  def in_parallel(manager = nil)
382
- @parallel_manager = manager || default_parallel_manager do
383
- warn 'Warning: `in_parallel` called but no parallel-capable adapter ' \
384
- 'on Faraday stack'
385
- warn caller[2, 10].join("\n")
270
+ @parallel_manager = manager || default_parallel_manager {
271
+ warn "Warning: `in_parallel` called but no parallel-capable adapter on Faraday stack"
272
+ warn caller[2,10].join("\n")
386
273
  nil
387
- end
274
+ }
388
275
  yield
389
- @parallel_manager&.run
276
+ @parallel_manager && @parallel_manager.run
390
277
  ensure
391
278
  @parallel_manager = nil
392
279
  end
393
280
 
394
- # Sets the Hash proxy options.
395
- #
396
- # @param new_value [Object]
281
+ # Public: Gets or Sets the Hash proxy options.
282
+ def proxy(arg = nil)
283
+ return @proxy if arg.nil?
284
+ warn 'Warning: use of proxy(new_value) to set connection proxy have been DEPRECATED and will be removed in Faraday 1.0'
285
+ @manual_proxy = true
286
+ @proxy = ProxyOptions.from(arg)
287
+ end
288
+
289
+ # Public: Sets the Hash proxy options.
397
290
  def proxy=(new_value)
398
291
  @manual_proxy = true
399
292
  @proxy = new_value ? ProxyOptions.from(new_value) : nil
@@ -402,14 +295,13 @@ module Faraday
402
295
  def_delegators :url_prefix, :scheme, :scheme=, :host, :host=, :port, :port=
403
296
  def_delegator :url_prefix, :path, :path_prefix
404
297
 
405
- # Parses the given URL with URI and stores the individual
406
- # components in this connection. These components serve as defaults for
298
+ # Public: Parses the giving url with URI and stores the individual
299
+ # components in this connection. These components serve as defaults for
407
300
  # requests made by this connection.
408
301
  #
409
- # @param url [String, URI]
410
- # @param encoder [Object]
302
+ # url - A String or URI.
411
303
  #
412
- # @example
304
+ # Examples
413
305
  #
414
306
  # conn = Faraday::Connection.new { ... }
415
307
  # conn.url_prefix = "https://sushi.com/api"
@@ -417,6 +309,8 @@ module Faraday
417
309
  # conn.path_prefix # => "/api"
418
310
  #
419
311
  # conn.get("nigiri?page=2") # accesses https://sushi.com/api/nigiri
312
+ #
313
+ # Returns the parsed URI from the given input..
420
314
  def url_prefix=(url, encoder = nil)
421
315
  uri = @url_prefix = Utils.URI(url)
422
316
  self.path_prefix = uri.path
@@ -428,65 +322,54 @@ module Faraday
428
322
  basic_auth user, password
429
323
  uri.user = uri.password = nil
430
324
  end
325
+
326
+ uri
431
327
  end
432
328
 
433
- # Sets the path prefix and ensures that it always has a leading
329
+ # Public: Sets the path prefix and ensures that it always has a leading
434
330
  # slash.
435
331
  #
436
- # @param value [String]
332
+ # value - A String.
437
333
  #
438
- # @return [String] the new path prefix
334
+ # Returns the new String path prefix.
439
335
  def path_prefix=(value)
440
336
  url_prefix.path = if value
441
- value = '/' + value unless value[0, 1] == '/'
442
- value
443
- end
337
+ value = '/' + value unless value[0,1] == '/'
338
+ value
339
+ end
444
340
  end
445
341
 
446
- # Takes a relative url for a request and combines it with the defaults
342
+ # Public: Takes a relative url for a request and combines it with the defaults
447
343
  # set on the connection instance.
448
344
  #
449
- # @param url [String]
450
- # @param extra_params [Hash]
451
- #
452
- # @example
453
345
  # conn = Faraday::Connection.new { ... }
454
346
  # conn.url_prefix = "https://sushi.com/api?token=abc"
455
347
  # conn.scheme # => https
456
348
  # conn.path_prefix # => "/api"
457
349
  #
458
- # conn.build_url("nigiri?page=2")
459
- # # => https://sushi.com/api/nigiri?token=abc&page=2
460
- #
461
- # conn.build_url("nigiri", page: 2)
462
- # # => https://sushi.com/api/nigiri?token=abc&page=2
350
+ # conn.build_url("nigiri?page=2") # => https://sushi.com/api/nigiri?token=abc&page=2
351
+ # conn.build_url("nigiri", :page => 2) # => https://sushi.com/api/nigiri?token=abc&page=2
463
352
  #
464
353
  def build_url(url = nil, extra_params = nil)
465
354
  uri = build_exclusive_url(url)
466
355
 
467
356
  query_values = params.dup.merge_query(uri.query, options.params_encoder)
468
- query_values.update(extra_params) if extra_params
469
- uri.query =
470
- if query_values.empty?
471
- nil
472
- else
473
- query_values.to_query(options.params_encoder)
474
- end
357
+ query_values.update extra_params if extra_params
358
+ uri.query = query_values.empty? ? nil : query_values.to_query(options.params_encoder)
475
359
 
476
360
  uri
477
361
  end
478
362
 
479
363
  # Builds and runs the Faraday::Request.
480
364
  #
481
- # @param method [Symbol] HTTP method.
482
- # @param url [String, URI] String or URI to access.
483
- # @param body [Object] The request body that will eventually be converted to
484
- # a string.
485
- # @param headers [Hash] unencoded HTTP header key/value pairs.
365
+ # method - The Symbol HTTP method.
366
+ # url - The String or URI to access.
367
+ # body - The request body that will eventually be converted to a string.
368
+ # headers - Hash of unencoded HTTP header key/value pairs.
486
369
  #
487
- # @return [Faraday::Response]
370
+ # Returns a Faraday::Response.
488
371
  def run_request(method, url, body, headers)
489
- unless METHODS.include?(method)
372
+ if !METHODS.include?(method)
490
373
  raise ArgumentError, "unknown http method: #{method}"
491
374
  end
492
375
 
@@ -494,7 +377,7 @@ module Faraday
494
377
  @temp_proxy = proxy_for_request(url)
495
378
 
496
379
  request = build_request(method) do |req|
497
- req.options = req.options.merge(proxy: @temp_proxy)
380
+ req.options = req.options.merge(:proxy => @temp_proxy)
498
381
  req.url(url) if url
499
382
  req.headers.update(headers) if headers
500
383
  req.body = body if body
@@ -506,93 +389,73 @@ module Faraday
506
389
 
507
390
  # Creates and configures the request object.
508
391
  #
509
- # @param method [Symbol]
510
- #
511
- # @yield [Faraday::Request] if block given
512
- # @return [Faraday::Request]
392
+ # Returns the new Request.
513
393
  def build_request(method)
514
394
  Request.create(method) do |req|
515
- req.params = params.dup
516
- req.headers = headers.dup
517
- req.options = options
395
+ req.params = self.params.dup
396
+ req.headers = self.headers.dup
397
+ req.options = self.options
518
398
  yield(req) if block_given?
519
399
  end
520
400
  end
521
401
 
522
- # Build an absolute URL based on url_prefix.
402
+ # Internal: Build an absolute URL based on url_prefix.
523
403
  #
524
- # @param url [String, URI]
525
- # @param params [Faraday::Utils::ParamsHash] A Faraday::Utils::ParamsHash to
526
- # replace the query values
404
+ # url - A String or URI-like object
405
+ # params - A Faraday::Utils::ParamsHash to replace the query values
527
406
  # of the resulting url (default: nil).
528
407
  #
529
- # @return [URI]
408
+ # Returns the resulting URI instance.
530
409
  def build_exclusive_url(url = nil, params = nil, params_encoder = nil)
531
- url = nil if url.respond_to?(:empty?) && url.empty?
410
+ url = nil if url.respond_to?(:empty?) and url.empty?
532
411
  base = url_prefix
533
- if url && base.path && base.path !~ %r{/$}
412
+ if url and base.path and base.path !~ /\/$/
534
413
  base = base.dup
535
- base.path = base.path + '/' # ensure trailing slash
414
+ base.path = base.path + '/' # ensure trailing slash
536
415
  end
537
416
  uri = url ? base + url : base
538
- if params
539
- uri.query = params.to_query(params_encoder || options.params_encoder)
540
- end
541
- # rubocop:disable Style/SafeNavigation
542
- uri.query = nil if uri.query && uri.query.empty?
543
- # rubocop:enable Style/SafeNavigation
417
+ uri.query = params.to_query(params_encoder || options.params_encoder) if params
418
+ uri.query = nil if uri.query and uri.query.empty?
544
419
  uri
545
420
  end
546
421
 
547
- # Creates a duplicate of this Faraday::Connection.
422
+ # Internal: Creates a duplicate of this Faraday::Connection.
548
423
  #
549
- # @api private
550
- #
551
- # @return [Faraday::Connection]
424
+ # Returns a Faraday::Connection.
552
425
  def dup
553
426
  self.class.new(build_exclusive_url,
554
- headers: headers.dup,
555
- params: params.dup,
556
- builder: builder.dup,
557
- ssl: ssl.dup,
558
- request: options.dup)
427
+ :headers => headers.dup,
428
+ :params => params.dup,
429
+ :builder => builder.dup,
430
+ :ssl => ssl.dup,
431
+ :request => options.dup)
559
432
  end
560
433
 
561
- # Yields username and password extracted from a URI if they both exist.
562
- #
563
- # @param uri [URI]
564
- # @yield [username, password] any username and password
565
- # @yieldparam username [String] any username from URI
566
- # @yieldparam password [String] any password from URI
567
- # @return [void]
568
- # @api private
434
+ # Internal: Yields username and password extracted from a URI if they both exist.
569
435
  def with_uri_credentials(uri)
570
- return unless uri.user && uri.password
571
-
572
- yield(Utils.unescape(uri.user), Utils.unescape(uri.password))
436
+ if uri.user and uri.password
437
+ yield(Utils.unescape(uri.user), Utils.unescape(uri.password))
438
+ end
573
439
  end
574
440
 
575
441
  def set_authorization_header(header_type, *args)
576
- header = Faraday::Request
577
- .lookup_middleware(header_type)
578
- .header(*args)
579
-
442
+ header = Faraday::Request.lookup_middleware(header_type).
443
+ header(*args)
580
444
  headers[Faraday::Request::Authorization::KEY] = header
581
445
  end
582
446
 
583
447
  def proxy_from_env(url)
584
448
  return if Faraday.ignore_env_proxy
585
-
586
449
  uri = nil
587
450
  if URI.parse('').respond_to?(:find_proxy)
588
451
  case url
589
452
  when String
590
- uri = Utils.URI(url)
591
- uri = URI.parse("#{uri.scheme}://#{uri.hostname}").find_proxy
592
- when URI
593
- uri = url.find_proxy
594
- when nil
595
- uri = find_default_proxy
453
+ uri = Utils.URI(url)
454
+ uri = URI.parse("#{uri.scheme}://#{uri.hostname}").find_proxy
455
+ when URI
456
+ uri = url.find_proxy
457
+ when nil
458
+ uri = find_default_proxy
596
459
  end
597
460
  else
598
461
  warn 'no_proxy is unsupported' if ENV['no_proxy'] || ENV['NO_PROXY']
@@ -603,24 +466,19 @@ module Faraday
603
466
 
604
467
  def find_default_proxy
605
468
  uri = ENV['http_proxy']
606
- return unless uri && !uri.empty?
607
-
608
- uri = 'http://' + uri if uri !~ /^http/i
609
- uri
469
+ if uri && !uri.empty?
470
+ uri = 'http://' + uri if uri !~ /^http/i
471
+ uri
472
+ end
610
473
  end
611
474
 
612
475
  def proxy_for_request(url)
613
- return proxy if @manual_proxy
614
-
476
+ return self.proxy if @manual_proxy
615
477
  if url && Utils.URI(url).absolute?
616
478
  proxy_from_env(url)
617
479
  else
618
- proxy
480
+ self.proxy
619
481
  end
620
482
  end
621
-
622
- def support_parallel?(adapter)
623
- adapter&.respond_to?(:supports_parallel?) && adapter&.supports_parallel?
624
- end
625
483
  end
626
484
  end