active_rest_client 0.9.66 → 0.9.67

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: ccfdfcc6a1a86c7b5715f7666c383abf9d99481b
4
- data.tar.gz: 678bcfbdab8714ce08456de2a1d1bd569017eba6
3
+ metadata.gz: bad00208370951aee8feea5b4880654dc658d124
4
+ data.tar.gz: 4fb5e5c094fe3482f1ac9ad24955bce475e0dd0d
5
5
  SHA512:
6
- metadata.gz: cffb6450b43e6346772df324e73bbdb23b7db4feb47c9b305c55a3db41c9414d04f308438d388e55d33d1d8f273946dd9711a008b671339d1fa58778da930e74
7
- data.tar.gz: 2140ca597671d258f351bcd040e827629f9ad9a2c3e92c726e75f509d05cdf782ec87f9837349bb3c6897a9ed34478eccda6240823281e51d8e7c01ff6fa8778
6
+ metadata.gz: 104fdb894bda35c42fe84a536b546b17aeeba7fb667bb0facf020cc1ee3b77c5b5c8c79c9e22c24c81a2824e9cafc7003a56280a2a7f735fe4083ac21aa2f0f2
7
+ data.tar.gz: 21c02661cea31ccff13ee10245396b1459c541aa5f9fa5fab70b01420b88ba5319473f8b4aeae9d67c90e8e9a0278acdfacfa51a379477f0962f1b0d74f90c3f
data/README.md CHANGED
@@ -104,6 +104,23 @@ puts @tv.properties["3d"]
104
104
 
105
105
  ## Advanced Features
106
106
 
107
+ ### Configuration
108
+
109
+ ActiveRestClient uses Faraday to allow switching HTTP backends, the default is Patron. To change the used backend just set it in the class by setting `adapter` to a Faraday supported adapter symbol.
110
+
111
+ ```ruby
112
+ ActiveRestClient::Base.adapter = :net_http
113
+ ```
114
+
115
+ If you want more control you can pass a complete configuration block. For available config variables look into the Faraday documentation.
116
+
117
+ ```ruby
118
+ ActiveRestClient::Base.faraday_config do |faraday|
119
+ faraday.adapter(:net_http)
120
+ faraday.options.timeout = 10
121
+ faraday.headers['User-Agent'] = "ActiveRestClient/#{ActiveRestClient::VERSION}"
122
+ end
123
+ ````
107
124
  ### Associations
108
125
 
109
126
  There are two types of association. One assumes when you call a method you actually want it to call the method on a separate class (as that class has other methods that are useful). The other is lazy loading related classes from a separate URL.
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "bundler", "~> 1.3"
23
23
  spec.add_development_dependency "rake"
24
24
  spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "webmock"
25
26
  spec.add_development_dependency "rspec_junit_formatter"
26
27
  spec.add_development_dependency "simplecov"
27
28
  spec.add_development_dependency "simplecov-rcov"
@@ -31,5 +32,6 @@ Gem::Specification.new do |spec|
31
32
 
32
33
  spec.add_runtime_dependency "oj", "=2.1.4" # 2.1.7 breaks under linux
33
34
  spec.add_runtime_dependency "activesupport"
34
- spec.add_runtime_dependency "patron", '=0.4.9' # 0.4.18 breaks against Curl v0.7.15
35
+ spec.add_runtime_dependency "faraday"
36
+ spec.add_runtime_dependency "patron", ">= 0.4.9" # 0.4.18 breaks against Curl v0.7.15 but works with webmock
35
37
  end
@@ -23,6 +23,23 @@ module ActiveRestClient
23
23
  @@base_url = value
24
24
  end
25
25
 
26
+ def adapter=(adapter)
27
+ ActiveRestClient::Logger.info "\033[1;4;32m#{name}\033[0m Adapter set to be #{adapter}"
28
+ @adapter = adapter
29
+ end
30
+
31
+ def adapter
32
+ @adapter ||= :patron
33
+ end
34
+
35
+ def faraday_config(&block)
36
+ if block
37
+ @faraday_config = block
38
+ else
39
+ @faraday_config ||= default_faraday_config
40
+ end
41
+ end
42
+
26
43
  def lazy_load!
27
44
  @lazy_load = true
28
45
  end
@@ -57,6 +74,21 @@ module ActiveRestClient
57
74
  @@base_url = nil
58
75
  @whiny_missing = nil
59
76
  @lazy_load = false
77
+ @faraday_config = default_faraday_config
78
+ @adapter = :patron
79
+ end
80
+
81
+ private
82
+
83
+ def default_faraday_config
84
+ Proc.new do |faraday|
85
+ faraday.adapter(adapter)
86
+ faraday.options.timeout = 10
87
+ faraday.options.open_timeout = 10
88
+ faraday.headers['User-Agent'] = "ActiveRestClient/#{ActiveRestClient::VERSION}"
89
+ faraday.headers['Connection'] = "Keep-Alive"
90
+ faraday.headers['Accept'] = "application/json"
91
+ end
60
92
  end
61
93
  end
62
94
 
@@ -1,4 +1,4 @@
1
- require 'patron'
1
+ require 'faraday'
2
2
 
3
3
  module ActiveRestClient
4
4
 
@@ -10,25 +10,11 @@ module ActiveRestClient
10
10
 
11
11
  def initialize(base_url)
12
12
  @base_url = base_url
13
- @session = Patron::Session.new
14
- @session.timeout = 10
15
- @session.connect_timeout = 10
16
- @session.base_url = base_url
17
- @session.insecure = true
18
- @session.headers['User-Agent'] = "ActiveRestClient/#{ActiveRestClient::VERSION}"
19
- @session.headers['Connection'] = "Keep-Alive"
20
- @session.headers['Accept'] = "application/json"
13
+ @session = new_session
21
14
  end
22
15
 
23
16
  def reconnect
24
- session = Patron::Session.new
25
- session.timeout = @session.timeout
26
- session.base_url = @session.base_url
27
- session.insecure = true
28
- @session.headers.each do |k,v|
29
- session.headers[k] = v
30
- end
31
- @session = session
17
+ @session = new_session
32
18
  end
33
19
 
34
20
  def headers
@@ -37,40 +23,56 @@ module ActiveRestClient
37
23
 
38
24
  def make_safe_request(path, &block)
39
25
  block.call
40
- rescue Patron::TimeoutError
26
+ rescue Faraday::TimeoutError
41
27
  raise ActiveRestClient::TimeoutException.new("Timed out getting #{@base_url}#{path}")
42
- rescue Patron::ConnectionFailed
28
+ rescue Faraday::ConnectionFailed
43
29
  begin
44
30
  reconnect
45
31
  block.call
46
- rescue Patron::ConnectionFailed
32
+ rescue Faraday::ConnectionFailed
47
33
  raise ActiveRestClient::ConnectionFailedException.new("Unable to connect to #{@base_url}#{path}")
48
34
  end
49
35
  end
50
36
 
51
37
  def get(path, headers={})
52
38
  make_safe_request(path) do
53
- @session.get(path, headers)
39
+ @session.get(path) do |req|
40
+ req.headers = headers
41
+ end
54
42
  end
55
43
  end
56
44
 
57
45
  def put(path, data, headers={})
58
46
  make_safe_request(path) do
59
- @session.put(path, data, headers)
47
+ @session.put(path) do |req|
48
+ req.headers = headers
49
+ req.body = data
50
+ end
60
51
  end
61
52
  end
62
53
 
63
54
  def post(path, data, headers={})
64
55
  make_safe_request(path) do
65
- @session.post(path, data, headers)
56
+ @session.post(path) do |req|
57
+ req.headers = headers
58
+ req.body = data
59
+ end
66
60
  end
67
61
  end
68
62
 
69
63
  def delete(path, headers={})
70
64
  make_safe_request(path) do
71
- @session.delete(path, headers)
65
+ @session.delete(path) do |req|
66
+ req.headers = headers
67
+ end
72
68
  end
73
69
  end
74
70
 
71
+ private
72
+
73
+ def new_session
74
+ Faraday.new({url: @base_url}, &ActiveRestClient::Base.faraday_config)
75
+ end
76
+
75
77
  end
76
78
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveRestClient
2
- VERSION = "0.9.66"
2
+ VERSION = "0.9.67"
3
3
  end
@@ -101,5 +101,30 @@ describe ActiveRestClient::Configuration do
101
101
  expect{ ConfigurationExample.proxy.respond_to?(:length) }.to be_true
102
102
  end
103
103
 
104
+ describe "faraday_config" do
105
+ let(:faraday_double){double(:faraday).as_null_object}
106
+
107
+ it "should use default adapter if no other block set" do
108
+ faraday_double.should_receive(:adapter).with(:patron)
109
+ ConfigurationExample.faraday_config.call(faraday_double)
110
+ end
111
+
112
+ it "should us set adapter if no other block set" do
113
+ ConfigurationExample.adapter = :net_http
114
+
115
+ faraday_double.should_receive(:adapter).with(:net_http)
116
+
117
+ ConfigurationExample.faraday_config.call(faraday_double)
118
+ end
119
+
120
+ it "should use the adapter of the passed in faraday_config block" do
121
+ ConfigurationExample.faraday_config {|faraday| faraday.adapter(:rack)}
122
+
123
+ faraday_double.should_receive(:adapter).with(:rack)
124
+
125
+ ConfigurationExample.faraday_config.call(faraday_double)
126
+ end
127
+
128
+ end
104
129
 
105
130
  end
@@ -5,68 +5,53 @@ describe ActiveRestClient::Connection do
5
5
  @connection = ActiveRestClient::Connection.new("http://www.example.com")
6
6
  end
7
7
 
8
- it "should contain a patron session" do
9
- expect(@connection.session).to be_a_kind_of(Patron::Session)
8
+ it "should contain a Farday connection" do
9
+ expect(@connection.session).to be_a_kind_of(Faraday::Connection)
10
10
  end
11
11
 
12
- it "should set the Base URL for Patron to be the one passed in" do
13
- expect(@connection.session.base_url).to eq("http://www.example.com")
12
+ it "should set the Base URL to be the one passed in" do
13
+ expect(@connection.session.url_prefix.to_s).to eq("http://www.example.com/")
14
14
  end
15
15
 
16
- it "should set a user agent for the Patron session" do
16
+ it "should set a user agent for the session" do
17
17
  expect(@connection.headers["User-Agent"]).to match(/^ActiveRestClient\/[0-9.]+$/)
18
18
  end
19
19
 
20
- it "should try to Keep-Alive Patron session connections" do
20
+ it "should try to Keep-Alive session connections" do
21
21
  expect(@connection.headers["Connection"]).to match(/Keep-Alive/)
22
22
  end
23
23
 
24
- it "should pass a GET request through to Patron" do
25
- @connection.session = double(Patron::Session)
26
- @connection.session.stub(:get).with("/foo", {}).and_return(OpenStruct.new(body:"{result:true}"))
24
+ it "should pass a GET request through to Faraday" do
25
+ stub_request(:get, "www.example.com/foo").to_return(body: "{result:true}")
27
26
  result = @connection.get("/foo")
28
27
  expect(result.body).to eq("{result:true}")
29
28
  end
30
29
 
31
- it "should pass a PUT request through to Patron" do
32
- @connection.session = double(Patron::Session)
33
- allow(@connection.session).to receive(:put).with("/foo", "body", {}).and_return(OpenStruct.new(body:"{result:true}"))
30
+ it "should pass a PUT request through to Faraday" do
31
+ stub_request(:put, "www.example.com/foo").with(body: "body").to_return(body: "{result:true}")
34
32
  result = @connection.put("/foo", "body")
35
33
  expect(result.body).to eq("{result:true}")
36
34
  end
37
35
 
38
- it "should pass a POST request through to Patron" do
39
- @connection.session = double(Patron::Session)
40
- @connection.session.stub(:post).with("/foo", "body", {}).and_return(OpenStruct.new(body:"{result:true}"))
36
+ it "should pass a POST request through to Faraday" do
37
+ stub_request(:post, "www.example.com/foo").with(body: "body").to_return(body: "{result:true}")
41
38
  result = @connection.post("/foo", "body")
42
39
  expect(result.body).to eq("{result:true}")
43
40
  end
44
41
 
45
- it "should pass a DELETE request through to Patron" do
46
- @connection.session = double(Patron::Session)
47
- @connection.session.stub(:delete).with("/foo", {}).and_return(OpenStruct.new(body:"{result:true}"))
42
+ it "should pass a DELETE request through to Faraday" do
43
+ stub_request(:delete, "www.example.com/foo").to_return(body: "{result:true}")
48
44
  result = @connection.delete("/foo")
49
45
  expect(result.body).to eq("{result:true}")
50
46
  end
51
47
 
52
48
  it "should retry once in the event of a connection failed" do
53
- @times_called = 0
54
- Patron::Session.any_instance.stub(:get).and_return do
55
- raise Patron::ConnectionFailed.new("Foo") if (@times_called += 1) == 1
56
- end
57
- expect { @connection.get("/foo") }.to_not raise_error
58
- end
59
-
60
- it "should not retry more than once in the event of a connection failed" do
61
- @times_called = 0
62
- Patron::Session.any_instance.stub(:get).and_return do
63
- raise Patron::ConnectionFailed.new("Foo")
64
- end
49
+ stub_request(:get, "www.example.com/foo").to_raise(Faraday::ConnectionFailed.new("Foo"))
65
50
  expect { @connection.get("/foo") }.to raise_error(ActiveRestClient::ConnectionFailedException)
66
51
  end
67
52
 
68
53
  it "should raise an exception on timeout" do
69
- Patron::Session.any_instance.stub(:get).and_raise Patron::TimeoutError.new("Foo")
54
+ stub_request(:get, "www.example.com/foo").to_timeout
70
55
  expect { @connection.get("/foo") }.to raise_error(ActiveRestClient::TimeoutException)
71
56
  end
72
57
 
data/spec/spec_helper.rb CHANGED
@@ -2,6 +2,7 @@ require 'rspec'
2
2
  require 'simplecov'
3
3
  require 'active_rest_client'
4
4
  require "ostruct"
5
+ require 'webmock/rspec'
5
6
 
6
7
  if ENV["JENKINS"]
7
8
  require 'simplecov-rcov'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_rest_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.66
4
+ version: 0.9.67
5
5
  platform: ruby
6
6
  authors:
7
7
  - Which Ltd
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-17 00:00:00.000000000 Z
12
+ date: 2014-02-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -53,6 +53,20 @@ dependencies:
53
53
  - - ">="
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: webmock
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
56
70
  - !ruby/object:Gem::Dependency
57
71
  name: rspec_junit_formatter
58
72
  requirement: !ruby/object:Gem::Requirement
@@ -165,18 +179,32 @@ dependencies:
165
179
  - - ">="
166
180
  - !ruby/object:Gem::Version
167
181
  version: '0'
182
+ - !ruby/object:Gem::Dependency
183
+ name: faraday
184
+ requirement: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - ">="
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ type: :runtime
190
+ prerelease: false
191
+ version_requirements: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - ">="
194
+ - !ruby/object:Gem::Version
195
+ version: '0'
168
196
  - !ruby/object:Gem::Dependency
169
197
  name: patron
170
198
  requirement: !ruby/object:Gem::Requirement
171
199
  requirements:
172
- - - '='
200
+ - - ">="
173
201
  - !ruby/object:Gem::Version
174
202
  version: 0.4.9
175
203
  type: :runtime
176
204
  prerelease: false
177
205
  version_requirements: !ruby/object:Gem::Requirement
178
206
  requirements:
179
- - - '='
207
+ - - ">="
180
208
  - !ruby/object:Gem::Version
181
209
  version: 0.4.9
182
210
  description: Accessing REST services in an ActiveRecord style