active_rest_client 0.9.73 → 0.9.75
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/lib/active_rest_client/configuration.rb +26 -6
- data/lib/active_rest_client/request.rb +22 -2
- data/lib/active_rest_client/version.rb +1 -1
- data/spec/lib/base_spec.rb +10 -0
- data/spec/lib/configuration_spec.rb +15 -0
- data/spec/lib/request_spec.rb +21 -2
- data/spec/lib/result_iterator_spec.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 38b0f0e4a23d6db8a525a38e0ee49e6514dffc50
|
4
|
+
data.tar.gz: 2df46c685aba909268ba5aad7c0c99491c997e90
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7cf6e65cf69fa56b970ed6223fdb5b485e0c2977a6d8f33455d1cc9ddb0c908b4b2d74b776d3447379fb35c87bf741d4d547b9460724dd0a0a224fca50f9266e
|
7
|
+
data.tar.gz: ca5f1b7c7957b877a0fd20563dc6c5182cf567d9d388f7f213983645941203a2053f23e3eae4e997e6586576a4c55a34d360bda0d2c1c3a0feec1880472187b9
|
data/README.md
CHANGED
@@ -382,6 +382,23 @@ class Person < ActiveRestClient::Base
|
|
382
382
|
end
|
383
383
|
```
|
384
384
|
|
385
|
+
### Body Types
|
386
|
+
|
387
|
+
By default ActiveRestClient puts the body in to normal CGI parameters in K=V&K2=V2 format. However, if you want to use JSON for your PUT/POST requests, you can use either (the other option, the default, is `:form_encoded`):
|
388
|
+
|
389
|
+
```ruby
|
390
|
+
class Person < ActiveRestClient::Base
|
391
|
+
request_body_type :json
|
392
|
+
# ...
|
393
|
+
end
|
394
|
+
```
|
395
|
+
|
396
|
+
or
|
397
|
+
|
398
|
+
```ruby
|
399
|
+
ActiveRestClient::Base.request_body_type = :json
|
400
|
+
```
|
401
|
+
|
385
402
|
### Faking Calls
|
386
403
|
|
387
404
|
There are times when an API hasn't been developed yet, so you want to fake the API call response. To do this, you can simply pass a `fake` option when mapping the call containing the response.
|
@@ -2,6 +2,7 @@ module ActiveRestClient
|
|
2
2
|
module Configuration
|
3
3
|
module ClassMethods
|
4
4
|
@@base_url = nil
|
5
|
+
@@request_body_type = :form_encoded
|
5
6
|
@lazy_load = false
|
6
7
|
|
7
8
|
def base_url(value = nil)
|
@@ -23,6 +24,23 @@ module ActiveRestClient
|
|
23
24
|
@@base_url = value
|
24
25
|
end
|
25
26
|
|
27
|
+
def request_body_type(value = nil)
|
28
|
+
if value.nil?
|
29
|
+
if @request_body_type.nil?
|
30
|
+
@@request_body_type
|
31
|
+
else
|
32
|
+
@request_body_type
|
33
|
+
end
|
34
|
+
else
|
35
|
+
@request_body_type = value
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def request_body_type=(value)
|
40
|
+
ActiveRestClient::Logger.info "\033[1;4;32m#{name}\033[0m Request Body Type set to be #{value}"
|
41
|
+
@@request_body_type = value
|
42
|
+
end
|
43
|
+
|
26
44
|
def adapter=(adapter)
|
27
45
|
ActiveRestClient::Logger.info "\033[1;4;32m#{name}\033[0m Adapter set to be #{adapter}"
|
28
46
|
@adapter = adapter
|
@@ -70,12 +88,14 @@ module ActiveRestClient
|
|
70
88
|
end
|
71
89
|
|
72
90
|
def _reset_configuration!
|
73
|
-
@base_url
|
74
|
-
@@base_url
|
75
|
-
@
|
76
|
-
|
77
|
-
@
|
78
|
-
@
|
91
|
+
@base_url = nil
|
92
|
+
@@base_url = nil
|
93
|
+
@request_body_type = nil
|
94
|
+
@@request_body_type = :form_encoded
|
95
|
+
@whiny_missing = nil
|
96
|
+
@lazy_load = false
|
97
|
+
@faraday_config = default_faraday_config
|
98
|
+
@adapter = :patron
|
79
99
|
end
|
80
100
|
|
81
101
|
private
|
@@ -44,6 +44,14 @@ module ActiveRestClient
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
+
def request_body_type
|
48
|
+
if object_is_class?
|
49
|
+
@object.request_body_type
|
50
|
+
else
|
51
|
+
@object.class.request_body_type
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
47
55
|
def verbose?
|
48
56
|
if object_is_class?
|
49
57
|
@object.verbose
|
@@ -171,7 +179,11 @@ module ActiveRestClient
|
|
171
179
|
end
|
172
180
|
|
173
181
|
def prepare_request_body(params = nil)
|
174
|
-
|
182
|
+
if request_body_type == :form_encoded
|
183
|
+
@body ||= (params || @post_params || {}).map {|k,v| "#{k}=#{CGI.escape(v.to_s)}"}.sort * "&"
|
184
|
+
elsif request_body_type == :json
|
185
|
+
@body ||= (params || @post_params || {}).to_json
|
186
|
+
end
|
175
187
|
end
|
176
188
|
|
177
189
|
def do_request(etag)
|
@@ -183,7 +195,7 @@ module ActiveRestClient
|
|
183
195
|
http_headers[key] = value
|
184
196
|
end
|
185
197
|
if @method[:options][:url] || @forced_url
|
186
|
-
@url = @method[:options][:url]
|
198
|
+
@url = @method[:options][:url] || @method[:url]
|
187
199
|
@url = @forced_url if @forced_url
|
188
200
|
if connection = ActiveRestClient::ConnectionManager.find_connection_for_url(@url)
|
189
201
|
@url = @url.slice(connection.base_url.length, 255)
|
@@ -192,12 +204,20 @@ module ActiveRestClient
|
|
192
204
|
if (parts.empty?) # Not a full URL, so use hostname/protocol from existing base_url
|
193
205
|
uri = URI.parse(base_url)
|
194
206
|
@base_url = "#{uri.scheme}://#{uri.host}#{":#{uri.port}" if uri.port != 80 && uri.port != 443}"
|
207
|
+
@url = "#{base_url}#{@url}".gsub(@base_url, "")
|
195
208
|
else
|
196
209
|
_, @base_url, @url = parts
|
197
210
|
end
|
198
211
|
connection = ActiveRestClient::ConnectionManager.get_connection(@base_url)
|
199
212
|
end
|
200
213
|
else
|
214
|
+
parts = @url.match(%r{^(https?://[a-z\d\.:-]+?)(/.*)}).to_a
|
215
|
+
if (parts.empty?) # Not a full URL, so use hostname/protocol from existing base_url
|
216
|
+
uri = URI.parse(base_url)
|
217
|
+
@base_url = "#{uri.scheme}://#{uri.host}#{":#{uri.port}" if uri.port != 80 && uri.port != 443}"
|
218
|
+
@url = "#{base_url}#{@url}".gsub(@base_url, "")
|
219
|
+
base_url = @base_url
|
220
|
+
end
|
201
221
|
connection = ActiveRestClient::ConnectionManager.get_connection(base_url)
|
202
222
|
end
|
203
223
|
ActiveRestClient::Logger.info " \033[1;4;32m#{ActiveRestClient::NAME}\033[0m #{@instrumentation_name} - Requesting #{connection.base_url}#{@url}"
|
data/spec/lib/base_spec.rb
CHANGED
@@ -32,6 +32,11 @@ class RecordResponseExample < ActiveRestClient::Base
|
|
32
32
|
get :all, "/all"
|
33
33
|
end
|
34
34
|
|
35
|
+
class NonHostnameBaseUrlExample < ActiveRestClient::Base
|
36
|
+
base_url "http://www.example.com/v1/"
|
37
|
+
get :all, "/all"
|
38
|
+
end
|
39
|
+
|
35
40
|
describe ActiveRestClient::Base do
|
36
41
|
it 'should instantiate a new descendant' do
|
37
42
|
expect{EmptyExample.new}.to_not raise_error(Exception)
|
@@ -208,6 +213,11 @@ describe ActiveRestClient::Base do
|
|
208
213
|
EmptyExample._request("http://api.example.com/")
|
209
214
|
end
|
210
215
|
|
216
|
+
it "should make an HTTP request including the path in the base_url" do
|
217
|
+
ActiveRestClient::Connection.any_instance.should_receive(:get).with('/v1/all', anything).and_return(OpenStruct.new(status:200, headers:{}, body:"{\"first_name\":\"Billy\"}"))
|
218
|
+
NonHostnameBaseUrlExample.all
|
219
|
+
end
|
220
|
+
|
211
221
|
it "should map the response from the directly called URL in the normal way" do
|
212
222
|
ActiveRestClient::Request.any_instance.should_receive(:do_request).with(any_args).and_return(OpenStruct.new(status:200, headers:{}, body:"{\"first_name\":\"Billy\"}"))
|
213
223
|
example = EmptyExample._request("http://api.example.com/")
|
@@ -8,6 +8,7 @@ describe ActiveRestClient::Configuration do
|
|
8
8
|
class ConfigurationExample
|
9
9
|
include ActiveRestClient::Configuration
|
10
10
|
base_url "http://www.example.com"
|
11
|
+
request_body_type :json
|
11
12
|
end
|
12
13
|
|
13
14
|
class ConfigurationExampleBare
|
@@ -54,6 +55,20 @@ describe ActiveRestClient::Configuration do
|
|
54
55
|
expect(ConfigurationExample2.base_url).to eq("http://specific.example.com")
|
55
56
|
end
|
56
57
|
|
58
|
+
it "should default to a form_encoded request_body_type" do
|
59
|
+
expect(ActiveRestClient::Base.request_body_type).to eq(:form_encoded)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should remember the request_body_type" do
|
63
|
+
expect(ConfigurationExample.request_body_type).to eq(:json)
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should remember the set base_url on a class, overriding a general one" do
|
67
|
+
ActiveRestClient::Base.request_body_type = :unknown
|
68
|
+
expect(ActiveRestClient::Base.request_body_type).to eq(:unknown)
|
69
|
+
expect(ConfigurationExample.request_body_type).to eq(:json)
|
70
|
+
end
|
71
|
+
|
57
72
|
it "should default to non-lazy loading" do
|
58
73
|
class LazyLoadingConfigurationExample1
|
59
74
|
include ActiveRestClient::Configuration
|
data/spec/lib/request_spec.rb
CHANGED
@@ -5,6 +5,7 @@ describe ActiveRestClient::Request do
|
|
5
5
|
class ExampleOtherClient < ActiveRestClient::Base ; end
|
6
6
|
class ExampleClient < ActiveRestClient::Base
|
7
7
|
base_url "http://www.example.com"
|
8
|
+
request_body_type :form_encoded
|
8
9
|
|
9
10
|
before_request do |name, request|
|
10
11
|
if request.method[:name] == :headers
|
@@ -26,12 +27,14 @@ describe ActiveRestClient::Request do
|
|
26
27
|
end
|
27
28
|
|
28
29
|
class LazyLoadedExampleClient < ExampleClient
|
30
|
+
base_url "http://www.example.com"
|
29
31
|
lazy_load!
|
30
32
|
get :fake, "/fake", fake:"{\"result\":true, \"list\":[1,2,3,{\"test\":true}], \"child\":{\"grandchild\":{\"test\":true}}}"
|
31
33
|
get :lazy_test, "/does-not-matter", fake:"{\"people\":[\"http://www.example.com/some/url\"]}", :lazy => [:people]
|
32
34
|
end
|
33
35
|
|
34
36
|
class VerboseExampleClient < ExampleClient
|
37
|
+
base_url "http://www.example.com"
|
35
38
|
verbose!
|
36
39
|
get :all, "/all"
|
37
40
|
end
|
@@ -100,6 +103,17 @@ describe ActiveRestClient::Request do
|
|
100
103
|
ExampleClient.update id:1234, debug:true
|
101
104
|
end
|
102
105
|
|
106
|
+
it "should encode the body in a form-encoded format by default" do
|
107
|
+
ActiveRestClient::Connection.any_instance.should_receive(:put).with("/put/1234", "debug=true&test=foo", an_instance_of(Hash)).and_return(OpenStruct.new(body:"{\"result\":true}", headers:{}))
|
108
|
+
ExampleClient.update id:1234, debug:true, test:'foo'
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should encode the body in a JSON format if specified" do
|
112
|
+
ActiveRestClient::Connection.any_instance.should_receive(:put).with("/put/1234", %q({"debug":true,"test":"foo"}), an_instance_of(Hash)).and_return(OpenStruct.new(body:"{\"result\":true}", headers:{}))
|
113
|
+
ExampleClient.request_body_type :json
|
114
|
+
ExampleClient.update id:1234, debug:true, test:'foo'
|
115
|
+
end
|
116
|
+
|
103
117
|
it "should pass through custom headers" do
|
104
118
|
ActiveRestClient::Connection.any_instance.should_receive(:get).with("/headers", hash_including("X-My-Header" => "myvalue")).and_return(OpenStruct.new(body:'{"result":true}', headers:{}))
|
105
119
|
ExampleClient.headers
|
@@ -331,6 +345,10 @@ describe ActiveRestClient::Request do
|
|
331
345
|
it "should raise an exception if you try to pass in an unsupport method" do
|
332
346
|
method = {:method => :wiggle, url:"/"}
|
333
347
|
class RequestFakeObject
|
348
|
+
def request_body_type
|
349
|
+
:form_encoded
|
350
|
+
end
|
351
|
+
|
334
352
|
def base_url
|
335
353
|
"http://www.example.com/"
|
336
354
|
end
|
@@ -397,11 +415,12 @@ describe ActiveRestClient::Request do
|
|
397
415
|
|
398
416
|
it "should allow requests to partial URLs using the current base_url" do
|
399
417
|
ActiveRestClient::ConnectionManager.reset!
|
400
|
-
connection = double("Connection")
|
418
|
+
connection = double("Connection")
|
419
|
+
allow(connection).to receive(:base_url).and_return("http://www.example.com")
|
401
420
|
ActiveRestClient::ConnectionManager.should_receive(:get_connection).with("http://www.example.com").and_return(connection)
|
402
421
|
connection.
|
403
422
|
should_receive(:get).
|
404
|
-
with("/people", an_instance_of(Hash)).
|
423
|
+
with("/v1/people", an_instance_of(Hash)).
|
405
424
|
and_return(OpenStruct.new(body:"{\"first_name\":\"John\", \"id\":1234}", headers:{}, status:200))
|
406
425
|
@obj = SameServerExampleClient._request('/people')
|
407
426
|
end
|
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.75
|
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-03-
|
12
|
+
date: 2014-03-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|