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 +4 -4
- data/README.md +81 -0
- data/Rakefile +3 -0
- data/app/services/webflow_sync/api.rb +16 -7
- data/lib/generators/webflow_sync/api_token_flow_generator.rb +55 -0
- data/lib/generators/webflow_sync/templates/api_token/omniauth_webflow.rb.erb +3 -0
- data/lib/generators/webflow_sync/templates/api_token/webflow_callback_controller.rb.erb +8 -0
- data/lib/generators/webflow_sync/templates/webflow_sync.rb +1 -0
- data/lib/tasks/{webflow_sync_tasks.rake → webflow_sync.rake} +0 -0
- data/lib/webflow_sync/configuration.rb +7 -4
- data/lib/webflow_sync/version.rb +1 -1
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 36b6c5dc80e752a95692afe908be134cdefd6193e0ed712c7a6d41440bb679e0
|
4
|
+
data.tar.gz: 5bd56794f4d0f09634a776b18ca613844b149afaf99cbbb841fe6294ae5cc67a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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,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(
|
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
|
-
|
43
|
-
raise "Cannot find collection #{collection_slug} for Webflow site #{site_id}" unless
|
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
|
-
|
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
|
File without changes
|
@@ -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
|
21
|
-
@
|
23
|
+
def api_token=(value)
|
24
|
+
@api_token = Webflow.config.api_token = value
|
22
25
|
end
|
23
26
|
end
|
24
27
|
end
|
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: 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
|
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/
|
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
|