remote_record 0.1.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +25 -2
- data/lib/remote_record.rb +1 -0
- data/lib/remote_record/base.rb +4 -2
- data/lib/remote_record/reference.rb +13 -5
- data/lib/remote_record/transformers.rb +3 -1
- data/lib/remote_record/transformers/base.rb +3 -0
- data/lib/remote_record/transformers/snake_case.rb +4 -2
- data/lib/remote_record/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4bf471bd03525a3f6dfdfedc1d88e2681264c85e98447607259fec7a94d56ddd
|
4
|
+
data.tar.gz: 076113352a325ee80c09cca58a00cdd8e703a584392319f008393fdae90ddbc2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3dc11719e885adf6220ef9b351b1605943ce25220c3e95ee0ee8820175685783777ed7f1bd43f22249f83cb95e8031509e2b85f8be04b65dc33aaab0c4fdca22
|
7
|
+
data.tar.gz: 478180ddc5b281c7a9873313608ad62368f9c947125fd1095b1c61a88e9cce5147637ca1391c801a0af1c2f53e4ac34418256a38563893f926a4f55ee37d62b4
|
data/README.md
CHANGED
@@ -1,6 +1,20 @@
|
|
1
|
-
|
1
|
+
![RemoteRecord: Ready-made remote resource structures.](doc/header.svg)
|
2
2
|
|
3
|
-
|
3
|
+
---
|
4
|
+
|
5
|
+
![Remote Record](https://github.com/raisedevs/remote_record/workflows/Remote%20Record/badge.svg)
|
6
|
+
[![Gem Version](https://badge.fury.io/rb/remote_record.svg)](https://badge.fury.io/rb/remote_record)
|
7
|
+
|
8
|
+
Every API speaks a different language. Maybe it's REST, maybe it's SOAP, maybe
|
9
|
+
it's GraphQL. Maybe it's got its own Ruby client, or maybe you need to roll your
|
10
|
+
own. But what if you could just pretend it existed in your database?
|
11
|
+
|
12
|
+
RemoteRecord provides a consistent ActiveRecord inspired interface for all of
|
13
|
+
your application's APIs. Store remote resources by ID, and RemoteRecord will
|
14
|
+
auto-populate instances of your ActiveRecord model with their attributes from
|
15
|
+
the API. Whether you're dealing with a user on GitHub, a track on Spotify, a
|
16
|
+
place on Google Maps, or a resource on your internal infrastructure, you can use
|
17
|
+
RemoteRecord to wrap fetching it.
|
4
18
|
|
5
19
|
## Setup
|
6
20
|
|
@@ -109,3 +123,12 @@ By default, this'll only make a request on initialize. For services that manage
|
|
109
123
|
caching by way of expiry or ETags, I recommend using `faraday-http-cache` for
|
110
124
|
your clients and setting `memoize` to `false`. Remote Record will eventually
|
111
125
|
gain support for caching.
|
126
|
+
|
127
|
+
### Forcing a fresh request
|
128
|
+
|
129
|
+
You might want to force a fresh request in some instances, even if you're using
|
130
|
+
`memoize`. To do this, call `fresh` on a reference, and it'll be repopulated.
|
131
|
+
|
132
|
+
### Skip fetching
|
133
|
+
|
134
|
+
You might not want to make a request on initialize sometimes. In this case, pass `fetching: false` to your query or `new` to make sure the resource isn't fetched.
|
data/lib/remote_record.rb
CHANGED
data/lib/remote_record/base.rb
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
module RemoteRecord
|
4
4
|
# Remote record classes should inherit from this class and define #get.
|
5
5
|
class Base
|
6
|
+
include ActiveSupport::Rescuable
|
7
|
+
|
6
8
|
def self.default_config
|
7
9
|
Config.defaults.merge(remote_record_class: self)
|
8
10
|
end
|
@@ -34,8 +36,8 @@ module RemoteRecord
|
|
34
36
|
private
|
35
37
|
|
36
38
|
def transform(data)
|
37
|
-
transformers.reduce(data) do |
|
38
|
-
transformer.new(
|
39
|
+
transformers.reduce(data) do |transformed_data, transformer|
|
40
|
+
transformer.new(transformed_data).transform
|
39
41
|
end
|
40
42
|
end
|
41
43
|
|
@@ -25,8 +25,13 @@ module RemoteRecord
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
+
# rubocop:disable Metrics/BlockLength
|
28
29
|
included do
|
30
|
+
include ActiveSupport::Rescuable
|
31
|
+
attr_accessor :fetching
|
32
|
+
|
29
33
|
after_initialize do |reference|
|
34
|
+
reference.fetching = true if reference.fetching.nil?
|
30
35
|
config = reference.class.remote_record_class.default_config.merge(
|
31
36
|
reference.class.remote_record_config.to_h
|
32
37
|
)
|
@@ -39,20 +44,22 @@ module RemoteRecord
|
|
39
44
|
def method_missing(method_name, *_args, &_block)
|
40
45
|
fetch_remote_resource unless @remote_record_config.memoize
|
41
46
|
|
42
|
-
|
47
|
+
instance.public_send(method_name)
|
43
48
|
end
|
44
49
|
|
45
50
|
def respond_to_missing?(method_name, _include_private = false)
|
46
51
|
instance.respond_to?(method_name, false)
|
47
52
|
end
|
48
53
|
|
49
|
-
def
|
50
|
-
|
51
|
-
|
54
|
+
def fetch_remote_resource
|
55
|
+
instance.fetch if fetching
|
56
|
+
rescue Exception => e # rubocop:disable Lint/RescueException
|
57
|
+
rescue_with_handler(e) || raise
|
52
58
|
end
|
53
59
|
|
54
|
-
def
|
60
|
+
def fresh
|
55
61
|
instance.fetch
|
62
|
+
self
|
56
63
|
end
|
57
64
|
|
58
65
|
private
|
@@ -61,5 +68,6 @@ module RemoteRecord
|
|
61
68
|
@instance ||= @remote_record_config.remote_record_class.new(self, @remote_record_config)
|
62
69
|
end
|
63
70
|
end
|
71
|
+
# rubocop:enable Metrics/BlockLength
|
64
72
|
end
|
65
73
|
end
|
@@ -1,8 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'remote_record/transformers/base'
|
2
4
|
require 'remote_record/transformers/snake_case'
|
3
5
|
|
4
6
|
module RemoteRecord
|
7
|
+
# Classes responsible for transforming the hash of data returned by API calls.
|
5
8
|
module Transformers
|
6
|
-
|
7
9
|
end
|
8
10
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module RemoteRecord
|
2
4
|
module Transformers
|
3
5
|
# Converts keys to snake case.
|
@@ -19,8 +21,8 @@ module RemoteRecord
|
|
19
21
|
end
|
20
22
|
end
|
21
23
|
|
22
|
-
def underscore_key(
|
23
|
-
|
24
|
+
def underscore_key(key)
|
25
|
+
key.to_s.underscore.to_sym
|
24
26
|
end
|
25
27
|
end
|
26
28
|
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.
|
4
|
+
version: 0.5.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:
|
12
|
+
date: 2021-01-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|