lhs 19.7.0 → 19.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.
- checksums.yaml +4 -4
- data/README.md +39 -0
- data/lib/lhs/concerns/record/provider.rb +23 -0
- data/lib/lhs/concerns/record/request.rb +3 -1
- data/lib/lhs/record.rb +3 -0
- data/lib/lhs/version.rb +1 -1
- data/spec/record/provider_spec.rb +41 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a6e339382991cfdddacfa4fe2c8e63b3fe03e6a25f897d7d2d0d87cdc3ebd28f
|
4
|
+
data.tar.gz: 65ecc97eac88e673a47bca919d7573c874806a13cb487a787100afffccf71ec9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 407bc810256e1503ab507385ef10e5711c47935ca7f2d070da8c5298c889a198bbd86401bd0206d3c3bef9c9ba4ad76d66654bce8e451a5ad278361488bba39b
|
7
|
+
data.tar.gz: 35d7f32e5eb28a95071b63d26d2c6211b37ea1354d90fb2c9e740fe93856e723ccaacddc5982f9663515261eac712454cf1443fef24b53ee660f82a75689198a
|
data/README.md
CHANGED
@@ -46,6 +46,7 @@ record.review # "Lunch was great
|
|
46
46
|
* [Endpoints](#endpoints)
|
47
47
|
* [Configure endpoint hosts](#configure-endpoint-hosts)
|
48
48
|
* [Endpoint Priorities](#endpoint-priorities)
|
49
|
+
* [Provider](#provider)
|
49
50
|
* [Record inheritance](#record-inheritance)
|
50
51
|
* [Find multiple records](#find-multiple-records)
|
51
52
|
* [fetch](#fetch)
|
@@ -260,6 +261,44 @@ GET https://service.example.com/records
|
|
260
261
|
|
261
262
|
**Be aware that, if you configure ambigious endpoints accross multiple classes, the order of things is not deteministic. Ambigious endpoints accross multiple classes need to be avoided.**
|
262
263
|
|
264
|
+
### Provider
|
265
|
+
|
266
|
+
Providers in LHS allow you to group shared endpoint options under a common provider.
|
267
|
+
|
268
|
+
```ruby
|
269
|
+
# app/models/provider/base_record.rb
|
270
|
+
|
271
|
+
module Provider
|
272
|
+
class BaseRecord < LHS::Record
|
273
|
+
provider params: { api_key: 123 }
|
274
|
+
end
|
275
|
+
end
|
276
|
+
```
|
277
|
+
|
278
|
+
Now every record, part of that particular provider can inherit the provider's `BaseRecord`.
|
279
|
+
|
280
|
+
```ruby
|
281
|
+
# app/models/provider/account.rb
|
282
|
+
|
283
|
+
module Provider
|
284
|
+
class Account < BaseRecord
|
285
|
+
endpoint '{+host}/records'
|
286
|
+
endpoint '{+host}/records/{id}'
|
287
|
+
end
|
288
|
+
end
|
289
|
+
```
|
290
|
+
|
291
|
+
```ruby
|
292
|
+
# app/controllers/some_controller.rb
|
293
|
+
|
294
|
+
Provider::Account.find(1)
|
295
|
+
```
|
296
|
+
```
|
297
|
+
GET https://provider/records/1?api_key=123
|
298
|
+
```
|
299
|
+
|
300
|
+
And requests made via those provider records apply the common provider options.
|
301
|
+
|
263
302
|
### Record inheritance
|
264
303
|
|
265
304
|
You can inherit from previously defined records and also inherit endpoints that way:
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_support/core_ext'
|
5
|
+
|
6
|
+
class LHS::Record
|
7
|
+
|
8
|
+
# A provider can define options used for that specific provider
|
9
|
+
module Provider
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
|
12
|
+
included do
|
13
|
+
class_attribute :provider_options unless defined? provider_options
|
14
|
+
self.provider_options = nil
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
def provider(options = nil)
|
19
|
+
self.provider_options = options
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -495,7 +495,9 @@ class LHS::Record
|
|
495
495
|
options[:ignored_errors] = ignored_errors if ignored_errors.present?
|
496
496
|
options[:params]&.deep_symbolize_keys!
|
497
497
|
options[:error_handler] = merge_error_handlers(options[:error_handler]) if options[:error_handler]
|
498
|
-
options = (
|
498
|
+
options = (provider_options || {})
|
499
|
+
.deep_merge(endpoint.options || {})
|
500
|
+
.deep_merge(options)
|
499
501
|
options[:url] = compute_url!(options[:params]) unless options.key?(:url)
|
500
502
|
merge_explicit_params!(options[:params])
|
501
503
|
options.delete(:params) if options[:params]&.empty?
|
data/lib/lhs/record.rb
CHANGED
@@ -33,6 +33,8 @@ class LHS::Record
|
|
33
33
|
'lhs/concerns/record/model'
|
34
34
|
autoload :Pagination,
|
35
35
|
'lhs/concerns/record/pagination'
|
36
|
+
autoload :Provider,
|
37
|
+
'lhs/concerns/record/provider'
|
36
38
|
autoload :Request,
|
37
39
|
'lhs/concerns/record/request'
|
38
40
|
autoload :Relations,
|
@@ -69,6 +71,7 @@ class LHS::Record
|
|
69
71
|
include Merge
|
70
72
|
include Model
|
71
73
|
include Pagination
|
74
|
+
include Provider
|
72
75
|
include Request
|
73
76
|
include Relations
|
74
77
|
include RequestCycleCache
|
data/lib/lhs/version.rb
CHANGED
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails_helper'
|
4
|
+
|
5
|
+
describe LHS::Record do
|
6
|
+
context 'provider' do
|
7
|
+
|
8
|
+
before do
|
9
|
+
module Provider
|
10
|
+
class BaseRecord < LHS::Record
|
11
|
+
provider params: { api_key: 123 }
|
12
|
+
end
|
13
|
+
|
14
|
+
class Record < Provider::BaseRecord
|
15
|
+
endpoint 'http://provider/records'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class AnotherRecord < LHS::Record
|
20
|
+
endpoint 'http://other_provider/records'
|
21
|
+
end
|
22
|
+
|
23
|
+
stub_request(:get, "http://provider/records?id=1&api_key=123")
|
24
|
+
.to_return(body: { name: 'Steve' }.to_json)
|
25
|
+
|
26
|
+
stub_request(:get, "http://other_provider/records?id=1")
|
27
|
+
.to_return(body: { name: 'Not Steve' }.to_json)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'applies provider options when making requests to that provider' do
|
31
|
+
record = Provider::Record.find(1)
|
32
|
+
expect(record.name).to eq 'Steve'
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'does not apply provider options when making requests to other records' do
|
36
|
+
Provider::Record.find(1)
|
37
|
+
record = AnotherRecord.find(1)
|
38
|
+
expect(record.name).to eq 'Not Steve'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
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: 19.
|
4
|
+
version: 19.8.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: 2019-07-
|
11
|
+
date: 2019-07-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -257,6 +257,7 @@ files:
|
|
257
257
|
- lib/lhs/concerns/record/merge.rb
|
258
258
|
- lib/lhs/concerns/record/model.rb
|
259
259
|
- lib/lhs/concerns/record/pagination.rb
|
260
|
+
- lib/lhs/concerns/record/provider.rb
|
260
261
|
- lib/lhs/concerns/record/relations.rb
|
261
262
|
- lib/lhs/concerns/record/request.rb
|
262
263
|
- lib/lhs/concerns/record/request_cycle_cache/interceptor.rb
|
@@ -445,6 +446,7 @@ files:
|
|
445
446
|
- spec/record/pagination_links_spec.rb
|
446
447
|
- spec/record/pagination_spec.rb
|
447
448
|
- spec/record/persisted_spec.rb
|
449
|
+
- spec/record/provider_spec.rb
|
448
450
|
- spec/record/references_spec.rb
|
449
451
|
- spec/record/relation_caching_spec.rb
|
450
452
|
- spec/record/reload_by_id_spec.rb
|
@@ -656,6 +658,7 @@ test_files:
|
|
656
658
|
- spec/record/pagination_links_spec.rb
|
657
659
|
- spec/record/pagination_spec.rb
|
658
660
|
- spec/record/persisted_spec.rb
|
661
|
+
- spec/record/provider_spec.rb
|
659
662
|
- spec/record/references_spec.rb
|
660
663
|
- spec/record/relation_caching_spec.rb
|
661
664
|
- spec/record/reload_by_id_spec.rb
|