faraday 0.9.2 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fc2292832e97072ea9b3292a56ee1c87e7f9ec19
4
+ data.tar.gz: 9aea82166ae97ea282ba1a4b5efc4740effeea14
5
+ SHA512:
6
+ metadata.gz: ba7bc351e2111733af22e1666f86d877050cd3256d3322e0418773aaf1a8c7447a02a780b812b0f2549b558c4c38a606189dad3c2915ed48019def848b8bc65c
7
+ data.tar.gz: d3cbb3fa12d1e37f162930bab8be286ca6b384e2ce622e8ebff9087386902b50aa318a423d054138d87064601034470a45d072c85bda910d29fbb84150a22a5f
data/README.md CHANGED
@@ -17,6 +17,10 @@ Faraday supports these adapters:
17
17
  It also includes a Rack adapter for hitting loaded Rack applications through
18
18
  Rack::Test, and a Test adapter for stubbing requests by hand.
19
19
 
20
+ ## API documentation
21
+
22
+ Available at [rubydoc.info](http://www.rubydoc.info/gems/faraday).
23
+
20
24
  ## Usage
21
25
 
22
26
  ```ruby
@@ -73,7 +77,7 @@ either per-connection or per-request basis.
73
77
 
74
78
  ```ruby
75
79
  # per-connection setting
76
- conn = Faraday.new :params_encoder => Faraday::FlatParamsEncoder
80
+ conn = Faraday.new :request => { :params_encoder => Faraday::FlatParamsEncoder }
77
81
 
78
82
  conn.get do |req|
79
83
  # per-request setting:
@@ -210,7 +214,7 @@ stubs.verify_stubbed_calls
210
214
  This library aims to support and is [tested against][travis] the following Ruby
211
215
  implementations:
212
216
 
213
- * Ruby 1.8.7+
217
+ * Ruby 1.9.3+
214
218
  * [JRuby][] 1.7+
215
219
  * [Rubinius][] 2+
216
220
 
@@ -234,10 +238,10 @@ See [LICENSE][] for details.
234
238
 
235
239
  [net_http]: http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/Net/HTTP.html
236
240
  [persistent]: https://github.com/drbrain/net-http-persistent
237
- [travis]: http://travis-ci.org/lostisland/faraday
238
- [excon]: https://github.com/geemus/excon#readme
241
+ [travis]: https://travis-ci.org/lostisland/faraday
242
+ [excon]: https://github.com/excon/excon#readme
239
243
  [typhoeus]: https://github.com/typhoeus/typhoeus#readme
240
- [patron]: http://toland.github.com/patron/
244
+ [patron]: http://toland.github.io/patron/
241
245
  [eventmachine]: https://github.com/igrigorik/em-http-request#readme
242
246
  [httpclient]: https://github.com/nahi/httpclient
243
247
  [jruby]: http://jruby.org/
@@ -14,7 +14,7 @@ require 'forwardable'
14
14
  # conn.get '/'
15
15
  #
16
16
  module Faraday
17
- VERSION = "0.9.2"
17
+ VERSION = "0.10.0"
18
18
 
19
19
  class << self
20
20
  # Public: Gets or sets the root path that Faraday is being loaded from.
@@ -92,6 +92,10 @@ module Faraday
92
92
 
93
93
  alias require_lib require_libs
94
94
 
95
+ def respond_to?(symbol, include_private = false)
96
+ default_connection.respond_to?(symbol, include_private) || super
97
+ end
98
+
95
99
  private
96
100
  # Internal: Proxies method calls on the Faraday constant to
97
101
  # #default_connection.
@@ -118,15 +122,6 @@ module Faraday
118
122
  @default_connection_options ||= ConnectionOptions.new
119
123
  end
120
124
 
121
- if (!defined?(RUBY_ENGINE) || "ruby" == RUBY_ENGINE) && RUBY_VERSION < '1.9'
122
- begin
123
- require 'system_timer'
124
- Timer = SystemTimer
125
- rescue LoadError
126
- warn "Faraday: you may want to install system_timer for reliable timeouts"
127
- end
128
- end
129
-
130
125
  unless const_defined? :Timer
131
126
  require 'timeout'
132
127
  Timer = Timeout
@@ -244,25 +239,3 @@ module Faraday
244
239
  require_lib 'autoload'
245
240
  end
246
241
  end
247
-
248
- # not pulling in active-support JUST for this method. And I love this method.
249
- class Object
250
- # The primary purpose of this method is to "tap into" a method chain,
251
- # in order to perform operations on intermediate results within the chain.
252
- #
253
- # Examples
254
- #
255
- # (1..10).tap { |x| puts "original: #{x.inspect}" }.to_a.
256
- # tap { |x| puts "array: #{x.inspect}" }.
257
- # select { |x| x%2 == 0 }.
258
- # tap { |x| puts "evens: #{x.inspect}" }.
259
- # map { |x| x*x }.
260
- # tap { |x| puts "squares: #{x.inspect}" }
261
- #
262
- # Yields self.
263
- # Returns self.
264
- def tap
265
- yield(self)
266
- self
267
- end unless Object.respond_to?(:tap)
268
- end
@@ -34,9 +34,10 @@ module Faraday
34
34
  env.clear_body if env.needs_body?
35
35
  end
36
36
 
37
- def save_response(env, status, body, headers = nil)
37
+ def save_response(env, status, body, headers = nil, reason_phrase = nil)
38
38
  env.status = status
39
39
  env.body = body
40
+ env.reason_phrase = reason_phrase && reason_phrase.to_s.strip
40
41
  env.response_headers = Utils::Headers.new.tap do |response_headers|
41
42
  response_headers.update headers unless headers.nil?
42
43
  yield(response_headers) if block_given?
@@ -140,7 +140,9 @@ module Faraday
140
140
  def perform_single_request(env)
141
141
  req = EventMachine::HttpRequest.new(env[:url], connection_config(env))
142
142
  req.setup_request(env[:method], request_config(env)).callback { |client|
143
- save_response(env, client.response_header.status, client.response) do |resp_headers|
143
+ status = client.response_header.status
144
+ reason = client.response_header.http_reason
145
+ save_response(env, status, client.response, nil, reason) do |resp_headers|
144
146
  client.response_header.each do |name, value|
145
147
  resp_headers[name.to_sym] = value
146
148
  end
@@ -39,7 +39,7 @@ module EmHttpSslPatch
39
39
  end
40
40
 
41
41
  def host
42
- parent.connopts.host
42
+ parent.uri.host
43
43
  end
44
44
 
45
45
  def certificate_store
@@ -54,7 +54,9 @@ module Faraday
54
54
 
55
55
  raise client.error if client.error
56
56
 
57
- save_response(env, client.response_header.status, client.response) do |resp_headers|
57
+ status = client.response_header.status
58
+ reason = client.response_header.http_reason
59
+ save_response(env, status, client.response, nil, reason) do |resp_headers|
58
60
  client.response_header.each do |name, value|
59
61
  resp_headers[name.to_sym] = value
60
62
  end
@@ -57,7 +57,7 @@ module Faraday
57
57
  :headers => env[:request_headers],
58
58
  :body => read_body(env)
59
59
 
60
- save_response(env, resp.status.to_i, resp.body, resp.headers)
60
+ save_response(env, resp.status.to_i, resp.body, resp.headers, resp.reason_phrase)
61
61
 
62
62
  @app.call env
63
63
  rescue ::Excon::Errors::SocketError => err
@@ -37,7 +37,7 @@ module Faraday
37
37
  :body => env[:body],
38
38
  :header => env[:request_headers]
39
39
 
40
- save_response env, resp.status, resp.body, resp.headers
40
+ save_response env, resp.status, resp.body, resp.headers, resp.reason
41
41
 
42
42
  @app.call env
43
43
  rescue ::HTTPClient::TimeoutError, Errno::ETIMEDOUT
@@ -17,6 +17,7 @@ module Faraday
17
17
  Errno::EHOSTUNREACH,
18
18
  Errno::EINVAL,
19
19
  Errno::ENETUNREACH,
20
+ Errno::EPIPE,
20
21
  Net::HTTPBadResponse,
21
22
  Net::HTTPHeaderSyntaxError,
22
23
  Net::ProtocolError,
@@ -46,7 +47,7 @@ module Faraday
46
47
  end
47
48
  end
48
49
 
49
- save_response(env, http_response.code.to_i, http_response.body || '') do |response_headers|
50
+ save_response(env, http_response.code.to_i, http_response.body || '', nil, http_response.message) do |response_headers|
50
51
  http_response.each_header do |key, value|
51
52
  response_headers[key] = value
52
53
  end
@@ -35,7 +35,10 @@ module Faraday
35
35
  raise Error::ConnectionFailed, $!
36
36
  end
37
37
 
38
- save_response(env, response.status, response.body, response.headers)
38
+ # Remove the "HTTP/1.1 200", leaving just the reason phrase
39
+ reason_phrase = response.status_line.gsub(/^.* \d{3} /, '')
40
+
41
+ save_response(env, response.status, response.body, response.headers, reason_phrase)
39
42
 
40
43
  @app.call env
41
44
  rescue ::Patron::TimeoutError => err
@@ -1,16 +1,41 @@
1
1
  module Faraday
2
2
  class Adapter
3
- # test = Faraday::Connection.new do
4
- # use Faraday::Adapter::Test do |stub|
5
- # stub.get '/nigiri/sake.json' do
6
- # [200, {}, 'hi world']
3
+ # Examples
4
+ #
5
+ # test = Faraday::Connection.new do
6
+ # use Faraday::Adapter::Test do |stub|
7
+ # # simply define matcher to match the request
8
+ # stub.get '/resource.json' do
9
+ # # return static content
10
+ # [200, {'Content-Type' => 'application/json'}, 'hi world']
11
+ # end
12
+ #
13
+ # # response with content generated based on request
14
+ # stub.get '/showget' do |env|
15
+ # [200, {'Content-Type' => 'text/plain'}, env[:method].to_s]
16
+ # end
17
+ #
18
+ # # regular expression can be used as matching filter
19
+ # stub.get /\A\/items\/(\d+)\z/ do |env, meta|
20
+ # # in case regular expression is used an instance of MatchData can be received
21
+ # [200, {'Content-Type' => 'text/plain'}, "showing item: #{meta[:match_data][1]}"]
22
+ # end
7
23
  # end
8
24
  # end
9
- # end
10
- #
11
- # resp = test.get '/nigiri/sake.json'
12
- # resp.body # => 'hi world'
25
+ #
26
+ # resp = test.get '/resource.json'
27
+ # resp.body # => 'hi world'
28
+ #
29
+ # resp = test.get '/showget'
30
+ # resp.body # => 'get'
31
+ #
32
+ # resp = test.get '/items/1'
33
+ # resp.body # => 'showing item: 1'
34
+ #
35
+ # resp = test.get '/items/2'
36
+ # resp.body # => 'showing item: 2'
13
37
  #
38
+
14
39
  class Test < Faraday::Adapter
15
40
  attr_accessor :stubs
16
41
 
@@ -33,12 +58,12 @@ module Faraday
33
58
  stack = @stack[request_method]
34
59
  consumed = (@consumed[request_method] ||= [])
35
60
 
36
- if stub = matches?(stack, path, headers, body)
61
+ stub, meta = matches?(stack, path, headers, body)
62
+ if stub
37
63
  consumed << stack.delete(stub)
38
- stub
39
- else
40
- matches?(consumed, path, headers, body)
64
+ return stub, meta
41
65
  end
66
+ matches?(consumed, path, headers, body)
42
67
  end
43
68
 
44
69
  def get(path, headers = {}, &block)
@@ -85,18 +110,22 @@ module Faraday
85
110
  protected
86
111
 
87
112
  def new_stub(request_method, path, headers = {}, body=nil, &block)
88
- normalized_path = Faraday::Utils.normalize_path(path)
113
+ normalized_path = path.is_a?(Regexp) ? path : Faraday::Utils.normalize_path(path)
89
114
  (@stack[request_method] ||= []) << Stub.new(normalized_path, headers, body, block)
90
115
  end
91
116
 
92
117
  def matches?(stack, path, headers, body)
93
- stack.detect { |stub| stub.matches?(path, headers, body) }
118
+ stack.each do |stub|
119
+ match_result, meta = stub.matches?(path, headers, body)
120
+ return stub, meta if match_result
121
+ end
122
+ nil
94
123
  end
95
124
  end
96
125
 
97
126
  class Stub < Struct.new(:path, :params, :headers, :body, :block)
98
127
  def initialize(full, headers, body, block)
99
- path, query = full.split('?')
128
+ path, query = full.respond_to?(:split) ? full.split("?") : full
100
129
  params = query ?
101
130
  Faraday::Utils.parse_nested_query(query) :
102
131
  {}
@@ -108,10 +137,21 @@ module Faraday
108
137
  request_params = request_query ?
109
138
  Faraday::Utils.parse_nested_query(request_query) :
110
139
  {}
111
- request_path == path &&
140
+ # meta is a hash use as carrier
141
+ # that will be yielded to consumer block
142
+ meta = {}
143
+ return path_match?(request_path, meta) &&
112
144
  params_match?(request_params) &&
113
145
  (body.to_s.size.zero? || request_body == body) &&
114
- headers_match?(request_headers)
146
+ headers_match?(request_headers), meta
147
+ end
148
+
149
+ def path_match?(request_path, meta)
150
+ if path.is_a? Regexp
151
+ !!(meta[:match_data] = path.match(request_path))
152
+ else
153
+ path == request_path
154
+ end
115
155
  end
116
156
 
117
157
  def params_match?(request_params)
@@ -146,11 +186,14 @@ module Faraday
146
186
  normalized_path = Faraday::Utils.normalize_path(env[:url])
147
187
  params_encoder = env.request.params_encoder || Faraday::Utils.default_params_encoder
148
188
 
149
- if stub = stubs.match(env[:method], normalized_path, env.request_headers, env[:body])
189
+ stub, meta = stubs.match(env[:method], normalized_path, env.request_headers, env[:body])
190
+ if stub
150
191
  env[:params] = (query = env[:url].query) ?
151
- params_encoder.decode(query) :
152
- {}
153
- status, headers, body = stub.block.call(env)
192
+ params_encoder.decode(query) : {}
193
+ block_arity = stub.block.arity
194
+ status, headers, body = (block_arity >= 0) ?
195
+ stub.block.call(*[env, meta].take(block_arity)) :
196
+ stub.block.call(env, meta)
154
197
  save_response(env, status, body, headers)
155
198
  else
156
199
  raise Stubs::NotFound, "no stubbed request for #{env[:method]} #{normalized_path} #{env[:body]}"
@@ -56,7 +56,7 @@ module Faraday
56
56
  # :password - String (optional)
57
57
  def initialize(url = nil, options = nil)
58
58
  if url.is_a?(Hash)
59
- options = ConnectionOptions.from(url)
59
+ options = options ? options.merge(url) : ConnectionOptions.from(url)
60
60
  url = options.url
61
61
  else
62
62
  options = ConnectionOptions.from(options)
@@ -126,7 +126,7 @@ module Faraday
126
126
  # req.body = JSON.generate(:query => {...})
127
127
  # end
128
128
  #
129
- # Yields a Faraday::Response for further request customizations.
129
+ # Yields a Faraday::Request for further request customizations.
130
130
  # Returns a Faraday::Response.
131
131
  #
132
132
  # Signature
@@ -163,7 +163,7 @@ module Faraday
163
163
  # req.body = JSON.generate(:user => 'kimchy', ...)
164
164
  # end
165
165
  #
166
- # Yields a Faraday::Response for further request customizations.
166
+ # Yields a Faraday::Request for further request customizations.
167
167
  # Returns a Faraday::Response.
168
168
  #
169
169
  # Signature
@@ -358,7 +358,7 @@ module Faraday
358
358
  #
359
359
  # method - The Symbol HTTP method.
360
360
  # url - The String or URI to access.
361
- # body - The String body
361
+ # body - The request body that will eventually be converted to a string.
362
362
  # headers - Hash of unencoded HTTP header key/value pairs.
363
363
  #
364
364
  # Returns a Faraday::Response.
@@ -29,7 +29,17 @@ module Faraday
29
29
  end
30
30
 
31
31
  def inspect
32
- %(#<#{self.class}>)
32
+ inner = ''
33
+ if @wrapped_exception
34
+ inner << " wrapped=#{@wrapped_exception.inspect}"
35
+ end
36
+ if @response
37
+ inner << " response=#{@response.inspect}"
38
+ end
39
+ if inner.empty?
40
+ inner << " #{super}"
41
+ end
42
+ %(#<#{self.class}#{inner}>)
33
43
  end
34
44
  end
35
45
 
@@ -252,7 +252,8 @@ module Faraday
252
252
  end
253
253
 
254
254
  class Env < Options.new(:method, :body, :url, :request, :request_headers,
255
- :ssl, :parallel_manager, :params, :response, :response_headers, :status)
255
+ :ssl, :parallel_manager, :params, :response, :response_headers, :status,
256
+ :reason_phrase)
256
257
 
257
258
  ContentLength = 'Content-Length'.freeze
258
259
  StatusesWithoutBody = Set.new [204, 304]
@@ -52,6 +52,8 @@ module Faraday
52
52
  path.query = nil
53
53
  end
54
54
  else
55
+ anchor_index = path.index('#')
56
+ path = path.slice(0, anchor_index) unless anchor_index.nil?
55
57
  path, query = path.split('?', 2)
56
58
  end
57
59
  self.path = path
@@ -37,6 +37,10 @@ module Faraday
37
37
  finished? ? env.status : nil
38
38
  end
39
39
 
40
+ def reason_phrase
41
+ finished? ? env.reason_phrase : nil
42
+ end
43
+
40
44
  def headers
41
45
  finished? ? env.response_headers : {}
42
46
  end
@@ -4,7 +4,7 @@ module Faraday
4
4
  class Response::Logger < Response::Middleware
5
5
  extend Forwardable
6
6
 
7
- DEFAULT_OPTIONS = { :bodies => false }
7
+ DEFAULT_OPTIONS = { :headers => true, :bodies => false }
8
8
 
9
9
  def initialize(app, logger = nil, options = {})
10
10
  super(app)
@@ -19,14 +19,14 @@ module Faraday
19
19
 
20
20
  def call(env)
21
21
  info "#{env.method} #{env.url.to_s}"
22
- debug('request') { dump_headers env.request_headers }
22
+ debug('request') { dump_headers env.request_headers } if log_headers?(:request)
23
23
  debug('request') { dump_body(env[:body]) } if env[:body] && log_body?(:request)
24
24
  super
25
25
  end
26
26
 
27
27
  def on_complete(env)
28
28
  info('Status') { env.status.to_s }
29
- debug('response') { dump_headers env.response_headers }
29
+ debug('response') { dump_headers env.response_headers } if log_headers?(:response)
30
30
  debug('response') { dump_body env[:body] } if env[:body] && log_body?(:response)
31
31
  end
32
32
 
@@ -49,6 +49,13 @@ module Faraday
49
49
  body.pretty_inspect
50
50
  end
51
51
 
52
+ def log_headers?(type)
53
+ case @options[:headers]
54
+ when Hash then @options[:headers][type]
55
+ else @options[:headers]
56
+ end
57
+ end
58
+
52
59
  def log_body?(type)
53
60
  case @options[:bodies]
54
61
  when Hash then @options[:bodies][type]
@@ -97,7 +97,7 @@ module Faraday
97
97
  return unless header_string && !header_string.empty?
98
98
  header_string.split(/\r\n/).
99
99
  tap { |a| a.shift if a.first.index('HTTP/') == 0 }. # drop the HTTP status line
100
- map { |h| h.split(/:\s+/, 2) }.reject { |p| p[0].nil? }. # split key and value, ignore blank lines
100
+ map { |h| h.split(/:\s*/, 2) }.reject { |p| p[0].nil? }. # split key and value, ignore blank lines
101
101
  each { |key, value|
102
102
  # join multiple values with a comma
103
103
  if self[key]
metadata CHANGED
@@ -1,53 +1,41 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: faraday
3
- version: !ruby/object:Gem::Version
4
- hash: 63
5
- prerelease:
6
- segments:
7
- - 0
8
- - 9
9
- - 2
10
- version: 0.9.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.10.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Rick Olson
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2015-10-06 00:00:00 +02:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
11
+ date: 2016-11-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
22
14
  name: multipart-post
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
27
17
  - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 11
30
- segments:
31
- - 1
32
- - 2
33
- version: "1.2"
34
- - - <
35
- - !ruby/object:Gem::Version
36
- hash: 5
37
- segments:
38
- - 3
39
- version: "3"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.2'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '3'
40
23
  type: :runtime
41
- version_requirements: *id001
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '1.2'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '3'
42
33
  description:
43
34
  email: technoweenie@gmail.com
44
35
  executables: []
45
-
46
36
  extensions: []
47
-
48
37
  extra_rdoc_files: []
49
-
50
- files:
38
+ files:
51
39
  - LICENSE.md
52
40
  - README.md
53
41
  - lib/faraday.rb
@@ -84,39 +72,28 @@ files:
84
72
  - lib/faraday/response/raise_error.rb
85
73
  - lib/faraday/upload_io.rb
86
74
  - lib/faraday/utils.rb
87
- has_rdoc: true
88
75
  homepage: https://github.com/lostisland/faraday
89
- licenses:
76
+ licenses:
90
77
  - MIT
78
+ metadata: {}
91
79
  post_install_message:
92
80
  rdoc_options: []
93
-
94
- require_paths:
81
+ require_paths:
95
82
  - lib
96
- required_ruby_version: !ruby/object:Gem::Requirement
97
- none: false
98
- requirements:
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
99
85
  - - ">="
100
- - !ruby/object:Gem::Version
101
- hash: 3
102
- segments:
103
- - 0
104
- version: "0"
105
- required_rubygems_version: !ruby/object:Gem::Requirement
106
- none: false
107
- requirements:
86
+ - !ruby/object:Gem::Version
87
+ version: '1.9'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
108
90
  - - ">="
109
- - !ruby/object:Gem::Version
110
- hash: 3
111
- segments:
112
- - 0
113
- version: "0"
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
114
93
  requirements: []
115
-
116
94
  rubyforge_project:
117
- rubygems_version: 1.6.2
95
+ rubygems_version: 2.4.5
118
96
  signing_key:
119
- specification_version: 3
97
+ specification_version: 4
120
98
  summary: HTTP/REST API client library.
121
99
  test_files: []
122
-