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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d2d72ef667a20e5c2773931c3b5c449af79ed096
4
- data.tar.gz: f0d464e0d0ffd5740c61d5ae6e251e755844e940
3
+ metadata.gz: 48aee54274e098eadf07461bb8c73efda0b9553b
4
+ data.tar.gz: 9d83d3d0a16de24eaf675521e6400ac781d54699
5
5
  SHA512:
6
- metadata.gz: 161b0138fea97c3cb5853514ddc3b799328bf01d8829276b84f1dadcd2b1513739f2a8a7163a35c54304c1549e184dd0d3d1731920b4c16e41e859fadbeb61fe
7
- data.tar.gz: 40177d79fd2a410a23adbb5e93037b72fc82657f7d9caa1353cc318aa296984fd1e862d5a11eb3ca8ea45b1d1d3320332146777b5b75d419cc5d387105272304
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
- RestfulResource::OpenObject.new(parse_json(response.body))
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
- begin
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
@@ -1,3 +1,3 @@
1
1
  module RestfulResource
2
- VERSION = '1.3.0'
2
+ VERSION = '1.4.0'
3
3
  end
@@ -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.3.0
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-10-24 00:00:00.000000000 Z
12
+ date: 2016-11-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler