consyncful 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +26 -22
- data/README.md +18 -0
- data/consyncful.gemspec +1 -0
- data/lib/consyncful/persisted_item.rb +74 -0
- data/lib/consyncful/sync.rb +23 -59
- data/lib/consyncful/version.rb +1 -1
- metadata +22 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5798e871965ab5ca699b12f71a397ef37025d75547786ea804e1c38a9b0091ac
|
4
|
+
data.tar.gz: abfc466d422b44adc07abdc2d1b6bc36ff6e3dde8b57006d856bd809aaae86d5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8dbb9580e3aaa1aee124608323d877ab21f89da591976394bedd69c400682f88714b83598a14bacf32c18987089bfd84225273d2c868a4c17a033bb366c0ff3d
|
7
|
+
data.tar.gz: e3dc1aaf34278f60014f30c7f6487ba4558a7a0c9810ece3b44ee61e387a2036e52bb4eeb8955a94297a800f87ee2f6a5dedc85e26fc24d3e1bc91e256a6fbe7
|
data/Gemfile.lock
CHANGED
@@ -1,39 +1,42 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
consyncful (0.
|
4
|
+
consyncful (0.4.0)
|
5
5
|
contentful (>= 2.11.1, < 3.0.0)
|
6
|
+
hooks (>= 0.4.1)
|
6
7
|
mongoid (>= 7.0.2, < 8.0.0)
|
7
8
|
rainbow
|
8
9
|
|
9
10
|
GEM
|
10
11
|
remote: https://rubygems.org/
|
11
12
|
specs:
|
12
|
-
activemodel (6.0.
|
13
|
-
activesupport (= 6.0.
|
14
|
-
activesupport (6.0.
|
13
|
+
activemodel (6.0.3.4)
|
14
|
+
activesupport (= 6.0.3.4)
|
15
|
+
activesupport (6.0.3.4)
|
15
16
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
16
17
|
i18n (>= 0.7, < 2)
|
17
18
|
minitest (~> 5.1)
|
18
19
|
tzinfo (~> 1.1)
|
19
|
-
zeitwerk (~> 2.2)
|
20
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
20
21
|
addressable (2.7.0)
|
21
22
|
public_suffix (>= 2.0.2, < 5.0)
|
22
23
|
ast (2.4.0)
|
23
|
-
bson (4.
|
24
|
-
concurrent-ruby (1.1.
|
25
|
-
contentful (2.15.
|
24
|
+
bson (4.11.1)
|
25
|
+
concurrent-ruby (1.1.8)
|
26
|
+
contentful (2.15.4)
|
26
27
|
http (> 0.8, < 5.0)
|
27
28
|
multi_json (~> 1)
|
28
29
|
database_cleaner (1.8.3)
|
29
30
|
diff-lcs (1.3)
|
30
31
|
domain_name (0.5.20190701)
|
31
32
|
unf (>= 0.0.5, < 1.0.0)
|
32
|
-
ffi (1.
|
33
|
+
ffi (1.14.2)
|
33
34
|
ffi-compiler (1.0.1)
|
34
35
|
ffi (>= 1.0.0)
|
35
36
|
rake
|
36
|
-
|
37
|
+
hooks (0.4.1)
|
38
|
+
uber (~> 0.0.14)
|
39
|
+
http (4.4.1)
|
37
40
|
addressable (~> 2.3)
|
38
41
|
http-cookie (~> 1.0)
|
39
42
|
http-form_data (~> 2.2)
|
@@ -41,22 +44,22 @@ GEM
|
|
41
44
|
http-cookie (1.0.3)
|
42
45
|
domain_name (~> 0.5)
|
43
46
|
http-form_data (2.3.0)
|
44
|
-
http-parser (1.2.
|
47
|
+
http-parser (1.2.3)
|
45
48
|
ffi-compiler (>= 1.0, < 2.0)
|
46
|
-
i18n (1.8.
|
49
|
+
i18n (1.8.7)
|
47
50
|
concurrent-ruby (~> 1.0)
|
48
51
|
jaro_winkler (1.5.4)
|
49
|
-
minitest (5.14.
|
50
|
-
mongo (2.
|
51
|
-
bson (>= 4.
|
52
|
-
mongoid (7.
|
52
|
+
minitest (5.14.3)
|
53
|
+
mongo (2.14.0)
|
54
|
+
bson (>= 4.8.2, < 5.0.0)
|
55
|
+
mongoid (7.2.0)
|
53
56
|
activemodel (>= 5.1, < 6.1)
|
54
|
-
mongo (>= 2.
|
55
|
-
multi_json (1.
|
57
|
+
mongo (>= 2.10.5, < 3.0.0)
|
58
|
+
multi_json (1.15.0)
|
56
59
|
parallel (1.19.1)
|
57
60
|
parser (2.7.0.2)
|
58
61
|
ast (~> 2.4.0)
|
59
|
-
public_suffix (4.0.
|
62
|
+
public_suffix (4.0.6)
|
60
63
|
rainbow (3.0.0)
|
61
64
|
rake (13.0.1)
|
62
65
|
rspec (3.9.0)
|
@@ -81,13 +84,14 @@ GEM
|
|
81
84
|
unicode-display_width (>= 1.4.0, < 1.7)
|
82
85
|
ruby-progressbar (1.10.1)
|
83
86
|
thread_safe (0.3.6)
|
84
|
-
tzinfo (1.2.
|
87
|
+
tzinfo (1.2.9)
|
85
88
|
thread_safe (~> 0.1)
|
89
|
+
uber (0.0.15)
|
86
90
|
unf (0.1.4)
|
87
91
|
unf_ext
|
88
|
-
unf_ext (0.0.7.
|
92
|
+
unf_ext (0.0.7.7)
|
89
93
|
unicode-display_width (1.6.1)
|
90
|
-
zeitwerk (2.
|
94
|
+
zeitwerk (2.4.2)
|
91
95
|
|
92
96
|
PLATFORMS
|
93
97
|
ruby
|
data/README.md
CHANGED
@@ -134,6 +134,24 @@ Because all contentful models are stored as polymorphic subtypes of Consyncful::
|
|
134
134
|
Consyncful::Base.where(title: 'a title') # [ #<ModelName>, #<OtherModelName> ]
|
135
135
|
```
|
136
136
|
|
137
|
+
### Sync callbacks
|
138
|
+
|
139
|
+
You may want to attach some application logic to happen before or after a sync run, for example to update caches or something.
|
140
|
+
|
141
|
+
Callbacks can be registered using:
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
Consyncful::Sync.before_run do
|
145
|
+
# do something before the run
|
146
|
+
end
|
147
|
+
```
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
Consyncful::Sync.after_run do |updated_ids|
|
151
|
+
# invalidate cache for updated_ids, or something
|
152
|
+
end
|
153
|
+
```
|
154
|
+
|
137
155
|
## Limitations
|
138
156
|
|
139
157
|
### Locales
|
data/consyncful.gemspec
CHANGED
@@ -37,6 +37,7 @@ Gem::Specification.new do |spec|
|
|
37
37
|
spec.add_development_dependency 'rubocop', '0.79.0'
|
38
38
|
|
39
39
|
spec.add_dependency 'contentful', ['>=2.11.1', '<3.0.0']
|
40
|
+
spec.add_dependency 'hooks', '>=0.4.1'
|
40
41
|
spec.add_dependency 'mongoid', ['>=7.0.2', '<8.0.0']
|
41
42
|
spec.add_dependency 'rainbow'
|
42
43
|
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Consyncful
|
4
|
+
# Takes a mapped item from contentful and applies it to the local storage.
|
5
|
+
class PersistedItem
|
6
|
+
DEFAULT_LOCALE = 'en-NZ'
|
7
|
+
|
8
|
+
def initialize(item, sync_id, stats)
|
9
|
+
@item = item
|
10
|
+
@sync_id = sync_id
|
11
|
+
@stats = stats
|
12
|
+
end
|
13
|
+
|
14
|
+
def persist
|
15
|
+
puts Rainbow("syncing: #{@item.id}").yellow
|
16
|
+
if @item.deletion?
|
17
|
+
delete_model(@item.id, @stats)
|
18
|
+
else
|
19
|
+
create_or_update_model(@item, @sync_id, @stats)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def delete_model(id, stats)
|
26
|
+
Base.find_by(id: id).destroy
|
27
|
+
stats.record_deleted
|
28
|
+
rescue Mongoid::Errors::DocumentNotFound
|
29
|
+
puts Rainbow("Deleted record not found: #{id}").yellow
|
30
|
+
nil
|
31
|
+
end
|
32
|
+
|
33
|
+
def create_or_update_model(item, sync_id, stats)
|
34
|
+
return if item.type.nil?
|
35
|
+
|
36
|
+
instance = find_or_initialize_item(item)
|
37
|
+
update_stats(instance, stats)
|
38
|
+
|
39
|
+
reset_fields(instance)
|
40
|
+
|
41
|
+
item.mapped_fields(DEFAULT_LOCALE).each do |field, value|
|
42
|
+
instance[field] = value
|
43
|
+
end
|
44
|
+
|
45
|
+
instance[:sync_id] = sync_id
|
46
|
+
|
47
|
+
instance.save
|
48
|
+
end
|
49
|
+
|
50
|
+
def find_or_initialize_item(item)
|
51
|
+
model_class(item.type).find_or_initialize_by(id: item.id)
|
52
|
+
end
|
53
|
+
|
54
|
+
def update_stats(instance, stats)
|
55
|
+
if instance.persisted?
|
56
|
+
stats.record_updated
|
57
|
+
else
|
58
|
+
stats.record_added
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def model_class(type)
|
63
|
+
Base.model_map[type] || Base
|
64
|
+
end
|
65
|
+
|
66
|
+
def reset_fields(instance)
|
67
|
+
instance.attributes.each do |field_name, _value|
|
68
|
+
next if field_name.in? %w[_id _type]
|
69
|
+
|
70
|
+
instance[field_name] = nil
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
data/lib/consyncful/sync.rb
CHANGED
@@ -2,11 +2,17 @@
|
|
2
2
|
|
3
3
|
require 'rainbow'
|
4
4
|
require 'consyncful/item_mapper'
|
5
|
+
require 'consyncful/persisted_item'
|
5
6
|
require 'consyncful/stats'
|
7
|
+
require 'hooks'
|
6
8
|
|
7
9
|
module Consyncful
|
8
10
|
class Sync
|
9
11
|
include Mongoid::Document
|
12
|
+
include Hooks
|
13
|
+
|
14
|
+
define_hook :before_run
|
15
|
+
define_hook :after_run
|
10
16
|
|
11
17
|
DEFAULT_LOCALE = 'en-NZ'
|
12
18
|
|
@@ -29,19 +35,21 @@ module Consyncful
|
|
29
35
|
end
|
30
36
|
|
31
37
|
def run
|
38
|
+
run_hook :before_run
|
39
|
+
|
32
40
|
stats = Consyncful::Stats.new
|
33
41
|
load_all_models
|
34
42
|
|
35
43
|
sync = start_sync
|
36
44
|
|
37
|
-
sync_items(sync, stats)
|
45
|
+
changed_ids = sync_items(sync, stats)
|
38
46
|
|
39
47
|
drop_stale
|
40
48
|
|
41
|
-
|
42
|
-
self.last_run_at = Time.current
|
43
|
-
save
|
49
|
+
update_run(sync.next_sync_url)
|
44
50
|
stats.print_stats
|
51
|
+
|
52
|
+
run_hook :after_run, changed_ids
|
45
53
|
end
|
46
54
|
|
47
55
|
private
|
@@ -52,6 +60,12 @@ module Consyncful
|
|
52
60
|
Rails.application.eager_load!
|
53
61
|
end
|
54
62
|
|
63
|
+
def update_run(next_url)
|
64
|
+
self.next_url = next_url
|
65
|
+
self.last_run_at = Time.current
|
66
|
+
save
|
67
|
+
end
|
68
|
+
|
55
69
|
def start_sync
|
56
70
|
if next_url.present?
|
57
71
|
puts Rainbow("Starting update, last update: #{last_run_at} (#{(Time.current - last_run_at).round(3)}s ago)").blue
|
@@ -63,69 +77,19 @@ module Consyncful
|
|
63
77
|
end
|
64
78
|
|
65
79
|
def sync_items(sync, stats)
|
80
|
+
ids = []
|
66
81
|
sync.each_page do |page|
|
67
82
|
page.items.each do |item|
|
68
|
-
sync_item(ItemMapper.new(item), stats)
|
83
|
+
ids << sync_item(ItemMapper.new(item), stats)
|
69
84
|
end
|
70
85
|
end
|
86
|
+
ids
|
71
87
|
end
|
72
88
|
|
73
89
|
def sync_item(item, stats)
|
74
90
|
puts Rainbow("syncing: #{item.id}").yellow
|
75
|
-
|
76
|
-
|
77
|
-
else
|
78
|
-
create_or_update_model(item, stats)
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
def delete_model(id, stats)
|
83
|
-
Base.find_by(id: id).destroy
|
84
|
-
stats.record_deleted
|
85
|
-
rescue Mongoid::Errors::DocumentNotFound
|
86
|
-
puts Rainbow("Deleted record not found: #{id}").yellow
|
87
|
-
nil
|
88
|
-
end
|
89
|
-
|
90
|
-
def create_or_update_model(item, stats)
|
91
|
-
return if item.type.nil?
|
92
|
-
|
93
|
-
instance = find_or_initialize_item(item)
|
94
|
-
update_stats(instance, stats)
|
95
|
-
|
96
|
-
reset_fields(instance)
|
97
|
-
|
98
|
-
item.mapped_fields(DEFAULT_LOCALE).each do |field, value|
|
99
|
-
instance[field] = value
|
100
|
-
end
|
101
|
-
|
102
|
-
instance[:sync_id] = id
|
103
|
-
|
104
|
-
instance.save
|
105
|
-
end
|
106
|
-
|
107
|
-
def find_or_initialize_item(item)
|
108
|
-
model_class(item.type).find_or_initialize_by(id: item.id)
|
109
|
-
end
|
110
|
-
|
111
|
-
def update_stats(instance, stats)
|
112
|
-
if instance.persisted?
|
113
|
-
stats.record_updated
|
114
|
-
else
|
115
|
-
stats.record_added
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
def model_class(type)
|
120
|
-
Base.model_map[type] || Base
|
121
|
-
end
|
122
|
-
|
123
|
-
def reset_fields(instance)
|
124
|
-
instance.attributes.each do |field_name, _value|
|
125
|
-
next if field_name.in? %w[_id _type]
|
126
|
-
|
127
|
-
instance[field_name] = nil
|
128
|
-
end
|
91
|
+
PersistedItem.new(item, id, stats).persist
|
92
|
+
item.id
|
129
93
|
end
|
130
94
|
end
|
131
95
|
end
|
data/lib/consyncful/version.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: consyncful
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andy Anastasiadis-Gray
|
8
8
|
- Montgomery Anderson
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2021-02-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -101,6 +101,20 @@ dependencies:
|
|
101
101
|
- - "<"
|
102
102
|
- !ruby/object:Gem::Version
|
103
103
|
version: 3.0.0
|
104
|
+
- !ruby/object:Gem::Dependency
|
105
|
+
name: hooks
|
106
|
+
requirement: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 0.4.1
|
111
|
+
type: :runtime
|
112
|
+
prerelease: false
|
113
|
+
version_requirements: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.4.1
|
104
118
|
- !ruby/object:Gem::Dependency
|
105
119
|
name: mongoid
|
106
120
|
requirement: !ruby/object:Gem::Requirement
|
@@ -135,7 +149,7 @@ dependencies:
|
|
135
149
|
- - ">="
|
136
150
|
- !ruby/object:Gem::Version
|
137
151
|
version: '0'
|
138
|
-
description:
|
152
|
+
description:
|
139
153
|
email:
|
140
154
|
- andy@boost.co.nz
|
141
155
|
- montgomery@boost.co.nz
|
@@ -162,6 +176,7 @@ files:
|
|
162
176
|
- lib/consyncful.rb
|
163
177
|
- lib/consyncful/base.rb
|
164
178
|
- lib/consyncful/item_mapper.rb
|
179
|
+
- lib/consyncful/persisted_item.rb
|
165
180
|
- lib/consyncful/railtie.rb
|
166
181
|
- lib/consyncful/stats.rb
|
167
182
|
- lib/consyncful/sync.rb
|
@@ -171,7 +186,7 @@ homepage: https://github.com/boost/consyncful
|
|
171
186
|
licenses:
|
172
187
|
- MIT
|
173
188
|
metadata: {}
|
174
|
-
post_install_message:
|
189
|
+
post_install_message:
|
175
190
|
rdoc_options: []
|
176
191
|
require_paths:
|
177
192
|
- lib
|
@@ -186,9 +201,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
186
201
|
- !ruby/object:Gem::Version
|
187
202
|
version: '0'
|
188
203
|
requirements: []
|
189
|
-
rubyforge_project:
|
204
|
+
rubyforge_project:
|
190
205
|
rubygems_version: 2.7.6.2
|
191
|
-
signing_key:
|
206
|
+
signing_key:
|
192
207
|
specification_version: 4
|
193
208
|
summary: Contentful to local database synchronisation for Rails
|
194
209
|
test_files: []
|