active_rest_client 1.0.6 → 1.0.7
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/.travis.yml +1 -0
- data/README.md +26 -5
- data/active_rest_client.gemspec +2 -2
- data/lib/active_rest_client/base.rb +31 -0
- data/lib/active_rest_client/configuration.rb +36 -0
- data/lib/active_rest_client/request.rb +34 -1
- data/lib/active_rest_client/result_iterator.rb +18 -0
- data/lib/active_rest_client/version.rb +1 -1
- data/spec/lib/base_spec.rb +68 -20
- data/spec/lib/caching_spec.rb +23 -23
- data/spec/lib/configuration_spec.rb +38 -14
- data/spec/lib/instrumentation_spec.rb +7 -8
- data/spec/lib/lazy_association_loader_spec.rb +7 -7
- data/spec/lib/lazy_loader_spec.rb +5 -5
- data/spec/lib/logger_spec.rb +13 -13
- data/spec/lib/proxy_spec.rb +15 -15
- data/spec/lib/recording_spec.rb +2 -2
- data/spec/lib/request_spec.rb +133 -98
- data/spec/lib/result_iterator_spec.rb +34 -5
- data/spec/lib/validation_spec.rb +1 -1
- data/spec/spec_helper.rb +11 -2
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9e90aa3aab74da133bb45147624a0723812c609e
|
4
|
+
data.tar.gz: fe25689f084ddcaa682c7230152e2c1a49fa03db
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a5376c72339436353eb5e46ef657c479bda1adb14bd446e7bc731aae88ace39c6a42a64a449de3cb49f3f123c3f2b1ef85a0e214ff01c4f838bc5fd07f1e7e0c
|
7
|
+
data.tar.gz: dbf38d4b667137b07724da1d3fb0d365f7cb415df6b7aafd28cbcb4c1b74d5e225077a221110342de9bded154501708022883f35e35d9bf5de90340d00db1729
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -75,6 +75,15 @@ id = @person.id
|
|
75
75
|
end
|
76
76
|
```
|
77
77
|
|
78
|
+
If an API returns an array of results and you have [will_paginate](https://rubygems.org/gems/will_paginate) installed then you can call the paginate method to return a particular page of the results (note: this doesn't reduce the load on the server, but it can help with pagination if you have a cached response).
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
@people = Person.all
|
82
|
+
@people.paginate(page: 1, per_page: 10).each do |person|
|
83
|
+
puts "You made the first page: " + person.first_name
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
78
87
|
Note, you can assign to any attribute, whether it exists or not before and read from any attribute (which will return nil if not found). If you pass a string or a number to a method it will assume that it's for the "id" field. Any other field values must be passed as a hash and you can't mix passing a string/number and a hash.
|
79
88
|
|
80
89
|
```ruby
|
@@ -317,7 +326,7 @@ ActiveRestClient::Base.cache_store = Redis::Store.new("redis://localhost:6379/0/
|
|
317
326
|
|
318
327
|
You can use filters to alter get/post parameters, the URL or set the post body (doing so overrides normal parameter insertion in to the body) before a request or to adjust the response after a request. This can either be a block or a named method (like ActionController's `before_filter`/`before_action` methods).
|
319
328
|
|
320
|
-
The filter is passed the name of the method (e.g. `:save`) and an object (a request object for `
|
329
|
+
The filter is passed the name of the method (e.g. `:save`) and an object (a request object for `before_request` and a response object for `after_request`). The request object has four public attributes `post_params` (a Hash of the POST parameters), `get_params` (a Hash of the GET parameters), headers and `url` (a String containing the full URL without GET parameters appended)
|
321
330
|
|
322
331
|
```ruby
|
323
332
|
require 'secure_random'
|
@@ -372,11 +381,11 @@ After filters work in exactly the same way:
|
|
372
381
|
|
373
382
|
```ruby
|
374
383
|
class Person < ActiveRestClient::Base
|
375
|
-
|
384
|
+
after_request :fix_empty_content
|
376
385
|
|
377
386
|
private
|
378
387
|
|
379
|
-
def fix_empty_content
|
388
|
+
def fix_empty_content(name, response)
|
380
389
|
if response.status == 204 && response.body.blank?
|
381
390
|
response.body = '{"empty": true}'
|
382
391
|
end
|
@@ -443,13 +452,25 @@ or
|
|
443
452
|
ActiveRestClient::Base.request_body_type = :json
|
444
453
|
```
|
445
454
|
|
455
|
+
This will also set the header `Content-Type` to `application/x-www-form-urlencoded` by default or `application/json` when `:json`.
|
456
|
+
|
457
|
+
If you have an API that is inconsistent in its body type requirements, you can also specify it on the individual method mapping:
|
458
|
+
|
459
|
+
```ruby
|
460
|
+
class Person < ActiveRestClient::Base
|
461
|
+
request_body_type :form_encoded # This is the default, but just for demo purposes
|
462
|
+
|
463
|
+
get :all, '/people', request_body_type: :json
|
464
|
+
end
|
465
|
+
```
|
466
|
+
|
446
467
|
### Faking Calls
|
447
468
|
|
448
469
|
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.
|
449
470
|
|
450
471
|
```ruby
|
451
472
|
class Person < ActiveRestClient::Base
|
452
|
-
get :all, '/people', :
|
473
|
+
get :all, '/people', fake: [{first_name:"Johnny"}, {first_name:"Bob"}]
|
453
474
|
end
|
454
475
|
```
|
455
476
|
|
@@ -457,7 +478,7 @@ You may want to run a proc when faking data (to put information from the paramet
|
|
457
478
|
|
458
479
|
```ruby
|
459
480
|
class Person < ActiveRestClient::Base
|
460
|
-
get :all, '/people', :
|
481
|
+
get :all, '/people', fake: ->(request) { {result: request.get_params[:id]} }
|
461
482
|
end
|
462
483
|
```
|
463
484
|
|
data/active_rest_client.gemspec
CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
|
22
22
|
spec.add_development_dependency "bundler", "~> 1.3"
|
23
23
|
spec.add_development_dependency "rake"
|
24
|
-
spec.add_development_dependency "rspec", "~>
|
24
|
+
spec.add_development_dependency "rspec", "~> 3"
|
25
25
|
spec.add_development_dependency "webmock"
|
26
26
|
spec.add_development_dependency "rspec_junit_formatter"
|
27
27
|
spec.add_development_dependency "simplecov"
|
@@ -33,5 +33,5 @@ Gem::Specification.new do |spec|
|
|
33
33
|
spec.add_runtime_dependency "multi_json"
|
34
34
|
spec.add_runtime_dependency "activesupport"
|
35
35
|
spec.add_runtime_dependency "faraday"
|
36
|
-
spec.
|
36
|
+
spec.add_runtime_dependency "patron", ">= 0.4.9" # 0.4.18 breaks against Curl v0.7.15 but works with webmock
|
37
37
|
end
|
@@ -8,6 +8,8 @@ module ActiveRestClient
|
|
8
8
|
include Recording
|
9
9
|
|
10
10
|
attr_accessor :_status
|
11
|
+
attr_accessor :_etag
|
12
|
+
attr_accessor :_headers
|
11
13
|
|
12
14
|
instance_methods.each do |m|
|
13
15
|
next unless %w{display errors presence load require hash untrust trust freeze method enable_warnings with_warnings suppress capture silence quietly debugger breakpoint}.map(&:to_sym).include? m
|
@@ -24,6 +26,8 @@ module ActiveRestClient
|
|
24
26
|
attribute_name = attribute_name.to_sym
|
25
27
|
if attribute_value.to_s[/\d{4}\-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(Z|[+-]\d{2}:\d{2})/]
|
26
28
|
@attributes[attribute_name] = DateTime.parse(attribute_value)
|
29
|
+
elsif attribute_value.to_s =~ /^\d{4}\-\d{2}-\d{2}$/
|
30
|
+
@attributes[attribute_name] = Date.parse(attribute_value)
|
27
31
|
else
|
28
32
|
@attributes[attribute_name] = attribute_value
|
29
33
|
end
|
@@ -95,6 +99,20 @@ module ActiveRestClient
|
|
95
99
|
end
|
96
100
|
end
|
97
101
|
|
102
|
+
def inspect
|
103
|
+
inspection = if @attributes.any?
|
104
|
+
@attributes.collect { |key, value|
|
105
|
+
"#{key}: #{value_for_inspect(value)}"
|
106
|
+
}.compact.join(", ")
|
107
|
+
else
|
108
|
+
"[uninitialized]"
|
109
|
+
end
|
110
|
+
inspection += "#{"," if @attributes.any?} ETag: #{@_etag}" unless @_etag.nil?
|
111
|
+
inspection += "#{"," if @attributes.any?} Status: #{@_status}" unless @_status.nil?
|
112
|
+
inspection += " (unsaved: #{@dirty_attributes.map(&:to_s).join(", ")})" if @dirty_attributes.any?
|
113
|
+
"#<#{self.class} #{inspection}>"
|
114
|
+
end
|
115
|
+
|
98
116
|
def method_missing(name, *args)
|
99
117
|
if name.to_s[-1,1] == "="
|
100
118
|
name = name.to_s.chop.to_sym
|
@@ -146,6 +164,19 @@ module ActiveRestClient
|
|
146
164
|
output = to_hash
|
147
165
|
output.to_json
|
148
166
|
end
|
167
|
+
|
168
|
+
private
|
169
|
+
|
170
|
+
def value_for_inspect(value)
|
171
|
+
if value.is_a?(String) && value.length > 50
|
172
|
+
"#{value[0..50]}...".inspect
|
173
|
+
elsif value.is_a?(Date) || value.is_a?(Time)
|
174
|
+
%("#{value.to_s(:db)}")
|
175
|
+
else
|
176
|
+
value.inspect
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
149
180
|
end
|
150
181
|
|
151
182
|
class NoAttributeException < StandardError ; end
|
@@ -2,6 +2,8 @@ module ActiveRestClient
|
|
2
2
|
module Configuration
|
3
3
|
module ClassMethods
|
4
4
|
@@base_url = nil
|
5
|
+
@@username = nil
|
6
|
+
@@password = nil
|
5
7
|
@@request_body_type = :form_encoded
|
6
8
|
@lazy_load = false
|
7
9
|
|
@@ -24,6 +26,40 @@ module ActiveRestClient
|
|
24
26
|
@@base_url = value
|
25
27
|
end
|
26
28
|
|
29
|
+
def username(value = nil)
|
30
|
+
if value.nil?
|
31
|
+
if @username.nil?
|
32
|
+
@@username
|
33
|
+
else
|
34
|
+
@username
|
35
|
+
end
|
36
|
+
else
|
37
|
+
@username = value
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def username=(value)
|
42
|
+
ActiveRestClient::Logger.info "\033[1;4;32m#{name}\033[0m Username set to be #{value}"
|
43
|
+
@@username = value
|
44
|
+
end
|
45
|
+
|
46
|
+
def password(value = nil)
|
47
|
+
if value.nil?
|
48
|
+
if @password.nil?
|
49
|
+
@@password
|
50
|
+
else
|
51
|
+
@password
|
52
|
+
end
|
53
|
+
else
|
54
|
+
@password = value
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def password=(value)
|
59
|
+
ActiveRestClient::Logger.info "\033[1;4;32m#{name}\033[0m Password set..."
|
60
|
+
@@password = value
|
61
|
+
end
|
62
|
+
|
27
63
|
def request_body_type(value = nil)
|
28
64
|
if value.nil?
|
29
65
|
if @request_body_type.nil?
|
@@ -44,8 +44,34 @@ module ActiveRestClient
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
-
def
|
47
|
+
def base_url
|
48
|
+
if object_is_class?
|
49
|
+
@object.base_url
|
50
|
+
else
|
51
|
+
@object.class.base_url
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def username
|
48
56
|
if object_is_class?
|
57
|
+
@object.username
|
58
|
+
else
|
59
|
+
@object.class.username
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def password
|
64
|
+
if object_is_class?
|
65
|
+
@object.password
|
66
|
+
else
|
67
|
+
@object.class.password
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def request_body_type
|
72
|
+
if @method[:options][:request_body_type]
|
73
|
+
@method[:options][:request_body_type]
|
74
|
+
elsif object_is_class?
|
49
75
|
@object.request_body_type
|
50
76
|
else
|
51
77
|
@object.class.request_body_type
|
@@ -183,8 +209,10 @@ module ActiveRestClient
|
|
183
209
|
def prepare_request_body(params = nil)
|
184
210
|
if request_body_type == :form_encoded
|
185
211
|
@body ||= (params || @post_params || {}).map {|k,v| "#{k}=#{CGI.escape(v.to_s)}"}.sort * "&"
|
212
|
+
headers["Content-Type"] ||= "application/x-www-form-urlencoded"
|
186
213
|
elsif request_body_type == :json
|
187
214
|
@body ||= (params || @post_params || {}).to_json
|
215
|
+
headers["Content-Type"] ||= "application/json"
|
188
216
|
end
|
189
217
|
end
|
190
218
|
|
@@ -210,6 +238,7 @@ module ActiveRestClient
|
|
210
238
|
else
|
211
239
|
_, @base_url, @url = parts
|
212
240
|
end
|
241
|
+
base_url.gsub!(%r{//(.)}, "//#{username}:#{password}@\\1") if username && !base_url[%r{//[^/]*:[^/]*@}]
|
213
242
|
connection = ActiveRestClient::ConnectionManager.get_connection(@base_url)
|
214
243
|
end
|
215
244
|
else
|
@@ -220,6 +249,7 @@ module ActiveRestClient
|
|
220
249
|
@url = "#{base_url}#{@url}".gsub(@base_url, "")
|
221
250
|
base_url = @base_url
|
222
251
|
end
|
252
|
+
base_url.gsub!(%r{//(.)}, "//#{username}:#{password}@\\1") if username && !base_url[%r{//[^/]*:[^/]*@}]
|
223
253
|
connection = ActiveRestClient::ConnectionManager.get_connection(base_url)
|
224
254
|
end
|
225
255
|
ActiveRestClient::Logger.info " \033[1;4;32m#{ActiveRestClient::NAME}\033[0m #{@instrumentation_name} - Requesting #{connection.base_url}#{@url}"
|
@@ -437,8 +467,11 @@ module ActiveRestClient
|
|
437
467
|
else
|
438
468
|
result = new_object(body, @overriden_name)
|
439
469
|
result._status = @response.status
|
470
|
+
result._headers = @response.headers
|
471
|
+
result._etag = @response.headers['ETag']
|
440
472
|
if !object_is_class? && options[:mutable] != false
|
441
473
|
@object._copy_from(result)
|
474
|
+
@object._clean!
|
442
475
|
result = @object
|
443
476
|
end
|
444
477
|
end
|
@@ -26,6 +26,10 @@ module ActiveRestClient
|
|
26
26
|
size == 0
|
27
27
|
end
|
28
28
|
|
29
|
+
def reverse
|
30
|
+
@reversed_items ||= @items.reverse
|
31
|
+
end
|
32
|
+
|
29
33
|
def each
|
30
34
|
@items.each do |el|
|
31
35
|
yield el
|
@@ -62,5 +66,19 @@ module ActiveRestClient
|
|
62
66
|
collected_responses
|
63
67
|
end
|
64
68
|
|
69
|
+
def paginate(options = {})
|
70
|
+
raise WillPaginateNotAvailableException.new unless Object.constants.include?(:WillPaginate)
|
71
|
+
|
72
|
+
page = options[:page] || 1
|
73
|
+
per_page = options[:per_page] || WillPaginate.per_page
|
74
|
+
total = options[:total_entries] || @items.length
|
75
|
+
|
76
|
+
WillPaginate::Collection.create(page, per_page, total) do |pager|
|
77
|
+
pager.replace @items[pager.offset, pager.per_page].to_a
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
65
81
|
end
|
82
|
+
|
83
|
+
class WillPaginateNotAvailableException < StandardError ; end
|
66
84
|
end
|
data/spec/lib/base_spec.rb
CHANGED
@@ -39,7 +39,7 @@ end
|
|
39
39
|
|
40
40
|
describe ActiveRestClient::Base do
|
41
41
|
it 'should instantiate a new descendant' do
|
42
|
-
expect{EmptyExample.new}.to_not raise_error
|
42
|
+
expect{EmptyExample.new}.to_not raise_error
|
43
43
|
end
|
44
44
|
|
45
45
|
it "should not instantiate a new base class" do
|
@@ -73,13 +73,20 @@ describe ActiveRestClient::Base do
|
|
73
73
|
expect(values).to eq(["Billy", "United Kingdom"])
|
74
74
|
end
|
75
75
|
|
76
|
-
it "should automatically parse ISO 8601 format
|
76
|
+
it "should automatically parse ISO 8601 format date and time" do
|
77
77
|
t = Time.now
|
78
78
|
client = EmptyExample.new(:test => t.iso8601)
|
79
79
|
expect(client["test"]).to be_an_instance_of(DateTime)
|
80
80
|
expect(client["test"].to_s).to eq(t.to_datetime.to_s)
|
81
81
|
end
|
82
82
|
|
83
|
+
it "should automatically parse ISO 8601 format dates" do
|
84
|
+
d = Date.today
|
85
|
+
client = EmptyExample.new(:test => d.iso8601)
|
86
|
+
expect(client["test"]).to be_an_instance_of(Date)
|
87
|
+
expect(client["test"]).to eq(d)
|
88
|
+
end
|
89
|
+
|
83
90
|
it "should store attributes set using missing method names and mark them as dirty" do
|
84
91
|
client = EmptyExample.new()
|
85
92
|
client.test = "Something"
|
@@ -105,8 +112,8 @@ describe ActiveRestClient::Base do
|
|
105
112
|
|
106
113
|
it 'should respond_to? attributes defined in the response' do
|
107
114
|
client = EmptyExample.new(:hello => "World")
|
108
|
-
client.respond_to?(:hello).
|
109
|
-
client.respond_to?(:world).
|
115
|
+
expect(client.respond_to?(:hello)).to be_truthy
|
116
|
+
expect(client.respond_to?(:world)).to be_falsey
|
110
117
|
end
|
111
118
|
|
112
119
|
it "should save the base URL for the API server" do
|
@@ -149,22 +156,63 @@ describe ActiveRestClient::Base do
|
|
149
156
|
end
|
150
157
|
|
151
158
|
it "should be able to lazy instantiate an object from a prefixed lazy_ method call" do
|
152
|
-
ActiveRestClient::Connection.
|
159
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with('/find/1', anything).and_return(OpenStruct.new(status:200, headers:{}, body:"{\"first_name\":\"Billy\"}"))
|
153
160
|
example = AlteringClientExample.lazy_find(1)
|
154
161
|
expect(example).to be_an_instance_of(ActiveRestClient::LazyLoader)
|
155
162
|
expect(example.first_name).to eq("Billy")
|
156
163
|
end
|
157
164
|
|
158
165
|
it "should be able to lazy instantiate an object from a prefixed lazy_ method call from an instance" do
|
159
|
-
ActiveRestClient::Connection.
|
166
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with('/find/1', anything).and_return(OpenStruct.new(status:200, headers:{}, body:"{\"first_name\":\"Billy\"}"))
|
160
167
|
example = AlteringClientExample.new.lazy_find(1)
|
161
168
|
expect(example).to be_an_instance_of(ActiveRestClient::LazyLoader)
|
162
169
|
expect(example.first_name).to eq("Billy")
|
163
170
|
end
|
164
171
|
|
172
|
+
context "#inspect output" do
|
173
|
+
it "displays a nice version" do
|
174
|
+
object = EmptyExample.new(id: 1, name: "John Smith")
|
175
|
+
expect(object.inspect).to match(/#<EmptyExample id: 1, name: "John Smith"/)
|
176
|
+
end
|
177
|
+
|
178
|
+
it "shows dirty attributes as a list of names at the end" do
|
179
|
+
object = EmptyExample.new(id: 1, name: "John Smith")
|
180
|
+
expect(object.inspect).to match(/#<EmptyExample id: 1, name: "John Smith" \(unsaved: id, name\)/)
|
181
|
+
end
|
182
|
+
|
183
|
+
it "doesn't show an empty list of dirty attributes" do
|
184
|
+
object = EmptyExample.new(id: 1, name: "John Smith")
|
185
|
+
object.instance_variable_set(:@dirty_attributes, Set.new)
|
186
|
+
expect(object.inspect).to_not match(/\(unsaved: id, name\)/)
|
187
|
+
end
|
188
|
+
|
189
|
+
it "shows dates in a nice format" do
|
190
|
+
object = EmptyExample.new(dob: Time.new(2015, 01, 02, 03, 04, 05))
|
191
|
+
expect(object.inspect).to match(/#<EmptyExample dob: "2015\-01\-02 03:04:05"/)
|
192
|
+
end
|
193
|
+
|
194
|
+
it "shows the etag if one is set" do
|
195
|
+
object = EmptyExample.new(id: 1)
|
196
|
+
object.instance_variable_set(:@_etag, "sample_etag")
|
197
|
+
expect(object.inspect).to match(/#<EmptyExample id: 1, ETag: sample_etag/)
|
198
|
+
end
|
199
|
+
|
200
|
+
it "shows the HTTP status code if one is set" do
|
201
|
+
object = EmptyExample.new(id: 1)
|
202
|
+
object.instance_variable_set(:@_status, 200)
|
203
|
+
expect(object.inspect).to match(/#<EmptyExample id: 1, Status: 200/)
|
204
|
+
end
|
205
|
+
|
206
|
+
it "shows [uninitialized] for new objects" do
|
207
|
+
object = EmptyExample.new
|
208
|
+
expect(object.inspect).to match(/#<EmptyExample \[uninitialized\]/)
|
209
|
+
end
|
210
|
+
|
211
|
+
end
|
212
|
+
|
165
213
|
context "accepts a Translator to reformat JSON" do
|
166
214
|
it "should log a deprecation warning when using a translator" do
|
167
|
-
ActiveRestClient::Logger.
|
215
|
+
expect(ActiveRestClient::Logger).to receive(:warn) do |message|
|
168
216
|
expect(message).to start_with("DEPRECATION")
|
169
217
|
end
|
170
218
|
Proc.new do
|
@@ -175,7 +223,7 @@ describe ActiveRestClient::Base do
|
|
175
223
|
end
|
176
224
|
|
177
225
|
it "should call Translator#method when calling the mapped method if it responds to it" do
|
178
|
-
TranslatorExample.
|
226
|
+
expect(TranslatorExample).to receive(:all).with(an_instance_of(Hash)).and_return({})
|
179
227
|
AlteringClientExample.all
|
180
228
|
end
|
181
229
|
|
@@ -198,35 +246,35 @@ describe ActiveRestClient::Base do
|
|
198
246
|
|
199
247
|
context "directly call a URL, rather than via a mapped method" do
|
200
248
|
it "should be able to directly call a URL" do
|
201
|
-
ActiveRestClient::Request.
|
249
|
+
expect_any_instance_of(ActiveRestClient::Request).to receive(:do_request).with(any_args).and_return(OpenStruct.new(status:200, headers:{}, body:"{\"first_name\":\"Billy\"}"))
|
202
250
|
EmptyExample._request("http://api.example.com/")
|
203
251
|
end
|
204
252
|
|
205
253
|
it "runs filters as usual" do
|
206
|
-
ActiveRestClient::Request.
|
207
|
-
EmptyExample.
|
254
|
+
expect_any_instance_of(ActiveRestClient::Request).to receive(:do_request).with(any_args).and_return(OpenStruct.new(status:200, headers:{}, body:"{\"first_name\":\"Billy\"}"))
|
255
|
+
expect(EmptyExample).to receive(:_filter_request).with(any_args).exactly(2).times
|
208
256
|
EmptyExample._request("http://api.example.com/")
|
209
257
|
end
|
210
258
|
|
211
259
|
it "should make an HTTP request" do
|
212
|
-
ActiveRestClient::Connection.
|
260
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with(any_args).and_return(OpenStruct.new(status:200, headers:{}, body:"{\"first_name\":\"Billy\"}"))
|
213
261
|
EmptyExample._request("http://api.example.com/")
|
214
262
|
end
|
215
263
|
|
216
264
|
it "should make an HTTP request including the path in the base_url" do
|
217
|
-
ActiveRestClient::Connection.
|
265
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with('/v1/all', anything).and_return(OpenStruct.new(status:200, headers:{}, body:"{\"first_name\":\"Billy\"}"))
|
218
266
|
NonHostnameBaseUrlExample.all
|
219
267
|
end
|
220
268
|
|
221
269
|
it "should map the response from the directly called URL in the normal way" do
|
222
|
-
ActiveRestClient::Request.
|
270
|
+
expect_any_instance_of(ActiveRestClient::Request).to receive(:do_request).with(any_args).and_return(OpenStruct.new(status:200, headers:{}, body:"{\"first_name\":\"Billy\"}"))
|
223
271
|
example = EmptyExample._request("http://api.example.com/")
|
224
272
|
expect(example.first_name).to eq("Billy")
|
225
273
|
end
|
226
274
|
|
227
275
|
it "should be able to pass the plain response from the directly called URL bypassing JSON loading" do
|
228
276
|
response = OpenStruct.new(_status:200, body:"This is another non-JSON string")
|
229
|
-
ActiveRestClient::Connection.
|
277
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:post).with(any_args).and_return(OpenStruct.new(status:200, headers:{}, body:response))
|
230
278
|
expect(EmptyExample._plain_request("http://api.example.com/", :post, {id:1234})).to eq(response)
|
231
279
|
end
|
232
280
|
|
@@ -236,7 +284,7 @@ describe ActiveRestClient::Base do
|
|
236
284
|
begin
|
237
285
|
response = OpenStruct.new(_status:200, body:"This is a non-JSON string")
|
238
286
|
other_response = OpenStruct.new(_status:200, body:"This is another non-JSON string")
|
239
|
-
allow_any_instance_of(ActiveRestClient::Connection).to receive(:get) do |url, others|
|
287
|
+
allow_any_instance_of(ActiveRestClient::Connection).to receive(:get) do |instance, url, others|
|
240
288
|
if url == "/?test=1"
|
241
289
|
OpenStruct.new(status:200, headers:{}, body:response)
|
242
290
|
else
|
@@ -254,19 +302,19 @@ describe ActiveRestClient::Base do
|
|
254
302
|
end
|
255
303
|
|
256
304
|
it "should be able to lazy load a direct URL request" do
|
257
|
-
ActiveRestClient::Request.
|
305
|
+
expect_any_instance_of(ActiveRestClient::Request).to receive(:do_request).with(any_args).and_return(OpenStruct.new(status:200, headers:{}, body:"{\"first_name\":\"Billy\"}"))
|
258
306
|
example = EmptyExample._lazy_request("http://api.example.com/")
|
259
307
|
expect(example).to be_an_instance_of(ActiveRestClient::LazyLoader)
|
260
308
|
expect(example.first_name).to eq("Billy")
|
261
309
|
end
|
262
310
|
|
263
311
|
it "should be able to specify a method and parameters for the call" do
|
264
|
-
ActiveRestClient::Connection.
|
312
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:post).with(any_args).and_return(OpenStruct.new(status:200, headers:{}, body:"{\"first_name\":\"Billy\"}"))
|
265
313
|
EmptyExample._request("http://api.example.com/", :post, {id:1234})
|
266
314
|
end
|
267
315
|
|
268
316
|
it "should be able to use mapped methods to create a request to pass in to _lazy_request" do
|
269
|
-
ActiveRestClient::Connection.
|
317
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with('/find/1', anything).and_return(OpenStruct.new(status:200, headers:{}, body:"{\"first_name\":\"Billy\"}"))
|
270
318
|
request = AlteringClientExample._request_for(:find, :id => 1)
|
271
319
|
example = AlteringClientExample._lazy_request(request)
|
272
320
|
expect(example.first_name).to eq("Billy")
|
@@ -275,7 +323,7 @@ describe ActiveRestClient::Base do
|
|
275
323
|
|
276
324
|
context "Recording a response" do
|
277
325
|
it "calls back to the record_response callback with the url and response body" do
|
278
|
-
ActiveRestClient::Connection.
|
326
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with(any_args).and_return(OpenStruct.new(status:200, headers:{}, body:"Hello world"))
|
279
327
|
expect{RecordResponseExample.all}.to raise_error(Exception, "/all|Hello world")
|
280
328
|
end
|
281
329
|
end
|