dato-dump 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +7 -0
- data/bin/setup +8 -0
- data/dato-dump.gemspec +33 -0
- data/exe/dato-dump +5 -0
- data/lib/dato-dump.rb +4 -0
- data/lib/dato_dump/cli.rb +25 -0
- data/lib/dato_dump/client.rb +53 -0
- data/lib/dato_dump/configuration.rb +22 -0
- data/lib/dato_dump/dumper.rb +60 -0
- data/lib/dato_dump/entities_repo.rb +43 -0
- data/lib/dato_dump/field_type_dumper/boolean.rb +9 -0
- data/lib/dato_dump/field_type_dumper/date.rb +9 -0
- data/lib/dato_dump/field_type_dumper/date_time.rb +9 -0
- data/lib/dato_dump/field_type_dumper/file.rb +22 -0
- data/lib/dato_dump/field_type_dumper/float.rb +9 -0
- data/lib/dato_dump/field_type_dumper/image.rb +27 -0
- data/lib/dato_dump/field_type_dumper/integer.rb +9 -0
- data/lib/dato_dump/field_type_dumper/lat_lon.rb +9 -0
- data/lib/dato_dump/field_type_dumper/link.rb +11 -0
- data/lib/dato_dump/field_type_dumper/links.rb +11 -0
- data/lib/dato_dump/field_type_dumper/seo.rb +13 -0
- data/lib/dato_dump/field_type_dumper/string.rb +9 -0
- data/lib/dato_dump/field_type_dumper/text.rb +9 -0
- data/lib/dato_dump/field_type_dumper/video.rb +9 -0
- data/lib/dato_dump/json_api_entity.rb +76 -0
- data/lib/dato_dump/record_dumper.rb +45 -0
- data/lib/dato_dump/version.rb +3 -0
- metadata +222 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6efc5e3740820b43a45c824813d5959e98660eab
|
4
|
+
data.tar.gz: 299c79004a95820fd0a0084db649266ea7163f3c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f427f47bb2c8abf08161298916ba1860942b2d5c6a2cb2f405b0224fe37507e00e63cc425d21ab78c99d4be29978b416fd00e7e9e4095150ed5a92be8565fc08
|
7
|
+
data.tar.gz: 57bc833517cc1f94296e572432797bbc23585ea6669aece66d828330318dd8e7361f23a63328e4eb30fafca6b18f8150d809ca3ef8d235150970951b18c2fc1b
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at s.verna@cantierecreativo.net. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Stefano Verna
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Dato
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/dato`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'dato'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install dato
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dato. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/dato-dump.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'dato_dump/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "dato-dump"
|
8
|
+
spec.version = DatoDump::VERSION
|
9
|
+
spec.authors = ["Stefano Verna"]
|
10
|
+
spec.email = ["s.verna@cantierecreativo.net"]
|
11
|
+
|
12
|
+
spec.summary = %q{Dump a DatoCMS space as data files into your static website generator tool.}
|
13
|
+
spec.description = %q{Dump a DatoCMS space as data files into your static website generator tool.}
|
14
|
+
spec.homepage = "https://github.com/datocms/dato-dump"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
|
26
|
+
spec.add_runtime_dependency "thor"
|
27
|
+
spec.add_runtime_dependency "dotenv"
|
28
|
+
spec.add_runtime_dependency "colored"
|
29
|
+
spec.add_runtime_dependency "faraday"
|
30
|
+
spec.add_runtime_dependency "faraday_middleware"
|
31
|
+
spec.add_runtime_dependency "activesupport"
|
32
|
+
spec.add_runtime_dependency "imgix"
|
33
|
+
end
|
data/exe/dato-dump
ADDED
data/lib/dato-dump.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'dotenv'
|
3
|
+
require 'dato_dump/configuration'
|
4
|
+
require 'dato_dump/client'
|
5
|
+
require 'dato_dump/entities_repo'
|
6
|
+
require 'dato_dump/dumper'
|
7
|
+
|
8
|
+
Dotenv.load
|
9
|
+
|
10
|
+
module DatoDump
|
11
|
+
class Cli < Thor
|
12
|
+
desc "dump", "Dumps space records into local JSON files"
|
13
|
+
option :watch, type: :boolean
|
14
|
+
option :config, type: :string, default: "dato.yml"
|
15
|
+
def produce
|
16
|
+
configuration = Configuration.new(options[:config])
|
17
|
+
client = Client.new(configuration)
|
18
|
+
repo = EntitiesRepo.new(client.space, client.records)
|
19
|
+
dumper = Dumper.new(repo, configuration)
|
20
|
+
dumper.dump
|
21
|
+
# rescue Exception => error
|
22
|
+
# raise Thor::Error, error.message
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'faraday_middleware'
|
3
|
+
require 'json'
|
4
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
5
|
+
|
6
|
+
module DatoDump
|
7
|
+
class Client
|
8
|
+
def initialize(configuration)
|
9
|
+
@host = configuration.api_host
|
10
|
+
@domain = configuration.domain
|
11
|
+
@token = configuration.token
|
12
|
+
end
|
13
|
+
|
14
|
+
def space
|
15
|
+
include = [
|
16
|
+
'content_types',
|
17
|
+
'content_types.fields'
|
18
|
+
]
|
19
|
+
get('space', include: include).body.with_indifferent_access
|
20
|
+
end
|
21
|
+
|
22
|
+
def records
|
23
|
+
get('records', 'page[limit]' => 10_000).body.with_indifferent_access
|
24
|
+
end
|
25
|
+
|
26
|
+
def get(*args)
|
27
|
+
connection.get(*args)
|
28
|
+
rescue Faraday::ClientError => e
|
29
|
+
body = JSON.parse(e.response[:body])
|
30
|
+
puts JSON.pretty_generate(body)
|
31
|
+
raise e
|
32
|
+
end
|
33
|
+
|
34
|
+
def connection
|
35
|
+
options = {
|
36
|
+
url: @host,
|
37
|
+
headers: {
|
38
|
+
'Content-Type' => 'application/json',
|
39
|
+
'Accept' => 'application/json',
|
40
|
+
'X-Space-Domain' => @domain,
|
41
|
+
'Authorization' => "Api-Key #{@token}"
|
42
|
+
}
|
43
|
+
}
|
44
|
+
@connection ||= Faraday.new(options) do |c|
|
45
|
+
c.request :json
|
46
|
+
c.response :json, content_type: /\bjson$/
|
47
|
+
c.response :raise_error
|
48
|
+
c.adapter :net_http
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module DatoDump
|
4
|
+
class Configuration
|
5
|
+
attr_reader :domain, :token, :api_host, :destination_path
|
6
|
+
|
7
|
+
def initialize(path)
|
8
|
+
File.exist?(path) or
|
9
|
+
raise RuntimeError, "config file #{path} is missing"
|
10
|
+
|
11
|
+
data = YAML.load_file(path)
|
12
|
+
|
13
|
+
data.is_a? Hash or
|
14
|
+
raise RuntimeError, "config file #{path} must be a hash"
|
15
|
+
|
16
|
+
@domain = data.fetch('domain')
|
17
|
+
@token = data.fetch('token')
|
18
|
+
@api_host = data.fetch('api_host', 'http://api.datocms.com')
|
19
|
+
@destination_path = data.fetch('destination_path', '_data')
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'active_support/core_ext/string'
|
2
|
+
require 'json'
|
3
|
+
require 'dato_dump/record_dumper'
|
4
|
+
|
5
|
+
module DatoDump
|
6
|
+
class Dumper
|
7
|
+
attr_reader :repo, :configuration
|
8
|
+
|
9
|
+
def initialize(repo, configuration)
|
10
|
+
@repo = repo
|
11
|
+
@configuration = configuration
|
12
|
+
end
|
13
|
+
|
14
|
+
def dump
|
15
|
+
collections_by_type = {}
|
16
|
+
|
17
|
+
content_types.each do |content_type|
|
18
|
+
key, singleton = content_type_key(content_type)
|
19
|
+
collections_by_type[key] = singleton ? nil : []
|
20
|
+
end
|
21
|
+
|
22
|
+
records.each do |record|
|
23
|
+
key, singleton = content_type_key(record.content_type)
|
24
|
+
if singleton
|
25
|
+
collections_by_type[key] = dump_entity(record)
|
26
|
+
else
|
27
|
+
collections_by_type[key].push dump_entity(record)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
collections_by_type.each do |key, value|
|
32
|
+
path = File.join(configuration.destination_path, key + ".yml")
|
33
|
+
File.open(path, 'w') do |file|
|
34
|
+
file.write JSON.pretty_generate(value)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def dump_entity(record)
|
40
|
+
RecordDumper.new(record, repo).dump
|
41
|
+
end
|
42
|
+
|
43
|
+
def content_type_key(content_type)
|
44
|
+
api_key = content_type.api_key
|
45
|
+
if content_type.singleton
|
46
|
+
[api_key, true]
|
47
|
+
else
|
48
|
+
[api_key.pluralize, false]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def content_types
|
53
|
+
@content_types ||= repo.find_entities_of_type('content_type')
|
54
|
+
end
|
55
|
+
|
56
|
+
def records
|
57
|
+
@records ||= repo.find_entities_of_type('record')
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'dato_dump/json_api_entity'
|
2
|
+
|
3
|
+
module DatoDump
|
4
|
+
class EntitiesRepo
|
5
|
+
attr_reader :entities
|
6
|
+
|
7
|
+
def initialize(*payloads)
|
8
|
+
@entities = {}
|
9
|
+
|
10
|
+
payloads.each do |payload|
|
11
|
+
EntitiesRepo.payload_entities(payload).each do |entity_payload|
|
12
|
+
object = JsonApiEntity.new(entity_payload, self)
|
13
|
+
@entities[object.type] ||= {}
|
14
|
+
@entities[object.type][object.id] = object
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def find_entities_of_type(type)
|
20
|
+
entities.fetch(type, {}).values
|
21
|
+
end
|
22
|
+
|
23
|
+
def find_entity(type, id)
|
24
|
+
entities.fetch(type, {}).fetch(id, nil)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.payload_entities(payload)
|
28
|
+
acc = []
|
29
|
+
|
30
|
+
if payload[:data]
|
31
|
+
acc = if payload[:data].is_a? Array
|
32
|
+
acc + payload[:data]
|
33
|
+
else
|
34
|
+
acc + [payload[:data]]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
acc += payload[:included] if payload[:included]
|
39
|
+
|
40
|
+
acc
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'imgix'
|
2
|
+
|
3
|
+
module DatoDump
|
4
|
+
module FieldTypeDumper
|
5
|
+
class File
|
6
|
+
def self.dump(value, _repo)
|
7
|
+
url = Imgix::Client.new(
|
8
|
+
host: 'dato-images.imgix.net',
|
9
|
+
secure: true
|
10
|
+
)
|
11
|
+
.path(value[:path])
|
12
|
+
.to_url
|
13
|
+
|
14
|
+
{
|
15
|
+
format: value[:format],
|
16
|
+
size: value[:size],
|
17
|
+
url: url
|
18
|
+
}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module DatoDump
|
2
|
+
module FieldTypeDumper
|
3
|
+
class Image
|
4
|
+
|
5
|
+
def self.dump(value, _repo)
|
6
|
+
if value
|
7
|
+
url = Imgix::Client.new(
|
8
|
+
host: 'dato-images.imgix.net',
|
9
|
+
secure: true
|
10
|
+
)
|
11
|
+
.path(value[:path])
|
12
|
+
.ch('DPR', 'Width')
|
13
|
+
.auto('compress', 'format')
|
14
|
+
.to_url
|
15
|
+
|
16
|
+
{
|
17
|
+
format: value[:format],
|
18
|
+
size: value[:size],
|
19
|
+
width: value[:width],
|
20
|
+
height: value[:height],
|
21
|
+
url: url
|
22
|
+
}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module DatoDump
|
2
|
+
class JsonApiEntity
|
3
|
+
attr_reader :payload, :data_source
|
4
|
+
|
5
|
+
def initialize(payload, data_source)
|
6
|
+
@payload = payload
|
7
|
+
@data_source = data_source
|
8
|
+
end
|
9
|
+
|
10
|
+
def id
|
11
|
+
@payload[:id]
|
12
|
+
end
|
13
|
+
|
14
|
+
def type
|
15
|
+
@payload[:type]
|
16
|
+
end
|
17
|
+
|
18
|
+
def ==(other)
|
19
|
+
if other.is_a? JsonApiEntity
|
20
|
+
id == other.id && type == other.type
|
21
|
+
else
|
22
|
+
false
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_s
|
27
|
+
"#<JsonApiEntity id=#{id} type=#{type} payload=#{payload}>"
|
28
|
+
end
|
29
|
+
alias inspect to_s
|
30
|
+
|
31
|
+
def [](key)
|
32
|
+
attributes[key]
|
33
|
+
end
|
34
|
+
|
35
|
+
def respond_to?(method, include_private = false)
|
36
|
+
if attributes.key?(method) || relationships.key?(method)
|
37
|
+
true
|
38
|
+
else
|
39
|
+
super
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def attributes
|
44
|
+
@payload.fetch(:attributes, {})
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def relationships
|
50
|
+
@payload.fetch(:relationships, {})
|
51
|
+
end
|
52
|
+
|
53
|
+
def dereference_linkage(linkage)
|
54
|
+
if linkage.is_a? Array
|
55
|
+
linkage.map do |item|
|
56
|
+
data_source.find_entity(item[:type], item[:id])
|
57
|
+
end
|
58
|
+
else
|
59
|
+
data_source.find_entity(linkage[:type], linkage[:id])
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def method_missing(method, *arguments, &block)
|
64
|
+
return super unless arguments.empty?
|
65
|
+
|
66
|
+
if attributes.key?(method)
|
67
|
+
attributes[method]
|
68
|
+
elsif relationships.key?(method)
|
69
|
+
dereference_linkage(relationships[method][:data])
|
70
|
+
else
|
71
|
+
super
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
@@ -0,0 +1,45 @@
|
|
1
|
+
Dir[File.dirname(__FILE__) + '/field_type_dumper/*.rb'].each do |file|
|
2
|
+
require file
|
3
|
+
end
|
4
|
+
|
5
|
+
module DatoDump
|
6
|
+
class RecordDumper
|
7
|
+
attr_reader :record, :repo
|
8
|
+
|
9
|
+
def initialize(record, repo)
|
10
|
+
@record = record
|
11
|
+
@repo = repo
|
12
|
+
end
|
13
|
+
|
14
|
+
def dump
|
15
|
+
attributes = { id: record.id }
|
16
|
+
record.content_type.fields.each do |field|
|
17
|
+
attributes[field.api_key] = dump_attribute(
|
18
|
+
record.send(field.api_key),
|
19
|
+
field
|
20
|
+
)
|
21
|
+
end
|
22
|
+
attributes.to_h
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def dump_attribute(value, field)
|
28
|
+
field_type = field.field_type
|
29
|
+
type_klass_name = "::DatoDump::FieldTypeDumper::#{field_type.camelize}"
|
30
|
+
type_klass = type_klass_name.safe_constantize
|
31
|
+
|
32
|
+
if type_klass
|
33
|
+
if field.localized
|
34
|
+
(value || {}).reduce({}) do |acc, (locale, locale_value)|
|
35
|
+
locale_value && type_klass.dump(locale_value, repo)
|
36
|
+
end
|
37
|
+
else
|
38
|
+
value && type_klass.dump(value, repo)
|
39
|
+
end
|
40
|
+
else
|
41
|
+
raise "Cannot convert field `#{value}` of type `#{field_type}`"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,222 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dato-dump
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stefano Verna
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: thor
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: dotenv
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: colored
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: faraday
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: faraday_middleware
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: activesupport
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :runtime
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: imgix
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :runtime
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
description: Dump a DatoCMS space as data files into your static website generator
|
154
|
+
tool.
|
155
|
+
email:
|
156
|
+
- s.verna@cantierecreativo.net
|
157
|
+
executables:
|
158
|
+
- dato-dump
|
159
|
+
extensions: []
|
160
|
+
extra_rdoc_files: []
|
161
|
+
files:
|
162
|
+
- ".gitignore"
|
163
|
+
- ".rspec"
|
164
|
+
- ".travis.yml"
|
165
|
+
- CODE_OF_CONDUCT.md
|
166
|
+
- Gemfile
|
167
|
+
- LICENSE.txt
|
168
|
+
- README.md
|
169
|
+
- Rakefile
|
170
|
+
- bin/console
|
171
|
+
- bin/setup
|
172
|
+
- dato-dump.gemspec
|
173
|
+
- exe/dato-dump
|
174
|
+
- lib/dato-dump.rb
|
175
|
+
- lib/dato_dump/cli.rb
|
176
|
+
- lib/dato_dump/client.rb
|
177
|
+
- lib/dato_dump/configuration.rb
|
178
|
+
- lib/dato_dump/dumper.rb
|
179
|
+
- lib/dato_dump/entities_repo.rb
|
180
|
+
- lib/dato_dump/field_type_dumper/boolean.rb
|
181
|
+
- lib/dato_dump/field_type_dumper/date.rb
|
182
|
+
- lib/dato_dump/field_type_dumper/date_time.rb
|
183
|
+
- lib/dato_dump/field_type_dumper/file.rb
|
184
|
+
- lib/dato_dump/field_type_dumper/float.rb
|
185
|
+
- lib/dato_dump/field_type_dumper/image.rb
|
186
|
+
- lib/dato_dump/field_type_dumper/integer.rb
|
187
|
+
- lib/dato_dump/field_type_dumper/lat_lon.rb
|
188
|
+
- lib/dato_dump/field_type_dumper/link.rb
|
189
|
+
- lib/dato_dump/field_type_dumper/links.rb
|
190
|
+
- lib/dato_dump/field_type_dumper/seo.rb
|
191
|
+
- lib/dato_dump/field_type_dumper/string.rb
|
192
|
+
- lib/dato_dump/field_type_dumper/text.rb
|
193
|
+
- lib/dato_dump/field_type_dumper/video.rb
|
194
|
+
- lib/dato_dump/json_api_entity.rb
|
195
|
+
- lib/dato_dump/record_dumper.rb
|
196
|
+
- lib/dato_dump/version.rb
|
197
|
+
homepage: https://github.com/datocms/dato-dump
|
198
|
+
licenses:
|
199
|
+
- MIT
|
200
|
+
metadata: {}
|
201
|
+
post_install_message:
|
202
|
+
rdoc_options: []
|
203
|
+
require_paths:
|
204
|
+
- lib
|
205
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
206
|
+
requirements:
|
207
|
+
- - ">="
|
208
|
+
- !ruby/object:Gem::Version
|
209
|
+
version: '0'
|
210
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
211
|
+
requirements:
|
212
|
+
- - ">="
|
213
|
+
- !ruby/object:Gem::Version
|
214
|
+
version: '0'
|
215
|
+
requirements: []
|
216
|
+
rubyforge_project:
|
217
|
+
rubygems_version: 2.5.1
|
218
|
+
signing_key:
|
219
|
+
specification_version: 4
|
220
|
+
summary: Dump a DatoCMS space as data files into your static website generator tool.
|
221
|
+
test_files: []
|
222
|
+
has_rdoc:
|