lhs 13.0.1 → 13.1.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.
- checksums.yaml +4 -4
- data/README.md +10 -0
- data/lhs.gemspec +1 -1
- data/lib/lhs/concerns/record/chainable.rb +25 -6
- data/lib/lhs/concerns/record/request.rb +9 -7
- data/lib/lhs/version.rb +1 -1
- data/spec/record/ignore_errors_spec.rb +57 -0
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 619fa442b169b251cb6501e59940d0c587010de6
|
4
|
+
data.tar.gz: e0fdac02b30115bab5c2e98eb8e1f0847d042921
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ab7fcafa5689e4796f82a7c2da65600c60068dbb2325763775f8d5db833d1814730d91b1819e97f7e29aa51001cc5225e983b2fb58a52ae106fcc269a3f27dd7
|
7
|
+
data.tar.gz: 6f73e3433aa5e955c336da8b2dddc8aebd9b557b8256efd2bf4adc6503376fb75951055d0cddc759f8b3f6efa4509a64c280bf052be8791d0cad00edb4b3bda6
|
data/README.md
CHANGED
@@ -181,6 +181,16 @@ record = Record.where(color: 'blue')
|
|
181
181
|
|
182
182
|
[List of possible error classes](https://github.com/local-ch/lhc/tree/master/lib/lhc/errors)
|
183
183
|
|
184
|
+
If an error handler returns `nil` an empty LHS::Record is returned, not `nil`!
|
185
|
+
|
186
|
+
In case you want to ignore errores and continue working with `nil` in those cases,
|
187
|
+
please use `ignore`:
|
188
|
+
|
189
|
+
```ruby
|
190
|
+
record = Record.ignore(LHC::NotFound).find_by(color: 'blue')
|
191
|
+
record # nil
|
192
|
+
```
|
193
|
+
|
184
194
|
## Resolve chains
|
185
195
|
|
186
196
|
LHS Chains can be resolved with `fetch`, similiar to ActiveRecord:
|
data/lhs.gemspec
CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |s|
|
|
20
20
|
s.requirements << 'Ruby >= 2.3.0'
|
21
21
|
s.required_ruby_version = '>= 2.3.0'
|
22
22
|
|
23
|
-
s.add_dependency 'lhc', '>= 6.3.
|
23
|
+
s.add_dependency 'lhc', '>= 6.3.1'
|
24
24
|
s.add_dependency 'activesupport', '> 4.2'
|
25
25
|
s.add_dependency 'activemodel'
|
26
26
|
|
@@ -45,6 +45,10 @@ class LHS::Record
|
|
45
45
|
Chain.new(self, ErrorHandling.new(error_class => handler))
|
46
46
|
end
|
47
47
|
|
48
|
+
def ignore(error_class)
|
49
|
+
Chain.new(self, IgnoredError.new(error_class))
|
50
|
+
end
|
51
|
+
|
48
52
|
def includes(*args)
|
49
53
|
Chain.new(self, Include.new(Chain.unfold(args)))
|
50
54
|
end
|
@@ -108,6 +112,10 @@ class LHS::Record
|
|
108
112
|
end
|
109
113
|
end
|
110
114
|
|
115
|
+
# IgnoredError: Ignore certain LHC errors when resolving the chain
|
116
|
+
class IgnoredError < Link
|
117
|
+
end
|
118
|
+
|
111
119
|
# A sequence of links
|
112
120
|
class Chain
|
113
121
|
|
@@ -193,6 +201,10 @@ class LHS::Record
|
|
193
201
|
push(Option.new(hash))
|
194
202
|
end
|
195
203
|
|
204
|
+
def ignore(error_class)
|
205
|
+
push(IgnoredError.new(error_class))
|
206
|
+
end
|
207
|
+
|
196
208
|
def page(page)
|
197
209
|
push(Pagination.new(page: page))
|
198
210
|
end
|
@@ -300,6 +312,7 @@ class LHS::Record
|
|
300
312
|
options = chain_options
|
301
313
|
options = options.deep_merge(params: chain_parameters.merge(chain_pagination))
|
302
314
|
options = options.merge(error_handler: chain_error_handler) if chain_error_handler.present?
|
315
|
+
options = options.merge(ignored_errors: chain_ignored_errors) if chain_ignored_errors.present?
|
303
316
|
options = options.merge(including: chain_includes) if chain_includes.present?
|
304
317
|
options = options.merge(referencing: chain_references) if chain_references.present?
|
305
318
|
options
|
@@ -350,23 +363,29 @@ class LHS::Record
|
|
350
363
|
end
|
351
364
|
|
352
365
|
def chain_parameters
|
353
|
-
merge_links(_links.select { |link| link.is_a? Parameter })
|
366
|
+
@chain_parameters ||= merge_links(_links.select { |link| link.is_a? Parameter })
|
354
367
|
end
|
355
368
|
|
356
369
|
def chain_options
|
357
|
-
merge_links(_links.select { |link| link.is_a? Option })
|
370
|
+
@chain_options ||= merge_links(_links.select { |link| link.is_a? Option })
|
358
371
|
end
|
359
372
|
|
360
373
|
def chain_error_handler
|
361
|
-
_links.select { |link| link.is_a? ErrorHandling }
|
374
|
+
@chain_error_handler ||= _links.select { |link| link.is_a? ErrorHandling }
|
375
|
+
end
|
376
|
+
|
377
|
+
def chain_ignored_errors
|
378
|
+
@chain_ignored_errors ||= _links
|
379
|
+
.select { |link| link.is_a? IgnoredError }
|
380
|
+
.map { |link| link.data }
|
362
381
|
end
|
363
382
|
|
364
383
|
def chain_pagination
|
365
|
-
resolve_pagination _links.select { |link| link.is_a? Pagination }
|
384
|
+
@chain_pagination ||= resolve_pagination _links.select { |link| link.is_a? Pagination }
|
366
385
|
end
|
367
386
|
|
368
387
|
def chain_includes
|
369
|
-
LHS::Complex.reduce(
|
388
|
+
@chain_includes ||= LHS::Complex.reduce(
|
370
389
|
_links
|
371
390
|
.select { |link| link.is_a?(Include) && link.data.present? }
|
372
391
|
.map { |link| link.data }
|
@@ -374,7 +393,7 @@ class LHS::Record
|
|
374
393
|
end
|
375
394
|
|
376
395
|
def chain_references
|
377
|
-
LHS::Complex.reduce(
|
396
|
+
@chain_references ||= LHS::Complex.reduce(
|
378
397
|
_links
|
379
398
|
.select { |link| link.is_a?(Reference) && link.data.present? }
|
380
399
|
.map { |link| link.data }
|
@@ -9,10 +9,11 @@ class LHS::Record
|
|
9
9
|
module ClassMethods
|
10
10
|
def request(options)
|
11
11
|
options ||= {}
|
12
|
-
options = options.
|
12
|
+
options = options.freeze
|
13
13
|
if options.is_a?(Array)
|
14
|
-
|
15
|
-
|
14
|
+
multiple_requests(
|
15
|
+
filter_empty_request_options(options)
|
16
|
+
)
|
16
17
|
else
|
17
18
|
single_request(options)
|
18
19
|
end
|
@@ -20,10 +21,9 @@ class LHS::Record
|
|
20
21
|
|
21
22
|
private
|
22
23
|
|
23
|
-
def
|
24
|
-
options.each_with_index do |option
|
25
|
-
|
26
|
-
options[index] = nil
|
24
|
+
def filter_empty_request_options(options)
|
25
|
+
options.each_with_index.map do |option|
|
26
|
+
option if !option || !option.key?(:url) || !option[:url].nil?
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
@@ -399,7 +399,9 @@ class LHS::Record
|
|
399
399
|
|
400
400
|
# Merge explicit params and take configured endpoints options as base
|
401
401
|
def process_options(options, endpoint)
|
402
|
+
ignored_errors = options[:ignored_errors]
|
402
403
|
options = options.deep_dup
|
404
|
+
options[:ignored_errors] = ignored_errors if ignored_errors.present?
|
403
405
|
options[:params].deep_symbolize_keys! if options[:params]
|
404
406
|
options[:error_handler] = merge_error_handlers(options[:error_handler]) if options[:error_handler]
|
405
407
|
options = (endpoint.options || {}).merge(options)
|
data/lib/lhs/version.rb
CHANGED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
describe LHS::Record do
|
4
|
+
let(:handler) { spy('handler') }
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
class Record < LHS::Record
|
8
|
+
endpoint 'http://local.ch/v2/records'
|
9
|
+
endpoint 'http://local.ch/v2/records/:id'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'ignore errors' do
|
14
|
+
before(:each) do
|
15
|
+
stub_request(:get, "http://local.ch/v2/records?color=blue").to_return(status: 404)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'allows to ignore errors' do
|
19
|
+
record = Record
|
20
|
+
.where(color: 'blue')
|
21
|
+
.ignore(LHC::NotFound)
|
22
|
+
.fetch
|
23
|
+
expect(record).to eq nil
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'multiple ignored errors' do
|
28
|
+
it 'ignores error if one of them is specified' do
|
29
|
+
stub_request(:get, "http://local.ch/v2/records?color=blue").to_return(status: 401)
|
30
|
+
record = Record
|
31
|
+
.ignore(LHC::Unauthorized)
|
32
|
+
.where(color: 'blue')
|
33
|
+
.ignore(LHC::NotFound)
|
34
|
+
.fetch
|
35
|
+
expect(record).to eq nil
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'ignores error if one of them is specified' do
|
39
|
+
stub_request(:get, "http://local.ch/v2/records?color=blue").to_return(status: 404)
|
40
|
+
record = Record
|
41
|
+
.ignore(LHC::Unauthorized)
|
42
|
+
.where(color: 'blue')
|
43
|
+
.ignore(LHC::NotFound)
|
44
|
+
.fetch
|
45
|
+
expect(record).to eq nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'also can ignore all LHC errors' do
|
50
|
+
stub_request(:get, "http://local.ch/v2/records?color=blue").to_return(status: 401)
|
51
|
+
record = Record
|
52
|
+
.ignore(LHC::Error)
|
53
|
+
.where(color: 'blue')
|
54
|
+
.fetch
|
55
|
+
expect(record).to eq nil
|
56
|
+
end
|
57
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lhs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 13.0
|
4
|
+
version: 13.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- https://github.com/local-ch/lhs/graphs/contributors
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-08-
|
11
|
+
date: 2017-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: lhc
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 6.3.
|
19
|
+
version: 6.3.1
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 6.3.
|
26
|
+
version: 6.3.1
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activesupport
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -358,6 +358,7 @@ files:
|
|
358
358
|
- spec/record/find_in_parallel_spec.rb
|
359
359
|
- spec/record/find_spec.rb
|
360
360
|
- spec/record/first_spec.rb
|
361
|
+
- spec/record/ignore_errors_spec.rb
|
361
362
|
- spec/record/immutable_chains_spec.rb
|
362
363
|
- spec/record/includes_all_spec.rb
|
363
364
|
- spec/record/includes_spec.rb
|
@@ -414,7 +415,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
414
415
|
requirements:
|
415
416
|
- Ruby >= 2.3.0
|
416
417
|
rubyforge_project:
|
417
|
-
rubygems_version: 2.6.
|
418
|
+
rubygems_version: 2.6.8
|
418
419
|
signing_key:
|
419
420
|
specification_version: 4
|
420
421
|
summary: Rails gem providing an easy, active-record-like interface for http json services
|
@@ -530,6 +531,7 @@ test_files:
|
|
530
531
|
- spec/record/find_in_parallel_spec.rb
|
531
532
|
- spec/record/find_spec.rb
|
532
533
|
- spec/record/first_spec.rb
|
534
|
+
- spec/record/ignore_errors_spec.rb
|
533
535
|
- spec/record/immutable_chains_spec.rb
|
534
536
|
- spec/record/includes_all_spec.rb
|
535
537
|
- spec/record/includes_spec.rb
|