webflow_sync 6.1.2 → 7.0.0

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: acaae64b1e573657f4983a9dc387f22da9be1f4c03a3d3475179cf9dcb288ab9
4
- data.tar.gz: f169ae62d45a2136607438bd7c329939f3459126a7e27fd62ff26bbfd713ee74
3
+ metadata.gz: f639ebd2128267a119b8dd6a93e2d06858e8713a2faeb6b6f17bc123e60438bb
4
+ data.tar.gz: 43294f31bc2fa6e5b0772ecbd28480b8c071c6472c1748b05a3109bd2818dc47
5
5
  SHA512:
6
- metadata.gz: 752e7ad6219a4f5062bf7ef970cf48117aa33a1ab8beb25df0630154f3e3cfc8bd997430753f4ea2731545072254e8c1e20ac73539246e00e4038709a23faae8
7
- data.tar.gz: ac6ac22d798f4dca3ac516be03a2877d9da52f0c3e4256fafa09b544a74ba7327ef183b17cf18a7e607f4ad8ff95c754ed659ea0d90f37504e6cc689da4e4473
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.perform_now(model_name, id, collection_slug) if record.webflow_item_id.present?
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
@@ -3,6 +3,7 @@
3
3
  module WebflowSync
4
4
  class DestroyItemJob < ApplicationJob
5
5
  def perform(collection_slug:, webflow_site_id:, webflow_item_id:)
6
+ return if WebflowSync.configuration.skip_webflow_sync
6
7
  return if webflow_site_id.blank?
7
8
  return if webflow_item_id.blank?
8
9
 
@@ -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.perform_now(model_name, id, collection_slug) if record.webflow_item_id.blank?
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
@@ -8,20 +8,20 @@ module WebflowSync
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)['_id']
13
- max_items_per_page = page_limit # Webflow::Error: 'limit' must be less than or equal to 100
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['total']
19
+ total_items = result.dig('pagination', 'total')
20
20
  total_pages = (total_items.to_f / max_items_per_page).ceil
21
- items = result['items']
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)['items']
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
@@ -33,47 +33,38 @@ module WebflowSync
33
33
  def get_item(collection_slug, webflow_item_id)
34
34
  collection = find_webflow_collection(collection_slug)
35
35
 
36
- make_request(:item, collection['_id'], webflow_item_id)
36
+ make_request(:item, collection.fetch('id'), webflow_item_id)
37
37
  end
38
38
 
39
39
  def create_item(record, collection_slug)
40
40
  collection = find_webflow_collection(collection_slug)
41
- response = make_request(:create_item, collection['_id'],
42
- 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)
43
42
 
44
43
  if update_record_columns(record, response)
45
44
  puts "Created #{record.inspect} in #{collection_slug}"
46
45
  response
47
46
  else
48
- raise "Failed to store webflow_item_id: '#{response['_id']}' " \
47
+ raise "Failed to store webflow_item_id: '#{response.fetch('id')}' " \
49
48
  "after creating item in WebFlow collection #{record.inspect}"
50
49
  end
51
50
  end
52
51
 
53
52
  def update_item(record, collection_slug)
54
53
  collection = find_webflow_collection(collection_slug)
55
- response = make_request(:update_item, { '_cid' => collection['_id'], '_id' => record.webflow_item_id },
56
- record.as_webflow_json.reverse_merge(_archived: false, _draft: false), live: true)
57
-
54
+ response = make_request(:update_item, collection.fetch('id'), record.webflow_item_id, record.as_webflow_json, publish: true)
58
55
  puts "Updated #{record.inspect} in #{collection_slug}"
59
56
  response
60
57
  end
61
58
 
62
59
  def delete_item(collection_slug, webflow_item_id)
63
60
  collection = find_webflow_collection(collection_slug)
64
- # deleting items from Webflow doesn't work as expected.
65
- # if we delete without `live: true`, the item will stay visible on the site until the site is published again
66
- # 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
67
- # we then call delete again to remove the item from Webflow CMS
68
- make_request(:delete_item, { '_cid' => collection['_id'], '_id' => webflow_item_id }, live: true)
69
- response = make_request(:delete_item, { '_cid' => collection['_id'], '_id' => webflow_item_id })
70
-
61
+ response = make_request(:delete_item, collection.fetch('id'), webflow_item_id)
71
62
  puts "Deleted #{webflow_item_id} from #{collection_slug}"
72
63
  response
73
64
  end
74
65
 
75
66
  def publish
76
- response = make_request(:publish, site_id, domain_names:)
67
+ response = make_request(:publish, site_id)
77
68
 
78
69
  puts "Publish all domains for Webflow site with id: #{site_id}"
79
70
  response
@@ -89,23 +80,7 @@ module WebflowSync
89
80
  end
90
81
 
91
82
  def collections
92
- @collections ||= make_request(:collections, site_id)
93
- end
94
-
95
- def domain_names
96
- @domain_names ||= find_domain_names
97
- end
98
-
99
- def find_domain_names
100
- # client.domains request does not return the default domain
101
- # We need to get default domain name if there are no custom domains set to avoid error:
102
- # Webflow::Error: Domain not found for site
103
- site = make_request(:site, site_id)
104
- default_domain_name = "#{site.fetch('shortName')}.webflow.io"
105
- names = [default_domain_name]
106
- make_request(:domains, site_id).each { |domain| names << domain.fetch('name') }
107
-
108
- names
83
+ @collections ||= make_request(:collections, site_id).fetch('collections')
109
84
  end
110
85
 
111
86
  def find_webflow_collection(collection_slug)
@@ -118,9 +93,9 @@ module WebflowSync
118
93
  def update_record_columns(record, response)
119
94
  # use update_column to skip callbacks to prevent WebflowSync::ItemSync to kick off
120
95
  if WebflowSync.configuration.sync_webflow_slug
121
- record.update_columns(webflow_item_id: response['_id'], webflow_slug: response['slug']) # rubocop:disable Rails/SkipsModelValidations
96
+ record.update_columns(webflow_item_id: response.fetch('id'), webflow_slug: response.dig('fieldData', 'slug')) # rubocop:disable Rails/SkipsModelValidations
122
97
  else
123
- record.update_column(:webflow_item_id, response['_id']) # rubocop:disable Rails/SkipsModelValidations
98
+ record.update_column(:webflow_item_id, response.fetch('id')) # rubocop:disable Rails/SkipsModelValidations
124
99
  end
125
100
  end
126
101
 
@@ -131,7 +106,7 @@ module WebflowSync
131
106
  client.public_send(method_name, *args)
132
107
  end
133
108
  rescue Webflow::Error => e
134
- raise unless e.message.strip == 'Rate limit hit'
109
+ raise unless e.message.strip == 'Too Many Requests'
135
110
 
136
111
  puts 'Sleeping 10 seconds'
137
112
  sleep 10
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'rails'
4
+
3
5
  module WebflowSync
4
6
  class Engine < ::Rails::Engine
5
7
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WebflowSync
4
- VERSION = '6.1.2'
4
+ VERSION = '7.0.0'
5
5
  end
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: 6.1.2
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-05-23 00:00:00.000000000 Z
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.3.26
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.