shutl_resource 1.3.0 → 1.3.1
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.
- data/lib/shutl/resource/rest.rb +5 -27
- data/lib/shutl/resource/rest_class_methods.rb +44 -44
- data/lib/shutl/resource/version.rb +1 -1
- data/shutl_resource.gemspec +0 -1
- data/spec/rest_resource_spec.rb +4 -3
- metadata +1 -17
data/lib/shutl/resource/rest.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
require 'httparty'
|
2
1
|
require 'active_support/core_ext/string'
|
3
2
|
require 'active_support/core_ext/hash'
|
4
3
|
require 'active_support/inflector'
|
@@ -8,10 +7,9 @@ require 'active_model'
|
|
8
7
|
|
9
8
|
module Shutl::Resource
|
10
9
|
module Rest
|
11
|
-
extend HTTParty
|
12
10
|
include ActiveModel::Serialization
|
13
11
|
|
14
|
-
|
12
|
+
attr_accessor :pagination, :errors
|
15
13
|
|
16
14
|
def self.included(base)
|
17
15
|
base.send :extend, Shutl::Resource::RestClassMethods
|
@@ -20,9 +18,8 @@ module Shutl::Resource
|
|
20
18
|
base.send :resource_id, :id
|
21
19
|
end
|
22
20
|
|
23
|
-
def initialize(args = {}
|
21
|
+
def initialize(args = {})
|
24
22
|
update_attributes args
|
25
|
-
@response = response
|
26
23
|
end
|
27
24
|
|
28
25
|
def as_json(_)
|
@@ -64,28 +61,15 @@ module Shutl::Resource
|
|
64
61
|
self.instance_variables.include?(:"@#{method}") ? true : super
|
65
62
|
end
|
66
63
|
|
67
|
-
def parsed
|
68
|
-
@parsed ||= response.body.with_indifferent_access
|
69
|
-
end
|
70
|
-
|
71
|
-
def status
|
72
|
-
response.code
|
73
|
-
end
|
74
|
-
|
75
|
-
def pagination
|
76
|
-
parsed[:pagination]
|
77
|
-
end
|
78
64
|
|
79
65
|
def next_resource
|
80
|
-
pagination[
|
66
|
+
pagination["next_resource"] if pagination
|
81
67
|
end
|
82
68
|
|
83
69
|
def previous_resource
|
84
|
-
pagination[
|
70
|
+
pagination["previous_resource"] if pagination
|
85
71
|
end
|
86
72
|
|
87
|
-
attr_accessor :errors
|
88
|
-
|
89
73
|
def valid?
|
90
74
|
errors.blank?
|
91
75
|
end
|
@@ -95,17 +79,11 @@ module Shutl::Resource
|
|
95
79
|
end
|
96
80
|
|
97
81
|
def attributes
|
98
|
-
(instance_variables
|
82
|
+
(instance_variables).inject({}.with_indifferent_access) do |h, var|
|
99
83
|
h.merge( { var.to_s.gsub('@','').to_sym => instance_variable_get(var)})
|
100
84
|
end
|
101
85
|
end
|
102
86
|
|
103
|
-
private
|
104
|
-
|
105
|
-
def check_fail *args
|
106
|
-
self.class.send :check_fail, *args
|
107
|
-
end
|
108
|
-
|
109
87
|
protected
|
110
88
|
|
111
89
|
def prefix
|
@@ -47,7 +47,7 @@ module Shutl::Resource
|
|
47
47
|
check_fail response, "Failed to find #{name}! args: #{args}, params: #{params}"
|
48
48
|
|
49
49
|
including_parent_attributes = response.body[@resource_name].merge args
|
50
|
-
new_object including_parent_attributes, response
|
50
|
+
new_object including_parent_attributes, response.body
|
51
51
|
end
|
52
52
|
|
53
53
|
def create attributes = {}, options = {}
|
@@ -61,10 +61,10 @@ module Shutl::Resource
|
|
61
61
|
|
62
62
|
check_fail response, "Create failed"
|
63
63
|
|
64
|
-
|
65
|
-
attributes =
|
64
|
+
body = response.body || {}
|
65
|
+
attributes = body[@resource_name] || {}
|
66
66
|
|
67
|
-
new_object attributes,
|
67
|
+
new_object attributes, body
|
68
68
|
end
|
69
69
|
|
70
70
|
def destroy instance, options = {}
|
@@ -113,7 +113,7 @@ module Shutl::Resource
|
|
113
113
|
check_fail response, "Failed to find all #{name.downcase.pluralize}"
|
114
114
|
|
115
115
|
response_object = response.body[@resource_name.pluralize].map do |h|
|
116
|
-
new_object(args.merge(h), response)
|
116
|
+
new_object(args.merge(h), response.body)
|
117
117
|
end
|
118
118
|
if order_collection?
|
119
119
|
response_object.sort! do |a,b|
|
@@ -240,6 +240,7 @@ module Shutl::Resource
|
|
240
240
|
attributes.delete "response" #used in debugging requests/responses
|
241
241
|
|
242
242
|
url = member_url attributes
|
243
|
+
|
243
244
|
response = connection.send(verb, url) do |req|
|
244
245
|
req.headers = headers
|
245
246
|
req.body = body
|
@@ -250,57 +251,56 @@ module Shutl::Resource
|
|
250
251
|
response
|
251
252
|
end
|
252
253
|
|
253
|
-
def new_object(args={},
|
254
|
-
instance = new add_resource_id_to(args)
|
254
|
+
def new_object(args={}, body)
|
255
|
+
instance = new add_resource_id_to(args)
|
255
256
|
|
256
257
|
instance.tap do |i|
|
257
|
-
|
258
|
-
|
259
|
-
if errors = (parsed_response and parsed_response["errors"])
|
260
|
-
i.errors = errors
|
261
|
-
end
|
258
|
+
i.errors = body["errors"]
|
259
|
+
i.pagination = body["pagination"]
|
262
260
|
end
|
263
261
|
end
|
264
262
|
|
265
263
|
def check_fail response, message
|
266
|
-
|
267
|
-
|
268
|
-
if Shutl::Resource.raise_exceptions_on_no_quotes_generated
|
269
|
-
Shutl::NoQuotesGenerated
|
270
|
-
else
|
271
|
-
nil
|
272
|
-
end
|
273
|
-
|
274
|
-
when 400 then Shutl::BadRequest
|
275
|
-
when 401 then Shutl::UnauthorizedAccess
|
276
|
-
when 403 then Shutl::ForbiddenAccess
|
277
|
-
when 404 then Shutl::ResourceNotFound
|
278
|
-
when 409 then Shutl::ResourceConflict
|
279
|
-
when 410 then Shutl::ResourceGone
|
280
|
-
when 422
|
281
|
-
if Shutl::Resource.raise_exceptions_on_validation
|
282
|
-
Shutl::ResourceInvalid
|
283
|
-
else
|
284
|
-
nil #handled as validation failure
|
285
|
-
end
|
286
|
-
|
287
|
-
when 411..499
|
288
|
-
Shutl::BadRequest
|
289
|
-
when 500 then Shutl::ServerError
|
290
|
-
when 503 then Shutl::ServiceUnavailable
|
291
|
-
when 501..Float::INFINITY
|
292
|
-
Shutl::ServerError
|
293
|
-
end
|
294
|
-
|
295
|
-
if failure_klass
|
296
|
-
body = if response.headers["content-type"] == "application/json"
|
264
|
+
if klass = failure_klass(response.status)
|
265
|
+
body = if response.headers["content-type"] =~ %r{application/json}
|
297
266
|
response.body
|
298
267
|
else
|
299
268
|
{debug_info: response.body}
|
300
269
|
end
|
301
270
|
|
302
271
|
|
303
|
-
raise
|
272
|
+
raise klass.new body, response.status
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
def failure_klass(status)
|
277
|
+
case status
|
278
|
+
when 299
|
279
|
+
if Shutl::Resource.raise_exceptions_on_no_quotes_generated
|
280
|
+
Shutl::NoQuotesGenerated
|
281
|
+
else
|
282
|
+
nil
|
283
|
+
end
|
284
|
+
|
285
|
+
when 400 then Shutl::BadRequest
|
286
|
+
when 401 then Shutl::UnauthorizedAccess
|
287
|
+
when 403 then Shutl::ForbiddenAccess
|
288
|
+
when 404 then Shutl::ResourceNotFound
|
289
|
+
when 409 then Shutl::ResourceConflict
|
290
|
+
when 410 then Shutl::ResourceGone
|
291
|
+
when 422
|
292
|
+
if Shutl::Resource.raise_exceptions_on_validation
|
293
|
+
Shutl::ResourceInvalid
|
294
|
+
else
|
295
|
+
nil #handled as validation failure
|
296
|
+
end
|
297
|
+
|
298
|
+
when 411..499
|
299
|
+
Shutl::BadRequest
|
300
|
+
when 500 then Shutl::ServerError
|
301
|
+
when 503 then Shutl::ServiceUnavailable
|
302
|
+
when 501..Float::INFINITY
|
303
|
+
Shutl::ServerError
|
304
304
|
end
|
305
305
|
end
|
306
306
|
|
data/shutl_resource.gemspec
CHANGED
@@ -18,7 +18,6 @@ Gem::Specification.new do |gem|
|
|
18
18
|
gem.require_paths = ["lib"]
|
19
19
|
gem.version = Shutl::Resource::VERSION
|
20
20
|
|
21
|
-
gem.add_dependency 'httparty', '~> 0.10.0'
|
22
21
|
gem.add_dependency 'shutl_auth', '0.8.0'
|
23
22
|
gem.add_dependency 'activemodel'
|
24
23
|
gem.add_dependency 'faraday'
|
data/spec/rest_resource_spec.rb
CHANGED
@@ -55,7 +55,7 @@ describe Shutl::Resource::Rest do
|
|
55
55
|
|
56
56
|
before do
|
57
57
|
@request = stub_request(:get, 'http://host/test_rests/a').
|
58
|
-
to_return(:
|
58
|
+
to_return(status: 200, body: body, headers: headers)
|
59
59
|
end
|
60
60
|
|
61
61
|
context 'both present' do
|
@@ -311,6 +311,7 @@ describe Shutl::Resource::Rest do
|
|
311
311
|
after do
|
312
312
|
Shutl::Resource.raise_exceptions_on_validation = true
|
313
313
|
end
|
314
|
+
|
314
315
|
specify do
|
315
316
|
errors = { "base" => "invalid", "some_field" => "some field is invalid" }
|
316
317
|
body = { "errors" => errors }.to_json
|
@@ -337,7 +338,7 @@ describe Shutl::Resource::Rest do
|
|
337
338
|
|
338
339
|
def stub_post status
|
339
340
|
stub_request(:post, 'http://host/test_rests').
|
340
|
-
to_return(:status => status, :body => '', :headers => headers)
|
341
|
+
to_return(:status => status, :body => '{}', :headers => headers)
|
341
342
|
end
|
342
343
|
|
343
344
|
it 'should raise error if the remote server returns an error' do
|
@@ -350,7 +351,7 @@ describe Shutl::Resource::Rest do
|
|
350
351
|
|
351
352
|
it 'should post the header content-type: json' do
|
352
353
|
request = stub_request(:post, 'http://host/test_rests').
|
353
|
-
with(:body =>
|
354
|
+
with(:body => '{"test_rest":{}}', :headers => headers)
|
354
355
|
|
355
356
|
TestRest.create
|
356
357
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shutl_resource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,22 +13,6 @@ bindir: bin
|
|
13
13
|
cert_chain: []
|
14
14
|
date: 2013-08-30 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
|
-
- !ruby/object:Gem::Dependency
|
17
|
-
name: httparty
|
18
|
-
requirement: !ruby/object:Gem::Requirement
|
19
|
-
none: false
|
20
|
-
requirements:
|
21
|
-
- - ~>
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 0.10.0
|
24
|
-
type: :runtime
|
25
|
-
prerelease: false
|
26
|
-
version_requirements: !ruby/object:Gem::Requirement
|
27
|
-
none: false
|
28
|
-
requirements:
|
29
|
-
- - ~>
|
30
|
-
- !ruby/object:Gem::Version
|
31
|
-
version: 0.10.0
|
32
16
|
- !ruby/object:Gem::Dependency
|
33
17
|
name: shutl_auth
|
34
18
|
requirement: !ruby/object:Gem::Requirement
|