lhs 22.1.1.pre → 24.1.0.pre.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +67 -5
- data/lhs.gemspec +2 -2
- data/lib/lhs/concerns/collection/internal_collection.rb +17 -1
- data/lib/lhs/concerns/data/extend.rb +77 -0
- data/lib/lhs/concerns/record/chainable.rb +4 -4
- data/lib/lhs/concerns/record/request.rb +9 -75
- data/lib/lhs/data.rb +15 -0
- data/lib/lhs/version.rb +1 -1
- data/spec/dummy/app/controllers/error_handling_with_chains_controller.rb +2 -2
- data/spec/record/compact_spec.rb +67 -0
- data/spec/record/error_handling_spec.rb +4 -4
- data/spec/record/find_in_parallel_spec.rb +1 -1
- data/spec/record/handle_includes_errors_spec.rb +1 -1
- data/spec/record/includes_first_page_spec.rb +14 -4
- metadata +10 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '0098eb4147ef54ee7e80bb67e1a466c39165ef4b65ad2ac18f6a2c024ee2e9f7'
|
4
|
+
data.tar.gz: 853bd4e9697debd6c559d24d295019473793c3c5bcf4c1a3f5cdba3d966a743c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d3db0060e2155188d1697ec6e9c9000d05b13592d6a8ec7c5f952a28b25d1162f72c009f5f70375e06e67637a8d4e488e1946a161bda24513cbb1ea17fe626a3
|
7
|
+
data.tar.gz: aa448eeb65c3b13ba529040d79b4a3cb6ac31f3961cf407e6d72d6f69bdc3dfc5f543c8d02cd1a3d4932b6a57e0c26fabbd91f365f7629676445cbc328e93e42
|
data/README.md
CHANGED
@@ -99,6 +99,8 @@ record.review # "Lunch was great
|
|
99
99
|
* [Change/Update existing records](#changeupdate-existing-records)
|
100
100
|
* [save](#save)
|
101
101
|
* [update](#update)
|
102
|
+
* [Directly via Record](#directly-via-record)
|
103
|
+
* [per Instance](#per-instance)
|
102
104
|
* [partial_update](#partial_update)
|
103
105
|
* [Endpoint url parameter injection during record creation/change](#endpoint-url-parameter-injection-during-record-creationchange)
|
104
106
|
* [Record validation](#record-validation)
|
@@ -119,8 +121,8 @@ record.review # "Lunch was great
|
|
119
121
|
* [Record getters](#record-getters)
|
120
122
|
* [Include linked resources (hyperlinks and hypermedia)](#include-linked-resources-hyperlinks-and-hypermedia)
|
121
123
|
* [Generate links from parameters](#generate-links-from-parameters)
|
122
|
-
* [Ensure the whole linked collection is included
|
123
|
-
* [Include the first linked page
|
124
|
+
* [Ensure the whole linked collection is included with includes](#ensure-the-whole-linked-collection-is-included-with-includes)
|
125
|
+
* [Include only the first linked page of a linked collection: includes_first_page](#include-only-the-first-linked-page-of-a-linked-collection-includes_first_page)
|
124
126
|
* [Include various levels of linked data](#include-various-levels-of-linked-data)
|
125
127
|
* [Identify and cast known records when including records](#identify-and-cast-known-records-when-including-records)
|
126
128
|
* [Apply options for requests performed to fetch included records](#apply-options-for-requests-performed-to-fetch-included-records)
|
@@ -152,6 +154,7 @@ record.review # "Lunch was great
|
|
152
154
|
|
153
155
|
|
154
156
|
|
157
|
+
|
155
158
|
## Installation/Startup checklist
|
156
159
|
|
157
160
|
- [ ] Install LHS gem, preferably via `Gemfile`
|
@@ -976,7 +979,7 @@ If you need to render some different view in Rails based on an LHS error raised
|
|
976
979
|
|
977
980
|
def show
|
978
981
|
@records = Record
|
979
|
-
.
|
982
|
+
.rescue(LHC::Error, ->(error){ rescue_from(error) })
|
980
983
|
.where(color: 'blue')
|
981
984
|
render 'show'
|
982
985
|
render_error if @error
|
@@ -984,7 +987,7 @@ end
|
|
984
987
|
|
985
988
|
private
|
986
989
|
|
987
|
-
def
|
990
|
+
def rescue_from(error)
|
988
991
|
@error = error
|
989
992
|
nil
|
990
993
|
end
|
@@ -1009,7 +1012,7 @@ If you want to inject values for the failing records, that might not have been f
|
|
1009
1012
|
# app/controllers/some_controller.rb
|
1010
1013
|
|
1011
1014
|
data = Record
|
1012
|
-
.
|
1015
|
+
.rescue(LHC::Unauthorized, ->(response) { Record.new(name: 'unknown') })
|
1013
1016
|
.find(1, 2, 3)
|
1014
1017
|
|
1015
1018
|
data[1].name # 'unknown'
|
@@ -2276,6 +2279,65 @@ In parallel:
|
|
2276
2279
|
GET https://service.example.com/places/4 { headers: { 'Authentication': 'Bearer 123' } }
|
2277
2280
|
```
|
2278
2281
|
|
2282
|
+
Here is another example, if you want to ignore errors, that occure while you fetch included resources:
|
2283
|
+
|
2284
|
+
```ruby
|
2285
|
+
# app/controllers/some_controller.rb
|
2286
|
+
|
2287
|
+
feedback = Feedback
|
2288
|
+
.includes(campaign: :entry)
|
2289
|
+
.references(campaign: { ignore: LHC::NotFound })
|
2290
|
+
.find(12345)
|
2291
|
+
```
|
2292
|
+
|
2293
|
+
#### compact: Remove included resources that didn't return any records
|
2294
|
+
|
2295
|
+
In case you include nested data and ignored errors while including, it can happen that you get back a collection that contains data based on response errors:
|
2296
|
+
|
2297
|
+
```ruby
|
2298
|
+
# app/controllers/some_controller.rb
|
2299
|
+
|
2300
|
+
user = User
|
2301
|
+
.includes(:places)
|
2302
|
+
.references(places: { ignore: LHC::NotFound })
|
2303
|
+
.find(123)
|
2304
|
+
```
|
2305
|
+
|
2306
|
+
```
|
2307
|
+
GET http://service/users/123
|
2308
|
+
{ "places": { "href": "http://service/users/123/places" } }
|
2309
|
+
|
2310
|
+
GET http://service/users/123/places
|
2311
|
+
{ "items": [
|
2312
|
+
{ "href": "http://service/places/1" },
|
2313
|
+
{ "href": "http://service/places/2" }
|
2314
|
+
] }
|
2315
|
+
|
2316
|
+
GET http://service/places/1
|
2317
|
+
200 { "name": "Casa Ferlin" }
|
2318
|
+
|
2319
|
+
GET http://service/places/2
|
2320
|
+
404 { "status": 404, "error": "not found" }
|
2321
|
+
```
|
2322
|
+
|
2323
|
+
```ruby
|
2324
|
+
user.places[1] # { "status": 404, "error": "not found" }
|
2325
|
+
```
|
2326
|
+
|
2327
|
+
In order to exclude items from a collection which where not based on successful responses, use `.compact` or `.compact!`:
|
2328
|
+
|
2329
|
+
```ruby
|
2330
|
+
# app/controllers/some_controller.rb
|
2331
|
+
|
2332
|
+
user = User
|
2333
|
+
.includes(:places)
|
2334
|
+
.references(places: { ignore: LHC::NotFound })
|
2335
|
+
.find(123)
|
2336
|
+
places = user.places.compact
|
2337
|
+
|
2338
|
+
places # { "items": [ { "href": "http://service/places/1", "name": "Casa Ferlin" } ] }
|
2339
|
+
```
|
2340
|
+
|
2279
2341
|
### Record batch processing
|
2280
2342
|
|
2281
2343
|
**Be careful using methods for batch processing. They could result in a lot of HTTP requests!**
|
data/lhs.gemspec
CHANGED
@@ -24,7 +24,7 @@ Gem::Specification.new do |s|
|
|
24
24
|
|
25
25
|
s.add_dependency 'activemodel'
|
26
26
|
s.add_dependency 'activesupport', '>= 4.2.11'
|
27
|
-
s.add_dependency 'lhc', '>=
|
27
|
+
s.add_dependency 'lhc', '>= 12.1.1', '< 13'
|
28
28
|
s.add_dependency 'local_uri'
|
29
29
|
|
30
30
|
s.add_development_dependency 'capybara'
|
@@ -39,5 +39,5 @@ Gem::Specification.new do |s|
|
|
39
39
|
s.add_development_dependency 'sprockets', '< 4'
|
40
40
|
s.add_development_dependency 'webmock'
|
41
41
|
|
42
|
-
s.license = 'GPL-3'
|
42
|
+
s.license = 'GPL-3.0'
|
43
43
|
end
|
@@ -14,7 +14,7 @@ class LHS::Collection < LHS::Proxy
|
|
14
14
|
|
15
15
|
attr_accessor :raw
|
16
16
|
delegate :length, :size, :first, :last, :sample, :[], :present?, :blank?, :empty?,
|
17
|
-
:<<, :push,
|
17
|
+
:<<, :push, to: :raw
|
18
18
|
|
19
19
|
def initialize(raw, parent, record)
|
20
20
|
self.raw = raw
|
@@ -36,6 +36,22 @@ class LHS::Collection < LHS::Proxy
|
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
+
def compact
|
40
|
+
dup.tap do |collection|
|
41
|
+
collection.compact! if collection.raw.present?
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def compact!
|
46
|
+
self.raw = raw.map do |item|
|
47
|
+
if item.is_a?(LHS::Data) && item._request && !item._request.response.success?
|
48
|
+
nil
|
49
|
+
else
|
50
|
+
item
|
51
|
+
end
|
52
|
+
end.compact
|
53
|
+
end
|
54
|
+
|
39
55
|
private
|
40
56
|
|
41
57
|
def cast_item(item)
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support'
|
4
|
+
|
5
|
+
class LHS::Data
|
6
|
+
|
7
|
+
module Extend
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
# Extends already fetched data (self) with additionally
|
11
|
+
# fetched data (addition) using the given key
|
12
|
+
def extend!(addition, key)
|
13
|
+
if collection?
|
14
|
+
extend_collection!(addition, key)
|
15
|
+
elsif self[key]._raw.is_a? Array
|
16
|
+
extend_array!(addition, key)
|
17
|
+
elsif item?
|
18
|
+
extend_item!(addition, key)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def extend_collection!(addition, key)
|
25
|
+
map do |item|
|
26
|
+
item_raw = item._raw[key]
|
27
|
+
item_raw.blank? ? [nil] : item_raw
|
28
|
+
end
|
29
|
+
.flatten
|
30
|
+
.each_with_index do |item, index|
|
31
|
+
item_addition = addition[index]
|
32
|
+
next if item_addition.nil? || item.nil?
|
33
|
+
if item_addition._raw.is_a?(Array)
|
34
|
+
item[items_key] ||= []
|
35
|
+
item[items_key].concat(item_addition._raw)
|
36
|
+
else
|
37
|
+
item.merge! item_addition._raw
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def extend_array!(addition, key)
|
43
|
+
self[key].zip(addition) do |item, additional_item|
|
44
|
+
item._raw.merge!(additional_item._raw) if additional_item.present?
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def extend_item!(addition, key)
|
49
|
+
return if addition.nil?
|
50
|
+
if addition.collection?
|
51
|
+
extend_item_with_collection!(addition, key)
|
52
|
+
else # simple case merges hash into hash
|
53
|
+
_raw[key.to_sym].merge!(addition._raw)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def extend_item_with_collection!(addition, key)
|
58
|
+
target = self[key]
|
59
|
+
if target._raw.is_a? Array
|
60
|
+
self[key] = addition.map(&:_raw)
|
61
|
+
else # hash with items
|
62
|
+
extend_item_with_hash_containing_items!(target, addition)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def extend_item_with_hash_containing_items!(target, addition)
|
67
|
+
LHS::Collection.nest(input: target._raw, value: [], record: self) # inits the nested collection
|
68
|
+
if LHS::Collection.access(input: target._raw, record: self).empty?
|
69
|
+
LHS::Collection.nest(input: target._raw, value: addition.reject { |item| item.nil? }, record: self)
|
70
|
+
else
|
71
|
+
LHS::Collection.access(input: target._raw, record: self).each_with_index do |item, index|
|
72
|
+
item.merge!(addition[index])
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -52,7 +52,7 @@ class LHS::Record
|
|
52
52
|
Chain.new(self, Pagination.new(per: argument))
|
53
53
|
end
|
54
54
|
|
55
|
-
def
|
55
|
+
def rescue(error_class, handler)
|
56
56
|
Chain.new(self, ErrorHandling.new(error_class => handler))
|
57
57
|
end
|
58
58
|
|
@@ -255,7 +255,7 @@ class LHS::Record
|
|
255
255
|
push(Pagination.new(per: argument))
|
256
256
|
end
|
257
257
|
|
258
|
-
def
|
258
|
+
def rescue(error_class, handler)
|
259
259
|
push(ErrorHandling.new(error_class => handler))
|
260
260
|
end
|
261
261
|
|
@@ -348,8 +348,8 @@ class LHS::Record
|
|
348
348
|
def resolved_options
|
349
349
|
options = chain_options
|
350
350
|
options = options.deep_merge(params: chain_parameters.merge(chain_pagination))
|
351
|
-
options = options.merge(
|
352
|
-
options = options.merge(
|
351
|
+
options = options.merge(rescue: chain_error_handler) if chain_error_handler.present?
|
352
|
+
options = options.merge(ignore: chain_ignored_errors) if chain_ignored_errors.present?
|
353
353
|
options = options.merge(including: chain_includes) if chain_includes.present?
|
354
354
|
options = options.merge(referencing: chain_references) if chain_references.present?
|
355
355
|
options
|
@@ -98,73 +98,6 @@ class LHS::Record
|
|
98
98
|
)
|
99
99
|
end
|
100
100
|
|
101
|
-
# Extends existing raw data with additionaly fetched data
|
102
|
-
def extend_raw_data!(data, addition, key)
|
103
|
-
if data.collection?
|
104
|
-
extend_base_collection!(data, addition, key)
|
105
|
-
elsif data[key]._raw.is_a? Array
|
106
|
-
extend_base_array!(data, addition, key)
|
107
|
-
elsif data.item?
|
108
|
-
extend_base_item!(data, addition, key)
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
def extend_base_collection!(data, addition, key)
|
113
|
-
data.map do |item|
|
114
|
-
item_raw = item._raw[key]
|
115
|
-
item_raw.blank? ? [nil] : item_raw
|
116
|
-
end
|
117
|
-
.flatten
|
118
|
-
.each_with_index do |item, index|
|
119
|
-
item_addition = addition[index]
|
120
|
-
next if item_addition.nil? || item.nil?
|
121
|
-
if item_addition._raw.is_a?(Array)
|
122
|
-
extend_base_collection_with_array!(item, item_addition._raw)
|
123
|
-
else
|
124
|
-
item.merge! item_addition._raw
|
125
|
-
end
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
def extend_base_collection_with_array!(item, addition)
|
130
|
-
item[items_key] ||= []
|
131
|
-
item[items_key].concat(addition)
|
132
|
-
end
|
133
|
-
|
134
|
-
def extend_base_array!(data, addition, key)
|
135
|
-
data[key].zip(addition) do |item, additional_item|
|
136
|
-
item._raw.merge!(additional_item._raw) if additional_item.present?
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
def extend_base_item!(data, addition, key)
|
141
|
-
if addition.collection?
|
142
|
-
extend_base_item_with_collection!(data, addition, key)
|
143
|
-
else # simple case merges hash into hash
|
144
|
-
data._raw[key.to_sym].merge!(addition._raw)
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
def extend_base_item_with_collection!(data, addition, key)
|
149
|
-
target = data[key]
|
150
|
-
if target._raw.is_a? Array
|
151
|
-
data[key] = addition.map(&:_raw)
|
152
|
-
else # hash with items
|
153
|
-
extend_base_item_with_hash_of_items!(target, addition)
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
def extend_base_item_with_hash_of_items!(target, addition)
|
158
|
-
LHS::Collection.nest(input: target._raw, value: [], record: self)
|
159
|
-
if LHS::Collection.access(input: target._raw, record: self).empty?
|
160
|
-
LHS::Collection.nest(input: target._raw, value: addition.compact.map(&:_raw), record: self)
|
161
|
-
else
|
162
|
-
LHS::Collection.access(input: target._raw, record: self).each_with_index do |item, index|
|
163
|
-
item.merge!(addition[index])
|
164
|
-
end
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
101
|
def handle_includes(includes, data, references = {})
|
169
102
|
references ||= {}
|
170
103
|
if includes.is_a? Hash
|
@@ -186,7 +119,7 @@ class LHS::Record
|
|
186
119
|
options = options_for_data(data, included)
|
187
120
|
options = extend_with_reference(options, reference)
|
188
121
|
addition = load_include(options, data, sub_includes, reference)
|
189
|
-
|
122
|
+
data.extend!(addition, included)
|
190
123
|
expand_addition!(data, included, options) unless expanded_data?(addition)
|
191
124
|
end
|
192
125
|
end
|
@@ -205,11 +138,11 @@ class LHS::Record
|
|
205
138
|
def expand_addition!(data, included, reference)
|
206
139
|
addition = data[included]
|
207
140
|
options = options_for_data(addition)
|
208
|
-
options = extend_with_reference(options, reference
|
141
|
+
options = extend_with_reference(options, reference)
|
209
142
|
record = record_for_options(options) || self
|
210
143
|
options = convert_options_to_endpoints(options) if record_for_options(options)
|
211
144
|
expanded_data = record.request(options)
|
212
|
-
|
145
|
+
data.extend!(expanded_data, included)
|
213
146
|
end
|
214
147
|
|
215
148
|
def expanded_data?(addition)
|
@@ -217,7 +150,7 @@ class LHS::Record
|
|
217
150
|
if addition.item?
|
218
151
|
(addition._raw.keys - [:href]).any?
|
219
152
|
elsif addition.collection?
|
220
|
-
addition.
|
153
|
+
addition.any? do |item|
|
221
154
|
next if item.blank?
|
222
155
|
if item._raw.is_a?(Hash)
|
223
156
|
(item._raw.keys - [:href]).any?
|
@@ -230,7 +163,8 @@ class LHS::Record
|
|
230
163
|
|
231
164
|
# Extends request options with options provided for this reference
|
232
165
|
def extend_with_reference(options, reference)
|
233
|
-
return options
|
166
|
+
return options if reference.blank?
|
167
|
+
reference = reference.except(:url)
|
234
168
|
options ||= {}
|
235
169
|
if options.is_a?(Array)
|
236
170
|
options.map { |request_options| request_options.merge(reference) if request_options.present? }
|
@@ -344,7 +278,7 @@ class LHS::Record
|
|
344
278
|
end
|
345
279
|
|
346
280
|
# Load additional resources that are requested with include
|
347
|
-
def load_include(options,
|
281
|
+
def load_include(options, _data, sub_includes, references)
|
348
282
|
record = record_for_options(options) || self
|
349
283
|
options = convert_options_to_endpoints(options) if record_for_options(options)
|
350
284
|
prepare_options_for_include_request!(options, sub_includes, references)
|
@@ -364,7 +298,7 @@ class LHS::Record
|
|
364
298
|
|
365
299
|
def load_include_simple!(options, record)
|
366
300
|
data = record.request(options)
|
367
|
-
warn "[WARNING] You included `#{options[:url]}`, but this endpoint is paginated. You might want to use `includes_all` instead of `includes` (https://github.com/local-ch/lhs#includes_all-for-paginated-endpoints)." if paginated?(data._raw)
|
301
|
+
warn "[WARNING] You included `#{options[:url]}`, but this endpoint is paginated. You might want to use `includes_all` instead of `includes` (https://github.com/local-ch/lhs#includes_all-for-paginated-endpoints)." if data && paginated?(data._raw)
|
368
302
|
data
|
369
303
|
end
|
370
304
|
|
@@ -502,7 +436,7 @@ class LHS::Record
|
|
502
436
|
options = options.deep_dup
|
503
437
|
options[:ignored_errors] = ignored_errors if ignored_errors.present?
|
504
438
|
options[:params]&.deep_symbolize_keys!
|
505
|
-
options[:
|
439
|
+
options[:rescue] = merge_error_handlers(options[:rescue]) if options[:rescue]
|
506
440
|
options = (provider_options || {})
|
507
441
|
.deep_merge(endpoint.options || {})
|
508
442
|
.deep_merge(options)
|
data/lib/lhs/data.rb
CHANGED
@@ -6,6 +6,8 @@ class LHS::Data
|
|
6
6
|
'lhs/concerns/data/becomes'
|
7
7
|
autoload :Equality,
|
8
8
|
'lhs/concerns/data/equality'
|
9
|
+
autoload :Extend,
|
10
|
+
'lhs/concerns/data/extend'
|
9
11
|
autoload :Json,
|
10
12
|
'lhs/concerns/data/json'
|
11
13
|
autoload :ToHash,
|
@@ -13,6 +15,7 @@ class LHS::Data
|
|
13
15
|
|
14
16
|
include Becomes
|
15
17
|
include Equality
|
18
|
+
include Extend
|
16
19
|
include Json
|
17
20
|
include ToHash
|
18
21
|
include LHS::Inspect
|
@@ -30,6 +33,7 @@ class LHS::Data
|
|
30
33
|
self._proxy = proxy_from_input(input)
|
31
34
|
self._request = request
|
32
35
|
self._endpoint = endpoint
|
36
|
+
preserve_input_requests!(input)
|
33
37
|
end
|
34
38
|
|
35
39
|
# merging data
|
@@ -97,6 +101,17 @@ class LHS::Data
|
|
97
101
|
|
98
102
|
private
|
99
103
|
|
104
|
+
def preserve_input_requests!(input)
|
105
|
+
if input.is_a?(LHS::Data)
|
106
|
+
_request = input._request if _request.nil?
|
107
|
+
elsif input.is_a?(Array) && input.first.is_a?(LHS::Data)
|
108
|
+
input.each_with_index do |item, index|
|
109
|
+
next if item.nil?
|
110
|
+
self[index]._request = item._request
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
100
115
|
def collection_proxy?(input)
|
101
116
|
(input.is_a?(Hash) && LHS::Collection.access(input: input, record: _record)) ||
|
102
117
|
input.is_a?(Array) ||
|
data/lib/lhs/version.rb
CHANGED
@@ -6,7 +6,7 @@ class ErrorHandlingWithChainsController < ApplicationController
|
|
6
6
|
# in the view (during render 'show')
|
7
7
|
def fetch_in_view
|
8
8
|
@records = DummyRecord
|
9
|
-
.
|
9
|
+
.rescue(LHC::Error, ->(error) { handle_error(error) })
|
10
10
|
.where(color: 'blue')
|
11
11
|
render 'show'
|
12
12
|
render_error if @error
|
@@ -16,7 +16,7 @@ class ErrorHandlingWithChainsController < ApplicationController
|
|
16
16
|
# before the view is rendered
|
17
17
|
def fetch_in_controller
|
18
18
|
@records = DummyRecord
|
19
|
-
.
|
19
|
+
.rescue(LHC::Error, ->(error) { handle_error(error) })
|
20
20
|
.where(color: 'blue').fetch
|
21
21
|
render 'show'
|
22
22
|
render_error if @error
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails_helper'
|
4
|
+
|
5
|
+
describe LHS::Record do
|
6
|
+
before do
|
7
|
+
class Place < LHS::Record
|
8
|
+
endpoint 'http://datastore/places/{id}'
|
9
|
+
end
|
10
|
+
|
11
|
+
class User < LHS::Record
|
12
|
+
endpoint 'http://datastore/users/{id}'
|
13
|
+
end
|
14
|
+
|
15
|
+
stub_request(:get, 'http://datastore/users/123')
|
16
|
+
.to_return(status: 200, body: {
|
17
|
+
href: 'http://datastore/users/123',
|
18
|
+
places: { href: 'http://datastore/users/123/places' }
|
19
|
+
}.to_json)
|
20
|
+
|
21
|
+
stub_request(:get, 'http://datastore/users/123/places?limit=100')
|
22
|
+
.to_return(status: 200, body: {
|
23
|
+
href: 'http://datastore/users/123/places?offset=0&limit=100',
|
24
|
+
items: [
|
25
|
+
{
|
26
|
+
href: 'http://datastore/users/123/places/789'
|
27
|
+
}
|
28
|
+
],
|
29
|
+
total: 4,
|
30
|
+
offset: 0,
|
31
|
+
limit: 10
|
32
|
+
}.to_json)
|
33
|
+
|
34
|
+
stub_request(:get, 'http://datastore/users/123/places/789?limit=100')
|
35
|
+
.to_return(
|
36
|
+
status: 404,
|
37
|
+
body: {
|
38
|
+
status: 404,
|
39
|
+
message: 'The requested resource does not exist.'
|
40
|
+
}.to_json
|
41
|
+
)
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
let(:places) do
|
46
|
+
User
|
47
|
+
.includes(:places)
|
48
|
+
.references(places: { ignore: LHC::NotFound })
|
49
|
+
.find('123')
|
50
|
+
.places
|
51
|
+
end
|
52
|
+
|
53
|
+
context '.compact' do
|
54
|
+
|
55
|
+
it 'removes linked resouces which could not get fetched' do
|
56
|
+
expect(places.compact.length).to eq 0
|
57
|
+
expect(places.length).not_to eq 0 # leaves the original intact
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context '.compact!' do
|
62
|
+
it 'removes linked resouces which could not get fetched' do
|
63
|
+
expect(places.compact!.length).to eq 0
|
64
|
+
expect(places.length).to eq 0 # and changes the original intact
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -15,14 +15,14 @@ describe LHS::Record do
|
|
15
15
|
|
16
16
|
it 'allows to chain error handling' do
|
17
17
|
expect {
|
18
|
-
Record.where(color: 'blue').
|
18
|
+
Record.where(color: 'blue').rescue(LHC::Error, ->(_error) { handler.handle }).first
|
19
19
|
}.not_to raise_error
|
20
20
|
expect(handler).to have_received(:handle)
|
21
21
|
end
|
22
22
|
|
23
23
|
it 'reraises in case chained error is not matched' do
|
24
24
|
expect {
|
25
|
-
Record.where(color: 'blue').
|
25
|
+
Record.where(color: 'blue').rescue(LHC::Conflict, ->(_error) { handler.handle }).first
|
26
26
|
}.to raise_error(LHC::Error)
|
27
27
|
expect(handler).not_to have_received(:handle)
|
28
28
|
end
|
@@ -30,8 +30,8 @@ describe LHS::Record do
|
|
30
30
|
it 'calls all the handlers' do
|
31
31
|
expect {
|
32
32
|
Record.where(color: 'blue')
|
33
|
-
.
|
34
|
-
.
|
33
|
+
.rescue(LHC::Error, ->(_error) { handler.handle_1 })
|
34
|
+
.rescue(LHC::Error, ->(_error) { handler.handle_2 })
|
35
35
|
.first
|
36
36
|
}.not_to raise_error
|
37
37
|
expect(handler).to have_received(:handle_1)
|
@@ -33,7 +33,7 @@ describe LHS::Record do
|
|
33
33
|
it 'applies error handlers from the chain and returns whatever the error handler returns' do
|
34
34
|
stub_request(:get, "http://datastore/records/2").to_return(status: 401)
|
35
35
|
data = Record
|
36
|
-
.
|
36
|
+
.rescue(LHC::Unauthorized, ->(_response) { Record.new(name: 'unknown') })
|
37
37
|
.find(1, 2, 3)
|
38
38
|
expect(data[1].name).to eq 'unknown'
|
39
39
|
end
|
@@ -25,7 +25,7 @@ describe LHS::Record do
|
|
25
25
|
|
26
26
|
it 'allows to pass error_handling for includes to LHC' do
|
27
27
|
handler = ->(_) { return { deleted: true } }
|
28
|
-
record = Record.includes_first_page(:other).references(other: {
|
28
|
+
record = Record.includes_first_page(:other).references(other: { rescue: { LHC::NotFound => handler } }).find(id: 1)
|
29
29
|
|
30
30
|
expect(record.other.deleted).to be(true)
|
31
31
|
end
|
@@ -224,7 +224,7 @@ describe LHS::Record do
|
|
224
224
|
end
|
225
225
|
|
226
226
|
context 'links pointing to nowhere' do
|
227
|
-
|
227
|
+
before do
|
228
228
|
class Feedback < LHS::Record
|
229
229
|
endpoint '{+datastore}/feedbacks'
|
230
230
|
endpoint '{+datastore}/feedbacks/{id}'
|
@@ -238,10 +238,20 @@ describe LHS::Record do
|
|
238
238
|
|
239
239
|
stub_request(:get, "#{datastore}/content-ads/51dfc5690cf271c375c5a12d")
|
240
240
|
.to_return(status: 404)
|
241
|
+
end
|
242
|
+
|
243
|
+
it 'raises LHC::NotFound for links that cannot be included' do
|
244
|
+
expect(-> {
|
245
|
+
Feedback.includes_first_page(campaign: :entry).find(123)
|
246
|
+
}).to raise_error LHC::NotFound
|
247
|
+
end
|
241
248
|
|
242
|
-
|
243
|
-
|
244
|
-
|
249
|
+
it 'ignores LHC::NotFound for links that cannot be included if configured so with reference options' do
|
250
|
+
feedback = Feedback
|
251
|
+
.includes_first_page(campaign: :entry)
|
252
|
+
.references(campaign: { ignore: [LHC::NotFound] })
|
253
|
+
.find(123)
|
254
|
+
expect(feedback.campaign._raw.keys.length).to eq 1
|
245
255
|
end
|
246
256
|
end
|
247
257
|
|
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:
|
4
|
+
version: 24.1.0.pre.1
|
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: 2020-
|
11
|
+
date: 2020-08-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -44,20 +44,20 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 12.1.1
|
48
48
|
- - "<"
|
49
49
|
- !ruby/object:Gem::Version
|
50
|
-
version: '
|
50
|
+
version: '13'
|
51
51
|
type: :runtime
|
52
52
|
prerelease: false
|
53
53
|
version_requirements: !ruby/object:Gem::Requirement
|
54
54
|
requirements:
|
55
55
|
- - ">="
|
56
56
|
- !ruby/object:Gem::Version
|
57
|
-
version:
|
57
|
+
version: 12.1.1
|
58
58
|
- - "<"
|
59
59
|
- !ruby/object:Gem::Version
|
60
|
-
version: '
|
60
|
+
version: '13'
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
62
|
name: local_uri
|
63
63
|
requirement: !ruby/object:Gem::Requirement
|
@@ -274,6 +274,7 @@ files:
|
|
274
274
|
- lib/lhs/concerns/configuration.rb
|
275
275
|
- lib/lhs/concerns/data/becomes.rb
|
276
276
|
- lib/lhs/concerns/data/equality.rb
|
277
|
+
- lib/lhs/concerns/data/extend.rb
|
277
278
|
- lib/lhs/concerns/data/json.rb
|
278
279
|
- lib/lhs/concerns/data/to_hash.rb
|
279
280
|
- lib/lhs/concerns/inspect.rb
|
@@ -471,6 +472,7 @@ files:
|
|
471
472
|
- spec/record/attribute_assignment_spec.rb
|
472
473
|
- spec/record/build_spec.rb
|
473
474
|
- spec/record/cast_nested_data_spec.rb
|
475
|
+
- spec/record/compact_spec.rb
|
474
476
|
- spec/record/create_spec.rb
|
475
477
|
- spec/record/creation_failed_spec.rb
|
476
478
|
- spec/record/custom_setters_spec.rb
|
@@ -548,7 +550,7 @@ files:
|
|
548
550
|
- spec/views/form_for_spec.rb
|
549
551
|
homepage: https://github.com/local-ch/lhs
|
550
552
|
licenses:
|
551
|
-
- GPL-3
|
553
|
+
- GPL-3.0
|
552
554
|
metadata: {}
|
553
555
|
post_install_message:
|
554
556
|
rdoc_options: []
|
@@ -699,6 +701,7 @@ test_files:
|
|
699
701
|
- spec/record/attribute_assignment_spec.rb
|
700
702
|
- spec/record/build_spec.rb
|
701
703
|
- spec/record/cast_nested_data_spec.rb
|
704
|
+
- spec/record/compact_spec.rb
|
702
705
|
- spec/record/create_spec.rb
|
703
706
|
- spec/record/creation_failed_spec.rb
|
704
707
|
- spec/record/custom_setters_spec.rb
|