webflow_sync 6.1.1 → 7.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/jobs/webflow_sync/create_item_job.rb +4 -1
- data/app/jobs/webflow_sync/destroy_item_job.rb +1 -0
- data/app/jobs/webflow_sync/update_item_job.rb +4 -1
- data/app/services/webflow_sync/api.rb +17 -51
- data/lib/webflow_sync/engine.rb +2 -0
- data/lib/webflow_sync/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f639ebd2128267a119b8dd6a93e2d06858e8713a2faeb6b6f17bc123e60438bb
|
4
|
+
data.tar.gz: 43294f31bc2fa6e5b0772ecbd28480b8c071c6472c1748b05a3109bd2818dc47
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2fac06bf9bc32b9618636acd2ab18437385f01071435757db1d311225c4f032800cb210c51168c9fb659bc095dcec5d716ce718d1c2e89449bbbf842e59bf4a8
|
7
|
+
data.tar.gz: 8109face21e162c8826b25fec73409009a4bc94ffd519c1648c9c7916f9a4ae6b7bdbe5c328d95031ae0fadfbc0ade771c196a83b3cf4232be80e1cefe19a8de
|
@@ -11,11 +11,14 @@ module WebflowSync
|
|
11
11
|
# collection_slug => slug of the WebFlow collection
|
12
12
|
# model_name = 'articles'; id = article.id, collection_slug = 'stories'
|
13
13
|
def perform(model_name, id, collection_slug = model_name.underscore.dasherize.pluralize)
|
14
|
+
return if WebflowSync.configuration.skip_webflow_sync
|
15
|
+
|
14
16
|
model_class = model_name.underscore.classify.constantize
|
15
17
|
record = model_class.find_by(id:)
|
16
18
|
return if record.blank?
|
19
|
+
return if record.skip_webflow_sync
|
17
20
|
return if record.webflow_site_id.blank?
|
18
|
-
return WebflowSync::UpdateItemJob.
|
21
|
+
return WebflowSync::UpdateItemJob.perform_later(model_name, id, collection_slug) if record.webflow_item_id.present?
|
19
22
|
|
20
23
|
WebflowSync::Api.new(record.webflow_site_id).create_item(record, collection_slug)
|
21
24
|
end
|
@@ -7,11 +7,14 @@ module WebflowSync
|
|
7
7
|
# 'JobListing'.underscore.dasherize.pluralize => 'job-listings'
|
8
8
|
# 'job_listing'.underscore.dasherize.pluralize => 'job-listings'
|
9
9
|
def perform(model_name, id, collection_slug = model_name.underscore.dasherize.pluralize)
|
10
|
+
return if WebflowSync.configuration.skip_webflow_sync
|
11
|
+
|
10
12
|
model_class = model_name.underscore.classify.constantize
|
11
13
|
record = model_class.find_by(id:)
|
12
14
|
return if record.blank?
|
15
|
+
return if record.skip_webflow_sync
|
13
16
|
return if record.webflow_site_id.blank?
|
14
|
-
return WebflowSync::CreateItemJob.
|
17
|
+
return WebflowSync::CreateItemJob.perform_later(model_name, id, collection_slug) if record.webflow_item_id.blank?
|
15
18
|
|
16
19
|
WebflowSync::Api.new(record.webflow_site_id).update_item(record, collection_slug)
|
17
20
|
end
|
@@ -1,27 +1,27 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module WebflowSync
|
4
|
-
class Api
|
4
|
+
class Api
|
5
5
|
attr_reader :site_id
|
6
6
|
|
7
7
|
def initialize(site_id = nil)
|
8
8
|
@site_id = site_id
|
9
9
|
end
|
10
10
|
|
11
|
-
def get_all_items(collection_slug:, page_limit: 100) # rubocop:disable Metrics/MethodLength
|
12
|
-
collection_id = find_webflow_collection(collection_slug)
|
13
|
-
max_items_per_page = page_limit
|
11
|
+
def get_all_items(collection_slug:, page_limit: 100) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
12
|
+
collection_id = find_webflow_collection(collection_slug).fetch('id')
|
13
|
+
max_items_per_page = [page_limit, 100].min
|
14
14
|
first_page_number = 1
|
15
15
|
|
16
16
|
result = make_request(:paginate_items, collection_id, page: first_page_number, per_page: max_items_per_page)
|
17
17
|
puts "Get all items from WebFlow for #{collection_slug} page: #{first_page_number}"
|
18
18
|
|
19
|
-
total_items = result
|
19
|
+
total_items = result.dig('pagination', 'total')
|
20
20
|
total_pages = (total_items.to_f / max_items_per_page).ceil
|
21
|
-
items = result
|
21
|
+
items = result.fetch('items')
|
22
22
|
|
23
23
|
(2..total_pages).each do |page_number|
|
24
|
-
next_page_items = make_request(:paginate_items, collection_id, page: page_number, per_page: max_items_per_page)
|
24
|
+
next_page_items = make_request(:paginate_items, collection_id, page: page_number, per_page: max_items_per_page).fetch('items')
|
25
25
|
puts "Get all items from WebFlow for #{collection_slug} page: #{page_number}"
|
26
26
|
|
27
27
|
items.concat next_page_items
|
@@ -30,59 +30,41 @@ module WebflowSync
|
|
30
30
|
items
|
31
31
|
end
|
32
32
|
|
33
|
-
def self.get_all_items(collection_slug:, page_limit: 100) = new.get_all_items(collection_slug:, page_limit:)
|
34
|
-
|
35
33
|
def get_item(collection_slug, webflow_item_id)
|
36
34
|
collection = find_webflow_collection(collection_slug)
|
37
35
|
|
38
|
-
make_request(:item, collection
|
36
|
+
make_request(:item, collection.fetch('id'), webflow_item_id)
|
39
37
|
end
|
40
|
-
def self.get_item(collection_slug, webflow_item_id) = new.get_item(collection_slug, webflow_item_id)
|
41
38
|
|
42
39
|
def create_item(record, collection_slug)
|
43
40
|
collection = find_webflow_collection(collection_slug)
|
44
|
-
response = make_request(:create_item, collection
|
45
|
-
record.as_webflow_json.reverse_merge(_archived: false, _draft: false), live: true)
|
41
|
+
response = make_request(:create_item, collection.fetch('id'), record.as_webflow_json, publish: true)
|
46
42
|
|
47
43
|
if update_record_columns(record, response)
|
48
44
|
puts "Created #{record.inspect} in #{collection_slug}"
|
49
45
|
response
|
50
46
|
else
|
51
|
-
raise "Failed to store webflow_item_id: '#{response
|
47
|
+
raise "Failed to store webflow_item_id: '#{response.fetch('id')}' " \
|
52
48
|
"after creating item in WebFlow collection #{record.inspect}"
|
53
49
|
end
|
54
50
|
end
|
55
51
|
|
56
|
-
def self.create_item(record, collection_slug) = new.create_item(record, collection_slug)
|
57
|
-
|
58
52
|
def update_item(record, collection_slug)
|
59
53
|
collection = find_webflow_collection(collection_slug)
|
60
|
-
response = make_request(:update_item,
|
61
|
-
record.as_webflow_json.reverse_merge(_archived: false, _draft: false), live: true)
|
62
|
-
|
54
|
+
response = make_request(:update_item, collection.fetch('id'), record.webflow_item_id, record.as_webflow_json, publish: true)
|
63
55
|
puts "Updated #{record.inspect} in #{collection_slug}"
|
64
56
|
response
|
65
57
|
end
|
66
58
|
|
67
|
-
def self.update_item(record, collection_slug) = new.update_item(record, collection_slug)
|
68
|
-
|
69
59
|
def delete_item(collection_slug, webflow_item_id)
|
70
60
|
collection = find_webflow_collection(collection_slug)
|
71
|
-
|
72
|
-
# if we delete without `live: true`, the item will stay visible on the site until the site is published again
|
73
|
-
# if we delete with `live: true`, the item will be set as draft and not visible on the site, but it will be visible in the Webflow CMS
|
74
|
-
# we then call delete again to remove the item from Webflow CMS
|
75
|
-
make_request(:delete_item, { '_cid' => collection['_id'], '_id' => webflow_item_id }, live: true)
|
76
|
-
response = make_request(:delete_item, { '_cid' => collection['_id'], '_id' => webflow_item_id })
|
77
|
-
|
61
|
+
response = make_request(:delete_item, collection.fetch('id'), webflow_item_id)
|
78
62
|
puts "Deleted #{webflow_item_id} from #{collection_slug}"
|
79
63
|
response
|
80
64
|
end
|
81
65
|
|
82
|
-
def self.delete_item(collection_slug, webflow_item_id) = new.delete_item(collection_slug, webflow_item_id)
|
83
|
-
|
84
66
|
def publish
|
85
|
-
response = make_request(:publish, site_id
|
67
|
+
response = make_request(:publish, site_id)
|
86
68
|
|
87
69
|
puts "Publish all domains for Webflow site with id: #{site_id}"
|
88
70
|
response
|
@@ -98,23 +80,7 @@ module WebflowSync
|
|
98
80
|
end
|
99
81
|
|
100
82
|
def collections
|
101
|
-
@collections ||= make_request(:collections, site_id)
|
102
|
-
end
|
103
|
-
|
104
|
-
def domain_names
|
105
|
-
@domain_names ||= find_domain_names
|
106
|
-
end
|
107
|
-
|
108
|
-
def find_domain_names
|
109
|
-
# client.domains request does not return the default domain
|
110
|
-
# We need to get default domain name if there are no custom domains set to avoid error:
|
111
|
-
# Webflow::Error: Domain not found for site
|
112
|
-
site = make_request(:site, site_id)
|
113
|
-
default_domain_name = "#{site.fetch('shortName')}.webflow.io"
|
114
|
-
names = [default_domain_name]
|
115
|
-
make_request(:domains, site_id).each { |domain| names << domain.fetch('name') }
|
116
|
-
|
117
|
-
names
|
83
|
+
@collections ||= make_request(:collections, site_id).fetch('collections')
|
118
84
|
end
|
119
85
|
|
120
86
|
def find_webflow_collection(collection_slug)
|
@@ -127,9 +93,9 @@ module WebflowSync
|
|
127
93
|
def update_record_columns(record, response)
|
128
94
|
# use update_column to skip callbacks to prevent WebflowSync::ItemSync to kick off
|
129
95
|
if WebflowSync.configuration.sync_webflow_slug
|
130
|
-
record.update_columns(webflow_item_id: response
|
96
|
+
record.update_columns(webflow_item_id: response.fetch('id'), webflow_slug: response.dig('fieldData', 'slug')) # rubocop:disable Rails/SkipsModelValidations
|
131
97
|
else
|
132
|
-
record.update_column(:webflow_item_id, response
|
98
|
+
record.update_column(:webflow_item_id, response.fetch('id')) # rubocop:disable Rails/SkipsModelValidations
|
133
99
|
end
|
134
100
|
end
|
135
101
|
|
@@ -140,7 +106,7 @@ module WebflowSync
|
|
140
106
|
client.public_send(method_name, *args)
|
141
107
|
end
|
142
108
|
rescue Webflow::Error => e
|
143
|
-
raise unless e.message.strip == '
|
109
|
+
raise unless e.message.strip == 'Too Many Requests'
|
144
110
|
|
145
111
|
puts 'Sleeping 10 seconds'
|
146
112
|
sleep 10
|
data/lib/webflow_sync/engine.rb
CHANGED
data/lib/webflow_sync/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: webflow_sync
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 7.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Viktor
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-11-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -104,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
104
|
- !ruby/object:Gem::Version
|
105
105
|
version: '0'
|
106
106
|
requirements: []
|
107
|
-
rubygems_version: 3.
|
107
|
+
rubygems_version: 3.4.21
|
108
108
|
signing_key:
|
109
109
|
specification_version: 4
|
110
110
|
summary: Keep Rails models in sync with WebFlow.
|