restful_resource 1.3.0 → 1.4.0
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/lib/restful_resource/base.rb +2 -2
- data/lib/restful_resource/rails_validations.rb +14 -1
- data/lib/restful_resource/version.rb +1 -1
- data/spec/restful_resource/base_spec.rb +1 -2
- data/spec/restful_resource/rails_validations_spec.rb +48 -0
- data/spec/spec_helper.rb +7 -0
- 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: 48aee54274e098eadf07461bb8c73efda0b9553b
|
4
|
+
data.tar.gz: 9d83d3d0a16de24eaf675521e6400ac781d54699
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1fd7d3aa052150f89935e0ac1465b91041ce26144e0632cb51b523cf20d8d528a898987c251997a5afb8b3c2229bbb157ec4ce5dc003206ac98fbb2fef16411a
|
7
|
+
data.tar.gz: f9fc76246b16b16d927320b6ce1980fd71ce7b04ebba6736dd9f2f997f69f9b27d3569b5f6d56c9a2b0cf554b553f7b13ab7b1580e7619d7b397623a90d141f7
|
@@ -31,9 +31,9 @@ module RestfulResource
|
|
31
31
|
self.paginate_response(response)
|
32
32
|
end
|
33
33
|
|
34
|
-
def self.get(params={})
|
34
|
+
def self.get(params = {})
|
35
35
|
response = http.get(collection_url(params))
|
36
|
-
|
36
|
+
self.new(parse_json(response.body))
|
37
37
|
end
|
38
38
|
|
39
39
|
def self.delete(id, **params)
|
@@ -18,8 +18,21 @@ module RestfulResource
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def post(data: {}, **params)
|
21
|
-
|
21
|
+
with_validations(data: data) do
|
22
22
|
super(data: data, **params)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def get(params = {})
|
27
|
+
with_validations do
|
28
|
+
super(params)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
def with_validations(data: {}, &block)
|
34
|
+
begin
|
35
|
+
block.call
|
23
36
|
rescue HttpClient::UnprocessableEntity => e
|
24
37
|
errors = parse_json(e.response.body)
|
25
38
|
result = nil
|
@@ -123,13 +123,12 @@ describe RestfulResource::Base do
|
|
123
123
|
|
124
124
|
describe "#get" do
|
125
125
|
it "should return an open_object" do
|
126
|
-
expected_response = RestfulResource::Response.new(body: {average_score: 4.3}.to_json)
|
126
|
+
expected_response = RestfulResource::Response.new(body: {average_score: 4.3}.to_json, status: 200)
|
127
127
|
expect_get('http://api.carwow.co.uk/makes/average_score?make_slug%5B%5D=Volkswagen&make_slug%5B%5D=Audi', expected_response)
|
128
128
|
|
129
129
|
object = Make.action(:average_score).get(make_slug: ['Volkswagen', 'Audi'])
|
130
130
|
|
131
131
|
expect(object.average_score).to eq 4.3
|
132
|
-
expect(object.class).to eq RestfulResource::OpenObject
|
133
132
|
end
|
134
133
|
end
|
135
134
|
|
@@ -126,4 +126,52 @@ describe RestfulResource::RailsValidations do
|
|
126
126
|
expect(@object.errors).to eq @error
|
127
127
|
end
|
128
128
|
end
|
129
|
+
|
130
|
+
context "#get without errors" do
|
131
|
+
before :each do
|
132
|
+
expected_response = RestfulResource::Response.new(body: {name: 'Barak'}.to_json)
|
133
|
+
expect_get("http://api.carwow.co.uk/dealers", expected_response)
|
134
|
+
|
135
|
+
@object = Dealer.get
|
136
|
+
end
|
137
|
+
|
138
|
+
it 'should return object' do
|
139
|
+
expect(@object.name).to eq 'Barak'
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'should return valid object' do
|
143
|
+
expect(@object.valid?).to be_truthy
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
context "#get with errors" do
|
148
|
+
before :each do
|
149
|
+
@error = 'Missing parameter'
|
150
|
+
expected_response = RestfulResource::Response.new(body: {errors: [@error]}.to_json)
|
151
|
+
expect_get_with_unprocessable_entity("http://api.carwow.co.uk/dealers", expected_response)
|
152
|
+
|
153
|
+
@object = Dealer.get
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should have an error" do
|
157
|
+
expect(@object.errors.count).to eq 1
|
158
|
+
end
|
159
|
+
|
160
|
+
it 'should have correct error' do
|
161
|
+
expect(@object.errors.first).to eq @error
|
162
|
+
end
|
163
|
+
|
164
|
+
it 'should return not valid object' do
|
165
|
+
expect(@object.valid?).to be_falsey
|
166
|
+
end
|
167
|
+
|
168
|
+
it 'should handle errors returned as root object' do
|
169
|
+
expected_response = RestfulResource::Response.new(body: @error.to_json)
|
170
|
+
expect_get_with_unprocessable_entity("http://api.carwow.co.uk/dealers", expected_response)
|
171
|
+
|
172
|
+
@object = Dealer.get
|
173
|
+
expect(@object.valid?).to be_falsey
|
174
|
+
expect(@object.errors).to eq @error
|
175
|
+
end
|
176
|
+
end
|
129
177
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -24,6 +24,13 @@ def expect_post(url, response, data: {})
|
|
24
24
|
expect(@mock_http).to receive(:post).with(url, data: data).and_return(response)
|
25
25
|
end
|
26
26
|
|
27
|
+
def expect_get_with_unprocessable_entity(url, response)
|
28
|
+
request = RestfulResource::Request.new(:get, url)
|
29
|
+
rest_client_response = OpenStruct.new({body: response.body, headers: response.headers, code: response.status})
|
30
|
+
exception = RestfulResource::HttpClient::UnprocessableEntity.new(request, rest_client_response)
|
31
|
+
expect(@mock_http).to receive(:get).with(url).and_raise(exception)
|
32
|
+
end
|
33
|
+
|
27
34
|
def expect_put_with_unprocessable_entity(url, response, data: {})
|
28
35
|
request = RestfulResource::Request.new(:put, url, body: data)
|
29
36
|
rest_client_response = OpenStruct.new({body: response.body, headers: response.headers, code: response.status})
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restful_resource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Santoro
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-
|
12
|
+
date: 2016-11-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|