ddy_remote_resource 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/remote_resource/builder.rb +3 -3
- data/lib/remote_resource/response.rb +11 -4
- data/lib/remote_resource/version.rb +1 -1
- data/spec/integration/all_spec.rb +30 -0
- data/spec/integration/naming_spec.rb +17 -3
- data/spec/integration/where_spec.rb +30 -0
- data/spec/lib/remote_resource/builder_spec.rb +4 -4
- data/spec/lib/remote_resource/response_spec.rb +8 -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: a83306ed9f8641a5842e6b886a0fad7ac91898f9
|
4
|
+
data.tar.gz: c47d38be2244a886ba8afaae18aa54f8953436be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d6f5cc8d3bbd70527df5337af6ec8e9ba4ac8257a34d683772654fc79e170a0a43629fa5985c3ba7cf7db9907fe53fbb86bb431dc5257783bc08a2949ab4e530
|
7
|
+
data.tar.gz: d29ffc3ad4f221bf3bc756aa0a1935de98fdb8f06da14bad8e745304e04214e2a3076827abce3b59be56bfefef967da9b22edcfe194494f92b49aa1e193246eb
|
@@ -19,9 +19,9 @@ module RemoteResource
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def build_collection(collection, options = {})
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
collection.is_a?(Array) || raise(ArgumentError, '`collection` must be an Array')
|
23
|
+
|
24
|
+
RemoteResource::Collection.new(self, collection, options)
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
@@ -44,13 +44,20 @@ module RemoteResource
|
|
44
44
|
|
45
45
|
def attributes
|
46
46
|
@attributes ||= begin
|
47
|
-
root_element = @connection_options[:root_element]
|
47
|
+
root_element = @connection_options[:root_element].to_s
|
48
|
+
data = nil
|
48
49
|
|
49
50
|
if root_element.present?
|
50
|
-
parsed_body.try(:key?, root_element
|
51
|
+
data = parsed_body.try(:key?, root_element) && parsed_body[root_element]
|
51
52
|
else
|
52
|
-
parsed_body
|
53
|
-
end
|
53
|
+
data = parsed_body
|
54
|
+
end
|
55
|
+
|
56
|
+
if data.is_a?(Array)
|
57
|
+
data
|
58
|
+
else
|
59
|
+
data.presence || {}
|
60
|
+
end
|
54
61
|
end
|
55
62
|
end
|
56
63
|
|
@@ -80,6 +80,36 @@ RSpec.describe '.all' do
|
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
83
|
+
describe 'with an empty response' do
|
84
|
+
let(:response_body) do
|
85
|
+
{
|
86
|
+
data: []
|
87
|
+
}
|
88
|
+
end
|
89
|
+
|
90
|
+
let!(:expected_request) do
|
91
|
+
mock_request = stub_request(:get, 'https://www.example.com/posts.json')
|
92
|
+
mock_request.with(query: nil, body: nil, headers: expected_default_headers)
|
93
|
+
mock_request.to_return(status: 200, body: response_body.to_json)
|
94
|
+
mock_request
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'performs the correct HTTP GET request' do
|
98
|
+
Post.all
|
99
|
+
expect(expected_request).to have_been_requested
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'builds the correct collection of resources' do
|
103
|
+
posts = Post.all
|
104
|
+
|
105
|
+
aggregate_failures do
|
106
|
+
expect(posts).to respond_to :each
|
107
|
+
expect(posts.to_a).to eql []
|
108
|
+
expect(posts.size).to eql 0
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
83
113
|
describe 'with connection_options[:params]' do
|
84
114
|
let!(:expected_request) do
|
85
115
|
mock_request = stub_request(:get, 'https://www.example.com/posts.json')
|
@@ -96,7 +96,7 @@ RSpec.describe 'connection_options for naming' do
|
|
96
96
|
end
|
97
97
|
|
98
98
|
describe 'connection_options[:path_postfix]' do
|
99
|
-
let(:
|
99
|
+
let(:response_body_singular) do
|
100
100
|
{
|
101
101
|
data: {
|
102
102
|
id: 12,
|
@@ -108,15 +108,29 @@ RSpec.describe 'connection_options for naming' do
|
|
108
108
|
}
|
109
109
|
end
|
110
110
|
|
111
|
+
let(:response_body_collection) do
|
112
|
+
{
|
113
|
+
data: [
|
114
|
+
{
|
115
|
+
id: 12,
|
116
|
+
title: 'Aliquam lobortis',
|
117
|
+
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
|
118
|
+
featured: false,
|
119
|
+
created_at: Time.new(2015, 10, 4, 9, 30, 0),
|
120
|
+
}
|
121
|
+
]
|
122
|
+
}
|
123
|
+
end
|
124
|
+
|
111
125
|
let!(:expected_request_singular) do
|
112
126
|
mock_request = stub_request(:get, 'https://www.example.com/posts/12/featured.json')
|
113
|
-
mock_request.to_return(status: 200, body:
|
127
|
+
mock_request.to_return(status: 200, body: response_body_singular.to_json)
|
114
128
|
mock_request
|
115
129
|
end
|
116
130
|
|
117
131
|
let!(:expected_request_collection) do
|
118
132
|
mock_request = stub_request(:get, 'https://www.example.com/posts/featured.json')
|
119
|
-
mock_request.to_return(status: 200, body:
|
133
|
+
mock_request.to_return(status: 200, body: response_body_collection.to_json)
|
120
134
|
mock_request
|
121
135
|
end
|
122
136
|
|
@@ -94,6 +94,36 @@ RSpec.describe '.where' do
|
|
94
94
|
end
|
95
95
|
end
|
96
96
|
|
97
|
+
describe 'with an empty response' do
|
98
|
+
let(:response_body) do
|
99
|
+
{
|
100
|
+
data: []
|
101
|
+
}
|
102
|
+
end
|
103
|
+
|
104
|
+
let!(:expected_request) do
|
105
|
+
mock_request = stub_request(:get, 'https://www.example.com/posts.json')
|
106
|
+
mock_request.with(query: { pseudonym: 'pseudonym' }, body: nil, headers: expected_default_headers)
|
107
|
+
mock_request.to_return(status: 200, body: response_body.to_json)
|
108
|
+
mock_request
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'performs the correct HTTP GET request' do
|
112
|
+
Post.where({ pseudonym: 'pseudonym' })
|
113
|
+
expect(expected_request).to have_been_requested
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'builds the correct collection of resources' do
|
117
|
+
posts = Post.where({ pseudonym: 'pseudonym' })
|
118
|
+
|
119
|
+
aggregate_failures do
|
120
|
+
expect(posts).to respond_to :each
|
121
|
+
expect(posts.to_a).to eql []
|
122
|
+
expect(posts.size).to eql 0
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
97
127
|
describe 'with a 404 response' do
|
98
128
|
let!(:expected_request) do
|
99
129
|
mock_request = stub_request(:get, 'https://www.example.com/posts.json')
|
@@ -144,11 +144,11 @@ RSpec.describe RemoteResource::Builder do
|
|
144
144
|
end
|
145
145
|
|
146
146
|
context 'when the given collection is NOT an Array' do
|
147
|
-
it 'returns
|
147
|
+
it 'returns an empty Array' do
|
148
148
|
aggregate_failures do
|
149
|
-
expect
|
150
|
-
expect
|
151
|
-
expect
|
149
|
+
expect { dummy_class.build_collection(nil) }.to raise_error(ArgumentError, '`collection` must be an Array')
|
150
|
+
expect { dummy_class.build_collection({}) }.to raise_error(ArgumentError, '`collection` must be an Array')
|
151
|
+
expect { dummy_class.build_collection('') }.to raise_error(ArgumentError, '`collection` must be an Array')
|
152
152
|
end
|
153
153
|
end
|
154
154
|
end
|
@@ -141,6 +141,14 @@ RSpec.describe RemoteResource::Response do
|
|
141
141
|
end
|
142
142
|
end
|
143
143
|
|
144
|
+
context 'when the parsed response body is an empty Array' do
|
145
|
+
let(:connection_response) { Typhoeus::Response.new(mock: true, code: 200, body: [].to_json, headers: { 'Content-Type' => 'application/json', 'Server' => 'nginx/1.4.6 (Ubuntu)' }) }
|
146
|
+
|
147
|
+
it 'returns an empty Array' do
|
148
|
+
expect(response.attributes).to eql([])
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
144
152
|
context 'when the parsed response body is NOT present' do
|
145
153
|
let(:connection_response) { Typhoeus::Response.new(mock: true, code: 500, body: '', headers: { 'Content-Type' => 'application/json', 'Server' => 'nginx/1.4.6 (Ubuntu)' }) }
|
146
154
|
|
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: 1.0.
|
4
|
+
version: 1.0.2
|
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: 2017-07-
|
11
|
+
date: 2017-07-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|