rom-http 0.7.0 → 0.8.0

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.
Files changed (46) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +26 -2
  3. data/LICENSE.txt +1 -1
  4. data/README.md +12 -15
  5. data/lib/rom-http.rb +2 -0
  6. data/lib/rom/http.rb +2 -0
  7. data/lib/rom/http/attribute.rb +10 -0
  8. data/lib/rom/http/commands.rb +2 -0
  9. data/lib/rom/http/commands/create.rb +2 -0
  10. data/lib/rom/http/commands/delete.rb +2 -0
  11. data/lib/rom/http/commands/update.rb +2 -0
  12. data/lib/rom/http/dataset.rb +152 -101
  13. data/lib/rom/http/error.rb +2 -0
  14. data/lib/rom/http/gateway.rb +44 -3
  15. data/lib/rom/http/handlers.rb +14 -0
  16. data/lib/rom/http/handlers/json.rb +65 -0
  17. data/lib/rom/http/mapper_compiler.rb +11 -0
  18. data/lib/rom/http/relation.rb +19 -64
  19. data/lib/rom/http/schema.rb +20 -0
  20. data/lib/rom/http/schema/dsl.rb +12 -0
  21. data/lib/rom/http/transformer.rb +2 -0
  22. data/lib/rom/http/types.rb +13 -0
  23. data/lib/rom/http/version.rb +3 -1
  24. metadata +32 -59
  25. data/.gitignore +0 -16
  26. data/.rspec +0 -3
  27. data/.rubocop.yml +0 -22
  28. data/.rubocop_todo.yml +0 -12
  29. data/.travis.yml +0 -20
  30. data/Gemfile +0 -24
  31. data/Rakefile +0 -24
  32. data/examples/repository_with_combine.rb +0 -154
  33. data/lib/rom/http/dataset/class_interface.rb +0 -33
  34. data/rakelib/rubocop.rake +0 -18
  35. data/rom-http.gemspec +0 -32
  36. data/spec/integration/abstract/commands/create_spec.rb +0 -119
  37. data/spec/integration/abstract/commands/delete_spec.rb +0 -52
  38. data/spec/integration/abstract/commands/update_spec.rb +0 -119
  39. data/spec/integration/abstract/relation_spec.rb +0 -78
  40. data/spec/shared/setup.rb +0 -18
  41. data/spec/shared/users_and_tasks.rb +0 -30
  42. data/spec/spec_helper.rb +0 -19
  43. data/spec/support/mutant.rb +0 -10
  44. data/spec/unit/rom/http/dataset_spec.rb +0 -824
  45. data/spec/unit/rom/http/gateway_spec.rb +0 -69
  46. data/spec/unit/rom/http/relation_spec.rb +0 -268
@@ -1,52 +0,0 @@
1
- RSpec.describe ROM::HTTP::Commands::Delete do
2
- include_context 'setup'
3
- let(:relation) do
4
- Class.new(ROM::HTTP::Relation) do
5
- schema(:users) do
6
- attribute :id, ROM::Types::Int
7
- end
8
-
9
- def by_id(id)
10
- with_params(id: id)
11
- end
12
- end
13
- end
14
- let(:response) { double }
15
- let(:tuple) { double }
16
- let(:tuples) { double(first: tuple) }
17
- let(:command) do
18
- Class.new(ROM::HTTP::Commands::Delete) do
19
- register_as :delete
20
- relation :users
21
- result :one
22
- end
23
- end
24
- let(:dataset) do
25
- ROM::HTTP::Dataset.new(
26
- {
27
- uri: uri,
28
- headers: headers,
29
- request_handler: request_handler,
30
- response_handler: response_handler,
31
- name: :users
32
- },
33
- request_method: :delete
34
- )
35
- end
36
-
37
- before do
38
- configuration.register_relation(relation)
39
- configuration.register_command(command)
40
-
41
- allow(request_handler).to receive(:call).and_return(response)
42
- allow(response_handler).to receive(:call).and_return(tuples)
43
- end
44
-
45
- subject! { container.commands[:users].delete.call }
46
-
47
- it do
48
- expect(request_handler).to have_received(:call).with(dataset)
49
- expect(response_handler).to have_received(:call).with(response, dataset)
50
- is_expected.to eq(tuple)
51
- end
52
- end
@@ -1,119 +0,0 @@
1
- RSpec.describe ROM::HTTP::Commands::Update do
2
- include_context 'setup'
3
- let(:relation) do
4
- Class.new(ROM::HTTP::Relation) do
5
- schema(:users) do
6
- attribute :id, ROM::Types::Int
7
- attribute :first_name, ROM::Types::String
8
- attribute :last_name, ROM::Types::String
9
- end
10
-
11
- def by_id(id)
12
- with_params(id: id)
13
- end
14
- end
15
- end
16
-
17
- context 'with single tuple' do
18
- let(:response) { double }
19
- let(:attributes) { { first_name: 'John', last_name: 'Jackson' } }
20
- let(:tuple) { attributes.merge(id: 1) }
21
- let(:command) do
22
- Class.new(ROM::HTTP::Commands::Update) do
23
- register_as :update
24
- relation :users
25
- result :one
26
- end
27
- end
28
- let(:dataset) do
29
- ROM::HTTP::Dataset.new(
30
- {
31
- uri: uri,
32
- headers: headers,
33
- request_handler: request_handler,
34
- response_handler: response_handler,
35
- name: :users
36
- },
37
- request_method: :put,
38
- params: attributes
39
- )
40
- end
41
-
42
- before do
43
- configuration.register_relation(relation)
44
- configuration.register_command(command)
45
-
46
- allow(request_handler).to receive(:call).and_return(response)
47
- allow(response_handler).to receive(:call).and_return(tuple)
48
- end
49
-
50
- subject! { container.commands[:users].update.call(attributes) }
51
-
52
- it do
53
- expect(request_handler).to have_received(:call).with(dataset)
54
- expect(response_handler).to have_received(:call).with(response, dataset)
55
- is_expected.to eq(tuple)
56
- end
57
- end
58
-
59
- context 'with a collection' do
60
- let(:response_1) { double }
61
- let(:response_2) { double }
62
- let(:attributes_1) { { first_name: 'John', last_name: 'Jackson' } }
63
- let(:attributes_2) { { first_name: 'Jill', last_name: 'Smith' } }
64
- let(:tuple_1) { attributes_1.merge(id: 1) }
65
- let(:tuple_2) { attributes_2.merge(id: 2) }
66
- let(:attributes) { [attributes_1, attributes_2] }
67
- let(:command) do
68
- Class.new(ROM::HTTP::Commands::Update) do
69
- register_as :update
70
- relation :users
71
- result :many
72
- end
73
- end
74
- let(:dataset_1) do
75
- ROM::HTTP::Dataset.new(
76
- {
77
- uri: uri,
78
- headers: headers,
79
- request_handler: request_handler,
80
- response_handler: response_handler,
81
- name: :users
82
- },
83
- request_method: :put,
84
- params: attributes_1
85
- )
86
- end
87
- let(:dataset_2) do
88
- ROM::HTTP::Dataset.new(
89
- {
90
- uri: uri,
91
- headers: headers,
92
- request_handler: request_handler,
93
- response_handler: response_handler,
94
- name: :users
95
- },
96
- request_method: :put,
97
- params: attributes_2
98
- )
99
- end
100
-
101
- before do
102
- configuration.register_relation(relation)
103
- configuration.register_command(command)
104
-
105
- allow(request_handler).to receive(:call).and_return(response_1, response_2)
106
- allow(response_handler).to receive(:call).and_return(tuple_1, tuple_2)
107
- end
108
-
109
- subject! { container.commands[:users].update.call(attributes) }
110
-
111
- it do
112
- expect(request_handler).to have_received(:call).with(dataset_1)
113
- expect(response_handler).to have_received(:call).with(response_1, dataset_1)
114
- expect(request_handler).to have_received(:call).with(dataset_2)
115
- expect(response_handler).to have_received(:call).with(response_2, dataset_2)
116
- is_expected.to eq([tuple_1, tuple_2])
117
- end
118
- end
119
- end
@@ -1,78 +0,0 @@
1
- require 'json'
2
- require 'rom-repository'
3
-
4
- RSpec.describe ROM::HTTP::Relation do
5
- subject(:users) { container.relations[:users].by_id(id).filter(params) }
6
-
7
- include_context 'setup'
8
-
9
- let(:relation) do
10
- Class.new(ROM::HTTP::Relation) do
11
- schema(:users) do
12
- attribute :id, ROM::Types::Int
13
- attribute :name, ROM::Types::String
14
- end
15
-
16
- def by_id(id)
17
- append_path(id.to_s)
18
- end
19
-
20
- def filter(params)
21
- with_params(params)
22
- end
23
- end
24
- end
25
-
26
- let(:response) { tuples.to_json }
27
- let(:tuples) { [{ id: 1337, name: 'John' }] }
28
- let(:id) { 1337 }
29
- let(:params) { { filters: { first_name: 'John' } } }
30
-
31
- let(:dataset) do
32
- ROM::HTTP::Dataset.new(
33
- {
34
- uri: uri,
35
- headers: headers,
36
- request_handler: request_handler,
37
- response_handler: response_handler,
38
- name: :users
39
- },
40
- path: "users/#{id}",
41
- params: params
42
- )
43
- end
44
-
45
- before do
46
- configuration.register_relation(relation)
47
-
48
- allow(request_handler).to receive(:call).and_return(response)
49
- allow(response_handler).to receive(:call).and_return(tuples)
50
- end
51
-
52
- it 'returns relation tuples' do
53
- expect(users.to_a).to eql(tuples)
54
-
55
- expect(request_handler).to have_received(:call).with(dataset).once
56
- expect(response_handler).to have_received(:call).with(response, dataset).once
57
- end
58
-
59
- context 'using a repo' do
60
- let(:repo) do
61
- Class.new(ROM::Repository) do
62
- def self.to_s
63
- 'UserRepo'
64
- end
65
- end.new(container)
66
- end
67
-
68
- it 'returns structs' do
69
- user = repo.users.by_id(1337).filter(params).first
70
-
71
- expect(user.id).to be(1337)
72
- expect(user.name).to eql('John')
73
-
74
- expect(request_handler).to have_received(:call).with(dataset).once
75
- expect(response_handler).to have_received(:call).with(response, dataset).once
76
- end
77
- end
78
- end
@@ -1,18 +0,0 @@
1
- RSpec.shared_context 'setup' do
2
- let(:configuration) do
3
- ROM::Configuration.new(
4
- :http,
5
- uri: uri,
6
- request_handler: request_handler,
7
- response_handler: response_handler,
8
- headers: headers
9
- )
10
- end
11
- let(:container) { ROM.container(configuration) }
12
- let(:rom) { container }
13
- let(:gateway) { container.gateways.fetch(:default) }
14
- let(:uri) { 'http://localhost:3000' }
15
- let(:request_handler) { double(Proc, freeze: self) }
16
- let(:response_handler) { double(Proc, freeze: self) }
17
- let(:headers) { { accept: 'application/json' } }
18
- end
@@ -1,30 +0,0 @@
1
- RSpec.shared_context 'users and tasks' do
2
- include_context 'setup'
3
- let(:users_relation) do
4
- Class.new(ROM::HTTP::Relation) do
5
- schema(:users) do
6
- attribute :id, ROM::Types::Int
7
- end
8
-
9
- def by_id(id)
10
- with_params(id: id)
11
- end
12
- end
13
- end
14
- let(:tasks_relation) do
15
- Class.new(ROM::HTTP::Relation) do
16
- schema(:tasks) do
17
- attribute :id, ROM::Types::Int
18
- end
19
-
20
- def by_id(id)
21
- with_params(id: id)
22
- end
23
- end
24
- end
25
-
26
- before do
27
- configuration.register_relation(users_relation)
28
- configuration.register_relation(tasks_relation)
29
- end
30
- end
@@ -1,19 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'bundler'
4
- Bundler.setup
5
-
6
- require 'rom-http'
7
- require 'rspec/its'
8
- require 'dry/configurable/test_interface'
9
-
10
- ROM::HTTP::Dataset.enable_test_interface
11
-
12
- begin
13
- require 'byebug'
14
- rescue LoadError; end
15
-
16
- root = Pathname(__FILE__).dirname
17
-
18
- Dir[root.join('support/**/*.rb').to_s].each { |file| require file }
19
- Dir[root.join('shared/**/*.rb').to_s].each { |file| require file }
@@ -1,10 +0,0 @@
1
- module Mutant
2
- class Selector
3
- # Expression based test selector
4
- class Expression < self
5
- def call(_subject)
6
- integration.all_tests
7
- end
8
- end # Expression
9
- end # Selector
10
- end # Mutant
@@ -1,824 +0,0 @@
1
- RSpec.describe ROM::HTTP::Dataset do
2
- let(:klass) { ROM::HTTP::Dataset }
3
- let(:dataset) { klass.new(config, options) }
4
- let(:config) do
5
- {
6
- uri: uri,
7
- request_handler: request_handler,
8
- response_handler: response_handler
9
- }
10
- end
11
- let(:options) { {} }
12
- let(:uri) { 'http://localhost:3000' }
13
- let(:request_handler) { double(Proc) }
14
- let(:response_handler) { double(Proc) }
15
-
16
- it { expect(klass).to be_kind_of(::Dry::Configurable) }
17
- it { expect(dataset).to be_kind_of(::Enumerable) }
18
-
19
- describe 'settings' do
20
- describe 'default_request_handler' do
21
- it 'defaults to nil' do
22
- expect(klass.config.default_request_handler).to be nil
23
- end
24
- end
25
-
26
- describe 'default_response_handler' do
27
- it 'defaults to nil' do
28
- expect(klass.config.default_response_handler).to be nil
29
- end
30
- end
31
- end
32
-
33
- describe 'defaults' do
34
- describe '#config' do
35
- subject { dataset.config }
36
-
37
- it { is_expected.to eq(config) }
38
- end
39
-
40
- describe 'options' do
41
- subject { dataset }
42
-
43
- context 'with options passed' do
44
- let(:options) do
45
- {
46
- request_method: :put,
47
- headers: {
48
- 'Accept' => 'application/json'
49
- }
50
- }
51
- end
52
-
53
- its(:base_path) { is_expected.to eq('') }
54
- its(:request_method) { is_expected.to eq(:put) }
55
- its(:path) { is_expected.to eq('') }
56
- its(:params) { is_expected.to eq({}) }
57
- its(:headers) { is_expected.to eq('Accept' => 'application/json') }
58
- end
59
-
60
- context 'with no options passed' do
61
- its(:base_path) { is_expected.to eq('') }
62
- its(:request_method) { is_expected.to eq(:get) }
63
- its(:path) { is_expected.to eq('') }
64
- its(:params) { is_expected.to eq({}) }
65
- its(:headers) { is_expected.to eq({}) }
66
- end
67
- end
68
- end
69
-
70
- describe '.default_request_handler' do
71
- before do
72
- module Test
73
- class Dataset < ROM::HTTP::Dataset; end
74
- end
75
-
76
- allow(Dry::Core::Deprecations).to receive(:announce)
77
- end
78
-
79
- after { Test::Dataset.reset_config }
80
-
81
- context 'when no default_request_handler set' do
82
- subject! { Test::Dataset.default_request_handler }
83
-
84
- it 'returns nil' do
85
- expect(Dry::Core::Deprecations).to have_received(:announce).with(
86
- :default_request_handler,
87
- 'use configuration instead'
88
- )
89
- is_expected.to be nil
90
- end
91
- end
92
-
93
- context 'when default_request_handler set' do
94
- before do
95
- Test::Dataset.default_request_handler(request_handler)
96
- end
97
-
98
- subject! { Test::Dataset.default_request_handler }
99
-
100
- it 'returns the default request handler' do
101
- expect(Dry::Core::Deprecations).to have_received(:announce).with(
102
- :default_request_handler,
103
- 'use configuration instead'
104
- ).twice
105
- is_expected.to eq request_handler
106
- end
107
- end
108
- end
109
-
110
- describe '.default_response_handler' do
111
- before do
112
- module Test
113
- class Dataset < ROM::HTTP::Dataset; end
114
- end
115
-
116
- allow(Dry::Core::Deprecations).to receive(:announce)
117
- end
118
-
119
- after { Test::Dataset.reset_config }
120
-
121
- context 'when no default_response_handler set' do
122
- subject! { Test::Dataset.default_response_handler }
123
-
124
- it 'returns nil' do
125
- expect(Dry::Core::Deprecations).to have_received(:announce).with(
126
- :default_response_handler,
127
- 'use configuration instead'
128
- )
129
- is_expected.to be nil
130
- end
131
- end
132
-
133
- context 'when default_response_handler set' do
134
- before do
135
- Test::Dataset.default_response_handler(response_handler)
136
- end
137
-
138
- subject! { Test::Dataset.default_response_handler }
139
-
140
- it 'returns the default response handler' do
141
- expect(Dry::Core::Deprecations).to have_received(:announce).with(
142
- :default_response_handler,
143
- 'use configuration instead'
144
- ).twice
145
- is_expected.to eq response_handler
146
- end
147
- end
148
- end
149
-
150
- describe '#uri' do
151
- context 'when no uri configured' do
152
- let(:config) { {} }
153
-
154
- it do
155
- expect { dataset.uri }.to raise_error(ROM::HTTP::Error)
156
- end
157
- end
158
-
159
- context 'when uri configured' do
160
- subject! { dataset.uri }
161
-
162
- context 'when request method is GET' do
163
- context 'with params' do
164
- let(:options) do
165
- {
166
- params: {
167
- username: 'John',
168
- role: 'admin'
169
- }
170
- }
171
- end
172
- let(:expected_uri) { URI("#{uri}?username=John&role=admin") }
173
-
174
- it { is_expected.to eq(expected_uri) }
175
- end
176
- end
177
-
178
- context 'when request method is not GET' do
179
- context 'with params' do
180
- let(:options) do
181
- {
182
- request_method: :post,
183
- params: {
184
- username: 'John',
185
- role: 'admin'
186
- }
187
- }
188
- end
189
- let(:expected_uri) { URI(uri) }
190
-
191
- it { is_expected.to eq(expected_uri) }
192
- end
193
- end
194
-
195
- context 'without dataset name' do
196
- context 'with no path' do
197
- let(:expected_uri) { URI(uri) }
198
-
199
- it { is_expected.to eq(expected_uri) }
200
- end
201
-
202
- context 'with path' do
203
- context 'without custom base_path' do
204
- let(:options) { { path: '/users' } }
205
- let(:expected_uri) { URI("#{uri}/users") }
206
-
207
- it { is_expected.to eq(expected_uri) }
208
- end
209
-
210
- context 'with custom base_path' do
211
- let(:options) { { base_path: '/blog', path: '/users' } }
212
- let(:expected_uri) { URI("#{uri}/blog/users") }
213
-
214
- it { is_expected.to eq(expected_uri) }
215
- end
216
- end
217
- end
218
-
219
- context 'with dataset name' do
220
- let(:config) { super().merge(name: :blog) }
221
-
222
- context 'with no path' do
223
- let(:expected_uri) { URI("#{uri}/blog") }
224
-
225
- it { is_expected.to eq(expected_uri) }
226
- end
227
-
228
- context 'with path' do
229
- context 'without custom base_path' do
230
- let(:options) { { path: '/users' } }
231
- let(:expected_uri) { URI("#{uri}/blog/users") }
232
-
233
- it { is_expected.to eq(expected_uri) }
234
- end
235
-
236
- context 'with custom base_path' do
237
- let(:options) { { base_path: '/bloggers', path: '/users' } }
238
- let(:expected_uri) { URI("#{uri}/bloggers/users") }
239
-
240
- it { is_expected.to eq(expected_uri) }
241
- end
242
- end
243
- end
244
-
245
- context 'with custom base_path' do
246
- let(:config) { super().merge(name: :blog) }
247
-
248
- context 'with no path' do
249
- let(:expected_uri) { URI("#{uri}/blog") }
250
-
251
- it { is_expected.to eq(expected_uri) }
252
- end
253
-
254
- context 'with path' do
255
- let(:options) { { path: '/users' } }
256
- let(:expected_uri) { URI("#{uri}/blog/users") }
257
-
258
- it { is_expected.to eq(expected_uri) }
259
- end
260
- end
261
- end
262
- end
263
-
264
- describe '#headers' do
265
- subject { dataset.headers }
266
-
267
- context 'with no headers configured' do
268
- context 'with no headers option' do
269
- it { is_expected.to eq({}) }
270
- end
271
-
272
- context 'with headers option' do
273
- let(:headers) { { 'Accept' => 'application/json' } }
274
- let(:options) { { headers: headers } }
275
-
276
- it { is_expected.to eq(headers) }
277
- end
278
- end
279
-
280
- context 'with headers configured' do
281
- context 'with no headers option' do
282
- let(:headers) { { 'Accept' => 'application/json' } }
283
- let(:config) do
284
- super().merge(headers: headers)
285
- end
286
-
287
- it { is_expected.to eq(headers) }
288
- end
289
-
290
- context 'with headers option' do
291
- let(:config_headers) { { 'Content-Type' => 'application/json' } }
292
- let(:config) do
293
- super().merge(headers: config_headers)
294
- end
295
- let(:option_headers) { { 'Accept' => 'application/json' } }
296
- let(:options) { { headers: option_headers } }
297
-
298
- it do
299
- is_expected.to eq(
300
- 'Content-Type' => 'application/json',
301
- 'Accept' => 'application/json'
302
- )
303
- end
304
- end
305
- end
306
- end
307
-
308
- describe '#name' do
309
- subject { dataset.name }
310
-
311
- context 'with no name configured' do
312
- it { is_expected.to eq('') }
313
- end
314
-
315
- context 'with name configured' do
316
- let(:name) { 'users' }
317
- let(:config) do
318
- super().merge(name: name)
319
- end
320
-
321
- it { is_expected.to eq(name) }
322
- end
323
- end
324
-
325
- describe '#base_path' do
326
- subject { dataset.base_path }
327
-
328
- context 'with no base_path option' do
329
- context 'when dataset name is set' do
330
- let(:config) do
331
- {
332
- uri: uri,
333
- request_handler: request_handler,
334
- response_handler: response_handler,
335
- name: :users
336
- }
337
- end
338
-
339
- it 'returns the dataset name as a string' do
340
- is_expected.to eq('users')
341
- end
342
- end
343
-
344
- context 'when dataset name is not set' do
345
- it 'returns an empty string' do
346
- is_expected.to eq('')
347
- end
348
- end
349
- end
350
-
351
- context 'with base_path option' do
352
- context 'when base_path is absolute' do
353
- let(:base_path) { '/users' }
354
- let(:options) { { base_path: base_path } }
355
-
356
- it 'removes the leading /' do
357
- is_expected.to eq('users')
358
- end
359
- end
360
-
361
- context 'when base_path is not absolute' do
362
- let(:base_path) { 'users' }
363
- let(:options) { { base_path: base_path } }
364
-
365
- it { is_expected.to eq(base_path) }
366
- end
367
- end
368
- end
369
-
370
- describe '#path' do
371
- subject { dataset.path }
372
-
373
- context 'with no path option' do
374
- context 'without dataset name' do
375
- it { is_expected.to eq('') }
376
- end
377
-
378
- context 'with dataset name' do
379
- let(:config) { super().merge(name: :users) }
380
- it { is_expected.to eq('users') }
381
- end
382
-
383
- context 'with base path' do
384
- let(:options) { { base_path: '/users' } }
385
- it { is_expected.to eq('users') }
386
- end
387
- end
388
-
389
- context 'with path option' do
390
- context 'when path is absolute' do
391
- let(:path) { '/users' }
392
-
393
- context 'without dataset name' do
394
- let(:options) { { path: path } }
395
-
396
- it 'removes the leading /' do
397
- is_expected.to eq('users')
398
- end
399
- end
400
-
401
- context 'with dataset name' do
402
- let(:config) { super().merge(name: :blog) }
403
- let(:options) { { path: path } }
404
-
405
- it { is_expected.to eq('blog/users') }
406
- end
407
-
408
- context 'with base path' do
409
- let(:options) { { base_path: '/blog', path: path } }
410
-
411
- it { is_expected.to eq('blog/users') }
412
- end
413
- end
414
-
415
- context 'when path is not absolute' do
416
- let(:path) { 'users' }
417
-
418
- context 'without dataset name' do
419
- let(:options) { { path: path } }
420
-
421
- it { is_expected.to eq(path) }
422
- end
423
-
424
- context 'with dataset name' do
425
- let(:config) { super().merge(name: :blog) }
426
- let(:options) { { path: path } }
427
-
428
- it { is_expected.to eq('blog/users') }
429
- end
430
-
431
- context 'with base path' do
432
- let(:options) { { base_path: '/blog', path: path } }
433
-
434
- it { is_expected.to eq('blog/users') }
435
- end
436
- end
437
- end
438
- end
439
-
440
- describe '#absolute_path' do
441
- subject { dataset.absolute_path }
442
-
443
- context 'with no path option' do
444
- it { is_expected.to eq('/') }
445
- end
446
-
447
- context 'with path option' do
448
- context 'when path is absolute' do
449
- let(:path) { '/users' }
450
- let(:options) { { path: path } }
451
-
452
- it { is_expected.to eq(path) }
453
- end
454
-
455
- context 'when path is not absolute' do
456
- let(:path) { 'users' }
457
- let(:options) { { path: path } }
458
-
459
- it { is_expected.to eq("/#{path}") }
460
- end
461
- end
462
- end
463
-
464
- describe '#request_method' do
465
- subject { dataset.request_method }
466
-
467
- context 'with no request_method option' do
468
- it { is_expected.to eq(:get) }
469
- end
470
-
471
- context 'with request_method option' do
472
- let(:request_method) { :put }
473
- let(:options) { { request_method: request_method } }
474
-
475
- it { is_expected.to eq(request_method) }
476
- end
477
- end
478
-
479
- describe '#params' do
480
- subject { dataset.params }
481
-
482
- context 'with no params option' do
483
- it { is_expected.to eq({}) }
484
- end
485
-
486
- context 'with params option' do
487
- let(:params) { { name: 'Jack' } }
488
- let(:options) { { params: params } }
489
-
490
- it { is_expected.to eq(params) }
491
- end
492
- end
493
-
494
- describe '#with_headers' do
495
- let(:headers) { { 'Accept' => 'application/json' } }
496
- let(:new_dataset) { dataset.with_headers(headers) }
497
-
498
- subject! { new_dataset }
499
-
500
- its(:config) { is_expected.to eq(config) }
501
- its(:base_path) { is_expected.to eq('') }
502
- its(:request_method) { is_expected.to eq(:get) }
503
- its(:path) { is_expected.to eq('') }
504
- its(:params) { is_expected.to eq({}) }
505
- its(:headers) { is_expected.to eq(headers) }
506
-
507
- it { is_expected.to_not be(dataset) }
508
- it { is_expected.to be_a(ROM::HTTP::Dataset) }
509
- end
510
-
511
- describe '#add_header' do
512
- let(:header_key) { 'Accept' }
513
- let(:header_value) { 'application/json' }
514
- let(:new_dataset) { double(ROM::HTTP::Dataset) }
515
-
516
- before do
517
- allow(dataset).to receive(:with_headers).and_return(new_dataset)
518
- end
519
-
520
- subject! { dataset.add_header(header_key, header_value) }
521
-
522
- context 'with existing headers configured' do
523
- let(:config_headers) { { 'Content-Type' => 'application/json', 'Accept' => 'text/html' } }
524
- let(:config) { super().merge(headers: config_headers) }
525
-
526
- it do
527
- expect(dataset).to have_received(:with_headers).with(
528
- 'Content-Type' => 'application/json',
529
- header_key => header_value
530
- )
531
- end
532
- it { is_expected.to eq(new_dataset) }
533
- end
534
-
535
- context 'without existing headers configured' do
536
- it do
537
- expect(dataset).to have_received(:with_headers).with(
538
- header_key => header_value
539
- )
540
- end
541
- it { is_expected.to eq(new_dataset) }
542
- end
543
- end
544
-
545
- describe '#with_options' do
546
- let(:name) { 'Jill' }
547
- let(:options) { { params: { name: name } } }
548
- let(:new_dataset) { dataset.with_options(options) }
549
-
550
- subject! { new_dataset }
551
-
552
- its(:config) { is_expected.to eq(config) }
553
- its(:base_path) { is_expected.to eq('') }
554
- its(:request_method) { is_expected.to eq(:get) }
555
- its(:path) { is_expected.to eq('') }
556
- its(:params) { is_expected.to eq(name: name) }
557
- its(:headers) { is_expected.to eq({}) }
558
-
559
- it { is_expected.to_not be(dataset) }
560
- it { is_expected.to be_a(ROM::HTTP::Dataset) }
561
- end
562
-
563
- describe '#with_base_path' do
564
- let(:base_path) { '/users/tasks' }
565
- let(:new_dataset) { double(ROM::HTTP::Dataset) }
566
-
567
- before do
568
- allow(dataset).to receive(:with_options).and_return(new_dataset)
569
- end
570
-
571
- subject! { dataset.with_base_path(base_path) }
572
-
573
- it { expect(dataset).to have_received(:with_options).with(base_path: base_path) }
574
- it { is_expected.to eq(new_dataset) }
575
- end
576
-
577
- describe '#with_path' do
578
- let(:path) { '/users/tasks' }
579
- let(:new_dataset) { double(ROM::HTTP::Dataset) }
580
-
581
- before do
582
- allow(dataset).to receive(:with_options).and_return(new_dataset)
583
- end
584
-
585
- subject! { dataset.with_path(path) }
586
-
587
- it { expect(dataset).to have_received(:with_options).with(path: path) }
588
- it { is_expected.to eq(new_dataset) }
589
- end
590
-
591
- describe '#append_path' do
592
- let(:path) { 'tasks' }
593
- let(:new_dataset) { double(ROM::HTTP::Dataset) }
594
-
595
- before do
596
- allow(dataset).to receive(:with_options).and_return(new_dataset)
597
- end
598
-
599
- subject! { dataset.append_path(path) }
600
-
601
- context 'without existing path' do
602
- it { expect(dataset).to have_received(:with_options).with(path: 'tasks') }
603
- it { is_expected.to eq(new_dataset) }
604
- end
605
-
606
- context 'with existing path' do
607
- let(:options) { { path: '/users' } }
608
-
609
- it { expect(dataset).to have_received(:with_options).with(path: 'users/tasks') }
610
- it { is_expected.to eq(new_dataset) }
611
- end
612
- end
613
-
614
- describe '#with_request_method' do
615
- let(:request_method) { :put }
616
- let(:new_dataset) { double(ROM::HTTP::Dataset) }
617
-
618
- before do
619
- allow(dataset).to receive(:with_options).and_return(new_dataset)
620
- end
621
-
622
- subject! { dataset.with_request_method(request_method) }
623
-
624
- it { expect(dataset).to have_received(:with_options).with(request_method: request_method) }
625
- it { is_expected.to eq(new_dataset) }
626
- end
627
-
628
- describe '#with_params' do
629
- let(:name) { 'Jack' }
630
- let(:params) { { user: { name: name } } }
631
- let(:new_dataset) { double(ROM::HTTP::Dataset) }
632
-
633
- before do
634
- allow(dataset).to receive(:with_options).and_return(new_dataset)
635
- end
636
-
637
- subject! { dataset.with_params(params) }
638
-
639
- it { expect(dataset).to have_received(:with_options).with(params: params) }
640
- it { is_expected.to eq(new_dataset) }
641
- end
642
-
643
- describe '#add_params' do
644
- let(:options) do
645
- {
646
- params: {
647
- user: {
648
- uid: 3
649
- }
650
- }
651
- }
652
- end
653
- let(:params) { { user: { name: 'Jack' } } }
654
- let(:new_dataset) { double(ROM::HTTP::Dataset) }
655
-
656
- before do
657
- allow(dataset).to receive(:with_options).and_return(new_dataset)
658
- end
659
-
660
- subject! { dataset.add_params(params) }
661
-
662
- it do
663
- expect(dataset).to have_received(:with_options).with(params: {
664
- user: {
665
- uid: 3,
666
- name: 'Jack'
667
- }
668
- })
669
- end
670
- it { is_expected.to eq(new_dataset) }
671
- end
672
-
673
- describe '#each' do
674
- let(:response) { double(Array) }
675
- let(:block) { proc {} }
676
- let(:result) { double }
677
-
678
- before do
679
- allow(dataset).to receive(:response).and_return(response)
680
- allow(response).to receive(:each).and_yield.and_return(result)
681
- end
682
-
683
- context 'with no block given' do
684
- subject! { dataset.each }
685
-
686
- it { expect(dataset).to_not have_received(:response) }
687
- it { expect(response).to_not have_received(:each) }
688
- it { is_expected.to be_kind_of(Enumerable) }
689
- end
690
-
691
- context 'with block given' do
692
- subject! { dataset.each(&block) }
693
-
694
- it { expect(dataset).to have_received(:response).once }
695
- it { expect(response).to have_received(:each) }
696
- it { is_expected.to eq(result) }
697
- end
698
- end
699
-
700
- describe '#insert' do
701
- let(:name) { 'Jill' }
702
- let(:params) { { user: { name: name } } }
703
- let(:new_dataset) { double(ROM::HTTP::Dataset) }
704
- let(:response) { double }
705
-
706
- before do
707
- allow(dataset).to receive(:with_options).and_return(new_dataset)
708
- allow(new_dataset).to receive(:response).and_return(response)
709
- end
710
-
711
- subject! { dataset.insert(params) }
712
-
713
- it do
714
- expect(dataset).to have_received(:with_options).with(
715
- request_method: :post,
716
- params: params
717
- )
718
- end
719
- it { expect(new_dataset).to have_received(:response) }
720
- it { is_expected.to eq(response) }
721
- end
722
-
723
- describe '#update' do
724
- let(:name) { 'Jill' }
725
- let(:params) { { user: { name: name } } }
726
- let(:new_dataset) { double(ROM::HTTP::Dataset) }
727
- let(:response) { double }
728
-
729
- before do
730
- allow(dataset).to receive(:with_options).and_return(new_dataset)
731
- allow(new_dataset).to receive(:response).and_return(response)
732
- end
733
-
734
- subject! { dataset.update(params) }
735
-
736
- it do
737
- expect(dataset).to have_received(:with_options).with(
738
- request_method: :put,
739
- params: params
740
- )
741
- end
742
- it { expect(new_dataset).to have_received(:response) }
743
- it { is_expected.to eq(response) }
744
- end
745
-
746
- describe '#delete' do
747
- let(:new_dataset) { double(ROM::HTTP::Dataset) }
748
- let(:response) { double }
749
-
750
- before do
751
- allow(dataset).to receive(:with_options).and_return(new_dataset)
752
- allow(new_dataset).to receive(:response).and_return(response)
753
- end
754
-
755
- subject! { dataset.delete }
756
-
757
- it do
758
- expect(dataset).to have_received(:with_options).with(
759
- request_method: :delete
760
- )
761
- end
762
- it { expect(new_dataset).to have_received(:response) }
763
- it { is_expected.to eq(response) }
764
- end
765
-
766
- describe '#response' do
767
- let(:response) { double }
768
- let(:result) { double }
769
-
770
- context 'when request_handler and response_handler configured' do
771
- before do
772
- allow(request_handler).to receive(:call).and_return(response)
773
- allow(response_handler).to receive(:call).and_return(result)
774
- end
775
-
776
- subject! { dataset.response }
777
-
778
- it { expect(request_handler).to have_received(:call).with(dataset) }
779
- it { expect(response_handler).to have_received(:call).with(response, dataset) }
780
- it { is_expected.to eq(result) }
781
- end
782
-
783
- context 'when request_handler and response_handler configured' do
784
- let(:dataset) { Test::Dataset.new(config, options) }
785
- let(:config) { {} }
786
-
787
- before do
788
- module Test
789
- class Dataset < ROM::HTTP::Dataset; end
790
- end
791
-
792
- Test::Dataset.default_request_handler(request_handler)
793
- Test::Dataset.default_response_handler(response_handler)
794
-
795
- allow(request_handler).to receive(:call).and_return(response)
796
- allow(response_handler).to receive(:call).and_return(result)
797
- end
798
-
799
- after { Test::Dataset.reset_config }
800
-
801
- subject! { dataset.response }
802
-
803
- it { expect(request_handler).to have_received(:call).with(dataset) }
804
- it { expect(response_handler).to have_received(:call).with(response, dataset) }
805
- it { is_expected.to eq(result) }
806
- end
807
-
808
- context 'when no request_handler configured and no default set' do
809
- let(:config) { { response_handler: response_handler } }
810
-
811
- it do
812
- expect { dataset.response }.to raise_error(ROM::HTTP::Error)
813
- end
814
- end
815
-
816
- context 'when no response_handler configured and no default set' do
817
- let(:config) { { request_handler: request_handler } }
818
-
819
- it do
820
- expect { dataset.response }.to raise_error(ROM::HTTP::Error)
821
- end
822
- end
823
- end
824
- end