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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5a258ef2eff87f5be59c4093c6828fc2e0731690
4
- data.tar.gz: 7f57020b0b01dad3bdb83f9de6f065e8c9fbf3c4
3
+ metadata.gz: a83306ed9f8641a5842e6b886a0fad7ac91898f9
4
+ data.tar.gz: c47d38be2244a886ba8afaae18aa54f8953436be
5
5
  SHA512:
6
- metadata.gz: 95d6b936bdbb2f51688872015867a5f8f5adf5c825ef75c73f451717805284c934ceeb2e4fb2f0de44b622628690ad9757662b0854e0a24f321385060de7b8d2
7
- data.tar.gz: 52d4ec2f67b9be0f016c07d9471ac9451b6df1bb873f6a071df23453932ddbe861a6263eb60a72a61c100e64feddb37978f06f0c428296349b3181b15869760f
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
- if collection.is_a?(Array)
23
- RemoteResource::Collection.new(self, collection, options)
24
- end
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.to_s) && parsed_body[root_element.to_s]
51
+ data = parsed_body.try(:key?, root_element) && parsed_body[root_element]
51
52
  else
52
- parsed_body
53
- end.presence || {}
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
 
@@ -1,3 +1,3 @@
1
1
  module RemoteResource
2
- VERSION = '1.0.1'.freeze
2
+ VERSION = '1.0.2'.freeze
3
3
  end
@@ -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(:response_body) do
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: response_body.to_json)
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: response_body.to_json)
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 nil' do
147
+ it 'returns an empty Array' do
148
148
  aggregate_failures do
149
- expect(dummy_class.build_collection(nil)).to be_nil
150
- expect(dummy_class.build_collection({})).to be_nil
151
- expect(dummy_class.build_collection('')).to be_nil
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
 
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.describe RemoteResource::VERSION do
4
- it { is_expected.to eql '1.0.1' }
4
+ it { is_expected.to eql '1.0.2' }
5
5
  end
6
6
 
7
7
 
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.1
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-07 00:00:00.000000000 Z
11
+ date: 2017-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler