remote_record 0.1.0 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a744d661d889b63488c0cae5a44803d4215e384f39d7c7d0671cae12c4e04600
4
- data.tar.gz: cef3b04a970a6cf4a48a966beb6e935525a72c2697367f81d2bf64a3ce21f9e9
3
+ metadata.gz: 285bbd2e1d94c57c73399a83c336404b2eb190e4b08e14287671152cf36dc866
4
+ data.tar.gz: 9b3ce895695588e43516e86a9498ed64d33ad76771afa13e739835dc12175abb
5
5
  SHA512:
6
- metadata.gz: f580bbc02f2ea512f80101a0f4dc7d1b0c293db974c75b820b18e680a48c674a9d266b1542e42eb4988b366a111e30fbf342a59e4e47a3d93ba4658137096e81
7
- data.tar.gz: '0792f2b3d53c925c348b500b768587eb7de9fea53cf80543f6cb3c4bb945749ad4be33006cb49e82fb17b46c53773e7e64bbd31df28988e19a22e4e8426a0109'
6
+ metadata.gz: d9105b2bd550f9553e2c04c8caa2a1ca1933628e51c3ccf318bc19c62721be4302d1ca490f29ab194dee971642ebb045f102b8e5a8179e3362cb630c7ae8db0a
7
+ data.tar.gz: 24966ceeb7d708fedaf6413e98496c485f77314d1837ca15cc5d7db9cd1d2e6ed0f3c06d50f7f709cc77f430e36b3f66d18e09e3970b246813b3e2e1c057d528
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
  ```
@@ -106,3 +109,8 @@ By default, this'll only make a request on initialize. For services that manage
106
109
  caching by way of expiry or ETags, I recommend using `faraday-http-cache` for
107
110
  your clients and setting `memoize` to `false`. Remote Record will eventually
108
111
  gain support for caching.
112
+
113
+ ### Forcing a fresh request
114
+
115
+ You might want to force a fresh request in some instances, even if you're using
116
+ `memoize`. To do this, call `fresh` on a reference, and it'll be repopulated.
@@ -7,6 +7,7 @@ require 'remote_record/config'
7
7
  require 'remote_record/dsl'
8
8
  require 'remote_record/reference'
9
9
  require 'remote_record/version'
10
+ require 'remote_record/transformers'
10
11
 
11
12
  # Generic interface for resources stored on external services.
12
13
  module RemoteRecord
@@ -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 |transformed_data, transformer|
38
+ transformer.new(transformed_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
@@ -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
 
@@ -25,8 +25,12 @@ module RemoteRecord
25
25
  end
26
26
  end
27
27
 
28
+ # rubocop:disable Metrics/BlockLength
28
29
  included do
30
+ attr_accessor :fetching
31
+
29
32
  after_initialize do |reference|
33
+ reference.fetching = true if reference.fetching.nil?
30
34
  config = reference.class.remote_record_class.default_config.merge(
31
35
  reference.class.remote_record_config.to_h
32
36
  )
@@ -39,20 +43,20 @@ module RemoteRecord
39
43
  def method_missing(method_name, *_args, &_block)
40
44
  fetch_remote_resource unless @remote_record_config.memoize
41
45
 
42
- @instance.public_send(method_name)
46
+ instance.public_send(method_name)
43
47
  end
44
48
 
45
49
  def respond_to_missing?(method_name, _include_private = false)
46
50
  instance.respond_to?(method_name, false)
47
51
  end
48
52
 
49
- def initialize(**args)
50
- @attrs = HashWithIndifferentAccess.new
51
- super
53
+ def fetch_remote_resource
54
+ instance.fetch if fetching
52
55
  end
53
56
 
54
- def fetch_remote_resource
57
+ def fresh
55
58
  instance.fetch
59
+ self
56
60
  end
57
61
 
58
62
  private
@@ -61,5 +65,6 @@ module RemoteRecord
61
65
  @instance ||= @remote_record_config.remote_record_class.new(self, @remote_record_config)
62
66
  end
63
67
  end
68
+ # rubocop:enable Metrics/BlockLength
64
69
  end
65
70
  end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'remote_record/transformers/base'
4
+ require 'remote_record/transformers/snake_case'
5
+
6
+ module RemoteRecord
7
+ # Classes responsible for transforming the hash of data returned by API calls.
8
+ module Transformers
9
+ end
10
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RemoteRecord
4
+ module Transformers
5
+ # Base transformer class. Inherit from this and implement `#transform`.
6
+ class Base
7
+ def initialize(data)
8
+ @data = data
9
+ end
10
+
11
+ def transform
12
+ raise NotImplementedError
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RemoteRecord
4
+ module Transformers
5
+ # Converts keys to snake case.
6
+ class SnakeCase < RemoteRecord::Transformers::Base
7
+ def transform
8
+ convert_hash_keys(@data)
9
+ end
10
+
11
+ private
12
+
13
+ def convert_hash_keys(value)
14
+ case value
15
+ when Array
16
+ value.map { |v| convert_hash_keys(v) }
17
+ when Hash
18
+ Hash[value.map { |k, v| [underscore_key(k), convert_hash_keys(v)] }]
19
+ else
20
+ value
21
+ end
22
+ end
23
+
24
+ def underscore_key(key)
25
+ key.to_s.underscore.to_sym
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RemoteRecord
4
- VERSION = '0.1.0'
4
+ VERSION = '0.4.0'
5
5
  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.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Fish
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-12-03 00:00:00.000000000 Z
12
+ date: 2021-01-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -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: