restful_resource 2.2.5 → 2.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/restful_resource/base.rb +17 -2
- data/lib/restful_resource/version.rb +1 -1
- data/spec/restful_resource/base_spec.rb +219 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b0958e356e2525fc286894548cc434ff68486f4c
|
4
|
+
data.tar.gz: e7ca6b8704b648c7d55482d988fa59ee8ab648a0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c6bde5bde53b14d936f6afbe2e483fc1a99502f6d6b9f58a19f4e4b2b1e50c532d2aae7367b626a8797355ba1439c69d69bc8789d0ca816c19033e9ee8b78fa4
|
7
|
+
data.tar.gz: 7ef2459add247da55d390c2c7dc2caac34f6473d9bd53e23f4be19f455daf054d973c0df27651a5713da024ba4edfb775757a7548be3b8b96dfa9271748d2746
|
@@ -8,9 +8,11 @@ module RestfulResource
|
|
8
8
|
logger: nil,
|
9
9
|
cache_store: nil,
|
10
10
|
instrumentation: {},
|
11
|
+
context_lambda: nil,
|
11
12
|
faraday_config: nil)
|
12
13
|
|
13
14
|
@base_url = URI.parse(base_url)
|
15
|
+
@context_lambda = context_lambda
|
14
16
|
|
15
17
|
@http = RestfulResource::HttpClient.new(username: username,
|
16
18
|
password: password,
|
@@ -54,7 +56,7 @@ module RestfulResource
|
|
54
56
|
|
55
57
|
def self.put(id, data: {}, headers: {}, **params)
|
56
58
|
params_without_options, options = format_params(params)
|
57
|
-
options.delete(:headers)
|
59
|
+
headers.merge! options.delete(:headers)
|
58
60
|
|
59
61
|
url = member_url(id, params_without_options)
|
60
62
|
|
@@ -64,7 +66,7 @@ module RestfulResource
|
|
64
66
|
|
65
67
|
def self.post(data: {}, headers: {}, **params)
|
66
68
|
params_without_options, options = format_params(params)
|
67
|
-
options.delete(:headers)
|
69
|
+
headers.merge! options.delete(:headers)
|
68
70
|
|
69
71
|
url = collection_url(params_without_options)
|
70
72
|
|
@@ -119,12 +121,18 @@ module RestfulResource
|
|
119
121
|
replace_parameters(url, params)
|
120
122
|
end
|
121
123
|
|
124
|
+
def self.context_lambda
|
125
|
+
superclass.instance_variable_get :@context_lambda
|
126
|
+
end
|
127
|
+
|
122
128
|
private
|
123
129
|
|
124
130
|
def self.format_params(params = {})
|
125
131
|
headers = params.delete(:headers) || {}
|
126
132
|
|
127
133
|
headers.merge!(cache_control: 'no-cache') if params.delete(:no_cache)
|
134
|
+
headers.merge!(context_headers) if self.context_lambda
|
135
|
+
|
128
136
|
open_timeout = params.delete(:open_timeout)
|
129
137
|
timeout = params.delete(:timeout)
|
130
138
|
|
@@ -184,5 +192,12 @@ module RestfulResource
|
|
184
192
|
array = parse_json(response.body).map { |attributes| self.new(attributes) }
|
185
193
|
PaginatedArray.new(array, previous_page_url: prev_url, next_page_url: next_url, total_count: response.headers[:x_total_count])
|
186
194
|
end
|
195
|
+
|
196
|
+
def self.context_headers
|
197
|
+
self
|
198
|
+
.context_lambda
|
199
|
+
.call
|
200
|
+
.select { |k,_| k.to_s.start_with?('source_') }
|
201
|
+
end
|
187
202
|
end
|
188
203
|
end
|
@@ -65,6 +65,32 @@ RSpec.describe RestfulResource::Base do
|
|
65
65
|
|
66
66
|
Make.find(12, no_cache: true)
|
67
67
|
end
|
68
|
+
|
69
|
+
it 'skips context headers when context_lambda is not set' do
|
70
|
+
expect_get("http://api.carwow.co.uk/makes/12",
|
71
|
+
RestfulResource::Response.new,
|
72
|
+
headers: { cache_control: 'no-cache' })
|
73
|
+
|
74
|
+
expect(RestfulResource::Base).to receive(:context_lambda).and_return(nil)
|
75
|
+
|
76
|
+
Make.find(12, no_cache: true)
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'adds context headers when context_lambda is set' do
|
80
|
+
expect_get("http://api.carwow.co.uk/makes/12",
|
81
|
+
RestfulResource::Response.new,
|
82
|
+
headers: {
|
83
|
+
cache_control: 'no-cache',
|
84
|
+
source_type: 'controller',
|
85
|
+
source_class: 'SomeController',
|
86
|
+
source_identifier: 'index',
|
87
|
+
}
|
88
|
+
)
|
89
|
+
|
90
|
+
expect(RestfulResource::Base).to receive(:context_lambda).and_return(context_lambda).twice
|
91
|
+
|
92
|
+
Make.find(12, no_cache: true)
|
93
|
+
end
|
68
94
|
end
|
69
95
|
|
70
96
|
describe "#where" do
|
@@ -106,6 +132,32 @@ RSpec.describe RestfulResource::Base do
|
|
106
132
|
|
107
133
|
Model.where(make_slug: 'Volkswagen', on_sale: true, group_id: 15, no_cache: true)
|
108
134
|
end
|
135
|
+
|
136
|
+
it 'skips context headers when context_lambda is not set' do
|
137
|
+
expect_get("http://api.carwow.co.uk/groups/15/makes/Volkswagen/models?on_sale=true",
|
138
|
+
RestfulResource::Response.new,
|
139
|
+
headers: { cache_control: 'no-cache' })
|
140
|
+
|
141
|
+
expect(RestfulResource::Base).to receive(:context_lambda).and_return(nil)
|
142
|
+
|
143
|
+
Model.where(make_slug: 'Volkswagen', on_sale: true, group_id: 15, no_cache: true)
|
144
|
+
end
|
145
|
+
|
146
|
+
it 'adds context headers when context_lambda is set' do
|
147
|
+
expect_get("http://api.carwow.co.uk/groups/15/makes/Volkswagen/models?on_sale=true",
|
148
|
+
RestfulResource::Response.new,
|
149
|
+
headers: {
|
150
|
+
cache_control: 'no-cache',
|
151
|
+
source_type: 'controller',
|
152
|
+
source_class: 'SomeController',
|
153
|
+
source_identifier: 'index',
|
154
|
+
}
|
155
|
+
)
|
156
|
+
|
157
|
+
expect(RestfulResource::Base).to receive(:context_lambda).and_return(context_lambda).twice
|
158
|
+
|
159
|
+
Model.where(make_slug: 'Volkswagen', on_sale: true, group_id: 15, no_cache: true)
|
160
|
+
end
|
109
161
|
end
|
110
162
|
|
111
163
|
describe "#all" do
|
@@ -134,6 +186,32 @@ RSpec.describe RestfulResource::Base do
|
|
134
186
|
|
135
187
|
Make.all(no_cache: true)
|
136
188
|
end
|
189
|
+
|
190
|
+
it 'skips context headers when context_lambda is not set' do
|
191
|
+
expect_get("http://api.carwow.co.uk/makes",
|
192
|
+
RestfulResource::Response.new,
|
193
|
+
headers: { cache_control: 'no-cache' })
|
194
|
+
|
195
|
+
expect(RestfulResource::Base).to receive(:context_lambda).and_return(nil)
|
196
|
+
|
197
|
+
Make.all(no_cache: true)
|
198
|
+
end
|
199
|
+
|
200
|
+
it 'adds context headers when context_lambda is set' do
|
201
|
+
expect_get("http://api.carwow.co.uk/makes",
|
202
|
+
RestfulResource::Response.new,
|
203
|
+
headers: {
|
204
|
+
cache_control: 'no-cache',
|
205
|
+
source_type: 'controller',
|
206
|
+
source_class: 'SomeController',
|
207
|
+
source_identifier: 'index',
|
208
|
+
}
|
209
|
+
)
|
210
|
+
|
211
|
+
expect(RestfulResource::Base).to receive(:context_lambda).and_return(context_lambda).twice
|
212
|
+
|
213
|
+
Make.all(no_cache: true)
|
214
|
+
end
|
137
215
|
end
|
138
216
|
|
139
217
|
describe "#base_url" do
|
@@ -194,6 +272,32 @@ RSpec.describe RestfulResource::Base do
|
|
194
272
|
|
195
273
|
Make.action(:average_score).get(no_cache: true)
|
196
274
|
end
|
275
|
+
|
276
|
+
it 'skips context headers when context_lambda is not set' do
|
277
|
+
expect_get("http://api.carwow.co.uk/makes/average_score",
|
278
|
+
RestfulResource::Response.new,
|
279
|
+
headers: { cache_control: 'no-cache' })
|
280
|
+
|
281
|
+
expect(RestfulResource::Base).to receive(:context_lambda).and_return(nil)
|
282
|
+
|
283
|
+
Make.action(:average_score).get(no_cache: true)
|
284
|
+
end
|
285
|
+
|
286
|
+
it 'adds context headers when context_lambda is set' do
|
287
|
+
expect_get("http://api.carwow.co.uk/makes/average_score",
|
288
|
+
RestfulResource::Response.new,
|
289
|
+
headers: {
|
290
|
+
cache_control: 'no-cache',
|
291
|
+
source_type: 'controller',
|
292
|
+
source_class: 'SomeController',
|
293
|
+
source_identifier: 'index',
|
294
|
+
}
|
295
|
+
)
|
296
|
+
|
297
|
+
expect(RestfulResource::Base).to receive(:context_lambda).and_return(context_lambda).twice
|
298
|
+
|
299
|
+
Make.action(:average_score).get(no_cache: true)
|
300
|
+
end
|
197
301
|
end
|
198
302
|
|
199
303
|
describe "#put" do
|
@@ -244,6 +348,32 @@ RSpec.describe RestfulResource::Base do
|
|
244
348
|
|
245
349
|
Make.put(1, data: {}, headers: { accept: 'application/json' })
|
246
350
|
end
|
351
|
+
|
352
|
+
it 'skips context headers when context_lambda is not set' do
|
353
|
+
expect_put("http://api.carwow.co.uk/makes/1",
|
354
|
+
RestfulResource::Response.new,
|
355
|
+
headers: { accept: 'application/json' })
|
356
|
+
|
357
|
+
expect(RestfulResource::Base).to receive(:context_lambda).and_return(nil)
|
358
|
+
|
359
|
+
Make.put(1, data: {}, headers: { accept: 'application/json' })
|
360
|
+
end
|
361
|
+
|
362
|
+
it 'adds context headers when context_lambda is set' do
|
363
|
+
expect_put("http://api.carwow.co.uk/makes/1",
|
364
|
+
RestfulResource::Response.new,
|
365
|
+
headers: {
|
366
|
+
accept: 'application/json',
|
367
|
+
source_type: 'controller',
|
368
|
+
source_class: 'SomeController',
|
369
|
+
source_identifier: 'index',
|
370
|
+
}
|
371
|
+
)
|
372
|
+
|
373
|
+
expect(RestfulResource::Base).to receive(:context_lambda).and_return(context_lambda).twice
|
374
|
+
|
375
|
+
Make.put(1, data: {}, headers: { accept: 'application/json' })
|
376
|
+
end
|
247
377
|
end
|
248
378
|
|
249
379
|
describe "#post" do
|
@@ -266,6 +396,32 @@ RSpec.describe RestfulResource::Base do
|
|
266
396
|
|
267
397
|
Make.post(data: {}, headers: { accept: 'application/json' })
|
268
398
|
end
|
399
|
+
|
400
|
+
it 'skips context headers when context_lambda is not set' do
|
401
|
+
expect_post("http://api.carwow.co.uk/makes",
|
402
|
+
RestfulResource::Response.new,
|
403
|
+
headers: { accept: 'application/json' })
|
404
|
+
|
405
|
+
expect(RestfulResource::Base).to receive(:context_lambda).and_return(nil)
|
406
|
+
|
407
|
+
Make.post(data: {}, headers: { accept: 'application/json' })
|
408
|
+
end
|
409
|
+
|
410
|
+
it 'adds context headers when context_lambda is set' do
|
411
|
+
expect_post("http://api.carwow.co.uk/makes",
|
412
|
+
RestfulResource::Response.new,
|
413
|
+
headers: {
|
414
|
+
accept: 'application/json',
|
415
|
+
source_type: 'controller',
|
416
|
+
source_class: 'SomeController',
|
417
|
+
source_identifier: 'index',
|
418
|
+
}
|
419
|
+
)
|
420
|
+
|
421
|
+
expect(RestfulResource::Base).to receive(:context_lambda).and_return(context_lambda).twice
|
422
|
+
|
423
|
+
Make.post(data: {}, headers: { accept: 'application/json' })
|
424
|
+
end
|
269
425
|
end
|
270
426
|
|
271
427
|
describe "#delete" do
|
@@ -285,6 +441,32 @@ RSpec.describe RestfulResource::Base do
|
|
285
441
|
|
286
442
|
Make.delete(1, headers: { accept: 'application/json' })
|
287
443
|
end
|
444
|
+
|
445
|
+
it 'skips context headers when context_lambda is not set' do
|
446
|
+
expect_delete("http://api.carwow.co.uk/makes/1",
|
447
|
+
RestfulResource::Response.new,
|
448
|
+
headers: { accept: 'application/json' })
|
449
|
+
|
450
|
+
expect(RestfulResource::Base).to receive(:context_lambda).and_return(nil)
|
451
|
+
|
452
|
+
Make.delete(1, headers: { accept: 'application/json' })
|
453
|
+
end
|
454
|
+
|
455
|
+
it 'adds context headers when context_lambda is set' do
|
456
|
+
expect_delete("http://api.carwow.co.uk/makes/1",
|
457
|
+
RestfulResource::Response.new,
|
458
|
+
headers: {
|
459
|
+
accept: 'application/json',
|
460
|
+
source_type: 'controller',
|
461
|
+
source_class: 'SomeController',
|
462
|
+
source_identifier: 'index',
|
463
|
+
}
|
464
|
+
)
|
465
|
+
|
466
|
+
expect(RestfulResource::Base).to receive(:context_lambda).and_return(context_lambda).twice
|
467
|
+
|
468
|
+
Make.delete(1, headers: { accept: 'application/json' })
|
469
|
+
end
|
288
470
|
end
|
289
471
|
|
290
472
|
describe ".as_json" do
|
@@ -334,6 +516,43 @@ RSpec.describe RestfulResource::Base do
|
|
334
516
|
instrumentation: instrumentation,
|
335
517
|
faraday_config: faraday_config)
|
336
518
|
end
|
519
|
+
|
520
|
+
it "stores a lambda when it is defined" do
|
521
|
+
a_lambda = lambda { {context: 'info'} }
|
522
|
+
|
523
|
+
RestfulResource::Base.configure(base_url: 'http://foo.bar',
|
524
|
+
instrumentation: {context_lambda: a_lambda})
|
525
|
+
|
526
|
+
RestfulResource::Base.instance_variable_get(:@context_lambda).equal?(a_lambda)
|
527
|
+
end
|
528
|
+
end
|
529
|
+
|
530
|
+
describe ".context_headers" do
|
531
|
+
it "builds headers from keys in context_lambda starting with 'source_' only" do
|
532
|
+
a_lambda = context_lambda(
|
533
|
+
source_present: 'value',
|
534
|
+
random_key: 'should not be present'
|
535
|
+
)
|
536
|
+
|
537
|
+
expect(RestfulResource::Base).to receive(:context_lambda).and_return(a_lambda)
|
538
|
+
|
539
|
+
expect(RestfulResource::Base.context_headers).to eq(
|
540
|
+
source_type: 'controller',
|
541
|
+
source_class: 'SomeController',
|
542
|
+
source_identifier: 'index',
|
543
|
+
source_present: 'value'
|
544
|
+
)
|
545
|
+
end
|
546
|
+
end
|
547
|
+
|
548
|
+
def context_lambda(args = {})
|
549
|
+
lambda do
|
550
|
+
{
|
551
|
+
source_type: 'controller',
|
552
|
+
source_class: 'SomeController',
|
553
|
+
source_identifier: 'index',
|
554
|
+
}.merge(args)
|
555
|
+
end
|
337
556
|
end
|
338
557
|
|
339
558
|
def response_with_page_information
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restful_resource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.2.
|
4
|
+
version: 2.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Santoro
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2018-
|
12
|
+
date: 2018-05-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -258,7 +258,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
258
258
|
version: '0'
|
259
259
|
requirements: []
|
260
260
|
rubyforge_project:
|
261
|
-
rubygems_version: 2.
|
261
|
+
rubygems_version: 2.5.2
|
262
262
|
signing_key:
|
263
263
|
specification_version: 4
|
264
264
|
summary: A simple activerecord inspired rest resource base class implemented using
|