consyncful 0.4.0 → 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/.rubocop_todo.yml +13 -11
- data/.ruby-version +1 -1
- data/Gemfile.lock +1 -1
- data/README.md +27 -31
- data/consyncful.gemspec +2 -2
- data/lib/consyncful.rb +1 -1
- data/lib/consyncful/base.rb +3 -0
- data/lib/consyncful/item_mapper.rb +31 -12
- data/lib/consyncful/persisted_item.rb +3 -1
- data/lib/consyncful/stats.rb +2 -0
- data/lib/consyncful/sync.rb +16 -2
- data/lib/consyncful/version.rb +1 -1
- metadata +9 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 14eda0769dbcdc516d59a9a4252fafb23eb6380ce0224a276e101f81d649c950
|
4
|
+
data.tar.gz: aa4aaf421824043dbea600c1cfabca2633a8d3470ed2a14aba5685b8bf8acd90
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ac1a66dda08d88396d83c736308516a798503afa519cd673e76f668d771dc933b6068a2e03c1f75926ec8980ad273c0936af6e2058d84d5919a250a498ebbd7
|
7
|
+
data.tar.gz: 260c353fe5eb6c646473830ecdec0a094016bf0f0e518ef357ceb7cc03862f38e76e4cccbcf656a4c368fad72641ef60788d13a5c76e768a5b8fc9652b375c96
|
data/.rubocop_todo.yml
CHANGED
@@ -1,22 +1,29 @@
|
|
1
1
|
# This configuration was generated by
|
2
2
|
# `rubocop --auto-gen-config`
|
3
|
-
# on
|
3
|
+
# on 2021-03-01 10:54:42 +1300 using RuboCop version 0.79.0.
|
4
4
|
# The point is for the user to remove these configuration records
|
5
5
|
# one by one as the offenses are removed from the code base.
|
6
6
|
# Note that changes in the inspected code, or installation of new
|
7
7
|
# versions of RuboCop, may require this file to be generated again.
|
8
8
|
|
9
|
+
# Offense count: 1
|
10
|
+
# Cop supports --auto-correct.
|
11
|
+
# Configuration parameters: AllowInHeredoc.
|
12
|
+
Layout/TrailingWhitespace:
|
13
|
+
Exclude:
|
14
|
+
- 'lib/consyncful/item_mapper.rb'
|
15
|
+
|
9
16
|
# Offense count: 1
|
10
17
|
Metrics/AbcSize:
|
11
18
|
Max: 19
|
12
19
|
|
13
|
-
# Offense count:
|
20
|
+
# Offense count: 10
|
14
21
|
# Configuration parameters: CountComments, ExcludedMethods.
|
15
22
|
# ExcludedMethods: refine
|
16
23
|
Metrics/BlockLength:
|
17
|
-
Max:
|
24
|
+
Max: 175
|
18
25
|
|
19
|
-
# Offense count:
|
26
|
+
# Offense count: 3
|
20
27
|
# Cop supports --auto-correct.
|
21
28
|
# Configuration parameters: AutoCorrect, EnforcedStyle.
|
22
29
|
# SupportedStyles: nested, compact
|
@@ -24,21 +31,16 @@ Style/ClassAndModuleChildren:
|
|
24
31
|
Exclude:
|
25
32
|
- 'lib/consyncful/railtie.rb'
|
26
33
|
- 'spec/consyncful/base_spec.rb'
|
27
|
-
- 'spec/consyncful/sync_spec.rb'
|
28
34
|
|
29
|
-
# Offense count:
|
35
|
+
# Offense count: 3
|
30
36
|
Style/Documentation:
|
31
37
|
Exclude:
|
32
38
|
- 'spec/**/*'
|
33
39
|
- 'test/**/*'
|
34
40
|
- 'lib/consyncful.rb'
|
35
|
-
- 'lib/consyncful/base.rb'
|
36
|
-
- 'lib/consyncful/item_mapper.rb'
|
37
41
|
- 'lib/consyncful/railtie.rb'
|
38
|
-
- 'lib/consyncful/stats.rb'
|
39
|
-
- 'lib/consyncful/sync.rb'
|
40
42
|
|
41
|
-
# Offense count:
|
43
|
+
# Offense count: 56
|
42
44
|
# Cop supports --auto-correct.
|
43
45
|
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
44
46
|
# URISchemes: http, https
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.7.1
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -2,18 +2,15 @@
|
|
2
2
|
|
3
3
|
Contentful -> local database synchronisation for Rails
|
4
4
|
|
5
|
-
Requesting complicated models from the Contentful Delivery API in Rails applications is often
|
6
|
-
too slow, and makes testing applications painful. Consyncful uses Contentful's syncronisation API
|
7
|
-
to keep a local copy of the entire content in a Mongo database up to date.
|
5
|
+
Requesting complicated models from the Contentful Delivery API in Rails applications is often too slow, and makes testing applications painful. Consyncful uses Contentful's synchronisation API to keep a local, up-to-date copy of the entire content in a Mongo database.
|
8
6
|
|
9
|
-
Once the content is
|
10
|
-
using [Mongoid](https://docs.mongodb.com/mongoid/current/tutorials/mongoid-documents/) ODM.
|
7
|
+
Once the content is available locally, finding and interact with contentful data is as easy as using [Mongoid](https://docs.mongodb.com/mongoid/current/tutorials/mongoid-documents/) ODM.
|
11
8
|
|
12
|
-
This gem doesn't provide any
|
9
|
+
This gem doesn't provide any integration with the management API, or any way to update Contentful models from the local store. It is strictly read only.
|
13
10
|
|
14
11
|
## Why do I have to use MongoDB?
|
15
12
|
|
16
|
-
Consyncful currently only supports Mongoid ODM because models have dynamic schemas. And that's all we've had a chance to work out so far. :)
|
13
|
+
Consyncful currently only supports Mongoid ODM because models have dynamic schemas. And that's all we've had a chance to work out so far. :)
|
17
14
|
The same pattern might be able to be extended to work with ActiveRecord, but having to migrate the local database as well as your contentful content type's seems tedious.
|
18
15
|
|
19
16
|
## Installation
|
@@ -28,12 +25,13 @@ And then execute:
|
|
28
25
|
|
29
26
|
$ bundle
|
30
27
|
|
31
|
-
If you don't already use
|
28
|
+
If you don't already use Mongoid, generate a mongoid.yml by running:
|
32
29
|
|
33
30
|
$ rake g mongoid:config
|
34
31
|
|
35
32
|
Add an initializer:
|
36
|
-
|
33
|
+
|
34
|
+
Consyncful uses [contentful.rb](https://github.com/contentful/contentful.rb); client options are as documented there.
|
37
35
|
```ruby
|
38
36
|
Consyncful.configure do |config|
|
39
37
|
config.locale = 'en-NZ'
|
@@ -49,7 +47,7 @@ Consyncful uses [contentful.rb](https://github.com/contentful/contentful.rb) so
|
|
49
47
|
|
50
48
|
## Usage
|
51
49
|
|
52
|
-
### Creating contentful models in your
|
50
|
+
### Creating contentful models in your Rails app
|
53
51
|
|
54
52
|
Create models by inheriting from `Consyncful::Base`
|
55
53
|
|
@@ -59,9 +57,9 @@ class ModelName < Consyncful::Base
|
|
59
57
|
end
|
60
58
|
```
|
61
59
|
|
62
|
-
Model fields will be
|
60
|
+
Model fields will be dynamically assigned, but Mongoid dynamic fields are not accessible if the entry has an empty field. If you want the accessor methods to be reliably available for fields it is recommended to define the fields in the model:
|
63
61
|
|
64
|
-
```ruby
|
62
|
+
```ruby
|
65
63
|
class ModelName < Consyncful::Base
|
66
64
|
contentful_model_name 'contentfulTypeName'
|
67
65
|
|
@@ -70,9 +68,9 @@ class ModelName < Consyncful::Base
|
|
70
68
|
end
|
71
69
|
```
|
72
70
|
|
73
|
-
Contentful reference fields are a bit special compared with standard
|
71
|
+
Contentful reference fields are a bit special compared with standard Mongoid associations. Consyncful provides the following helpers to set up the correct relationships:
|
74
72
|
|
75
|
-
```ruby
|
73
|
+
```ruby
|
76
74
|
class ModelWithReferences < Consyncful::Base
|
77
75
|
contentful_model_name 'contentfulTypeName'
|
78
76
|
|
@@ -81,26 +79,26 @@ class ModelWithReferences < Consyncful::Base
|
|
81
79
|
end
|
82
80
|
```
|
83
81
|
|
84
|
-
###
|
82
|
+
### Synchronizing contentful data
|
85
83
|
|
86
|
-
To run a
|
84
|
+
To run a synchronization process run:
|
87
85
|
|
88
86
|
$ rake consyncful:sync
|
89
87
|
|
90
|
-
The first time you run this it will download all the
|
88
|
+
The first time you run this it will download all the Contentful content. It will then check every 15 seconds for changes to the content and update/delete records in the database when changes are made in Contentful.
|
91
89
|
|
92
|
-
If you want to
|
90
|
+
If you want to synchronise from scratch, run:
|
93
91
|
|
94
92
|
$ rake consyncful:refresh
|
95
93
|
|
96
94
|
It is recommended to refresh your data if you change model names.
|
97
95
|
|
98
|
-
Now you've synced your data, it is all available via your
|
96
|
+
Now you've synced your data, it is all available via your Rails models.
|
99
97
|
|
100
98
|
### Finding and interacting with models
|
101
99
|
|
102
100
|
#### Querying
|
103
|
-
Models are available using standard
|
101
|
+
Models are available using standard Mongoid [queries](https://docs.mongodb.com/mongoid/current/tutorials/mongoid-queries/).
|
104
102
|
|
105
103
|
```ruby
|
106
104
|
instance = ModelName.find_by(instance: 'foo')
|
@@ -109,7 +107,7 @@ instance.is_awesome # true
|
|
109
107
|
```
|
110
108
|
|
111
109
|
#### References
|
112
|
-
References work like you
|
110
|
+
References work like you would expect:
|
113
111
|
|
114
112
|
```ruby
|
115
113
|
|
@@ -120,23 +118,23 @@ instance.other_things # all the referenced things, polymorphic, so might be diff
|
|
120
118
|
```
|
121
119
|
|
122
120
|
**Except**:
|
123
|
-
`references_many` associations return objects in a different order from how they are ordered in
|
121
|
+
`references_many` associations return objects in a different order from how they are ordered in Contentful. If you want them in the order they appear in Contentful, use the `.in_order` helper:
|
124
122
|
|
125
123
|
```ruby
|
126
|
-
instance.other_things.in_order # ordered the same as in
|
124
|
+
instance.other_things.in_order # ordered the same as in Contentful
|
127
125
|
```
|
128
126
|
|
129
|
-
#### Finding
|
127
|
+
#### Finding entries from different content types
|
130
128
|
|
131
|
-
Because all
|
129
|
+
Because all Contentful models are stored as polymorphic subtypes of `Consyncful::Base`, you can query all entries without knowing what type you are looking for:
|
132
130
|
|
133
131
|
```ruby
|
134
132
|
Consyncful::Base.where(title: 'a title') # [ #<ModelName>, #<OtherModelName> ]
|
135
133
|
```
|
136
134
|
|
137
|
-
### Sync callbacks
|
135
|
+
### Sync callbacks
|
138
136
|
|
139
|
-
You may want to attach some application logic to happen before or after a sync run, for example to update caches
|
137
|
+
You may want to attach some application logic to happen before or after a sync run, for example to update caches.
|
140
138
|
|
141
139
|
Callbacks can be registered using:
|
142
140
|
|
@@ -152,11 +150,9 @@ Consyncful::Sync.after_run do |updated_ids|
|
|
152
150
|
end
|
153
151
|
```
|
154
152
|
|
155
|
-
|
156
|
-
|
157
|
-
### Locales
|
153
|
+
### Using Locales for specific fields
|
158
154
|
|
159
|
-
|
155
|
+
If fields have multiple locales then the default locale will be mapped to the field name. Additional locales will have a suffix (lower snake case) on the field name. e.g title (default), title_mi_nz (New Zealand Maori mi-NZ)
|
160
156
|
|
161
157
|
## Development
|
162
158
|
|
data/consyncful.gemspec
CHANGED
@@ -7,8 +7,8 @@ require 'consyncful/version'
|
|
7
7
|
Gem::Specification.new do |spec|
|
8
8
|
spec.name = 'consyncful'
|
9
9
|
spec.version = Consyncful::VERSION
|
10
|
-
spec.authors = ['Andy Anastasiadis-Gray', 'Montgomery Anderson']
|
11
|
-
spec.email = ['andy@boost.co.nz', 'montgomery@boost.co.nz']
|
10
|
+
spec.authors = ['Andy Anastasiadis-Gray', 'Montgomery Anderson', 'Greg Rogan']
|
11
|
+
spec.email = ['andy@boost.co.nz', 'montgomery@boost.co.nz', 'greg@boost.co.nz']
|
12
12
|
|
13
13
|
spec.summary = 'Contentful to local database synchronisation for Rails'
|
14
14
|
spec.homepage = 'https://github.com/boost/consyncful'
|
data/lib/consyncful.rb
CHANGED
data/lib/consyncful/base.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Consyncful
|
4
|
+
##
|
5
|
+
# Responsible for mapping an update received from Contentful's syncronisation API
|
6
|
+
# into useful fields for Consyncful::PersistedItem to store in the database.
|
4
7
|
class ItemMapper
|
5
8
|
def initialize(item)
|
6
9
|
@item = item
|
@@ -22,17 +25,11 @@ module Consyncful
|
|
22
25
|
@item.id
|
23
26
|
end
|
24
27
|
|
25
|
-
def mapped_fields(
|
28
|
+
def mapped_fields(default_locale)
|
26
29
|
fields = generic_fields
|
27
30
|
|
28
|
-
|
29
|
-
|
30
|
-
next if value.is_a? Contentful::File # it is special
|
31
|
-
|
32
|
-
assign_field(fields, field, value)
|
33
|
-
end
|
34
|
-
|
35
|
-
fields[:file] = raw_file(locale) if type == 'asset'
|
31
|
+
fields.merge!(localized_fields(default_locale))
|
32
|
+
fields.merge!(localized_asset_fields(default_locale)) if type == 'asset'
|
36
33
|
|
37
34
|
fields
|
38
35
|
end
|
@@ -49,9 +46,31 @@ module Consyncful
|
|
49
46
|
fields
|
50
47
|
end
|
51
48
|
|
52
|
-
def
|
53
|
-
|
54
|
-
|
49
|
+
def localized_fields(default_locale)
|
50
|
+
fields = {}
|
51
|
+
|
52
|
+
@item.fields_with_locales.each do |field, value_with_locales|
|
53
|
+
value_with_locales.each do |locale_code, value|
|
54
|
+
next if value.is_a? Contentful::File # assets are handeled below
|
55
|
+
|
56
|
+
fieldname = locale_code == default_locale.to_sym ? field : "#{field}_#{locale_code.to_s.underscore}".to_sym
|
57
|
+
assign_field(fields, fieldname, value)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
fields
|
62
|
+
end
|
63
|
+
|
64
|
+
def localized_asset_fields(default_locale)
|
65
|
+
fields = {}
|
66
|
+
files_by_locale = @item.raw.dig('fields', 'file') || {}
|
67
|
+
|
68
|
+
files_by_locale.each do |locale_code, details|
|
69
|
+
fieldname = locale_code == default_locale ? 'file' : "file_#{locale_code.to_s.underscore}"
|
70
|
+
fields[fieldname.to_sym] = details
|
71
|
+
end
|
72
|
+
|
73
|
+
fields
|
55
74
|
end
|
56
75
|
|
57
76
|
def reference_value?(value)
|
@@ -1,7 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Consyncful
|
4
|
-
|
4
|
+
##
|
5
|
+
# Takes a mapped item from Contentful and creates/updates/deletes
|
6
|
+
# the relevant model in the local database.
|
5
7
|
class PersistedItem
|
6
8
|
DEFAULT_LOCALE = 'en-NZ'
|
7
9
|
|
data/lib/consyncful/stats.rb
CHANGED
data/lib/consyncful/sync.rb
CHANGED
@@ -7,6 +7,14 @@ require 'consyncful/stats'
|
|
7
7
|
require 'hooks'
|
8
8
|
|
9
9
|
module Consyncful
|
10
|
+
##
|
11
|
+
# A mongoid model that stores the state of a syncronisation feed. Stores the
|
12
|
+
# next URL provided by Contentfuls Sync API.
|
13
|
+
#
|
14
|
+
# Sync's are affectivly singletons,
|
15
|
+
# there should only ever be one in the database
|
16
|
+
#
|
17
|
+
# Is also the entrypoint of a Syncronization run
|
10
18
|
class Sync
|
11
19
|
include Mongoid::Document
|
12
20
|
include Hooks
|
@@ -14,8 +22,6 @@ module Consyncful
|
|
14
22
|
define_hook :before_run
|
15
23
|
define_hook :after_run
|
16
24
|
|
17
|
-
DEFAULT_LOCALE = 'en-NZ'
|
18
|
-
|
19
25
|
field :next_url
|
20
26
|
field :last_run_at, type: DateTime
|
21
27
|
|
@@ -23,17 +29,25 @@ module Consyncful
|
|
23
29
|
last || new
|
24
30
|
end
|
25
31
|
|
32
|
+
##
|
33
|
+
# Delete the previous sync chains from database and create a fresh one.
|
34
|
+
# Used to completely resync all items from Contentful.
|
26
35
|
def self.fresh
|
27
36
|
destroy_all
|
28
37
|
latest
|
29
38
|
end
|
30
39
|
|
40
|
+
##
|
41
|
+
# Makes sure that the database contains only records that have been provided
|
42
|
+
# during this chain of syncronisation.
|
31
43
|
def drop_stale
|
32
44
|
stale = Base.where(:sync_id.ne => id, :sync_id.exists => true)
|
33
45
|
puts Rainbow("Dropping #{stale.count} records that haven't been touched in this sync").red
|
34
46
|
stale.destroy
|
35
47
|
end
|
36
48
|
|
49
|
+
##
|
50
|
+
# Entry point to a syncronization run. Is responsible for updating Sync state
|
37
51
|
def run
|
38
52
|
run_hook :before_run
|
39
53
|
|
data/lib/consyncful/version.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: consyncful
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andy Anastasiadis-Gray
|
8
8
|
- Montgomery Anderson
|
9
|
-
|
9
|
+
- Greg Rogan
|
10
|
+
autorequire:
|
10
11
|
bindir: exe
|
11
12
|
cert_chain: []
|
12
|
-
date: 2021-
|
13
|
+
date: 2021-03-01 00:00:00.000000000 Z
|
13
14
|
dependencies:
|
14
15
|
- !ruby/object:Gem::Dependency
|
15
16
|
name: bundler
|
@@ -149,10 +150,11 @@ dependencies:
|
|
149
150
|
- - ">="
|
150
151
|
- !ruby/object:Gem::Version
|
151
152
|
version: '0'
|
152
|
-
description:
|
153
|
+
description:
|
153
154
|
email:
|
154
155
|
- andy@boost.co.nz
|
155
156
|
- montgomery@boost.co.nz
|
157
|
+
- greg@boost.co.nz
|
156
158
|
executables: []
|
157
159
|
extensions: []
|
158
160
|
extra_rdoc_files: []
|
@@ -186,7 +188,7 @@ homepage: https://github.com/boost/consyncful
|
|
186
188
|
licenses:
|
187
189
|
- MIT
|
188
190
|
metadata: {}
|
189
|
-
post_install_message:
|
191
|
+
post_install_message:
|
190
192
|
rdoc_options: []
|
191
193
|
require_paths:
|
192
194
|
- lib
|
@@ -201,9 +203,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
201
203
|
- !ruby/object:Gem::Version
|
202
204
|
version: '0'
|
203
205
|
requirements: []
|
204
|
-
|
205
|
-
|
206
|
-
signing_key:
|
206
|
+
rubygems_version: 3.1.2
|
207
|
+
signing_key:
|
207
208
|
specification_version: 4
|
208
209
|
summary: Contentful to local database synchronisation for Rails
|
209
210
|
test_files: []
|