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 +4 -4
- data/lib/restful_resource.rb +1 -0
- data/lib/restful_resource/base.rb +3 -2
- data/lib/restful_resource/http_client.rb +13 -1
- data/lib/restful_resource/old_base.rb +1 -1
- data/lib/restful_resource/rails_validations.rb +23 -0
- data/lib/restful_resource/version.rb +1 -1
- data/spec/fixtures.rb +6 -0
- data/spec/restful_resource/base_spec.rb +36 -5
- data/spec/restful_resource/http_client_spec.rb +4 -0
- data/spec/restful_resource/rails_validations_spec.rb +36 -0
- data/spec/spec_helper.rb +6 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 75f132684cd87920b174105e1f5c2a119b10c47a
|
4
|
+
data.tar.gz: 1e9567b9c0c5a11f6b9c4e0c4a691df725f40b8f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0e1f0075a6cf5cc0176528123e583197305d618d5118541ef48a94893344daf48983a2a85907be8fd689ea52e81081f1ea08e215d920377bb71eea93955143c8
|
7
|
+
data.tar.gz: e15ec1d715e9ec81df46eab480ba81dc365b9a17e6b810b7b58bda68df9de97234d2d9679455331176d257c3dad8b06a887c603428e6a66b984d251eee4de7db
|
data/lib/restful_resource.rb
CHANGED
@@ -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(
|
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
|
-
|
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
|
@@ -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
|
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
|
124
|
-
expected_response = RestfulResource::Response.new(body:
|
125
|
-
expect_put('http://api.carwow.co.uk/makes
|
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(
|
127
|
+
object = Make.put(1)
|
128
128
|
|
129
|
-
expect(
|
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.
|
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-
|
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
|