faraday 0.9.2 → 0.12.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,8 +16,7 @@ module Faraday
16
16
 
17
17
  # Internal
18
18
  def self.build_hash(type, hash)
19
- offset = KEY.size + type.size + 3
20
- comma = ",\n#{' ' * offset}"
19
+ comma = ", "
21
20
  values = []
22
21
  hash.each do |key, value|
23
22
  values << "#{key}=#{value.to_s.inspect}"
@@ -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
@@ -27,7 +27,7 @@ module Faraday
27
27
  DEFAULT_CHECK = lambda { |env,exception| false }
28
28
 
29
29
  def self.from(value)
30
- if Fixnum === value
30
+ if Integer === value
31
31
  new(value)
32
32
  else
33
33
  super(value)
@@ -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,14 @@ module Faraday
150
151
  @options.methods.include?(env[:method]) || @options.retry_if.call(env, exception)
151
152
  end
152
153
 
154
+ def rewind_files(body)
155
+ return unless body.is_a?(Hash)
156
+ body.each do |_, value|
157
+ if value.is_a? UploadIO
158
+ value.rewind
159
+ end
160
+ end
161
+ end
162
+
153
163
  end
154
164
  end
@@ -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
@@ -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)
@@ -12,22 +12,28 @@ module Faraday
12
12
  require 'logger'
13
13
  ::Logger.new(STDOUT)
14
14
  end
15
+ @filter = []
15
16
  @options = DEFAULT_OPTIONS.merge(options)
17
+ yield self if block_given?
16
18
  end
17
19
 
18
20
  def_delegators :@logger, :debug, :info, :warn, :error, :fatal
19
21
 
20
22
  def call(env)
21
- info "#{env.method} #{env.url.to_s}"
22
- debug('request') { dump_headers env.request_headers }
23
- debug('request') { dump_body(env[:body]) } if env[:body] && log_body?(:request)
23
+ info "#{env.method} #{apply_filters(env.url.to_s)}"
24
+ debug('request') { apply_filters( dump_headers env.request_headers ) } if log_headers?(:request)
25
+ debug('request') { apply_filters( dump_body(env[:body]) ) } if env[:body] && log_body?(:request)
24
26
  super
25
27
  end
26
28
 
27
29
  def on_complete(env)
28
30
  info('Status') { env.status.to_s }
29
- debug('response') { dump_headers env.response_headers }
30
- debug('response') { dump_body env[:body] } if env[:body] && log_body?(:response)
31
+ debug('response') { apply_filters( dump_headers env.response_headers ) } if log_headers?(:response)
32
+ debug('response') { apply_filters( dump_body env[:body] ) } if env[:body] && log_body?(:response)
33
+ end
34
+
35
+ def filter(filter_word, filter_replacement)
36
+ @filter.push([ filter_word, filter_replacement ])
31
37
  end
32
38
 
33
39
  private
@@ -49,11 +55,26 @@ module Faraday
49
55
  body.pretty_inspect
50
56
  end
51
57
 
58
+ def log_headers?(type)
59
+ case @options[:headers]
60
+ when Hash then @options[:headers][type]
61
+ else @options[:headers]
62
+ end
63
+ end
64
+
52
65
  def log_body?(type)
53
66
  case @options[:bodies]
54
67
  when Hash then @options[:bodies][type]
55
68
  else @options[:bodies]
56
69
  end
57
70
  end
71
+
72
+ def apply_filters(output)
73
+ @filter.each do |pattern, replacement|
74
+ output = output.to_s.gsub(pattern, replacement)
75
+ end
76
+ output
77
+ end
78
+
58
79
  end
59
80
  end
@@ -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
data/lib/faraday/utils.rb CHANGED
@@ -10,12 +10,22 @@ module Faraday
10
10
  new(value)
11
11
  end
12
12
 
13
+ def self.allocate
14
+ new_self = super
15
+ new_self.initialize_names
16
+ new_self
17
+ end
18
+
13
19
  def initialize(hash = nil)
14
20
  super()
15
21
  @names = {}
16
22
  self.update(hash || {})
17
23
  end
18
24
 
25
+ def initialize_names
26
+ @names = {}
27
+ end
28
+
19
29
  # on dup/clone, we need to duplicate @names hash
20
30
  def initialize_copy(other)
21
31
  super
@@ -95,9 +105,16 @@ module Faraday
95
105
 
96
106
  def parse(header_string)
97
107
  return unless header_string && !header_string.empty?
98
- header_string.split(/\r\n/).
108
+
109
+ headers = header_string.split(/\r\n/)
110
+
111
+ # Find the last set of response headers.
112
+ start_index = headers.rindex { |x| x.match(/^HTTP\//) }
113
+ last_response = headers.slice(start_index, headers.size)
114
+
115
+ last_response.
99
116
  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
117
+ map { |h| h.split(/:\s*/, 2) }.reject { |p| p[0].nil? }. # split key and value, ignore blank lines
101
118
  each { |key, value|
102
119
  # join multiple values with a comma
103
120
  if self[key]
data/lib/faraday.rb CHANGED
@@ -14,7 +14,7 @@ require 'forwardable'
14
14
  # conn.get '/'
15
15
  #
16
16
  module Faraday
17
- VERSION = "0.9.2"
17
+ VERSION = "0.12.2"
18
18
 
19
19
  class << self
20
20
  # Public: Gets or sets the root path that Faraday is being loaded from.
@@ -34,9 +34,6 @@ module Faraday
34
34
  # Faraday.get "https://faraday.com"
35
35
  attr_writer :default_connection
36
36
 
37
- # Public: Sets the default options used when calling Faraday#new.
38
- attr_writer :default_connection_options
39
-
40
37
  # Public: Initializes a new Faraday::Connection.
41
38
  #
42
39
  # url - The optional String base URL to use as a prefix for all
@@ -66,7 +63,7 @@ module Faraday
66
63
  # Returns a Faraday::Connection.
67
64
  def new(url = nil, options = nil)
68
65
  block = block_given? ? Proc.new : nil
69
- options = options ? default_connection_options.merge(options) : default_connection_options.dup
66
+ options = options ? default_connection_options.merge(options) : default_connection_options
70
67
  Faraday::Connection.new(url, options, &block)
71
68
  end
72
69
 
@@ -92,6 +89,10 @@ module Faraday
92
89
 
93
90
  alias require_lib require_libs
94
91
 
92
+ def respond_to?(symbol, include_private = false)
93
+ default_connection.respond_to?(symbol, include_private) || super
94
+ end
95
+
95
96
  private
96
97
  # Internal: Proxies method calls on the Faraday constant to
97
98
  # #default_connection.
@@ -108,7 +109,7 @@ module Faraday
108
109
  #
109
110
  # Returns a Faraday::Connection, configured with the #default_adapter.
110
111
  def self.default_connection
111
- @default_connection ||= Connection.new
112
+ @default_connection ||= Connection.new(default_connection_options)
112
113
  end
113
114
 
114
115
  # Gets the default connection options used when calling Faraday#new.
@@ -118,13 +119,10 @@ module Faraday
118
119
  @default_connection_options ||= ConnectionOptions.new
119
120
  end
120
121
 
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
122
+ # Public: Sets the default options used when calling Faraday#new.
123
+ def self.default_connection_options=(options)
124
+ @default_connection = nil
125
+ @default_connection_options = ConnectionOptions.from(options)
128
126
  end
129
127
 
130
128
  unless const_defined? :Timer
@@ -244,25 +242,3 @@ module Faraday
244
242
  require_lib 'autoload'
245
243
  end
246
244
  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
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.12.2
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: 2017-07-21 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
-