active_rest_client 0.9.66 → 0.9.67
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.
- checksums.yaml +4 -4
- data/README.md +17 -0
- data/active_rest_client.gemspec +3 -1
- data/lib/active_rest_client/configuration.rb +32 -0
- data/lib/active_rest_client/connection.rb +26 -24
- data/lib/active_rest_client/version.rb +1 -1
- data/spec/lib/configuration_spec.rb +25 -0
- data/spec/lib/connection_spec.rb +16 -31
- data/spec/spec_helper.rb +1 -0
- metadata +32 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bad00208370951aee8feea5b4880654dc658d124
|
4
|
+
data.tar.gz: 4fb5e5c094fe3482f1ac9ad24955bce475e0dd0d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
data/active_rest_client.gemspec
CHANGED
@@ -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 "
|
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 '
|
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 =
|
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
|
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
|
26
|
+
rescue Faraday::TimeoutError
|
41
27
|
raise ActiveRestClient::TimeoutException.new("Timed out getting #{@base_url}#{path}")
|
42
|
-
rescue
|
28
|
+
rescue Faraday::ConnectionFailed
|
43
29
|
begin
|
44
30
|
reconnect
|
45
31
|
block.call
|
46
|
-
rescue
|
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
|
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
|
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
|
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
|
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
|
@@ -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
|
data/spec/lib/connection_spec.rb
CHANGED
@@ -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
|
9
|
-
expect(@connection.session).to be_a_kind_of(
|
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
|
13
|
-
expect(@connection.session.
|
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
|
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
|
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
|
25
|
-
|
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
|
32
|
-
|
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
|
39
|
-
|
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
|
46
|
-
|
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
|
-
|
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
|
-
|
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
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.
|
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-
|
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
|