ddy_remote_resource 0.4.4 → 0.4.5
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/remote_resource/builder.rb +1 -1
- data/lib/remote_resource/collection.rb +13 -3
- data/lib/remote_resource/response.rb +9 -0
- data/lib/remote_resource/version.rb +1 -1
- data/spec/lib/remote_resource/builder_spec.rb +24 -6
- data/spec/lib/remote_resource/collection_spec.rb +17 -1
- data/spec/lib/remote_resource/response_spec.rb +53 -0
- data/spec/lib/remote_resource/version_spec.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 81623623abfc2c02eca8ba86097669fe5228e691
|
4
|
+
data.tar.gz: d6d96b4787ab3c8fbbc9489025da6b8fa680c3b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5891d80cd394f2682b5df1f1962ac676d42966d50153637a9250c0e096ae3ddce3dd1fffd979224561669c6e02bfda14ddf2da50bd1ef86575e416c024e32257
|
7
|
+
data.tar.gz: 93d0f8606a690a56b8fe2f319beac338735810772ba4af8d6b9179947799dd12d06cdb6d645fdc1b1399c13575413d25ec9a4880ed0986bf647254231648321e
|
@@ -4,18 +4,28 @@ module RemoteResource
|
|
4
4
|
|
5
5
|
delegate :[], :at, :reverse, :size, to: :to_a
|
6
6
|
|
7
|
-
attr_reader :resource_klass, :resources_collection, :_response
|
7
|
+
attr_reader :resource_klass, :resources_collection, :meta, :_response
|
8
8
|
|
9
9
|
def initialize(resource_klass, resources_collection, response_hash)
|
10
10
|
@resource_klass = resource_klass
|
11
11
|
@resources_collection = resources_collection
|
12
12
|
@response_hash = response_hash
|
13
|
+
@meta = response_hash[:meta] || {}
|
13
14
|
@_response = response_hash[:_response]
|
14
15
|
end
|
15
16
|
|
16
|
-
def each
|
17
|
+
def each(&block)
|
17
18
|
if resources_collection.is_a? Array
|
18
|
-
|
19
|
+
if defined?(@collection)
|
20
|
+
@collection.each(&block)
|
21
|
+
else
|
22
|
+
@collection = []
|
23
|
+
resources_collection.each do |element|
|
24
|
+
record = resource_klass.new element.merge(@response_hash)
|
25
|
+
@collection << record
|
26
|
+
yield(record)
|
27
|
+
end
|
28
|
+
end
|
19
29
|
end
|
20
30
|
end
|
21
31
|
|
@@ -33,6 +33,15 @@ module RemoteResource
|
|
33
33
|
unpacked_parsed_response_body
|
34
34
|
end
|
35
35
|
|
36
|
+
def sanitized_response_meta
|
37
|
+
return empty_hash if response_body.blank?
|
38
|
+
return empty_hash if parsed_response_body.blank?
|
39
|
+
|
40
|
+
return parsed_response_body['meta'] if parsed_response_body.try :has_key?, 'meta'
|
41
|
+
|
42
|
+
empty_hash
|
43
|
+
end
|
44
|
+
|
36
45
|
def error_messages_response_body
|
37
46
|
return empty_hash if response_body.blank?
|
38
47
|
return empty_hash if parsed_response_body.blank?
|
@@ -21,11 +21,17 @@ describe RemoteResource::Builder do
|
|
21
21
|
let(:sanitized_response_body) do
|
22
22
|
{ "id" => "12", "username" => "foobar" }
|
23
23
|
end
|
24
|
+
let(:sanitized_response_meta) do
|
25
|
+
{ 'total' => '1'}
|
26
|
+
end
|
24
27
|
|
25
|
-
before
|
28
|
+
before do
|
29
|
+
allow(response).to receive(:sanitized_response_body) { sanitized_response_body }
|
30
|
+
allow(response).to receive(:sanitized_response_meta) { sanitized_response_meta }
|
31
|
+
end
|
26
32
|
|
27
33
|
it 'calls the .build_resource' do
|
28
|
-
expect(dummy_class).to receive(:build_resource).with sanitized_response_body, { _response: an_instance_of(RemoteResource::Response) }
|
34
|
+
expect(dummy_class).to receive(:build_resource).with sanitized_response_body, { _response: an_instance_of(RemoteResource::Response), meta: sanitized_response_meta }
|
29
35
|
dummy_class.build_resource_from_response response
|
30
36
|
end
|
31
37
|
end
|
@@ -80,11 +86,17 @@ describe RemoteResource::Builder do
|
|
80
86
|
{ "id" => "12", "username" => "aapmies" }
|
81
87
|
]
|
82
88
|
end
|
89
|
+
let(:sanitized_response_meta) do
|
90
|
+
{ 'total' => '3'}
|
91
|
+
end
|
83
92
|
|
84
|
-
before
|
93
|
+
before do
|
94
|
+
allow(response).to receive(:sanitized_response_body) { sanitized_response_body }
|
95
|
+
allow(response).to receive(:sanitized_response_meta) { sanitized_response_meta }
|
96
|
+
end
|
85
97
|
|
86
98
|
it 'calls the .build_collection' do
|
87
|
-
expect(dummy_class).to receive(:build_collection).with sanitized_response_body, { _response: an_instance_of(RemoteResource::Response) }
|
99
|
+
expect(dummy_class).to receive(:build_collection).with sanitized_response_body, { _response: an_instance_of(RemoteResource::Response), meta: sanitized_response_meta }
|
88
100
|
dummy_class.build_collection_from_response response
|
89
101
|
end
|
90
102
|
end
|
@@ -141,11 +153,17 @@ describe RemoteResource::Builder do
|
|
141
153
|
let(:sanitized_response_body) do
|
142
154
|
{ "id" => "12", "username" => "foobar" }
|
143
155
|
end
|
156
|
+
let(:sanitized_response_meta) do
|
157
|
+
{ 'total' => '1'}
|
158
|
+
end
|
144
159
|
|
145
|
-
before
|
160
|
+
before do
|
161
|
+
allow(response).to receive(:sanitized_response_body) { sanitized_response_body }
|
162
|
+
allow(response).to receive(:sanitized_response_meta) { sanitized_response_meta }
|
163
|
+
end
|
146
164
|
|
147
165
|
it 'calls the #rebuild_resource' do
|
148
|
-
expect(dummy).to receive(:rebuild_resource).with sanitized_response_body, { _response: an_instance_of(RemoteResource::Response) }
|
166
|
+
expect(dummy).to receive(:rebuild_resource).with sanitized_response_body, { _response: an_instance_of(RemoteResource::Response), meta: sanitized_response_meta }
|
149
167
|
dummy.rebuild_resource_from_response response
|
150
168
|
end
|
151
169
|
end
|
@@ -16,8 +16,9 @@ describe RemoteResource::Collection do
|
|
16
16
|
let(:dummy) { dummy_class.new }
|
17
17
|
|
18
18
|
let(:response) { RemoteResource::Response.new double.as_null_object }
|
19
|
+
let(:response_meta) { { total: '1' } }
|
19
20
|
let(:response_hash) do
|
20
|
-
{ _response: response }
|
21
|
+
{ _response: response, meta: response_meta }
|
21
22
|
end
|
22
23
|
|
23
24
|
let(:resources_collection) do
|
@@ -91,6 +92,16 @@ describe RemoteResource::Collection do
|
|
91
92
|
expect(collection[1]._response).to eql response
|
92
93
|
end
|
93
94
|
end
|
95
|
+
|
96
|
+
it 'returns the same objects each time' do
|
97
|
+
expected = collection.collect(&:object_id)
|
98
|
+
actual = collection.collect(&:object_id)
|
99
|
+
|
100
|
+
aggregate_failures do
|
101
|
+
expect(expected.length).to eq(2)
|
102
|
+
expect(expected).to eql(actual)
|
103
|
+
end
|
104
|
+
end
|
94
105
|
end
|
95
106
|
|
96
107
|
context 'when the resources_collection is NOT an Array' do
|
@@ -144,5 +155,10 @@ describe RemoteResource::Collection do
|
|
144
155
|
end
|
145
156
|
end
|
146
157
|
|
158
|
+
describe '#meta' do
|
159
|
+
it 'returns :meta' do
|
160
|
+
expect(collection.meta).to eql(response_meta)
|
161
|
+
end
|
162
|
+
end
|
147
163
|
|
148
164
|
end
|
@@ -112,6 +112,59 @@ describe RemoteResource::Response do
|
|
112
112
|
end
|
113
113
|
end
|
114
114
|
|
115
|
+
describe '#sanitized_response_meta' do
|
116
|
+
let(:response) { described_class.new double.as_null_object }
|
117
|
+
|
118
|
+
before { allow(response).to receive(:response_body) { response_body } }
|
119
|
+
|
120
|
+
context 'when response_body is nil' do
|
121
|
+
let(:response_body) { nil }
|
122
|
+
|
123
|
+
it 'returns an empty Hash' do
|
124
|
+
expect(response.sanitized_response_meta).to eql({})
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
context 'when response_body is empty' do
|
129
|
+
let(:response_body) { '' }
|
130
|
+
|
131
|
+
it 'returns an empty Hash' do
|
132
|
+
expect(response.sanitized_response_meta).to eql({})
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
context 'when response_body is NOT parseable' do
|
137
|
+
let(:response_body) { 'foo' }
|
138
|
+
|
139
|
+
before { allow(JSON).to receive(:parse).and_raise JSON::ParserError }
|
140
|
+
|
141
|
+
it 'returns an empty Hash' do
|
142
|
+
expect(response.sanitized_response_meta).to eql({})
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
context 'when response_body is parseable' do
|
147
|
+
context 'and the connection_options contain a root_element' do
|
148
|
+
let(:connection_options) { { root_element: :foobar } }
|
149
|
+
let(:response) { described_class.new double.as_null_object, connection_options }
|
150
|
+
|
151
|
+
let(:response_body) { '{"foobar":{"id":"12"},"meta":{"total":"1"}}' }
|
152
|
+
|
153
|
+
it 'returns the parsed meta from root' do
|
154
|
+
expect(response.sanitized_response_meta).to match({ 'total' => '1' })
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
context 'and the connection_options do NOT contain a root_element' do
|
159
|
+
let(:response_body) { '{"id":"12","meta":{"total":"1"}}' }
|
160
|
+
|
161
|
+
it 'returns the parsed meta from root' do
|
162
|
+
expect(response.sanitized_response_meta).to match({ 'total' => '1' })
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
115
168
|
describe '#error_messages_response_body' do
|
116
169
|
let(:response) { described_class.new double.as_null_object }
|
117
170
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ddy_remote_resource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan van der Pas
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-03-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|