wcc-contentful 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +20 -1
- data/README.md +6 -3
- data/lib/generators/wcc/USAGE +3 -3
- data/lib/generators/wcc/{menu_generator.rb → model_generator.rb} +31 -8
- data/lib/generators/wcc/templates/contentful_shell_wrapper +4 -2
- data/lib/generators/wcc/templates/menu/generated_add_menus.ts +5 -0
- data/lib/generators/wcc/templates/menu/{menu.rb → models/menu.rb} +0 -0
- data/lib/generators/wcc/templates/menu/{menu_button.rb → models/menu_button.rb} +0 -0
- data/lib/generators/wcc/templates/page/generated_add_pages.ts +50 -0
- data/lib/generators/wcc/templates/page/models/page.rb +23 -0
- data/lib/wcc/contentful.rb +24 -11
- data/lib/wcc/contentful/configuration.rb +42 -8
- data/lib/wcc/contentful/engine.rb +0 -2
- data/lib/wcc/contentful/helpers.rb +1 -2
- data/lib/wcc/contentful/model.rb +1 -0
- data/lib/wcc/contentful/model/page.rb +8 -0
- data/lib/wcc/contentful/model/redirect.rb +19 -0
- data/lib/wcc/contentful/model_builder.rb +8 -2
- data/lib/wcc/contentful/rails.rb +5 -0
- data/lib/wcc/contentful/simple_client.rb +29 -6
- data/lib/wcc/contentful/store.rb +6 -2
- data/lib/wcc/contentful/version.rb +1 -1
- data/wcc-contentful.gemspec +2 -2
- metadata +33 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58ee6d756754247484bf90a993340caf891a7471
|
4
|
+
data.tar.gz: 947b949744aacf47612a91237e70807e91c53664
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b89c3971ea984941b7f6dcba32cf1b9942417236a8074224304716379bccc9e8a7e10cfb646cece2d4989b514ee8ada7e55bc65f03668d8686482418d2699dc9
|
7
|
+
data.tar.gz: 47ebcda216d33570a8a99018131c84411bbc42c3dead50c47632bc3b2e1d516d3af18447b31e2c17dc003e7805423e4bef891545c3302fd6fa1792504cf4d6bd
|
data/CHANGELOG.md
CHANGED
@@ -23,4 +23,23 @@
|
|
23
23
|
* Application models can be registered to be instantiated for a given content type
|
24
24
|
* New 'lazy sync' delivery method acts as a cache that is kept up to date by the sync API
|
25
25
|
* 'eager sync' is now hooked up to a webhook which can be mounted to receive publish events
|
26
|
-
* Major changes to configuration methods
|
26
|
+
* Major changes to configuration methods
|
27
|
+
|
28
|
+
# v0.2.2
|
29
|
+
|
30
|
+
* Add preview_client for doing contentful calls to their preview api
|
31
|
+
* 'find_by' can now receive a preview param set to a boolean value
|
32
|
+
* Can configure your preview_api by passing a preview_token to configure block
|
33
|
+
* The Redirect model provides a 'href' method that will give you the url it points to
|
34
|
+
|
35
|
+
# v0.3.0
|
36
|
+
|
37
|
+
* Now neccesary to require the engine in a Gemfile when using in Rails:
|
38
|
+
|
39
|
+
`gem 'wcc-contentful', require: 'wcc/contentful/rails'`
|
40
|
+
|
41
|
+
* The gem can be configured to point to a non-master environment with the following configuration parameter:
|
42
|
+
|
43
|
+
`config.environment = 'my_environment'`
|
44
|
+
|
45
|
+
* When a model is not found in contentful, `Model.find_by` returns `nil` rather than raising an error.
|
data/README.md
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
Add this line to your application's Gemfile:
|
6
6
|
|
7
7
|
```ruby
|
8
|
-
gem 'wcc-contentful'
|
8
|
+
gem 'wcc-contentful', require: 'wcc/contentful/rails'
|
9
9
|
```
|
10
10
|
|
11
11
|
And then execute:
|
@@ -21,6 +21,7 @@ Or install it yourself as:
|
|
21
21
|
```ruby
|
22
22
|
WCC::Contentful.configure do |config|
|
23
23
|
config.access_token = <CONTENTFUL_ACCESS_TOKEN>
|
24
|
+
config.preview_token = <CONTENTFUL_PREVIEW_TOKEN>
|
24
25
|
config.space = <CONTENTFUL_SPACE_ID>
|
25
26
|
config.default_locale = "en-US"
|
26
27
|
end
|
@@ -29,9 +30,11 @@ end
|
|
29
30
|
## Usage
|
30
31
|
|
31
32
|
```ruby
|
32
|
-
redirect_object = WCC::Contentful::Redirect.
|
33
|
+
redirect_object = WCC::Contentful::Model::Redirect.find_by({slug: 'published-redirect'}, preview: false)
|
34
|
+
redirect_object.href
|
33
35
|
|
34
|
-
|
36
|
+
preview_redirect_object = WCC::Contentful::Model::Redirect.find_by({slug: 'draft-redirect'}, preview: true)
|
37
|
+
preview_redirect_object.href
|
35
38
|
```
|
36
39
|
|
37
40
|
## Development
|
data/lib/generators/wcc/USAGE
CHANGED
@@ -3,14 +3,14 @@ Description:
|
|
3
3
|
to your contentful space.
|
4
4
|
|
5
5
|
Example:
|
6
|
-
rails generate wcc:
|
6
|
+
rails generate wcc:model MODEL
|
7
7
|
|
8
8
|
This will install:
|
9
9
|
https://www.github.com/watermarkchurch/migration-cli
|
10
10
|
|
11
11
|
This will create:
|
12
|
-
db/migrate/[date]
|
13
|
-
this migration is run by the migration CLI to create
|
12
|
+
db/migrate/[date]_generated_add_[MODEL].ts -
|
13
|
+
this migration is run by the migration CLI to create MODEL in your
|
14
14
|
contentful space
|
15
15
|
bin/release -
|
16
16
|
Adds a release command to your rails app which runs migrations.
|
@@ -1,13 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Wcc
|
4
|
-
class
|
4
|
+
class ModelGenerator < Rails::Generators::Base
|
5
5
|
source_root File.expand_path('templates', __dir__)
|
6
|
+
argument :model, type: :string
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
VALID_MODELS = %w[menu page].freeze
|
9
|
+
|
10
|
+
def initialize(*)
|
11
|
+
super
|
12
|
+
|
13
|
+
return if VALID_MODELS.include?(singular)
|
14
|
+
|
15
|
+
raise ArgumentError, "Model must be #{VALID_MODELS.to_sentence}"
|
11
16
|
end
|
12
17
|
|
13
18
|
def ensure_migration_tools_installed
|
@@ -17,7 +22,7 @@ module Wcc
|
|
17
22
|
deps = package['dependencies']
|
18
23
|
|
19
24
|
unless deps.try(:[], 'contentful-migration-cli').present?
|
20
|
-
run 'npm install --save watermarkchurch/migration-cli ts-node typescript'
|
25
|
+
run 'npm install --save watermarkchurch/migration-cli ts-node typescript contentful-export'
|
21
26
|
end
|
22
27
|
end
|
23
28
|
end
|
@@ -59,9 +64,27 @@ module Wcc
|
|
59
64
|
copy_file 'wcc_contentful.rb', 'config/initializers/wcc_contentful.rb'
|
60
65
|
end
|
61
66
|
|
67
|
+
def create_model_migration
|
68
|
+
copy_file "#{singular}/generated_add_#{plural}.ts",
|
69
|
+
"db/migrate/#{timestamp}01_generated_add_#{plural}.ts"
|
70
|
+
end
|
71
|
+
|
62
72
|
def drop_model_overrides_in_app_models
|
63
|
-
|
64
|
-
|
73
|
+
directory "#{singular}/models", 'app/models'
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
def singular
|
79
|
+
model.downcase.singularize
|
80
|
+
end
|
81
|
+
|
82
|
+
def plural
|
83
|
+
model.downcase.pluralize
|
84
|
+
end
|
85
|
+
|
86
|
+
def timestamp
|
87
|
+
Time.now.strftime('%Y%m%d%H%M')
|
65
88
|
end
|
66
89
|
end
|
67
90
|
end
|
@@ -176,13 +176,15 @@ migrate() {
|
|
176
176
|
|
177
177
|
require_environment
|
178
178
|
|
179
|
+
[[ ! -z "$CONTENTFUL_ENVIRONMENT" ]] && ENV="--environment-id $CONTENTFUL_ENVIRONMENT"
|
180
|
+
|
179
181
|
execv node_modules/.bin/ts-node node_modules/.bin/contentful-migration \
|
180
|
-
-s $CONTENTFUL_SPACE_ID -a $CONTENTFUL_MANAGEMENT_TOKEN \
|
182
|
+
-s $CONTENTFUL_SPACE_ID $ENV -a $CONTENTFUL_MANAGEMENT_TOKEN \
|
181
183
|
$YES -p $ARG
|
182
184
|
|
183
185
|
mkdir -p db
|
184
186
|
execv node_modules/.bin/contentful-export --export-dir db --content-file contentful-schema.json \
|
185
|
-
--space-id $CONTENTFUL_SPACE_ID --management-token $CONTENTFUL_MANAGEMENT_TOKEN \
|
187
|
+
--space-id $CONTENTFUL_SPACE_ID $ENV --management-token $CONTENTFUL_MANAGEMENT_TOKEN \
|
186
188
|
--query-entries 'content_type=migrationHistory' \
|
187
189
|
--query-assets 'sys.id=false'
|
188
190
|
|
File without changes
|
File without changes
|
@@ -0,0 +1,50 @@
|
|
1
|
+
import Migration from 'contentful-migration-cli'
|
2
|
+
|
3
|
+
export = function (migration: Migration) {
|
4
|
+
const page = migration.createContentType('page')
|
5
|
+
.name('Page')
|
6
|
+
.description('A page describes a collection of sections that correspond' +
|
7
|
+
'to a URL slug')
|
8
|
+
.displayField('title')
|
9
|
+
|
10
|
+
page.createField('title')
|
11
|
+
.name('Title')
|
12
|
+
.type('Symbol')
|
13
|
+
.required(true)
|
14
|
+
|
15
|
+
page.createField('slug')
|
16
|
+
.name('Slug')
|
17
|
+
.type('Symbol')
|
18
|
+
.required(true)
|
19
|
+
.validations([
|
20
|
+
{
|
21
|
+
unique: true
|
22
|
+
},
|
23
|
+
{
|
24
|
+
regexp: { pattern: "(\\/|\\/([\w#!:.?+=&%@!\\-\\/]))?$" },
|
25
|
+
message: "The slug must look like the path part of a URL and begin with a forward slash, example: '/my-page-slug'"
|
26
|
+
}
|
27
|
+
])
|
28
|
+
|
29
|
+
page.createField('sections')
|
30
|
+
.name('Sections')
|
31
|
+
.type('Array')
|
32
|
+
.items({
|
33
|
+
type: 'Link',
|
34
|
+
linkType: 'Entry'
|
35
|
+
})
|
36
|
+
|
37
|
+
page.createField('subpages')
|
38
|
+
.name('Subpages')
|
39
|
+
.type('Array')
|
40
|
+
.items({
|
41
|
+
type: 'Link',
|
42
|
+
linkType: 'Entry',
|
43
|
+
validations: [
|
44
|
+
{
|
45
|
+
linkContentType: [ 'page' ]
|
46
|
+
}
|
47
|
+
]
|
48
|
+
})
|
49
|
+
|
50
|
+
}
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# This model represents the 'page' content type in Contentful. Any linked
|
4
|
+
# entries of the 'page' content type will be resolved as instances of this class.
|
5
|
+
# It exposes #find, #find_by, and #find_all methods to query Contentful.
|
6
|
+
class Page < WCC::Contentful::Model::Page
|
7
|
+
# Add custom validations to ensure that app-specific properties exist:
|
8
|
+
# validate_field :foo, :String, :required
|
9
|
+
# validate_field :bar_links, :Array, link_to: %w[bar baz]
|
10
|
+
|
11
|
+
# Override functionality or add utilities
|
12
|
+
#
|
13
|
+
# # Example: override equality
|
14
|
+
# def ===(other)
|
15
|
+
# ...
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# # Example: override "title" attribute to always be titlecase.
|
19
|
+
# # `@title` is populated by the gem in the initializer.
|
20
|
+
# def title
|
21
|
+
# @title_titlecased ||= @title.titlecase
|
22
|
+
# end
|
23
|
+
end
|
data/lib/wcc/contentful.rb
CHANGED
@@ -35,8 +35,12 @@ module WCC::Contentful
|
|
35
35
|
##
|
36
36
|
# Gets a {CDN Client}[rdoc-ref:WCC::Contentful::SimpleClient::Cdn] which provides
|
37
37
|
# methods for getting and paging raw JSON data from the Contentful CDN.
|
38
|
-
def self.client
|
39
|
-
|
38
|
+
def self.client(preview: false)
|
39
|
+
if preview
|
40
|
+
configuration&.preview_client
|
41
|
+
else
|
42
|
+
configuration&.client
|
43
|
+
end
|
40
44
|
end
|
41
45
|
|
42
46
|
##
|
@@ -59,6 +63,10 @@ module WCC::Contentful
|
|
59
63
|
WCC::Contentful::Model.store
|
60
64
|
end
|
61
65
|
|
66
|
+
def self.preview_store
|
67
|
+
WCC::Contentful::Model.preview_store
|
68
|
+
end
|
69
|
+
|
62
70
|
##
|
63
71
|
# Configures the WCC::Contentful gem to talk to a Contentful space.
|
64
72
|
# This must be called first in your initializer, before #init! or accessing the
|
@@ -68,8 +76,7 @@ module WCC::Contentful
|
|
68
76
|
@next_sync_token = nil
|
69
77
|
yield(configuration)
|
70
78
|
|
71
|
-
|
72
|
-
raise ArgumentError, 'Please provide "access_token"' unless configuration.access_token.present?
|
79
|
+
configuration.validate!
|
73
80
|
|
74
81
|
configuration.configure_contentful
|
75
82
|
|
@@ -90,6 +97,7 @@ module WCC::Contentful
|
|
90
97
|
raise ArgumentError, 'Please first call WCC:Contentful.configure' if configuration.nil?
|
91
98
|
@mutex ||= Mutex.new
|
92
99
|
|
100
|
+
use_preview_client = false
|
93
101
|
# we want as much as possible the raw JSON from the API
|
94
102
|
content_types_resp =
|
95
103
|
if configuration.management_client
|
@@ -97,6 +105,9 @@ module WCC::Contentful
|
|
97
105
|
else
|
98
106
|
configuration.client.content_types(limit: 1000)
|
99
107
|
end
|
108
|
+
|
109
|
+
(use_preview_client = true) unless configuration.preview_client.nil?
|
110
|
+
|
100
111
|
@content_types = content_types_resp.items
|
101
112
|
|
102
113
|
indexer =
|
@@ -105,8 +116,15 @@ module WCC::Contentful
|
|
105
116
|
end
|
106
117
|
@types = indexer.types
|
107
118
|
|
108
|
-
|
109
|
-
|
119
|
+
if use_preview_client
|
120
|
+
store = configuration.store(preview: false)
|
121
|
+
WCC::Contentful::Model.store = store
|
122
|
+
preview_store = configuration.store(preview: use_preview_client)
|
123
|
+
WCC::Contentful::Model.preview_store = preview_store
|
124
|
+
else
|
125
|
+
store = configuration.store(preview: use_preview_client)
|
126
|
+
WCC::Contentful::Model.store = store
|
127
|
+
end
|
110
128
|
|
111
129
|
if store.respond_to?(:index)
|
112
130
|
@next_sync_token = store.find("sync:#{configuration.space}:token")
|
@@ -120,11 +138,6 @@ module WCC::Contentful
|
|
120
138
|
file = File.dirname(__FILE__) + "/contentful/model/#{t.name.underscore}.rb"
|
121
139
|
require file if File.exist?(file)
|
122
140
|
end
|
123
|
-
|
124
|
-
return unless defined?(Rails)
|
125
|
-
|
126
|
-
# load up the engine so it gets automatically mounted
|
127
|
-
require 'wcc/contentful/engine'
|
128
141
|
end
|
129
142
|
|
130
143
|
##
|
@@ -5,8 +5,10 @@ class WCC::Contentful::Configuration
|
|
5
5
|
access_token
|
6
6
|
management_token
|
7
7
|
space
|
8
|
+
environment
|
8
9
|
default_locale
|
9
10
|
content_delivery
|
11
|
+
preview_token
|
10
12
|
http_adapter
|
11
13
|
sync_cache_store
|
12
14
|
webhook_username
|
@@ -59,12 +61,20 @@ class WCC::Contentful::Configuration
|
|
59
61
|
|
60
62
|
##
|
61
63
|
# Initializes the configured Sync Store.
|
62
|
-
def store
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
64
|
+
def store(preview: false)
|
65
|
+
if preview
|
66
|
+
@preview_store ||= WCC::Contentful::Store::Factory.new(
|
67
|
+
self,
|
68
|
+
:direct,
|
69
|
+
[{ preview: preview }]
|
70
|
+
).build_sync_store
|
71
|
+
else
|
72
|
+
@store ||= WCC::Contentful::Store::Factory.new(
|
73
|
+
self,
|
74
|
+
@content_delivery,
|
75
|
+
@content_delivery_params
|
76
|
+
).build_sync_store
|
77
|
+
end
|
68
78
|
end
|
69
79
|
|
70
80
|
##
|
@@ -84,6 +94,7 @@ class WCC::Contentful::Configuration
|
|
84
94
|
def initialize
|
85
95
|
@access_token = ''
|
86
96
|
@management_token = ''
|
97
|
+
@preview_token = ''
|
87
98
|
@space = ''
|
88
99
|
@default_locale = nil
|
89
100
|
@content_delivery = :direct
|
@@ -94,6 +105,7 @@ class WCC::Contentful::Configuration
|
|
94
105
|
# methods for getting and paging raw JSON data from the Contentful CDN.
|
95
106
|
attr_reader :client
|
96
107
|
attr_reader :management_client
|
108
|
+
attr_reader :preview_client
|
97
109
|
|
98
110
|
##
|
99
111
|
# Called by WCC::Contentful.init! to configure the
|
@@ -105,6 +117,7 @@ class WCC::Contentful::Configuration
|
|
105
117
|
def configure_contentful
|
106
118
|
@client = nil
|
107
119
|
@management_client = nil
|
120
|
+
@preview_client = nil
|
108
121
|
|
109
122
|
if defined?(::ContentfulModel)
|
110
123
|
ContentfulModel.configure do |config|
|
@@ -121,14 +134,35 @@ class WCC::Contentful::Configuration
|
|
121
134
|
access_token: access_token,
|
122
135
|
space: space,
|
123
136
|
default_locale: default_locale,
|
124
|
-
adapter: http_adapter
|
137
|
+
adapter: http_adapter,
|
138
|
+
environment: environment
|
125
139
|
)
|
140
|
+
|
141
|
+
if preview_token.present?
|
142
|
+
@preview_client = WCC::Contentful::SimpleClient::Preview.new(
|
143
|
+
preview_token: preview_token,
|
144
|
+
space: space,
|
145
|
+
default_locale: default_locale,
|
146
|
+
adapter: http_adapter
|
147
|
+
)
|
148
|
+
end
|
149
|
+
|
126
150
|
return unless management_token.present?
|
127
151
|
@management_client = WCC::Contentful::SimpleClient::Management.new(
|
128
152
|
management_token: management_token,
|
129
153
|
space: space,
|
130
154
|
default_locale: default_locale,
|
131
|
-
adapter: http_adapter
|
155
|
+
adapter: http_adapter,
|
156
|
+
environment: environment
|
132
157
|
)
|
133
158
|
end
|
159
|
+
|
160
|
+
def validate!
|
161
|
+
raise ArgumentError, 'Please provide "space"' unless space.present?
|
162
|
+
raise ArgumentError, 'Please provide "access_token"' unless access_token.present?
|
163
|
+
|
164
|
+
return if environment.nil? || %i[direct custom].include?(content_delivery)
|
165
|
+
raise ArgumentError, 'The Contentful Sync API currently does not work with environments. ' \
|
166
|
+
'You can use the ":direct" content_delivery method, or provide a custom store implementation.'
|
167
|
+
end
|
134
168
|
end
|
@@ -1,4 +1,3 @@
|
|
1
|
-
|
2
1
|
# frozen_string_literal: true
|
3
2
|
|
4
3
|
module WCC::Contentful::Helpers
|
@@ -16,7 +15,7 @@ module WCC::Contentful::Helpers
|
|
16
15
|
end
|
17
16
|
|
18
17
|
def constant_from_content_type(content_type)
|
19
|
-
content_type.
|
18
|
+
content_type.gsub(/[^_a-zA-Z0-9]/, '_').camelize
|
20
19
|
end
|
21
20
|
|
22
21
|
def shared_prefix(string_array)
|
data/lib/wcc/contentful/model.rb
CHANGED
@@ -17,6 +17,7 @@ class WCC::Contentful::Model
|
|
17
17
|
# See the {sync_store}[rdoc-ref:WCC::Contentful::Configuration.sync_store] parameter
|
18
18
|
# on the WCC::Contentful::Configuration class.
|
19
19
|
attr_accessor :store
|
20
|
+
attr_accessor :preview_store
|
20
21
|
|
21
22
|
def const_missing(name)
|
22
23
|
raise WCC::Contentful::ContentTypeNotFoundError,
|
@@ -0,0 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class WCC::Contentful::Model::Page < WCC::Contentful::Model
|
4
|
+
validate_field :title, :String
|
5
|
+
validate_field :slug, :String
|
6
|
+
validate_field :subpages, :Array, link_to: %w[page]
|
7
|
+
validate_field :sections, :Array, link_to: /^section/
|
8
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class WCC::Contentful::Model::Redirect < WCC::Contentful::Model
|
4
|
+
def href
|
5
|
+
if !url.nil?
|
6
|
+
url
|
7
|
+
elsif valid_page_reference?(pageReference)
|
8
|
+
"/#{pageReference.url}"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def valid_page_reference?(page_ref)
|
13
|
+
if !page_ref.nil? && !defined?(page_ref.url).nil?
|
14
|
+
true
|
15
|
+
else
|
16
|
+
false
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -57,7 +57,13 @@ module WCC::Contentful
|
|
57
57
|
bad_fields = filter.keys.reject { |k| fields.include?(k) }
|
58
58
|
raise ArgumentError, "These fields do not exist: #{bad_fields}" unless bad_fields.empty?
|
59
59
|
|
60
|
-
result =
|
60
|
+
result =
|
61
|
+
if defined?(context[:preview]) && context[:preview] == true
|
62
|
+
WCC::Contentful::Model.preview_store.find_by(content_type: content_type, filter: filter)
|
63
|
+
else
|
64
|
+
WCC::Contentful::Model.store.find_by(content_type: content_type, filter: filter)
|
65
|
+
end
|
66
|
+
|
61
67
|
new(result, context) if result
|
62
68
|
end
|
63
69
|
|
@@ -89,7 +95,7 @@ module WCC::Contentful
|
|
89
95
|
if raw_value.present?
|
90
96
|
case f.type
|
91
97
|
when :DateTime
|
92
|
-
raw_value = Time.
|
98
|
+
raw_value = Time.parse(raw_value).localtime
|
93
99
|
when :Int
|
94
100
|
raw_value = Integer(raw_value)
|
95
101
|
when :Float
|
@@ -23,6 +23,9 @@ module WCC::Contentful
|
|
23
23
|
@options = options
|
24
24
|
@query_defaults = {}
|
25
25
|
@query_defaults[:locale] = @options[:default_locale] if @options[:default_locale]
|
26
|
+
|
27
|
+
return unless env = options[:environment]
|
28
|
+
@api_url = URI.join(@api_url, 'environments/', env + '/')
|
26
29
|
end
|
27
30
|
|
28
31
|
def get(path, query = {})
|
@@ -99,6 +102,10 @@ module WCC::Contentful
|
|
99
102
|
)
|
100
103
|
end
|
101
104
|
|
105
|
+
def client_type
|
106
|
+
'cdn'
|
107
|
+
end
|
108
|
+
|
102
109
|
##
|
103
110
|
# Gets an entry by ID
|
104
111
|
def entry(key, query = {})
|
@@ -155,22 +162,38 @@ module WCC::Contentful
|
|
155
162
|
end
|
156
163
|
|
157
164
|
class Management < SimpleClient
|
158
|
-
def initialize(management_token:, **options)
|
165
|
+
def initialize(space:, management_token:, **options)
|
159
166
|
super(
|
160
167
|
api_url: options[:api_url] || 'https://api.contentful.com',
|
161
|
-
space:
|
168
|
+
space: space,
|
162
169
|
access_token: management_token,
|
163
170
|
**options
|
164
171
|
)
|
165
172
|
end
|
166
173
|
|
167
|
-
def
|
168
|
-
|
169
|
-
|
174
|
+
def client_type
|
175
|
+
'management'
|
176
|
+
end
|
170
177
|
|
171
|
-
|
178
|
+
def content_types(**query)
|
179
|
+
resp = get('content_types', query)
|
172
180
|
resp.assert_ok!
|
173
181
|
end
|
174
182
|
end
|
183
|
+
|
184
|
+
class Preview < Cdn
|
185
|
+
def initialize(space:, preview_token:, **options)
|
186
|
+
super(
|
187
|
+
api_url: options[:api_url] || 'https://preview.contentful.com/',
|
188
|
+
space: space,
|
189
|
+
access_token: preview_token,
|
190
|
+
**options
|
191
|
+
)
|
192
|
+
end
|
193
|
+
|
194
|
+
def client_type
|
195
|
+
'preview'
|
196
|
+
end
|
197
|
+
end
|
175
198
|
end
|
176
199
|
end
|
data/lib/wcc/contentful/store.rb
CHANGED
@@ -56,8 +56,12 @@ module WCC::Contentful::Store
|
|
56
56
|
)
|
57
57
|
end
|
58
58
|
|
59
|
-
def build_direct(config, *
|
60
|
-
|
59
|
+
def build_direct(config, *options)
|
60
|
+
if options.find { |array| array[:preview] == true }
|
61
|
+
CDNAdapter.new(config.preview_client)
|
62
|
+
else
|
63
|
+
CDNAdapter.new(config.client)
|
64
|
+
end
|
61
65
|
end
|
62
66
|
|
63
67
|
def validate_eager_sync(_config, store = nil, *_options)
|
data/wcc-contentful.gemspec
CHANGED
@@ -46,8 +46,8 @@ Gem::Specification.new do |spec|
|
|
46
46
|
spec.add_development_dependency 'timecop', '~> 0.9.1'
|
47
47
|
|
48
48
|
# optional dependencies
|
49
|
-
spec.add_development_dependency 'contentful', '
|
50
|
-
spec.add_development_dependency 'contentful-management', '
|
49
|
+
spec.add_development_dependency 'contentful', '2.6.0'
|
50
|
+
spec.add_development_dependency 'contentful-management', '2.0.2'
|
51
51
|
spec.add_development_dependency 'graphql', '~> 1.7'
|
52
52
|
spec.add_development_dependency 'http', '> 1.0', '< 3.0'
|
53
53
|
spec.add_development_dependency 'pg', '~> 1.0'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wcc-contentful
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Watermark Dev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dotenv
|
@@ -238,30 +238,30 @@ dependencies:
|
|
238
238
|
name: contentful
|
239
239
|
requirement: !ruby/object:Gem::Requirement
|
240
240
|
requirements:
|
241
|
-
- -
|
241
|
+
- - '='
|
242
242
|
- !ruby/object:Gem::Version
|
243
|
-
version:
|
243
|
+
version: 2.6.0
|
244
244
|
type: :development
|
245
245
|
prerelease: false
|
246
246
|
version_requirements: !ruby/object:Gem::Requirement
|
247
247
|
requirements:
|
248
|
-
- -
|
248
|
+
- - '='
|
249
249
|
- !ruby/object:Gem::Version
|
250
|
-
version:
|
250
|
+
version: 2.6.0
|
251
251
|
- !ruby/object:Gem::Dependency
|
252
252
|
name: contentful-management
|
253
253
|
requirement: !ruby/object:Gem::Requirement
|
254
254
|
requirements:
|
255
|
-
- -
|
255
|
+
- - '='
|
256
256
|
- !ruby/object:Gem::Version
|
257
|
-
version:
|
257
|
+
version: 2.0.2
|
258
258
|
type: :development
|
259
259
|
prerelease: false
|
260
260
|
version_requirements: !ruby/object:Gem::Requirement
|
261
261
|
requirements:
|
262
|
-
- -
|
262
|
+
- - '='
|
263
263
|
- !ruby/object:Gem::Version
|
264
|
-
version:
|
264
|
+
version: 2.0.2
|
265
265
|
- !ruby/object:Gem::Dependency
|
266
266
|
name: graphql
|
267
267
|
requirement: !ruby/object:Gem::Requirement
|
@@ -396,13 +396,15 @@ files:
|
|
396
396
|
- config/initializers/mime_types.rb
|
397
397
|
- config/routes.rb
|
398
398
|
- lib/generators/wcc/USAGE
|
399
|
-
- lib/generators/wcc/
|
399
|
+
- lib/generators/wcc/model_generator.rb
|
400
400
|
- lib/generators/wcc/templates/.keep
|
401
401
|
- lib/generators/wcc/templates/Procfile
|
402
402
|
- lib/generators/wcc/templates/contentful_shell_wrapper
|
403
403
|
- lib/generators/wcc/templates/menu/generated_add_menus.ts
|
404
|
-
- lib/generators/wcc/templates/menu/menu.rb
|
405
|
-
- lib/generators/wcc/templates/menu/menu_button.rb
|
404
|
+
- lib/generators/wcc/templates/menu/models/menu.rb
|
405
|
+
- lib/generators/wcc/templates/menu/models/menu_button.rb
|
406
|
+
- lib/generators/wcc/templates/page/generated_add_pages.ts
|
407
|
+
- lib/generators/wcc/templates/page/models/page.rb
|
406
408
|
- lib/generators/wcc/templates/release
|
407
409
|
- lib/generators/wcc/templates/wcc_contentful.rb
|
408
410
|
- lib/wcc/contentful.rb
|
@@ -419,9 +421,12 @@ files:
|
|
419
421
|
- lib/wcc/contentful/model.rb
|
420
422
|
- lib/wcc/contentful/model/menu.rb
|
421
423
|
- lib/wcc/contentful/model/menu_button.rb
|
424
|
+
- lib/wcc/contentful/model/page.rb
|
425
|
+
- lib/wcc/contentful/model/redirect.rb
|
422
426
|
- lib/wcc/contentful/model_builder.rb
|
423
427
|
- lib/wcc/contentful/model_validators.rb
|
424
428
|
- lib/wcc/contentful/model_validators/dsl.rb
|
429
|
+
- lib/wcc/contentful/rails.rb
|
425
430
|
- lib/wcc/contentful/simple_client.rb
|
426
431
|
- lib/wcc/contentful/simple_client/http_adapter.rb
|
427
432
|
- lib/wcc/contentful/simple_client/response.rb
|
@@ -454,20 +459,24 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
454
459
|
version: '0'
|
455
460
|
requirements: []
|
456
461
|
rubyforge_project:
|
457
|
-
rubygems_version: 2.
|
462
|
+
rubygems_version: 2.6.11
|
458
463
|
signing_key:
|
459
464
|
specification_version: 4
|
460
465
|
summary: '# WCC::Contentful ## Installation Add this line to your application''s
|
461
|
-
Gemfile: ```ruby gem ''wcc-contentful'' ``` And
|
462
|
-
it yourself as: $ gem install wcc-contentful ##
|
463
|
-
do |config| config.access_token = <CONTENTFUL_ACCESS_TOKEN>
|
464
|
-
config.
|
465
|
-
``` ##
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
466
|
+
Gemfile: ```ruby gem ''wcc-contentful'', require: ''wcc/contentful/rails'' ``` And
|
467
|
+
then execute: $ bundle Or install it yourself as: $ gem install wcc-contentful ##
|
468
|
+
Configure ```ruby WCC::Contentful.configure do |config| config.access_token = <CONTENTFUL_ACCESS_TOKEN>
|
469
|
+
config.preview_token = <CONTENTFUL_PREVIEW_TOKEN> config.space = <CONTENTFUL_SPACE_ID>
|
470
|
+
config.default_locale = "en-US" end ``` ## Usage ```ruby redirect_object = WCC::Contentful::Model::Redirect.find_by({slug:
|
471
|
+
''published-redirect''}, preview: false) redirect_object.href preview_redirect_object
|
472
|
+
= WCC::Contentful::Model::Redirect.find_by({slug: ''draft-redirect''}, preview:
|
473
|
+
true) preview_redirect_object.href ``` ## Development After checking out the repo,
|
474
|
+
run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
|
475
|
+
You can also run `bin/console` for an interactive prompt that will allow you to
|
476
|
+
experiment. To install this gem onto your local machine, run `bundle exec rake
|
477
|
+
install`. To release a new version, update the version number in `version.rb`, and
|
478
|
+
then run `bundle exec rake release`, which will create a git tag for the version,
|
479
|
+
push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). ##
|
471
480
|
Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/wcc-contentful.
|
472
481
|
This project is intended to be a safe, welcoming space for collaboration, and contributors
|
473
482
|
are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org)
|