dato 0.3.31 → 0.4.0.pre
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/dato/cli.rb +3 -15
- data/lib/dato/dump/runner.rb +4 -3
- data/lib/dato/json_api_deserializer.rb +4 -1
- data/lib/dato/local/loader.rb +9 -6
- data/lib/dato/site/paginator.rb +1 -1
- data/lib/dato/site/repo/item.rb +10 -1
- data/lib/dato/version.rb +1 -1
- metadata +3 -4
- data/lib/dato/migrate_slugs/runner.rb +0 -155
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f85c8c3d4e34d55aa8964841977e367a733be9a6
|
4
|
+
data.tar.gz: 5f28df9e2518781045640b7f6cd5a98f4f4cd6c1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e272ffbaccb62c8ce4f738ba2868adbfa6f200af79ae74d36833f10b7ffee3a82008cc7c047331ba097e1262e3debc2ffa9c77a320b29e0f837cc35f47cfd75
|
7
|
+
data.tar.gz: 32a7c6185fad31b06d5a2ce34c78c36c7ad287a3ed2dcb1ff23e0b4ee4d249a17e4dac0d4867c32482150a6d56a6064d60e9b41f335caeb241b128b598380574
|
data/lib/dato/cli.rb
CHANGED
@@ -14,10 +14,12 @@ module Dato
|
|
14
14
|
desc 'dump', 'dumps DatoCMS content into local files'
|
15
15
|
option :config, default: 'dato.config.rb'
|
16
16
|
option :token, default: ENV['DATO_API_TOKEN'], required: true
|
17
|
+
option :draft, default: false, type: :boolean
|
17
18
|
option :watch, default: false, type: :boolean
|
18
19
|
def dump
|
19
20
|
config_file = File.expand_path(options[:config])
|
20
21
|
watch_mode = options[:watch]
|
22
|
+
draft_mode = options[:draft]
|
21
23
|
|
22
24
|
client = Dato::Site::Client.new(
|
23
25
|
options[:token],
|
@@ -44,7 +46,7 @@ module Dato
|
|
44
46
|
|
45
47
|
sleep
|
46
48
|
else
|
47
|
-
Dump::Runner.new(config_file, client).run
|
49
|
+
Dump::Runner.new(config_file, client, draft_mode).run
|
48
50
|
end
|
49
51
|
end
|
50
52
|
|
@@ -69,20 +71,6 @@ module Dato
|
|
69
71
|
exit 0
|
70
72
|
end
|
71
73
|
|
72
|
-
desc 'migrate-slugs', 'migrates a Site so that it uses slug fields'
|
73
|
-
option :token, default: ENV['DATO_API_TOKEN'], required: true
|
74
|
-
option :skip_id_prefix, type: :boolean
|
75
|
-
def migrate_slugs
|
76
|
-
client = Dato::Site::Client.new(
|
77
|
-
options[:token],
|
78
|
-
extra_headers: {
|
79
|
-
'X-Reason' => 'migrate-slugs'
|
80
|
-
}
|
81
|
-
)
|
82
|
-
|
83
|
-
MigrateSlugs::Runner.new(client, options[:skip_id_prefix]).run
|
84
|
-
end
|
85
|
-
|
86
74
|
no_tasks do
|
87
75
|
def watch_config_file(config_file, &block)
|
88
76
|
Listen.to(
|
data/lib/dato/dump/runner.rb
CHANGED
@@ -7,10 +7,11 @@ require 'dato/local/loader'
|
|
7
7
|
module Dato
|
8
8
|
module Dump
|
9
9
|
class Runner
|
10
|
-
attr_reader :config_path, :client, :destination_path
|
10
|
+
attr_reader :config_path, :client, :destination_path, :draft_mode
|
11
11
|
|
12
|
-
def initialize(config_path, client, destination_path = Dir.pwd)
|
12
|
+
def initialize(config_path, client, draft_mode, destination_path = Dir.pwd)
|
13
13
|
@config_path = config_path
|
14
|
+
@draft_mode = draft_mode
|
14
15
|
@client = client
|
15
16
|
@destination_path = destination_path
|
16
17
|
end
|
@@ -39,7 +40,7 @@ module Dato
|
|
39
40
|
end
|
40
41
|
|
41
42
|
def loader
|
42
|
-
@loader ||= Dato::Local::Loader.new(client)
|
43
|
+
@loader ||= Dato::Local::Loader.new(client, draft_mode)
|
43
44
|
end
|
44
45
|
end
|
45
46
|
end
|
data/lib/dato/local/loader.rb
CHANGED
@@ -8,9 +8,11 @@ module Dato
|
|
8
8
|
attr_reader :client
|
9
9
|
attr_reader :entities_repo
|
10
10
|
attr_reader :items_repo
|
11
|
+
attr_reader :draft_mode
|
11
12
|
|
12
|
-
def initialize(client)
|
13
|
+
def initialize(client, draft_mode = false)
|
13
14
|
@client = client
|
15
|
+
@draft_mode = draft_mode
|
14
16
|
@entities_repo = EntitiesRepo.new
|
15
17
|
@items_repo = ItemsRepo.new(@entities_repo)
|
16
18
|
end
|
@@ -23,15 +25,16 @@ module Dato
|
|
23
25
|
private
|
24
26
|
|
25
27
|
def site
|
26
|
-
include = [
|
27
|
-
'item_types',
|
28
|
-
'item_types.fields'
|
29
|
-
]
|
28
|
+
include = ['item_types', 'item_types.fields']
|
30
29
|
client.request(:get, '/site', include: include)
|
31
30
|
end
|
32
31
|
|
33
32
|
def all_items
|
34
|
-
client.items.all(
|
33
|
+
client.items.all(
|
34
|
+
{ version: draft_mode ? 'current' : 'published' },
|
35
|
+
deserialize_response: false,
|
36
|
+
all_pages: true
|
37
|
+
)
|
35
38
|
end
|
36
39
|
end
|
37
40
|
end
|
data/lib/dato/site/paginator.rb
CHANGED
data/lib/dato/site/repo/item.rb
CHANGED
@@ -28,7 +28,8 @@ module Dato
|
|
28
28
|
:created_at,
|
29
29
|
:is_valid,
|
30
30
|
:item_type,
|
31
|
-
:published_version
|
31
|
+
:published_version,
|
32
|
+
:current_version
|
32
33
|
)
|
33
34
|
|
34
35
|
body = JsonApiSerializer.new(
|
@@ -39,6 +40,14 @@ module Dato
|
|
39
40
|
put_request "/items/#{item_id}", body
|
40
41
|
end
|
41
42
|
|
43
|
+
def publish(item_id)
|
44
|
+
put_request "/items/#{item_id}/publish", {}
|
45
|
+
end
|
46
|
+
|
47
|
+
def unpublish(item_id)
|
48
|
+
put_request "/items/#{item_id}/unpublish", {}
|
49
|
+
end
|
50
|
+
|
42
51
|
def all(filters = {}, options = {})
|
43
52
|
options.symbolize_keys!
|
44
53
|
|
data/lib/dato/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dato
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0.pre
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stefano Verna
|
@@ -420,7 +420,6 @@ files:
|
|
420
420
|
- lib/dato/local/json_api_entity.rb
|
421
421
|
- lib/dato/local/loader.rb
|
422
422
|
- lib/dato/local/site.rb
|
423
|
-
- lib/dato/migrate_slugs/runner.rb
|
424
423
|
- lib/dato/site/client.rb
|
425
424
|
- lib/dato/site/paginator.rb
|
426
425
|
- lib/dato/site/repo/access_token.rb
|
@@ -470,9 +469,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
470
469
|
version: '0'
|
471
470
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
472
471
|
requirements:
|
473
|
-
- - "
|
472
|
+
- - ">"
|
474
473
|
- !ruby/object:Gem::Version
|
475
|
-
version:
|
474
|
+
version: 1.3.1
|
476
475
|
requirements: []
|
477
476
|
rubyforge_project:
|
478
477
|
rubygems_version: 2.5.1
|
@@ -1,155 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
require 'dato/json_api_deserializer'
|
3
|
-
require 'active_support/inflector/transliterate'
|
4
|
-
|
5
|
-
module Dato
|
6
|
-
module MigrateSlugs
|
7
|
-
class Runner
|
8
|
-
attr_reader :client, :skip_id_prefix
|
9
|
-
|
10
|
-
def initialize(client, skip_id_prefix)
|
11
|
-
@client = client
|
12
|
-
@skip_id_prefix = skip_id_prefix
|
13
|
-
end
|
14
|
-
|
15
|
-
def run
|
16
|
-
print 'Fetching site informations... '
|
17
|
-
title_fields
|
18
|
-
puts "\e[32m✓\e[0m"
|
19
|
-
|
20
|
-
title_fields.each do |title_field|
|
21
|
-
item_type = item_types.find do |i|
|
22
|
-
i['id'] == title_field['item_type']
|
23
|
-
end
|
24
|
-
|
25
|
-
print "Adding slug field to Item type `#{item_type['name']}`... "
|
26
|
-
add_slug_field(title_field)
|
27
|
-
puts "\e[32m✓\e[0m"
|
28
|
-
|
29
|
-
items = items_for(title_field['item_type'])
|
30
|
-
print "Generating slugs for #{items.count} items"
|
31
|
-
|
32
|
-
items.each do |item|
|
33
|
-
update_item(title_field, item)
|
34
|
-
print '.'
|
35
|
-
end
|
36
|
-
puts "\e[32m✓\e[0m"
|
37
|
-
|
38
|
-
puts
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def simple_slugify(item, title, suffix)
|
43
|
-
return nil unless title
|
44
|
-
|
45
|
-
slug = title.parameterize[0..50].gsub(/(^\-|\-$)/, '')
|
46
|
-
skip_id_prefix ? "#{slug}#{suffix}" : "#{item['id']}-#{slug}#{suffix}"
|
47
|
-
end
|
48
|
-
|
49
|
-
def slugify(item, title, suffix)
|
50
|
-
if title.is_a?(Hash)
|
51
|
-
Hash[
|
52
|
-
title.map do |locale, value|
|
53
|
-
[locale, simple_slugify(item, value, suffix)]
|
54
|
-
end
|
55
|
-
]
|
56
|
-
else
|
57
|
-
simple_slugify(item, title, suffix)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def update_item(title_field, item)
|
62
|
-
title = item[title_field['api_key']]
|
63
|
-
counter = 0
|
64
|
-
|
65
|
-
loop do
|
66
|
-
begin
|
67
|
-
slug = slugify(item, title, counter.zero? ? '' : "-#{counter}")
|
68
|
-
return client.items.update(item['id'], item.merge(slug: slug))
|
69
|
-
rescue ApiError => e
|
70
|
-
error = e.body['data'][0]
|
71
|
-
|
72
|
-
if error['id'] == 'INVALID_FIELD' &&
|
73
|
-
error['attributes']['details']['field'] == 'slug' &&
|
74
|
-
error['attributes']['details']['code'] == 'VALIDATION_UNIQUE'
|
75
|
-
|
76
|
-
counter += 1
|
77
|
-
else
|
78
|
-
raise e
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
def items_for(item_type_id)
|
85
|
-
items_per_page = 500
|
86
|
-
base_response = client.request(
|
87
|
-
:get,
|
88
|
-
'/items',
|
89
|
-
'page[limit]' => 500,
|
90
|
-
'filter[type]' => item_type_id
|
91
|
-
)
|
92
|
-
|
93
|
-
extra_pages = (
|
94
|
-
base_response[:meta][:total_count] / items_per_page.to_f
|
95
|
-
).ceil - 1
|
96
|
-
|
97
|
-
extra_pages.times do |page|
|
98
|
-
base_response[:data] += client.request(
|
99
|
-
:get,
|
100
|
-
'/items',
|
101
|
-
'page[offset]' => items_per_page * (page + 1),
|
102
|
-
'page[limit]' => items_per_page
|
103
|
-
)[:data]
|
104
|
-
end
|
105
|
-
|
106
|
-
JsonApiDeserializer.new.deserialize(base_response)
|
107
|
-
end
|
108
|
-
|
109
|
-
def add_slug_field(field)
|
110
|
-
validators = {
|
111
|
-
unique: {}
|
112
|
-
}
|
113
|
-
|
114
|
-
validators[:required] = {} if field['validators']['required']
|
115
|
-
|
116
|
-
slug_field = client.fields.create(
|
117
|
-
field['item_type'],
|
118
|
-
field_type: 'slug',
|
119
|
-
appeareance: { title_field_id: field['id'] },
|
120
|
-
validators: validators,
|
121
|
-
position: 99,
|
122
|
-
api_key: 'slug',
|
123
|
-
label: 'Slug',
|
124
|
-
hint: '',
|
125
|
-
localized: field['localized']
|
126
|
-
)
|
127
|
-
|
128
|
-
client.fields.update(
|
129
|
-
slug_field['id'],
|
130
|
-
position: field['position'] + 1
|
131
|
-
)
|
132
|
-
end
|
133
|
-
|
134
|
-
def title_fields
|
135
|
-
@title_fields ||= item_types.map do |item_type|
|
136
|
-
fields = client.fields.all(item_type['id'])
|
137
|
-
|
138
|
-
any_slug_present = fields.any? do |f|
|
139
|
-
f['field_type'] == 'slug' || f['api_key'] == 'slug'
|
140
|
-
end
|
141
|
-
|
142
|
-
next if any_slug_present
|
143
|
-
fields.find do |field|
|
144
|
-
field['field_type'] == 'string' &&
|
145
|
-
field['appeareance']['type'] == 'title'
|
146
|
-
end
|
147
|
-
end.compact
|
148
|
-
end
|
149
|
-
|
150
|
-
def item_types
|
151
|
-
@item_types ||= client.item_types.all
|
152
|
-
end
|
153
|
-
end
|
154
|
-
end
|
155
|
-
end
|