remote_record 0.1.0 → 0.1.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 +3 -0
- data/lib/remote_record.rb +1 -0
- data/lib/remote_record/base.rb +14 -1
- data/lib/remote_record/config.rb +3 -2
- data/lib/remote_record/transformers.rb +8 -0
- data/lib/remote_record/transformers/base.rb +13 -0
- data/lib/remote_record/transformers/snake_case.rb +27 -0
- data/lib/remote_record/version.rb +1 -1
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a0631c2b3e0edcedb6cbe8b8c82898fcf9fd2c7ccb79f3ed81e636d22c1d7e2
|
4
|
+
data.tar.gz: 903e0ee0541c3f7165f117d5589977b45e6f99c2964541fcd130ebcc602913f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0487c9c421d736e9bb4b3e65d105a7963339c3a05ceb30bf31ebe1fcda345e96aac094f8ed91deeacf3825534e1dbe3079f4a6c5abd40c3ca3c076a73dfa5f1a'
|
7
|
+
data.tar.gz: dfb4cbeca331e5a3ff84465046d055aefb36f52662060b98ab221a385c395c06162e38062be007539594b6643d53e771a179882067377f94339117f256f5c84f
|
data/README.md
CHANGED
@@ -55,6 +55,7 @@ Calling `remote_record` in addition to this lets you set some options:
|
|
55
55
|
| id_field | `:remote_resource_id` | The field on the reference that contains the remote resource ID |
|
56
56
|
| authorization | `''` | An object that can be used by the remote record class to authorize a request. This can be a value, or a proc that returns a value that can be used within the remote record class. |
|
57
57
|
| memoize | true | Whether reference instances should memoize the response that populates them |
|
58
|
+
| transform | [] | Whether the response should be put through a transformer (under RemoteRecord::Transformers). Currently, only `[:snake_case]` is available. |
|
58
59
|
|
59
60
|
```ruby
|
60
61
|
module GitHub
|
@@ -68,6 +69,7 @@ module GitHub
|
|
68
69
|
# c.id_field :remote_resource_id
|
69
70
|
# c.klass RemoteRecord::GitHub::User, # Inferred from module and class name
|
70
71
|
# c.memoize true
|
72
|
+
# c.transform []
|
71
73
|
end
|
72
74
|
end
|
73
75
|
end
|
@@ -85,6 +87,7 @@ class JsonPlaceholderAPIReference < ApplicationRecord
|
|
85
87
|
# c.id_field :remote_resource_id
|
86
88
|
# c.klass RemoteRecord::JsonPlaceholderAPI, # Inferred from module and class name
|
87
89
|
# c.memoize true
|
90
|
+
# c.transform []
|
88
91
|
# end
|
89
92
|
end
|
90
93
|
```
|
data/lib/remote_record.rb
CHANGED
data/lib/remote_record/base.rb
CHANGED
@@ -14,7 +14,7 @@ module RemoteRecord
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def method_missing(method_name, *_args, &_block)
|
17
|
-
@attrs.fetch(method_name)
|
17
|
+
transform(@attrs).fetch(method_name)
|
18
18
|
rescue KeyError
|
19
19
|
super
|
20
20
|
end
|
@@ -33,6 +33,19 @@ module RemoteRecord
|
|
33
33
|
|
34
34
|
private
|
35
35
|
|
36
|
+
def transform(data)
|
37
|
+
transformers.reduce(data) do |data, transformer|
|
38
|
+
transformer.new(data).transform
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Robots in disguise.
|
43
|
+
def transformers
|
44
|
+
@options.transform.map do |transformer_name|
|
45
|
+
"RemoteRecord::Transformers::#{transformer_name.to_s.camelize}".constantize
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
36
49
|
def authorization
|
37
50
|
authz = @options.authorization
|
38
51
|
authz.respond_to?(:call) ? authz.call(@reference, @options) : authz
|
data/lib/remote_record/config.rb
CHANGED
@@ -7,7 +7,7 @@ module RemoteRecord
|
|
7
7
|
# defaults of the remote record class and the overrides set when
|
8
8
|
# `remote_record` is called.
|
9
9
|
class Config
|
10
|
-
OPTIONS = %i[remote_record_class authorization memoize id_field].freeze
|
10
|
+
OPTIONS = %i[remote_record_class authorization memoize id_field transform].freeze
|
11
11
|
|
12
12
|
def initialize(**options)
|
13
13
|
@options = options
|
@@ -17,7 +17,8 @@ module RemoteRecord
|
|
17
17
|
new(
|
18
18
|
authorization: '',
|
19
19
|
memoize: true,
|
20
|
-
id_field: :remote_resource_id
|
20
|
+
id_field: :remote_resource_id,
|
21
|
+
transform: []
|
21
22
|
)
|
22
23
|
end
|
23
24
|
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module RemoteRecord
|
2
|
+
module Transformers
|
3
|
+
# Converts keys to snake case.
|
4
|
+
class SnakeCase < RemoteRecord::Transformers::Base
|
5
|
+
def transform
|
6
|
+
convert_hash_keys(@data)
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def convert_hash_keys(value)
|
12
|
+
case value
|
13
|
+
when Array
|
14
|
+
value.map { |v| convert_hash_keys(v) }
|
15
|
+
when Hash
|
16
|
+
Hash[value.map { |k, v| [underscore_key(k), convert_hash_keys(v)] }]
|
17
|
+
else
|
18
|
+
value
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def underscore_key(k)
|
23
|
+
k.to_s.underscore.to_sym
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: remote_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Simon Fish
|
@@ -195,6 +195,9 @@ files:
|
|
195
195
|
- lib/remote_record/config.rb
|
196
196
|
- lib/remote_record/dsl.rb
|
197
197
|
- lib/remote_record/reference.rb
|
198
|
+
- lib/remote_record/transformers.rb
|
199
|
+
- lib/remote_record/transformers/base.rb
|
200
|
+
- lib/remote_record/transformers/snake_case.rb
|
198
201
|
- lib/remote_record/version.rb
|
199
202
|
homepage: https://github.com/raisedevs/remote_record
|
200
203
|
licenses:
|