restful_resource 0.8.5 → 0.8.6

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: a07bcb86564c632f57564ed219caa375746d34b5
4
- data.tar.gz: 8c2a169aa6eb5d3d1375f3d1db51c71a02507d67
3
+ metadata.gz: 75f132684cd87920b174105e1f5c2a119b10c47a
4
+ data.tar.gz: 1e9567b9c0c5a11f6b9c4e0c4a691df725f40b8f
5
5
  SHA512:
6
- metadata.gz: 25e1171702db43da2dde02f46ea774f035f17599d3f90ec21715f801ad60baabb4109393f4f1346077fb88ab6cdd67add5d88938211282c0fc4f6392aed2ad89
7
- data.tar.gz: bfc2b2cec9c773f70b024614a0d01dc6c3b871312ff131afe659ec4f63ab439292433764f54f6adf454e5bfb959179b0b43db599e5a33bdba143d8ac0039dae3
6
+ metadata.gz: 0e1f0075a6cf5cc0176528123e583197305d618d5118541ef48a94893344daf48983a2a85907be8fd689ea52e81081f1ea08e215d920377bb71eea93955143c8
7
+ data.tar.gz: e15ec1d715e9ec81df46eab480ba81dc365b9a17e6b810b7b58bda68df9de97234d2d9679455331176d257c3dad8b06a887c603428e6a66b984d251eee4de7db
@@ -12,6 +12,7 @@ require_relative 'restful_resource/response'
12
12
  require_relative 'restful_resource/authorization'
13
13
  require_relative 'restful_resource/http_client'
14
14
  require_relative "restful_resource/associations"
15
+ require_relative 'restful_resource/rails_validations'
15
16
  require_relative "restful_resource/base"
16
17
  require_relative "restful_resource/old_base"
17
18
 
@@ -37,8 +37,9 @@ module RestfulResource
37
37
  RestfulResource::OpenObject.new(parse_json(response.body))
38
38
  end
39
39
 
40
- def self.put(data: {}, **params)
41
- http.put(collection_url(params), data)
40
+ def self.put(id, data: {}, **params)
41
+ response = http.put(member_url(id, params), data)
42
+ self.new(parse_json(response.body))
42
43
  end
43
44
 
44
45
  def self.all
@@ -1,5 +1,13 @@
1
1
  module RestfulResource
2
2
  class HttpClient
3
+ class UnprocessableEntity < RuntimeError
4
+ attr_reader :response
5
+
6
+ def initialize(response)
7
+ @response = Response.new(body: response.body, headers: response.headers, status: response.code)
8
+ end
9
+ end
10
+
3
11
  def initialize(authorization: nil)
4
12
  @authorization = authorization
5
13
  end
@@ -10,7 +18,11 @@ module RestfulResource
10
18
  end
11
19
 
12
20
  def put(url, data: {})
13
- response = RestClient.put(url, data, :accept => :json, authorization: @authorization)
21
+ begin
22
+ response = RestClient.put(url, data, :accept => :json, authorization: @authorization)
23
+ rescue RestClient::UnprocessableEntity => e
24
+ raise HttpClient::UnprocessableEntity.new(e.response)
25
+ end
14
26
  Response.new(body: response.body, headers: response.headers, status: response.code)
15
27
  end
16
28
  end
@@ -68,7 +68,7 @@ module RestfulResource
68
68
  end
69
69
 
70
70
  def valid?
71
- errors.nil? || errors.count == g
71
+ errors.nil? || errors.count == 0
72
72
  end
73
73
 
74
74
  def self.find(id, url_params={})
@@ -0,0 +1,23 @@
1
+ module RestfulResource
2
+ module RailsValidations
3
+ module ClassMethods
4
+ def put(id, data: {}, **params)
5
+ begin
6
+ super(id, data: data, **params)
7
+ rescue HttpClient::UnprocessableEntity => e
8
+ errors = parse_json(e.response.body)
9
+ result = data.merge(errors)
10
+ self.new(result)
11
+ end
12
+ end
13
+ end
14
+
15
+ def self.included(base)
16
+ base.extend(ClassMethods)
17
+ end
18
+
19
+ def valid?
20
+ errors.nil? || errors.count == 0
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module RestfulResource
2
- VERSION = "0.8.5"
2
+ VERSION = "0.8.6"
3
3
  end
data/spec/fixtures.rb CHANGED
@@ -8,6 +8,12 @@ class Model < RestfulResource::Base
8
8
  resource_path "groups/:group_id/makes/:make_slug/models"
9
9
  end
10
10
 
11
+ class Dealer < RestfulResource::Base
12
+ include RestfulResource::RailsValidations
13
+
14
+ resource_path "dealers"
15
+ end
16
+
11
17
  class BaseA < RestfulResource::Base
12
18
  end
13
19
 
@@ -120,13 +120,44 @@ describe RestfulResource::Base do
120
120
  end
121
121
 
122
122
  describe "#put" do
123
- it 'should put data properly' do
124
- expected_response = RestfulResource::Response.new(body: nil, status: 200)
125
- expect_put('http://api.carwow.co.uk/makes?make_slug=Volkswagen', expected_response)
123
+ it 'should put no data with no params' do
124
+ expected_response = RestfulResource::Response.new(body: {name: 'Audi'}.to_json, status: 200)
125
+ expect_put('http://api.carwow.co.uk/makes/1', expected_response)
126
126
 
127
- Make.put(make_slug: 'Volkswagen')
127
+ object = Make.put(1)
128
128
 
129
- expect(expected_response.status).to eq 200
129
+ expect(object.name).to eq 'Audi'
130
+ end
131
+
132
+ it 'should put no data with params passed' do
133
+ expected_response = RestfulResource::Response.new(body: {name: 'Audi'}.to_json, status: 200)
134
+ expect_put('http://api.carwow.co.uk/makes/1?make_slug=Volkswagen', expected_response)
135
+
136
+ object = Make.put(1, make_slug: 'Volkswagen')
137
+
138
+ expect(object.name).to eq 'Audi'
139
+ end
140
+
141
+ it 'should put data with no params passed' do
142
+ data = {make_slug: 'Audi'}.to_json
143
+
144
+ expected_response = RestfulResource::Response.new(body: {name: 'Audi'}.to_json, status: 200)
145
+ expect_put('http://api.carwow.co.uk/makes/1', expected_response, data: data)
146
+
147
+ object = Make.put(1, data: data)
148
+
149
+ expect(object.name).to eq 'Audi'
150
+ end
151
+
152
+ it 'should put data with params passed' do
153
+ data = {make_slug: 'Audi'}.to_json
154
+
155
+ expected_response = RestfulResource::Response.new(body: {name: 'Audi'}.to_json, status: 200)
156
+ expect_put('http://api.carwow.co.uk/makes/1?make_slug=Volkswagen', expected_response, data: data)
157
+
158
+ object = Make.put(1, data: data, make_slug: 'Volkswagen')
159
+
160
+ expect(object.name).to eq 'Audi'
130
161
  end
131
162
  end
132
163
 
@@ -15,6 +15,10 @@ describe RestfulResource::HttpClient do
15
15
  response = @http_client.put('http://httpbin.org/put', data: { name: 'Alfred' })
16
16
  expect(response.status).to eq 200
17
17
  end
18
+
19
+ it 'should raise error 422' do
20
+ expect { @http_client.put('http://httpbin.org/status/422', data: { name: 'Mad cow' }) }.to raise_error(RestfulResource::HttpClient::UnprocessableEntity)
21
+ end
18
22
  end
19
23
 
20
24
  describe 'Authentication' do
@@ -0,0 +1,36 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe RestfulResource::RailsValidations do
4
+ before :each do
5
+ @mock_http = double("mock_http")
6
+ RestfulResource::Base.http = @mock_http
7
+ RestfulResource::Base.base_url = "http://api.carwow.co.uk/"
8
+ end
9
+
10
+ context "#put with errors" do
11
+ before :each do
12
+ data = {name: 'Leonardo'}
13
+ @error = 'Cannot use Ninja Turtles names'
14
+ expected_response = RestfulResource::Response.new(body: {errors: [@error]}.to_json)
15
+ expect_put_with_unprocessable_entity("http://api.carwow.co.uk/dealers/1", expected_response, data: data)
16
+
17
+ @object = Dealer.put(1, data: data)
18
+ end
19
+
20
+ it "should have an error" do
21
+ expect(@object.errors.count).to eq 1
22
+ end
23
+
24
+ it 'should have correct error' do
25
+ expect(@object.errors.first).to eq @error
26
+ end
27
+
28
+ it 'should return properly built object' do
29
+ expect(@object.name).to eq 'Leonardo'
30
+ end
31
+
32
+ it 'should return not valid object' do
33
+ expect(@object.valid?).to be_falsey
34
+ end
35
+ end
36
+ end
data/spec/spec_helper.rb CHANGED
@@ -15,3 +15,9 @@ end
15
15
  def expect_put(url, response, data: {})
16
16
  expect(@mock_http).to receive(:put).with(url, data).and_return(response)
17
17
  end
18
+
19
+ def expect_put_with_unprocessable_entity(url, response, data: {})
20
+ rest_client_response = OpenStruct.new({body: response.body, headers: response.headers, code: response.status})
21
+ exception = RestfulResource::HttpClient::UnprocessableEntity.new(rest_client_response)
22
+ expect(@mock_http).to receive(:put).with(url, data).and_raise(exception)
23
+ 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.5
4
+ version: 0.8.6
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-10-24 00:00:00.000000000 Z
12
+ date: 2014-10-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -131,6 +131,7 @@ files:
131
131
  - lib/restful_resource/open_object.rb
132
132
  - lib/restful_resource/paginated_array.rb
133
133
  - lib/restful_resource/parameter_missing_error.rb
134
+ - lib/restful_resource/rails_validations.rb
134
135
  - lib/restful_resource/response.rb
135
136
  - lib/restful_resource/version.rb
136
137
  - restful_resource.gemspec
@@ -140,6 +141,7 @@ files:
140
141
  - spec/restful_resource/http_client_spec.rb
141
142
  - spec/restful_resource/old_base_spec.rb
142
143
  - spec/restful_resource/open_object_spec.rb
144
+ - spec/restful_resource/rails_validations_spec.rb
143
145
  - spec/restful_resource/rest_client_spec.rb
144
146
  - spec/spec_helper.rb
145
147
  homepage: http://www.github.com/carwow/restful_resource
@@ -174,5 +176,6 @@ test_files:
174
176
  - spec/restful_resource/http_client_spec.rb
175
177
  - spec/restful_resource/old_base_spec.rb
176
178
  - spec/restful_resource/open_object_spec.rb
179
+ - spec/restful_resource/rails_validations_spec.rb
177
180
  - spec/restful_resource/rest_client_spec.rb
178
181
  - spec/spec_helper.rb