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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a744d661d889b63488c0cae5a44803d4215e384f39d7c7d0671cae12c4e04600
4
- data.tar.gz: cef3b04a970a6cf4a48a966beb6e935525a72c2697367f81d2bf64a3ce21f9e9
3
+ metadata.gz: 0a0631c2b3e0edcedb6cbe8b8c82898fcf9fd2c7ccb79f3ed81e636d22c1d7e2
4
+ data.tar.gz: 903e0ee0541c3f7165f117d5589977b45e6f99c2964541fcd130ebcc602913f2
5
5
  SHA512:
6
- metadata.gz: f580bbc02f2ea512f80101a0f4dc7d1b0c293db974c75b820b18e680a48c674a9d266b1542e42eb4988b366a111e30fbf342a59e4e47a3d93ba4658137096e81
7
- data.tar.gz: '0792f2b3d53c925c348b500b768587eb7de9fea53cf80543f6cb3c4bb945749ad4be33006cb49e82fb17b46c53773e7e64bbd31df28988e19a22e4e8426a0109'
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
  ```
@@ -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 |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
@@ -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,8 @@
1
+ require 'remote_record/transformers/base'
2
+ require 'remote_record/transformers/snake_case'
3
+
4
+ module RemoteRecord
5
+ module Transformers
6
+
7
+ end
8
+ end
@@ -0,0 +1,13 @@
1
+ module RemoteRecord
2
+ module Transformers
3
+ class Base
4
+ def initialize(data)
5
+ @data = data
6
+ end
7
+
8
+ def transform
9
+ raise NotImplementedError
10
+ end
11
+ end
12
+ end
13
+ end
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RemoteRecord
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
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.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: