active_rest_client 1.0.7 → 1.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +34 -3
- data/active_rest_client.gemspec +1 -0
- data/lib/active_rest_client/base.rb +1 -0
- data/lib/active_rest_client/configuration.rb +35 -8
- data/lib/active_rest_client/connection.rb +12 -1
- data/lib/active_rest_client/logger.rb +4 -4
- data/lib/active_rest_client/request.rb +26 -14
- data/lib/active_rest_client/version.rb +1 -1
- data/spec/lib/configuration_spec.rb +26 -0
- data/spec/lib/connection_spec.rb +31 -0
- data/spec/lib/logger_spec.rb +4 -4
- data/spec/lib/request_spec.rb +20 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 323d3696fb921ff5d23de556ec82ca0ee07fadea
|
4
|
+
data.tar.gz: 852a377147513500e2a0d1fdc92bbdf38d30c76d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 911ece68f67242d67f06e279f13398624ed35a0828dd92e2f960b8ceb9ed1663b6790f8f25899e2b9224ff7fb2182fe7faf4d68eb4b64cc7451a1075de655afb
|
7
|
+
data.tar.gz: f62c9f442fe875811da762bcb6b87a3a7d52ccd3cd161b8926a1397b201f0fdf0112eade77ae1b6bcd8d8fca8bb5b570b05fc5e2554a87dc155798ffc4ea0398
|
data/README.md
CHANGED
@@ -146,7 +146,7 @@ There are two types of association. One assumes when you call a method you actu
|
|
146
146
|
|
147
147
|
#### Association Type 1 - Loading Other Classes
|
148
148
|
|
149
|
-
If the call would return a list of instances that should be considered another object, you can also specify this when mapping the method using the `:has_many`
|
149
|
+
If the call would return a single instance or a list of instances that should be considered another object, you can also specify this when mapping the method using the `:has_one` or `:has_many` options respectively. It doesn't call anything on that object except for instantiate it, but it does let you have objects of a different class to the one you initially called.
|
150
150
|
|
151
151
|
```ruby
|
152
152
|
class Expense < ActiveRestClient::Base
|
@@ -155,12 +155,19 @@ class Expense < ActiveRestClient::Base
|
|
155
155
|
end
|
156
156
|
end
|
157
157
|
|
158
|
+
class Address < ActiveRestClient::Base
|
159
|
+
def full_string
|
160
|
+
return "#{self.street}, #{self.city}, #{self.region}, #{self.country}"
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
158
164
|
class Person < ActiveRestClient::Base
|
159
|
-
get :find, "/people/:id", :has_many => {:expenses => Expense}
|
165
|
+
get :find, "/people/:id", :has_many => {:expenses => Expense}, :has_one => {:address => Address}
|
160
166
|
end
|
161
167
|
|
162
168
|
@person = Person.find(1)
|
163
169
|
puts @person.expenses.reduce {|e| e.inc_vat}
|
170
|
+
puts @person.address.full_string
|
164
171
|
```
|
165
172
|
|
166
173
|
#### Association Type 2 - Lazy Loading From Other URLs
|
@@ -345,6 +352,8 @@ class Person < ActiveRestClient::Base
|
|
345
352
|
|
346
353
|
before_request :replace_body
|
347
354
|
|
355
|
+
before_request :override_default_content_type
|
356
|
+
|
348
357
|
private
|
349
358
|
|
350
359
|
def replace_token_in_url(name, request)
|
@@ -360,6 +369,12 @@ class Person < ActiveRestClient::Base
|
|
360
369
|
request.body = request.post_params.to_json
|
361
370
|
end
|
362
371
|
end
|
372
|
+
|
373
|
+
def override_default_content_type(name, request)
|
374
|
+
if name == :save
|
375
|
+
request.headers["Content-Type"] = "application/json"
|
376
|
+
end
|
377
|
+
end
|
363
378
|
end
|
364
379
|
```
|
365
380
|
|
@@ -424,6 +439,8 @@ This will return an array of the named method for each object or the response fr
|
|
424
439
|
|
425
440
|
### Authentication
|
426
441
|
|
442
|
+
#### Basic
|
443
|
+
|
427
444
|
You can authenticate with Basic authentication by putting the username and password in to the `base_url` or by setting them within the specific model:
|
428
445
|
|
429
446
|
```ruby
|
@@ -435,6 +452,20 @@ class Person < ActiveRestClient::Base
|
|
435
452
|
end
|
436
453
|
```
|
437
454
|
|
455
|
+
#### Api-Auth
|
456
|
+
|
457
|
+
Using the [Api-Auth](https://github.com/mgomes/api_auth) integration it is very easy to sign requests. Include the Api-Auth gem in your Gemfile and in then add it to your application. Then simply configure Api-Auth one time in your app and all requests will be signed from then on.
|
458
|
+
|
459
|
+
```ruby
|
460
|
+
require 'api-auth'
|
461
|
+
|
462
|
+
@access_id = '123456'
|
463
|
+
@secret_key = 'abcdef'
|
464
|
+
ActiveRestClient::Base.api_auth_credentials(@access_id, @secret_key)
|
465
|
+
```
|
466
|
+
|
467
|
+
For more information on how to generate an access id and secret key please read the [Api-Auth](https://github.com/mgomes/api_auth) documentation.
|
468
|
+
|
438
469
|
### Body Types
|
439
470
|
|
440
471
|
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`):
|
@@ -452,7 +483,7 @@ or
|
|
452
483
|
ActiveRestClient::Base.request_body_type = :json
|
453
484
|
```
|
454
485
|
|
455
|
-
This will also set the header `Content-Type` to `application/x-www-form-urlencoded` by default or `application/json` when `:json`.
|
486
|
+
This will also set the header `Content-Type` to `application/x-www-form-urlencoded` by default or `application/json; charset=utf-8` when `:json`. You can override this using the filter `before_request`.
|
456
487
|
|
457
488
|
If you have an API that is inconsistent in its body type requirements, you can also specify it on the individual method mapping:
|
458
489
|
|
data/active_rest_client.gemspec
CHANGED
@@ -29,6 +29,7 @@ Gem::Specification.new do |spec|
|
|
29
29
|
spec.add_development_dependency "guard-rspec"
|
30
30
|
spec.add_development_dependency 'terminal-notifier-guard'
|
31
31
|
spec.add_development_dependency 'coveralls'
|
32
|
+
spec.add_development_dependency "api-auth", ">= 1.3.1"
|
32
33
|
|
33
34
|
spec.add_runtime_dependency "multi_json"
|
34
35
|
spec.add_runtime_dependency "activesupport"
|
@@ -6,6 +6,8 @@ module ActiveRestClient
|
|
6
6
|
@@password = nil
|
7
7
|
@@request_body_type = :form_encoded
|
8
8
|
@lazy_load = false
|
9
|
+
@@api_auth_access_id = nil
|
10
|
+
@@api_auth_secret_key = nil
|
9
11
|
|
10
12
|
def base_url(value = nil)
|
11
13
|
if value.nil?
|
@@ -106,6 +108,29 @@ module ActiveRestClient
|
|
106
108
|
value ? @whiny_missing = value : @whiny_missing || false
|
107
109
|
end
|
108
110
|
|
111
|
+
def api_auth_credentials(access_id, secret_key)
|
112
|
+
begin
|
113
|
+
require 'api-auth'
|
114
|
+
rescue LoadError
|
115
|
+
raise MissingOptionalLibraryError.new("You must include the gem 'api-auth' in your Gemfile to set api-auth credentials.")
|
116
|
+
end
|
117
|
+
|
118
|
+
@@api_auth_access_id = access_id
|
119
|
+
@@api_auth_secret_key = secret_key
|
120
|
+
end
|
121
|
+
|
122
|
+
def using_api_auth?
|
123
|
+
!@@api_auth_access_id.nil? && !@@api_auth_secret_key.nil?
|
124
|
+
end
|
125
|
+
|
126
|
+
def api_auth_access_id
|
127
|
+
@@api_auth_access_id
|
128
|
+
end
|
129
|
+
|
130
|
+
def api_auth_secret_key
|
131
|
+
@@api_auth_secret_key
|
132
|
+
end
|
133
|
+
|
109
134
|
def verbose!
|
110
135
|
@verbose = true
|
111
136
|
end
|
@@ -124,14 +149,16 @@ module ActiveRestClient
|
|
124
149
|
end
|
125
150
|
|
126
151
|
def _reset_configuration!
|
127
|
-
@base_url
|
128
|
-
@@base_url
|
129
|
-
@request_body_type
|
130
|
-
@@request_body_type
|
131
|
-
@whiny_missing
|
132
|
-
@lazy_load
|
133
|
-
@faraday_config
|
134
|
-
@adapter
|
152
|
+
@base_url = nil
|
153
|
+
@@base_url = nil
|
154
|
+
@request_body_type = nil
|
155
|
+
@@request_body_type = :form_encoded
|
156
|
+
@whiny_missing = nil
|
157
|
+
@lazy_load = false
|
158
|
+
@faraday_config = default_faraday_config
|
159
|
+
@adapter = :patron
|
160
|
+
@@api_auth_access_id = nil
|
161
|
+
@@api_auth_secret_key = nil
|
135
162
|
end
|
136
163
|
|
137
164
|
private
|
@@ -38,6 +38,7 @@ module ActiveRestClient
|
|
38
38
|
make_safe_request(path) do
|
39
39
|
@session.get(path) do |req|
|
40
40
|
req.headers = req.headers.merge(headers)
|
41
|
+
sign_request(req)
|
41
42
|
end
|
42
43
|
end
|
43
44
|
end
|
@@ -47,6 +48,7 @@ module ActiveRestClient
|
|
47
48
|
@session.put(path) do |req|
|
48
49
|
req.headers = req.headers.merge(headers)
|
49
50
|
req.body = data
|
51
|
+
sign_request(req)
|
50
52
|
end
|
51
53
|
end
|
52
54
|
end
|
@@ -56,6 +58,7 @@ module ActiveRestClient
|
|
56
58
|
@session.post(path) do |req|
|
57
59
|
req.headers = req.headers.merge(headers)
|
58
60
|
req.body = data
|
61
|
+
sign_request(req)
|
59
62
|
end
|
60
63
|
end
|
61
64
|
end
|
@@ -64,6 +67,7 @@ module ActiveRestClient
|
|
64
67
|
make_safe_request(path) do
|
65
68
|
@session.delete(path) do |req|
|
66
69
|
req.headers = req.headers.merge(headers)
|
70
|
+
sign_request(req)
|
67
71
|
end
|
68
72
|
end
|
69
73
|
end
|
@@ -74,9 +78,16 @@ module ActiveRestClient
|
|
74
78
|
Faraday.new({url: @base_url}, &ActiveRestClient::Base.faraday_config)
|
75
79
|
end
|
76
80
|
|
77
|
-
|
78
81
|
def full_url(path)
|
79
82
|
@session.build_url(path).to_s
|
80
83
|
end
|
84
|
+
|
85
|
+
def sign_request(request)
|
86
|
+
return if !ActiveRestClient::Base.using_api_auth?
|
87
|
+
ApiAuth.sign!(
|
88
|
+
request,
|
89
|
+
ActiveRestClient::Base.api_auth_access_id,
|
90
|
+
ActiveRestClient::Base.api_auth_secret_key)
|
91
|
+
end
|
81
92
|
end
|
82
93
|
end
|
@@ -21,7 +21,7 @@ module ActiveRestClient
|
|
21
21
|
Rails.logger.debug(message)
|
22
22
|
elsif @logfile
|
23
23
|
File.open(@logfile, "a") do |f|
|
24
|
-
f << message
|
24
|
+
f << "#{message}\n"
|
25
25
|
end
|
26
26
|
else
|
27
27
|
@messages << message
|
@@ -33,7 +33,7 @@ module ActiveRestClient
|
|
33
33
|
Rails.logger.info(message)
|
34
34
|
elsif @logfile
|
35
35
|
File.open(@logfile, "a") do |f|
|
36
|
-
f << message
|
36
|
+
f << "#{message}\n"
|
37
37
|
end
|
38
38
|
else
|
39
39
|
@messages << message
|
@@ -45,7 +45,7 @@ module ActiveRestClient
|
|
45
45
|
Rails.logger.warn(message)
|
46
46
|
elsif @logfile
|
47
47
|
File.open(@logfile, "a") do |f|
|
48
|
-
f << message
|
48
|
+
f << "#{message}\n"
|
49
49
|
end
|
50
50
|
else
|
51
51
|
@messages << message
|
@@ -57,7 +57,7 @@ module ActiveRestClient
|
|
57
57
|
Rails.logger.error(message)
|
58
58
|
elsif @logfile
|
59
59
|
File.open(@logfile, "a") do |f|
|
60
|
-
f << message
|
60
|
+
f << "#{message}\n"
|
61
61
|
end
|
62
62
|
else
|
63
63
|
@messages << message
|
@@ -7,13 +7,14 @@ module ActiveRestClient
|
|
7
7
|
attr_accessor :post_params, :get_params, :url, :path, :headers, :method, :object, :body, :forced_url, :original_url
|
8
8
|
|
9
9
|
def initialize(method, object, params = {})
|
10
|
-
@method
|
11
|
-
@method[:options]
|
12
|
-
@method[:options][:lazy]
|
13
|
-
@
|
14
|
-
@
|
15
|
-
@
|
16
|
-
@
|
10
|
+
@method = method
|
11
|
+
@method[:options] ||= {}
|
12
|
+
@method[:options][:lazy] ||= []
|
13
|
+
@method[:options][:has_one] ||= {}
|
14
|
+
@overriden_name = @method[:options][:overriden_name]
|
15
|
+
@object = object
|
16
|
+
@params = params
|
17
|
+
@headers = HeadersList.new
|
17
18
|
end
|
18
19
|
|
19
20
|
def object_is_class?
|
@@ -201,18 +202,17 @@ module ActiveRestClient
|
|
201
202
|
|
202
203
|
def append_get_parameters
|
203
204
|
if @get_params.any?
|
204
|
-
|
205
|
-
@url += "?" + params.sort * "&"
|
205
|
+
@url += "?" + @get_params.to_query
|
206
206
|
end
|
207
207
|
end
|
208
208
|
|
209
209
|
def prepare_request_body(params = nil)
|
210
210
|
if request_body_type == :form_encoded
|
211
|
-
@body ||= (params || @post_params || {}).
|
211
|
+
@body ||= (params || @post_params || {}).to_query
|
212
212
|
headers["Content-Type"] ||= "application/x-www-form-urlencoded"
|
213
213
|
elsif request_body_type == :json
|
214
214
|
@body ||= (params || @post_params || {}).to_json
|
215
|
-
headers["Content-Type"] ||= "application/json"
|
215
|
+
headers["Content-Type"] ||= "application/json; charset=utf-8"
|
216
216
|
end
|
217
217
|
end
|
218
218
|
|
@@ -355,6 +355,9 @@ module ActiveRestClient
|
|
355
355
|
if @method[:options][:has_many][name]
|
356
356
|
overriden_name = name
|
357
357
|
object = @method[:options][:has_many][name].new
|
358
|
+
elsif @method[:options][:has_one][name]
|
359
|
+
overriden_name = name
|
360
|
+
object = @method[:options][:has_one][name].new
|
358
361
|
else
|
359
362
|
if object_is_class?
|
360
363
|
object = @object.new
|
@@ -369,15 +372,16 @@ module ActiveRestClient
|
|
369
372
|
|
370
373
|
attributes.each do |k,v|
|
371
374
|
k = k.to_sym
|
375
|
+
overriden_name = select_name(k, overriden_name)
|
372
376
|
if @method[:options][:lazy].include?(k)
|
373
|
-
object._attributes[k] = ActiveRestClient::LazyAssociationLoader.new(overriden_name
|
377
|
+
object._attributes[k] = ActiveRestClient::LazyAssociationLoader.new(overriden_name, v, self, overriden_name:(overriden_name))
|
374
378
|
elsif v.is_a? Hash
|
375
|
-
object._attributes[k] = new_object(v, overriden_name
|
379
|
+
object._attributes[k] = new_object(v, overriden_name )
|
376
380
|
elsif v.is_a? Array
|
377
381
|
object._attributes[k] = ActiveRestClient::ResultIterator.new
|
378
382
|
v.each do |item|
|
379
383
|
if item.is_a? Hash
|
380
|
-
object._attributes[k] << new_object(item, overriden_name
|
384
|
+
object._attributes[k] << new_object(item, overriden_name)
|
381
385
|
else
|
382
386
|
object._attributes[k] << item
|
383
387
|
end
|
@@ -444,6 +448,14 @@ module ActiveRestClient
|
|
444
448
|
|
445
449
|
private
|
446
450
|
|
451
|
+
def select_name(name, parent_name)
|
452
|
+
if @method[:options][:has_many][name] || @method[:options][:has_one][name]
|
453
|
+
return name
|
454
|
+
end
|
455
|
+
|
456
|
+
parent_name || name
|
457
|
+
end
|
458
|
+
|
447
459
|
def is_json_response?
|
448
460
|
@response.headers['Content-Type'].nil? || @response.headers['Content-Type'].include?('json')
|
449
461
|
end
|
@@ -108,6 +108,32 @@ describe ActiveRestClient::Configuration do
|
|
108
108
|
expect(LazyLoadingConfigurationExample2.lazy_load?).to be_truthy
|
109
109
|
end
|
110
110
|
|
111
|
+
describe 'api auth' do
|
112
|
+
context 'default' do
|
113
|
+
it "should be false using_api_auth?" do
|
114
|
+
expect(ActiveRestClient::Base.using_api_auth?).to be_falsey
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
context 'setting api auth credentials' do
|
119
|
+
before(:each) do
|
120
|
+
ConfigurationExample.api_auth_credentials('id123', 'secret123')
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should remember setting using_api_auth?" do
|
124
|
+
expect(ConfigurationExample.using_api_auth?).to be_truthy
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should remember setting api_auth_access_id" do
|
128
|
+
expect(ConfigurationExample.api_auth_access_id).to eq('id123')
|
129
|
+
end
|
130
|
+
|
131
|
+
it "should remember setting api_auth_secret_key" do
|
132
|
+
expect(ConfigurationExample.api_auth_secret_key).to eq('secret123')
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
111
137
|
it "should default to non-verbose loggingg" do
|
112
138
|
class VerboseConfigurationExample1
|
113
139
|
include ActiveRestClient::Configuration
|
data/spec/lib/connection_spec.rb
CHANGED
@@ -98,6 +98,37 @@ describe ActiveRestClient::Connection do
|
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
|
+
context 'with api auth signing requests' do
|
102
|
+
before(:each) do
|
103
|
+
ActiveRestClient::Base.api_auth_credentials('id123', 'secret123')
|
104
|
+
|
105
|
+
@default_headers = {'Date' => 'Sat, 14 Mar 2015 15:13:24 GMT'}
|
106
|
+
|
107
|
+
ActiveRestClient::Base.faraday_config do |faraday|
|
108
|
+
faraday.adapter ActiveRestClient::Base.adapter
|
109
|
+
faraday.headers.update(@default_headers)
|
110
|
+
end
|
111
|
+
@connection.reconnect
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'should have an Authorization header' do
|
115
|
+
stub_request(:get, "www.example.com/foo")
|
116
|
+
.with(:headers => @default_headers)
|
117
|
+
.to_return(body: "{result:true}")
|
118
|
+
result = @connection.get("/foo")
|
119
|
+
expect(result.env.request_headers['Authorization']).to eq("APIAuth id123:PMWBThkB8vKbvUccHvoqu9G3eVk=")
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'should have an Content-MD5 header' do
|
123
|
+
stub_request(:put, "www.example.com/foo").
|
124
|
+
with(body: "body", :headers => @default_headers).
|
125
|
+
to_return(body: "{result:true}")
|
126
|
+
|
127
|
+
result = @connection.put("/foo", "body")
|
128
|
+
expect(result.env.request_headers['Content-MD5']).to eq("hBotaJrYa9FhFEdFPCLG/A==")
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
101
132
|
it "should retry once in the event of a connection failed" do
|
102
133
|
stub_request(:get, "www.example.com/foo").to_raise(Faraday::Error::ConnectionFailed.new("Foo"))
|
103
134
|
expect { @connection.get("/foo") }.to raise_error(ActiveRestClient::ConnectionFailedException)
|
data/spec/lib/logger_spec.rb
CHANGED
@@ -28,22 +28,22 @@ describe ActiveRestClient::Instrumentation do
|
|
28
28
|
ActiveRestClient::Logger.logfile = "/dev/null"
|
29
29
|
file = double('file')
|
30
30
|
expect(File).to receive(:open).with("/dev/null", "a").and_yield(file)
|
31
|
-
expect(file).to receive(:<<).with("Hello world")
|
31
|
+
expect(file).to receive(:<<).with("Hello world\n")
|
32
32
|
ActiveRestClient::Logger.debug("Hello world")
|
33
33
|
|
34
34
|
file = double('file')
|
35
35
|
expect(File).to receive(:open).with("/dev/null", "a").and_yield(file)
|
36
|
-
expect(file).to receive(:<<).with("Hello info")
|
36
|
+
expect(file).to receive(:<<).with("Hello info\n")
|
37
37
|
ActiveRestClient::Logger.info("Hello info")
|
38
38
|
|
39
39
|
file = double('file')
|
40
40
|
expect(File).to receive(:open).with("/dev/null", "a").and_yield(file)
|
41
|
-
expect(file).to receive(:<<).with("Hello error")
|
41
|
+
expect(file).to receive(:<<).with("Hello error\n")
|
42
42
|
ActiveRestClient::Logger.error("Hello error")
|
43
43
|
|
44
44
|
file = double('file')
|
45
45
|
expect(File).to receive(:open).with("/dev/null", "a").and_yield(file)
|
46
|
-
expect(file).to receive(:<<).with("Hello warn")
|
46
|
+
expect(file).to receive(:<<).with("Hello warn\n")
|
47
47
|
ActiveRestClient::Logger.warn("Hello warn")
|
48
48
|
end
|
49
49
|
|
data/spec/lib/request_spec.rb
CHANGED
@@ -3,6 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe ActiveRestClient::Request do
|
4
4
|
before :each do
|
5
5
|
class ExampleOtherClient < ActiveRestClient::Base ; end
|
6
|
+
class ExampleSingleClient < ActiveRestClient::Base ; end
|
6
7
|
class ExampleClient < ActiveRestClient::Base
|
7
8
|
base_url "http://www.example.com"
|
8
9
|
request_body_type :form_encoded
|
@@ -21,6 +22,7 @@ describe ActiveRestClient::Request do
|
|
21
22
|
|
22
23
|
get :all, "/", :has_many => {:expenses => ExampleOtherClient}
|
23
24
|
get :babies, "/babies", :has_many => {:children => ExampleOtherClient}
|
25
|
+
get :single_association, "/single", :has_one => {:single => ExampleSingleClient}, :has_many => {:children => ExampleOtherClient}
|
24
26
|
get :headers, "/headers"
|
25
27
|
put :headers_default, "/headers_default"
|
26
28
|
put :headers_json, "/headers_json", request_body_type: :json
|
@@ -135,6 +137,11 @@ describe ActiveRestClient::Request do
|
|
135
137
|
ExampleClient.update id:1234, debug:true
|
136
138
|
end
|
137
139
|
|
140
|
+
it "should pass through 'array type' get parameters" do
|
141
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/?include%5B%5D=your&include%5B%5D=friends", an_instance_of(Hash)).and_return(OpenStruct.new(body:"{\"result\":true}", headers:{}))
|
142
|
+
ExampleClient.all :include => [:your,:friends]
|
143
|
+
end
|
144
|
+
|
138
145
|
it "should encode the body in a form-encoded format by default" do
|
139
146
|
expect_any_instance_of(ActiveRestClient::Connection).to receive(:put).with("/put/1234", "debug=true&test=foo", an_instance_of(Hash)).and_return(OpenStruct.new(body:"{\"result\":true}", headers:{}))
|
140
147
|
ExampleClient.update id:1234, debug:true, test:'foo'
|
@@ -163,7 +170,7 @@ describe ActiveRestClient::Request do
|
|
163
170
|
end
|
164
171
|
|
165
172
|
it "should set request header with content-type for JSON" do
|
166
|
-
expect_any_instance_of(ActiveRestClient::Connection).to receive(:put).with("/headers_json", "{}", hash_including("Content-Type" => "application/json")).and_return(OpenStruct.new(body:'{"result":true}', headers:{}))
|
173
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:put).with("/headers_json", "{}", hash_including("Content-Type" => "application/json; charset=utf-8")).and_return(OpenStruct.new(body:'{"result":true}', headers:{}))
|
167
174
|
ExampleClient.headers_json
|
168
175
|
end
|
169
176
|
|
@@ -236,6 +243,18 @@ describe ActiveRestClient::Request do
|
|
236
243
|
expect(object.children.eldest.first).to be_instance_of(ExampleOtherClient)
|
237
244
|
end
|
238
245
|
|
246
|
+
it "should instantiate other classes using has_one when required to do so" do
|
247
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/single", an_instance_of(Hash)).and_return(OpenStruct.new(body:"{\"first_name\":\"Johnny\", \"single\":{\"name\":\"Billy\"}}", status:200, headers:{}))
|
248
|
+
object = ExampleClient.single_association
|
249
|
+
expect(object.single).to be_instance_of(ExampleSingleClient)
|
250
|
+
end
|
251
|
+
|
252
|
+
it "should instantiate other classes using has_one even if nested off the root" do
|
253
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/single", an_instance_of(Hash)).and_return(OpenStruct.new(body:"{\"first_name\":\"Johnny\", \"children\":[{\"single\":{\"name\":\"Billy\"}}, {\"single\":{\"name\":\"Sharon\"}}]}", status:200, headers:{}))
|
254
|
+
object = ExampleClient.single_association
|
255
|
+
expect(object.children.first.single).to be_instance_of(ExampleSingleClient)
|
256
|
+
end
|
257
|
+
|
239
258
|
it "should assign new attributes to the existing object if possible" do
|
240
259
|
expect_any_instance_of(ActiveRestClient::Connection).
|
241
260
|
to receive(:post).
|
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: 1.0.
|
4
|
+
version: 1.0.8
|
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: 2015-
|
12
|
+
date: 2015-04-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -151,6 +151,20 @@ dependencies:
|
|
151
151
|
- - ">="
|
152
152
|
- !ruby/object:Gem::Version
|
153
153
|
version: '0'
|
154
|
+
- !ruby/object:Gem::Dependency
|
155
|
+
name: api-auth
|
156
|
+
requirement: !ruby/object:Gem::Requirement
|
157
|
+
requirements:
|
158
|
+
- - ">="
|
159
|
+
- !ruby/object:Gem::Version
|
160
|
+
version: 1.3.1
|
161
|
+
type: :development
|
162
|
+
prerelease: false
|
163
|
+
version_requirements: !ruby/object:Gem::Requirement
|
164
|
+
requirements:
|
165
|
+
- - ">="
|
166
|
+
- !ruby/object:Gem::Version
|
167
|
+
version: 1.3.1
|
154
168
|
- !ruby/object:Gem::Dependency
|
155
169
|
name: multi_json
|
156
170
|
requirement: !ruby/object:Gem::Requirement
|