webflow_sync 0.1.0 → 0.2.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: 14705897f82379cd4aa7b11ceda57374b6066d26b96344318ec4089048c377bb
4
- data.tar.gz: b17539c21bc34e339ddd12843831fcf61294876d6bc136d6ac7abbced15826fb
3
+ metadata.gz: 36b6c5dc80e752a95692afe908be134cdefd6193e0ed712c7a6d41440bb679e0
4
+ data.tar.gz: 5bd56794f4d0f09634a776b18ca613844b149afaf99cbbb841fe6294ae5cc67a
5
5
  SHA512:
6
- metadata.gz: fb9e2b651a04fd77506a3c869c5b33716131fa765ad413ac6180ca5c1805e141bb6b3b7310c4a8dbdc3f6e71782e7a1bf1a62bdbb78b8c224d8ef3c0669c3e74
7
- data.tar.gz: e6bcaf360ecf06bbaf08f78664715dceea5f10a3f529912f9d445aaa39fbe769b4c0fc4c11c583d5e6977e5e3885dc90c17b3a1c8f17c404b585140a3de57dc0
6
+ metadata.gz: 83fe81d28e2aec28ef31bee07956f8a105b368053f60dbcff1e668ae853671e708e543db1444aad37571117562ac7c6412913600c1da0d35d80851d5438a827c
7
+ data.tar.gz: 9e40ae99fa67b4d6ae787900e288009f2d7bc3c6633908e36f13efec62df3b676ec23ddcf0710cbd82a80beb5a573bf13da69c2d13c8f49c9db57686f8d60428
data/README.md CHANGED
@@ -28,6 +28,14 @@ bundle exec rails generate webflow_sync:install
28
28
 
29
29
  ## Usage
30
30
 
31
+ ### Generate and set WebFlow API token
32
+
33
+ Run API token Rails generator and follow instructions:
34
+
35
+ ```bash
36
+ bundle exec rails generate webflow_sync:api_token_flow
37
+ ```
38
+
31
39
  ### Add WebflowSync to models
32
40
 
33
41
  For each model that you want to sync to WebFlow, you need to run the collection generator:
@@ -53,6 +61,79 @@ For example, for `Article` model:
53
61
 
54
62
  Your WebFlow collection `slug` should be `"articles"`.
55
63
 
64
+ ### Set `webflow_site_id`
65
+
66
+ There are couple of ways how you can set the `webflow_site_id` to be used.
67
+
68
+ #### Set `webflow_site_id` through configuration
69
+
70
+ In `config/initializers/webflow_sync.rb` you can specify `webflow_site_id`:
71
+
72
+ ```ruby
73
+ WebflowSync.configure do |config|
74
+ config.webflow_site_id = ENV.fetch('WEBFLOW_SITE_ID')
75
+ end
76
+ ```
77
+
78
+ #### Set `webflow_site_id` for each model individually
79
+
80
+ You can set `webflow_site_id` per model, or even per record.
81
+
82
+ To do this, override the `#webflow_site_id` method provided by `WebflowSync::ItemSync` in your ActiveRecord model.
83
+
84
+ For example, you could have `Site` model in your codebase:
85
+
86
+ ```ruby
87
+ # app/models/site.rb
88
+ class Site < ApplicationRecord
89
+ has_many :articles
90
+ end
91
+
92
+ # app/models/article.rb
93
+ class Article < ApplicationRecord
94
+ include WebflowSync::ItemSync
95
+
96
+ belongs_to :site
97
+
98
+ def webflow_site_id
99
+ self.site.webflow_site_id
100
+ end
101
+ end
102
+ ```
103
+
104
+ ### Customize fields to synchronize
105
+
106
+ By default, WebflowSync calls `#as_webflow_json` on a record to get the fields that it needs to push to WebFlow. `#as_webflow_json` simply calls `#as_json` in its default implementation. To change this behavior, you can override `#as_json` in your model:
107
+
108
+ ```ruby
109
+ # app/models/article.rb
110
+ class Article < ApplicationRecord
111
+ include WebflowSync::ItemSync
112
+
113
+ def as_json
114
+ {
115
+ title: self.title.capitalize,
116
+ slug: self.title.parameterize,
117
+ published_at: self.created_at,
118
+ image: self.image_url
119
+ }
120
+ end
121
+ end
122
+ ```
123
+
124
+ Or if you already use `#as_json` for some other use-case and cannot modify it, you can also override `#as_webflow_json` method. Here's the default `#as_webflow_json` implementation (you don't need to add this to your model):
125
+
126
+ ```ruby
127
+ # app/models/article.rb
128
+ class Article < ApplicationRecord
129
+ include WebflowSync::ItemSync
130
+
131
+ def as_webflow_json
132
+ self.as_json
133
+ end
134
+ end
135
+ ```
136
+
56
137
  ### Run the initial sync
57
138
 
58
139
  After setting up which models you want to sync to WebFlow, you can run the initial sync for each of the models:
data/Rakefile CHANGED
@@ -10,3 +10,6 @@ load 'rails/tasks/statistics.rake'
10
10
  require 'bundler/gem_tasks'
11
11
 
12
12
  require 'stylecheck/rake_tasks' unless Rails.env.production?
13
+
14
+ load 'rspec/rails/tasks/rspec.rake'
15
+ task default: :spec
@@ -10,22 +10,31 @@ module WebflowSync
10
10
 
11
11
  def create_item(record, collection_slug)
12
12
  collection = find_webflow_collection(collection_slug)
13
- response = client.create_item(collection['_id'], record.as_webflow_json, live: true)
13
+ response = client.create_item(
14
+ collection['_id'],
15
+ record.as_webflow_json.reverse_merge(_archived: false, _draft: false), live: true
16
+ )
14
17
 
15
18
  record.update!(webflow_item_id: response['_id'])
19
+ puts "Created #{record.inspect} in #{collection_slug}"
20
+ response
16
21
  end
17
22
 
18
23
  def update_item(record, collection_slug)
19
24
  collection = find_webflow_collection(collection_slug)
20
- client.update_item(
25
+ response = client.update_item(
21
26
  { '_cid' => collection['_id'], '_id' => record.webflow_item_id },
22
- record.as_webflow_json, live: true
27
+ record.as_webflow_json.reverse_merge(_archived: false, _draft: false), live: true
23
28
  )
29
+ puts "Updated #{record.inspect} in #{collection_slug}"
30
+ response
24
31
  end
25
32
 
26
33
  def delete_item(collection_slug, webflow_item_id)
27
34
  collection = find_webflow_collection(collection_slug)
28
- client.delete_item({ '_cid' => collection['_id'], '_id' => webflow_item_id })
35
+ response = client.delete_item({ '_cid' => collection['_id'], '_id' => webflow_item_id })
36
+ puts "Deleted #{webflow_item_id} from #{collection_slug}"
37
+ response
29
38
  end
30
39
 
31
40
  private
@@ -39,10 +48,10 @@ module WebflowSync
39
48
  end
40
49
 
41
50
  def find_webflow_collection(collection_slug)
42
- result = collections.find { |collection| collection['slug'] == collection_slug }
43
- raise "Cannot find collection #{collection_slug} for Webflow site #{site_id}" unless result
51
+ response = collections.find { |collection| collection['slug'] == collection_slug }
52
+ raise "Cannot find collection #{collection_slug} for Webflow site #{site_id}" unless response
44
53
 
45
- result
54
+ response
46
55
  end
47
56
  end
48
57
  end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators/active_record'
4
+
5
+ module WebflowSync
6
+ module Generators
7
+ class ApiTokenFlowGenerator < Rails::Generators::Base
8
+ desc 'Generates Omniauth flow for authenticating WebFlow application to get WebFlow API token'
9
+
10
+ source_root File.expand_path('templates/api_token', __dir__)
11
+
12
+ def generate
13
+ gem 'omniauth-webflow'
14
+ run 'bundle install'
15
+ template 'webflow_callback_controller.rb.erb', 'app/controllers/webflow_callback_controller.rb'
16
+ template 'omniauth_webflow.rb.erb', 'config/initializers/omniauth_webflow.rb'
17
+ route "post '/auth/webflow/callback', to: 'webflow_callback#index'"
18
+ route "get '/auth/webflow/callback', to: 'webflow_callback#index'"
19
+
20
+ puts <<~END_OF_INSTRUCTIONS.indent(4)
21
+
22
+
23
+ We've generated all the files needed to successfully authenticate
24
+ with WebFlow to get API token.
25
+ There are couple of steps left to get the token:
26
+
27
+ 1. Download ngrok: https://ngrok.com/download
28
+ 2. Run `ngrok http 3000`
29
+ 3. Copy the URL that ngrok gives you (something like: https://dd7cc807bf91.ngrok.io)
30
+ 4. Go to: https://webflow.com/dashboard/account/integrations
31
+ 5. Register New Application
32
+ 6. In "Redirect URI" put: https://dd7cc807bf91.ngrok.io/auth/webflow/callback
33
+ 7. Save
34
+ 8. Copy client ID and client secret to ENV variables or add
35
+ them directly in config/initializers/omniauth_webflow.rb
36
+ 9. Start rails server locally
37
+ 10. Go to https://dd7cc807bf91.ngrok.io/auth/webflow
38
+ 11. Finally authenticate, beware which permissions to give
39
+ (some are "allow access", some are "restrict access", it's confusing)
40
+ 12. Copy the rendered API token and put it in ENV.fetch('WEBFLOW_API_TOKEN')
41
+ 13. After you've got the API token, you probably don't need
42
+ the Omniauth WebFlow code so you can remove the code that this
43
+ generator created from your codebase.
44
+ It is also a security issue to leave the Omniauth endpoint in your
45
+ codebase, that has the ability to generate API tokens for your WebFlow
46
+ account!!!
47
+
48
+ Easy.
49
+
50
+
51
+ END_OF_INSTRUCTIONS
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ Rails.application.config.middleware.use OmniAuth::Builder do
2
+ provider :webflow, ENV.fetch('WEBFLOW_CLIENT_ID'), ENV.fetch('WEBFLOW_CLIENT_SECRET')
3
+ end
@@ -0,0 +1,8 @@
1
+ class WebflowCallbackController < ApplicationController
2
+ skip_before_action :verify_authenticity_token, only: :index
3
+
4
+ # Go to /auth/webflow to start oauth flow
5
+ def index
6
+ render plain: request.env['omniauth.auth'].dig('credentials', 'token')
7
+ end
8
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  WebflowSync.configure do |config|
4
+ # config.api_token = ENV.fetch('WEBFLOW_API_TOKEN')
4
5
  # config.skip_webflow_sync = Rails.env.test? # default
5
6
  config.webflow_site_id = ENV.fetch('WEBFLOW_SITE_ID')
6
7
  end
@@ -6,7 +6,10 @@ module WebflowSync
6
6
 
7
7
  def configure
8
8
  self.configuration ||= Configuration.new
9
- yield(configuration)
9
+ yield(self.configuration)
10
+
11
+ self.configuration.api_token ||= ENV.fetch('WEBFLOW_API_TOKEN')
12
+ self.configuration.skip_webflow_sync ||= Rails.env.test?
10
13
  end
11
14
 
12
15
  private
@@ -15,10 +18,10 @@ module WebflowSync
15
18
  end
16
19
 
17
20
  class Configuration
18
- attr_accessor :skip_webflow_sync, :webflow_site_id
21
+ attr_accessor :api_token, :skip_webflow_sync, :webflow_site_id
19
22
 
20
- def initialize
21
- @skip_webflow_sync = Rails.env.test?
23
+ def api_token=(value)
24
+ @api_token = Webflow.config.api_token = value
22
25
  end
23
26
  end
24
27
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WebflowSync
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.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: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Viktor
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-29 00:00:00.000000000 Z
11
+ date: 2021-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -139,11 +139,14 @@ files:
139
139
  - app/models/concerns/webflow_sync/callbacks.rb
140
140
  - app/models/concerns/webflow_sync/item_sync.rb
141
141
  - app/services/webflow_sync/api.rb
142
+ - lib/generators/webflow_sync/api_token_flow_generator.rb
142
143
  - lib/generators/webflow_sync/collection_generator.rb
143
144
  - lib/generators/webflow_sync/install_generator.rb
145
+ - lib/generators/webflow_sync/templates/api_token/omniauth_webflow.rb.erb
146
+ - lib/generators/webflow_sync/templates/api_token/webflow_callback_controller.rb.erb
144
147
  - lib/generators/webflow_sync/templates/migration.rb.erb
145
148
  - lib/generators/webflow_sync/templates/webflow_sync.rb
146
- - lib/tasks/webflow_sync_tasks.rake
149
+ - lib/tasks/webflow_sync.rake
147
150
  - lib/webflow_sync.rb
148
151
  - lib/webflow_sync/configuration.rb
149
152
  - lib/webflow_sync/engine.rb