restful_resource 0.8.21 → 0.8.22

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: 2a732e7bc8b6e209642bf2606202c10c111d9e94
4
- data.tar.gz: 9e78ed6cfafb5b9705465d7e7991a4b89550b919
3
+ metadata.gz: fbd9d3fc2f8b4e1308a4bb92d007f0894ee8c8e7
4
+ data.tar.gz: 55b69822c4588ccb230023dd1e715045e82fd16c
5
5
  SHA512:
6
- metadata.gz: b6697c2d6ee89fc98dc290122f671ff04af6f261d38527ecbefa2a54652741ea3cdffc220758977c87068893d07d56101e3efda5afee908d04c6d1d0dcdd9744
7
- data.tar.gz: 712efa62b49ef8978636cee84959faef7615ae9ddf0b1b0a935c086566ebe20771384656155908b7775976bcf7d83f505b953044cf6ac3ff3fe1d92f5afe770f
6
+ metadata.gz: 644cd658071c796ebdf3d858881141a807030d07dc290a4afba422c200f607b9f953761df6dc37f3cd44f02922f8b7e858fb143c6c03c2a44a106cc4c387ba6d
7
+ data.tar.gz: 1193a0bd6224381ed8d0c5c8d02296ff688508d4f028fdcad80a0bbbbc6c6a1e7167ca97f401ccf556265426524023ee98e64fa247c645153caef62ad5a24d42
@@ -10,6 +10,16 @@ module RestfulResource
10
10
  self.new(result)
11
11
  end
12
12
  end
13
+
14
+ def post(data: {}, **params)
15
+ begin
16
+ super(data: data, **params)
17
+ rescue HttpClient::UnprocessableEntity => e
18
+ errors = parse_json(e.response.body)
19
+ result = data.merge(errors)
20
+ self.new(result)
21
+ end
22
+ end
13
23
  end
14
24
 
15
25
  def self.included(base)
@@ -1,3 +1,3 @@
1
1
  module RestfulResource
2
- VERSION = "0.8.21"
2
+ VERSION = "0.8.22"
3
3
  end
@@ -22,9 +22,13 @@ describe RestfulResource::HttpClient do
22
22
  expect(response.status).to eq 200
23
23
  end
24
24
 
25
- it 'should raise error 422' do
25
+ it 'put should raise error 422' do
26
26
  expect { @http_client.put('http://httpbin.org/status/422', data: { name: 'Mad cow' }) }.to raise_error(RestfulResource::HttpClient::UnprocessableEntity)
27
27
  end
28
+
29
+ it 'post should raise error 422' do
30
+ expect { @http_client.post('http://httpbin.org/status/422', data: { name: 'Mad cow' }) }.to raise_error(RestfulResource::HttpClient::UnprocessableEntity)
31
+ end
28
32
  end
29
33
 
30
34
  describe 'Authentication' do
@@ -51,4 +51,49 @@ describe RestfulResource::RailsValidations do
51
51
  expect(@object.valid?).to be_falsey
52
52
  end
53
53
  end
54
+
55
+ context "#post without errors" do
56
+ before :each do
57
+ data = {name: 'Barak'}
58
+ expected_response = RestfulResource::Response.new(body: {name: 'Barak'}.to_json)
59
+ expect_post("http://api.carwow.co.uk/dealers", expected_response, data: data)
60
+
61
+ @object = Dealer.post(data: data)
62
+ end
63
+
64
+ it 'should return object' do
65
+ expect(@object.name).to eq 'Barak'
66
+ end
67
+
68
+ it 'should return valid object' do
69
+ expect(@object.valid?).to be_truthy
70
+ end
71
+ end
72
+
73
+ context "#post with errors" do
74
+ before :each do
75
+ data = {name: 'Leonardo'}
76
+ @error = 'Cannot use Ninja Turtles names'
77
+ expected_response = RestfulResource::Response.new(body: {errors: [@error]}.to_json)
78
+ expect_post_with_unprocessable_entity("http://api.carwow.co.uk/dealers", expected_response, data: data)
79
+
80
+ @object = Dealer.post(data: data)
81
+ end
82
+
83
+ it "should have an error" do
84
+ expect(@object.errors.count).to eq 1
85
+ end
86
+
87
+ it 'should have correct error' do
88
+ expect(@object.errors.first).to eq @error
89
+ end
90
+
91
+ it 'should return properly built object' do
92
+ expect(@object.name).to eq 'Leonardo'
93
+ end
94
+
95
+ it 'should return not valid object' do
96
+ expect(@object.valid?).to be_falsey
97
+ end
98
+ end
54
99
  end
@@ -25,3 +25,9 @@ def expect_put_with_unprocessable_entity(url, response, data: {})
25
25
  exception = RestfulResource::HttpClient::UnprocessableEntity.new(rest_client_response)
26
26
  expect(@mock_http).to receive(:put).with(url, data: data).and_raise(exception)
27
27
  end
28
+
29
+ def expect_post_with_unprocessable_entity(url, response, data: {})
30
+ rest_client_response = OpenStruct.new({body: response.body, headers: response.headers, code: response.status})
31
+ exception = RestfulResource::HttpClient::UnprocessableEntity.new(rest_client_response)
32
+ expect(@mock_http).to receive(:post).with(url, data: data).and_raise(exception)
33
+ end
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: 0.8.21
4
+ version: 0.8.22
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: 2014-12-01 00:00:00.000000000 Z
12
+ date: 2015-01-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -165,7 +165,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
165
165
  version: '0'
166
166
  requirements: []
167
167
  rubyforge_project:
168
- rubygems_version: 2.4.1
168
+ rubygems_version: 2.0.14
169
169
  signing_key:
170
170
  specification_version: 4
171
171
  summary: A simple activerecord inspired rest resource base class implemented using