faraday 0.7.2 → 0.7.3

Sign up to get free protection for your applications and to get access to all the features.
data/faraday.gemspec CHANGED
@@ -12,8 +12,8 @@ Gem::Specification.new do |s|
12
12
  ## If your rubyforge_project name is different, then edit it and comment out
13
13
  ## the sub! line in the Rakefile
14
14
  s.name = 'faraday'
15
- s.version = '0.7.2'
16
- s.date = '2011-06-12'
15
+ s.version = '0.7.3'
16
+ s.date = '2011-06-27'
17
17
  s.rubyforge_project = 'faraday'
18
18
 
19
19
  ## Make sure your summary is short. The description may be as long
data/lib/faraday.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Faraday
2
- VERSION = "0.7.2"
2
+ VERSION = "0.7.3"
3
3
 
4
4
  class << self
5
5
  attr_accessor :default_adapter
@@ -21,6 +21,13 @@ module Faraday
21
21
  :headers => env[:request_headers],
22
22
  :disable_ssl_peer_verification => (env[:ssl] && !env[:ssl].fetch(:verify, true))
23
23
 
24
+ if ssl = env[:ssl]
25
+ req.ssl_cert = ssl[:client_cert_file] if ssl[:client_cert_file]
26
+ req.ssl_key = ssl[:client_key_file] if ssl[:client_key_file]
27
+ req.ssl_cacert = ssl[:ca_file] if ssl[:ca_file]
28
+ req.ssl_capath = ssl[:ca_path] if ssl[:ca_path]
29
+ end
30
+
24
31
  env_req = env[:request]
25
32
  req.timeout = req.connect_timeout = (env_req[:timeout] * 1000) if env_req[:timeout]
26
33
  req.connect_timeout = (env_req[:open_timeout] * 1000) if env_req[:open_timeout]
@@ -217,7 +217,9 @@ module Faraday
217
217
  def build_url(url, extra_params = nil)
218
218
  uri = URI.parse(url.to_s)
219
219
  if @path_prefix && uri.path !~ /^\//
220
- uri.path = "#{@path_prefix.size > 1 ? @path_prefix : nil}/#{uri.path}"
220
+ new_path = @path_prefix.size > 1 ? @path_prefix.dup : ''
221
+ new_path << "/#{uri.path}" unless uri.path.empty?
222
+ uri.path = new_path
221
223
  end
222
224
  uri.host ||= @host
223
225
  uri.port ||= @port
data/lib/faraday/error.rb CHANGED
@@ -34,5 +34,7 @@ module Faraday
34
34
  class ConnectionFailed < ClientError; end
35
35
  class ResourceNotFound < ClientError; end
36
36
  class ParsingError < ClientError; end
37
+
38
+ class MissingDependency < StandardError; end
37
39
  end
38
40
  end
@@ -9,7 +9,7 @@ module Faraday
9
9
  # req.body = 'abc'
10
10
  # end
11
11
  #
12
- class Request < Struct.new(:path, :params, :headers, :body)
12
+ class Request < Struct.new(:path, :params, :headers, :body, :options)
13
13
  extend AutoloadHelper
14
14
 
15
15
  autoload_all 'faraday/request',
@@ -32,8 +32,9 @@ module Faraday
32
32
 
33
33
  def initialize(request_method)
34
34
  @method = request_method
35
- self.params = {}
35
+ self.params = {}
36
36
  self.headers = {}
37
+ self.options = {}
37
38
  end
38
39
 
39
40
  def url(path, params = {})
@@ -68,13 +69,15 @@ module Faraday
68
69
  def to_env(connection)
69
70
  env_params = connection.params.merge(params)
70
71
  env_headers = connection.headers.merge(headers)
72
+ request_options = Utils.deep_merge(connection.options, options)
73
+ Utils.deep_merge!(request_options, :proxy => connection.proxy)
71
74
 
72
75
  { :method => method,
73
76
  :body => body,
74
77
  :url => connection.build_url(path, env_params),
75
78
  :request_headers => env_headers,
76
79
  :parallel_manager => connection.parallel_manager,
77
- :request => connection.options.merge(:proxy => connection.proxy),
80
+ :request => request_options,
78
81
  :ssl => connection.ssl}
79
82
  end
80
83
  end
@@ -3,7 +3,11 @@ module Faraday
3
3
  self.mime_type = 'application/json'.freeze
4
4
 
5
5
  class << self
6
- attr_accessor :adapter
6
+ attr_writer :adapter
7
+
8
+ def adapter
9
+ @adapter or raise Error::MissingDependency, "No JSON adapter available. Install either activesupport or yajl-ruby."
10
+ end
7
11
  end
8
12
 
9
13
  # loads the JSON encoder either from yajl-ruby or activesupport
data/lib/faraday/utils.rb CHANGED
@@ -136,6 +136,23 @@ module Faraday
136
136
  (url.query ? "?#{sort_query_params(url.query)}" : "")
137
137
  end
138
138
 
139
+ # Recursive hash update
140
+ def deep_merge!(target, hash)
141
+ hash.each do |key, value|
142
+ if Hash === value and Hash === target[key]
143
+ target[key] = deep_merge(target[key], value)
144
+ else
145
+ target[key] = value
146
+ end
147
+ end
148
+ target
149
+ end
150
+
151
+ # Recursive hash merge
152
+ def deep_merge(source, hash)
153
+ deep_merge!(source.dup, hash)
154
+ end
155
+
139
156
  protected
140
157
 
141
158
  def sort_query_params(query)
@@ -147,6 +147,13 @@ class TestConnection < Faraday::TestCase
147
147
  assert_equal '/sake.html', uri.path
148
148
  end
149
149
 
150
+ def test_build_url_doesnt_add_ending_slash
151
+ conn = Faraday::Connection.new
152
+ conn.url_prefix = "http://sushi.com/nigiri"
153
+ uri = conn.build_url(nil)
154
+ assert_equal "/nigiri", uri.path
155
+ end
156
+
150
157
  def test_build_url_parses_url_params_into_query
151
158
  conn = Faraday::Connection.new
152
159
  uri = conn.build_url("http://sushi.com/sake.html", 'a[b]' => '1 + 2')
data/test/env_test.rb CHANGED
@@ -2,55 +2,79 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
2
 
3
3
  class EnvTest < Faraday::TestCase
4
4
  def setup
5
- @conn = Faraday.new :url => 'http://sushi.com/api', :headers => {'Mime-Version' => '1.0'}
5
+ @conn = Faraday.new :url => 'http://sushi.com/api',
6
+ :headers => {'Mime-Version' => '1.0'},
7
+ :request => {:oauth => {:consumer_key => 'anonymous'}}
8
+
6
9
  @conn.options[:timeout] = 3
7
10
  @conn.options[:open_timeout] = 5
8
11
  @conn.ssl[:verify] = false
9
12
  @conn.proxy 'http://proxy.com'
10
- @input = { :body => 'abc' }
11
- @env = env_for @conn do |req|
12
- req.url 'foo.json', 'a' => 1
13
- req['Server'] = 'Faraday'
14
- req.body = @input[:body]
15
- end
16
13
  end
17
14
 
18
15
  def test_request_create_stores_method
19
- assert_equal :get, @env[:method]
16
+ env = make_env(:get)
17
+ assert_equal :get, env[:method]
20
18
  end
21
19
 
22
20
  def test_request_create_stores_addressable_uri
23
- assert_equal 'http://sushi.com/api/foo.json?a=1', @env[:url].to_s
21
+ env = make_env do |req|
22
+ req.url 'foo.json', 'a' => 1
23
+ end
24
+ assert_equal 'http://sushi.com/api/foo.json?a=1', env[:url].to_s
24
25
  end
25
26
 
26
27
  def test_request_create_stores_headers
27
- headers = @env[:request_headers]
28
+ env = make_env do |req|
29
+ req['Server'] = 'Faraday'
30
+ end
31
+ headers = env[:request_headers]
28
32
  assert_equal '1.0', headers['mime-version']
29
33
  assert_equal 'Faraday', headers['server']
30
34
  end
31
35
 
32
36
  def test_request_create_stores_body
33
- assert_equal @input[:body], @env[:body]
37
+ env = make_env do |req|
38
+ req.body = 'hi'
39
+ end
40
+ assert_equal 'hi', env[:body]
34
41
  end
35
42
 
36
- def test_request_create_stores_timeout_options
37
- assert_equal 3, @env[:request][:timeout]
38
- assert_equal 5, @env[:request][:open_timeout]
43
+ def test_global_request_options
44
+ env = make_env
45
+ assert_equal 3, env[:request][:timeout]
46
+ assert_equal 5, env[:request][:open_timeout]
47
+ end
48
+
49
+ def test_per_request_options
50
+ env = make_env do |req|
51
+ req.options[:timeout] = 10
52
+ req.options[:custom] = true
53
+ req.options[:oauth] = {:consumer_secret => 'xyz'}
54
+ end
55
+ assert_equal 10, env[:request][:timeout]
56
+ assert_equal 5, env[:request][:open_timeout]
57
+ assert_equal true, env[:request][:custom]
58
+
59
+ oauth_expected = {:consumer_secret => 'xyz', :consumer_key => 'anonymous'}
60
+ assert_equal oauth_expected, env[:request][:oauth]
39
61
  end
40
62
 
41
63
  def test_request_create_stores_ssl_options
42
- assert_equal false, @env[:ssl][:verify]
64
+ env = make_env
65
+ assert_equal false, env[:ssl][:verify]
43
66
  end
44
67
 
45
68
  def test_request_create_stores_proxy_options
46
- assert_equal 'proxy.com', @env[:request][:proxy][:uri].host
69
+ env = make_env
70
+ assert_equal 'proxy.com', env[:request][:proxy][:uri].host
47
71
  end
48
72
 
49
- def env_for(connection)
50
- env_setup = Faraday::Request.create(:get) do |req|
51
- yield req
52
- end
53
- env_setup.to_env(connection)
73
+ private
74
+
75
+ def make_env(method = :get, connection = @conn, &block)
76
+ request = Faraday::Request.create(method, &block)
77
+ request.to_env(connection)
54
78
  end
55
79
  end
56
80
 
@@ -42,6 +42,37 @@ class RequestMiddlewareTest < Faraday::TestCase
42
42
  assert_equal '{"a":"b"}', response.body
43
43
  end
44
44
 
45
+ def test_json_fails_with_useful_message_when_no_json_adapter_available
46
+ without_json_adapter do
47
+ expected_msg = "No JSON adapter available. Install either activesupport or yajl-ruby."
48
+ # assert_raise doesn't work to check the message (at least on 1.8.7)
49
+ begin
50
+ @conn.post('/echo', { :fruit => %w[apples oranges] }, 'content-type' => 'application/json')
51
+ fail "Exception not raised"
52
+ rescue Faraday::Error::MissingDependency => e
53
+ assert_equal expected_msg, e.message
54
+ end
55
+ end
56
+ end
57
+
58
+ def test_url_encoded_does_not_fail_when_no_json_adapter_available
59
+ without_json_adapter do
60
+ assert_nothing_raised {
61
+ @conn.post('/echo', { :fruit => %w[apples oranges] })
62
+ }
63
+ end
64
+ end
65
+
66
+ def without_json_adapter
67
+ original_adapter = Faraday::Request::JSON.adapter
68
+ Faraday::Request::JSON.adapter = nil
69
+ begin
70
+ yield
71
+ ensure
72
+ Faraday::Request::JSON.adapter = original_adapter
73
+ end
74
+ end
75
+
45
76
  def test_url_encoded_no_header
46
77
  response = @conn.post('/echo', { :fruit => %w[apples oranges] })
47
78
  assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
metadata CHANGED
@@ -1,131 +1,91 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: faraday
3
- version: !ruby/object:Gem::Version
4
- hash: 7
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.7.3
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 7
9
- - 2
10
- version: 0.7.2
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Rick Olson
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-06-12 00:00:00 -07:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2011-06-27 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: rake
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70354659979760 !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
18
+ requirements:
27
19
  - - ~>
28
- - !ruby/object:Gem::Version
29
- hash: 25
30
- segments:
31
- - 0
32
- - 9
33
- version: "0.9"
20
+ - !ruby/object:Gem::Version
21
+ version: '0.9'
34
22
  type: :development
35
- version_requirements: *id001
36
- - !ruby/object:Gem::Dependency
37
- name: test-unit
38
23
  prerelease: false
39
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *70354659979760
25
+ - !ruby/object:Gem::Dependency
26
+ name: test-unit
27
+ requirement: &70354659979200 !ruby/object:Gem::Requirement
40
28
  none: false
41
- requirements:
29
+ requirements:
42
30
  - - ~>
43
- - !ruby/object:Gem::Version
44
- hash: 5
45
- segments:
46
- - 2
47
- - 3
48
- version: "2.3"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.3'
49
33
  type: :development
50
- version_requirements: *id002
51
- - !ruby/object:Gem::Dependency
52
- name: webmock
53
34
  prerelease: false
54
- requirement: &id003 !ruby/object:Gem::Requirement
35
+ version_requirements: *70354659979200
36
+ - !ruby/object:Gem::Dependency
37
+ name: webmock
38
+ requirement: &70354659978700 !ruby/object:Gem::Requirement
55
39
  none: false
56
- requirements:
40
+ requirements:
57
41
  - - ~>
58
- - !ruby/object:Gem::Version
59
- hash: 3
60
- segments:
61
- - 1
62
- - 6
63
- version: "1.6"
42
+ - !ruby/object:Gem::Version
43
+ version: '1.6'
64
44
  type: :development
65
- version_requirements: *id003
66
- - !ruby/object:Gem::Dependency
67
- name: addressable
68
45
  prerelease: false
69
- requirement: &id004 !ruby/object:Gem::Requirement
46
+ version_requirements: *70354659978700
47
+ - !ruby/object:Gem::Dependency
48
+ name: addressable
49
+ requirement: &70354659978200 !ruby/object:Gem::Requirement
70
50
  none: false
71
- requirements:
51
+ requirements:
72
52
  - - ~>
73
- - !ruby/object:Gem::Version
74
- hash: 11
75
- segments:
76
- - 2
77
- - 2
78
- - 6
53
+ - !ruby/object:Gem::Version
79
54
  version: 2.2.6
80
55
  type: :runtime
81
- version_requirements: *id004
82
- - !ruby/object:Gem::Dependency
83
- name: multipart-post
84
56
  prerelease: false
85
- requirement: &id005 !ruby/object:Gem::Requirement
57
+ version_requirements: *70354659978200
58
+ - !ruby/object:Gem::Dependency
59
+ name: multipart-post
60
+ requirement: &70354659977700 !ruby/object:Gem::Requirement
86
61
  none: false
87
- requirements:
62
+ requirements:
88
63
  - - ~>
89
- - !ruby/object:Gem::Version
90
- hash: 19
91
- segments:
92
- - 1
93
- - 1
94
- - 0
64
+ - !ruby/object:Gem::Version
95
65
  version: 1.1.0
96
66
  type: :runtime
97
- version_requirements: *id005
98
- - !ruby/object:Gem::Dependency
99
- name: rack
100
67
  prerelease: false
101
- requirement: &id006 !ruby/object:Gem::Requirement
68
+ version_requirements: *70354659977700
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack
71
+ requirement: &70354659977160 !ruby/object:Gem::Requirement
102
72
  none: false
103
- requirements:
104
- - - ">="
105
- - !ruby/object:Gem::Version
106
- hash: 19
107
- segments:
108
- - 1
109
- - 1
110
- - 0
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
111
76
  version: 1.1.0
112
77
  - - <
113
- - !ruby/object:Gem::Version
114
- hash: 7
115
- segments:
116
- - 2
117
- version: "2"
78
+ - !ruby/object:Gem::Version
79
+ version: '2'
118
80
  type: :runtime
119
- version_requirements: *id006
81
+ prerelease: false
82
+ version_requirements: *70354659977160
120
83
  description: HTTP/REST API client library.
121
84
  email: technoweenie@gmail.com
122
85
  executables: []
123
-
124
86
  extensions: []
125
-
126
87
  extra_rdoc_files: []
127
-
128
- files:
88
+ files:
129
89
  - Gemfile
130
90
  - LICENSE.md
131
91
  - README.md
@@ -165,43 +125,31 @@ files:
165
125
  - test/middleware_stack_test.rb
166
126
  - test/request_middleware_test.rb
167
127
  - test/response_middleware_test.rb
168
- has_rdoc: true
169
128
  homepage: http://github.com/technoweenie/faraday
170
129
  licenses: []
171
-
172
130
  post_install_message:
173
131
  rdoc_options: []
174
-
175
- require_paths:
132
+ require_paths:
176
133
  - lib
177
- required_ruby_version: !ruby/object:Gem::Requirement
134
+ required_ruby_version: !ruby/object:Gem::Requirement
178
135
  none: false
179
- requirements:
180
- - - ">="
181
- - !ruby/object:Gem::Version
182
- hash: 3
183
- segments:
184
- - 0
185
- version: "0"
186
- required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ! '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
187
141
  none: false
188
- requirements:
189
- - - ">="
190
- - !ruby/object:Gem::Version
191
- hash: 17
192
- segments:
193
- - 1
194
- - 3
195
- - 5
142
+ requirements:
143
+ - - ! '>='
144
+ - !ruby/object:Gem::Version
196
145
  version: 1.3.5
197
146
  requirements: []
198
-
199
147
  rubyforge_project: faraday
200
- rubygems_version: 1.6.2
148
+ rubygems_version: 1.8.5
201
149
  signing_key:
202
150
  specification_version: 2
203
151
  summary: HTTP/REST API client library.
204
- test_files:
152
+ test_files:
205
153
  - test/adapters/live_test.rb
206
154
  - test/adapters/logger_test.rb
207
155
  - test/adapters/net_http_test.rb