dato 0.1.8 → 0.1.9

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
  SHA1:
3
- metadata.gz: aa6cd26ac346a262967adba55486b614dec900d2
4
- data.tar.gz: 5df73d12129516a164298059aee45ee57c21a98f
3
+ metadata.gz: a123bdb067779fadf0d29ab434db81b9f33f48d7
4
+ data.tar.gz: af8b3f7ce4632f450b3081040f900ba8fa1146ef
5
5
  SHA512:
6
- metadata.gz: b251136ad1db77b6ba2d98adb07a8557980ca829871f11726a9cc8c0881265212eb2783c9fc2768cbd7096527515aa89f8b6a8369e3dbba37e73533becb6320a
7
- data.tar.gz: ce4c415f552fdafefaea3ba81b158b34e17e86fc669ffb7d0a3f417134e22294ae88056ffd4c69df822f7cea9d6ee9b3af42018f8277fde281a60c76592342ce
6
+ metadata.gz: 7dd0d16ffb9cabf60d28c3805099af9fe36ebfa162d4c59d61d1e57d58948fbf3f5863b34393205e3e1a611333ae9a4cd6bba12b76f9fb0aa2c3256aa155b8a1
7
+ data.tar.gz: 49362731b3cb91401b040601daad608ca09baaf68cb22fb522a3751ab54772ad01927deebde0484e7107e3756eeec554d93aa113b08dab38c6506974fddbee90
data/README.md CHANGED
@@ -1,69 +1,23 @@
1
1
  # DatoCMS Ruby Client
2
2
 
3
- [![Build Status](https://travis-ci.org/datocms/ruby-datocms-client.svg?branch=master)](https://travis-ci.org/datocms/ruby-datocms-client)
3
+ [![Build Status](https://travis-ci.org/datocms/ruby-datocms-client.svg?branch=master)](https://travis-ci.org/datocms/ruby-datocms-client) [![Gem Version](https://badge.fury.io/rb/dato.svg)](https://badge.fury.io/rb/dato)
4
4
 
5
- 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.
5
+ Ruby client for the DatoCMS API.
6
6
 
7
- TODO: Delete this and the text above, and describe your gem
7
+ [DatoCMS](https://www.datocms.com/) is a fully customizable administrative area for your static websites:
8
8
 
9
- ## Installation
10
-
11
- Add this line to your application's Gemfile:
12
-
13
- ```ruby
14
- gem 'dato'
15
- ```
16
-
17
- And then execute:
18
-
19
- $ bundle
20
-
21
- Or install it yourself as:
22
-
23
- $ gem install dato
9
+ 1. Use your favorite static website generator (Middleman, Hugo, Jekyll, and many others);
10
+ 2. Let your clients publish new content independently;
11
+ 3. Let your site build with any Continuous Deployment service (Netlify, Gitlab, CircleCI, etc.);
12
+ 4. Host the site anywhere you like (Amazon S3, Netlify, Surge.sh, etc.)
24
13
 
25
14
  ## Usage
26
15
 
27
- ```ruby
28
- client = Dato::Site::Client.new("YOUR_SITE_API_TOKEN")
29
-
30
- article_type = client.item_types.create(
31
- name: "Article",
32
- singleton: false,
33
- sortable: false,
34
- api_key: "article"
35
- )
36
-
37
- client.fields.create(
38
- article_type[:id],
39
- api_key: "title",
40
- field_type: "string",
41
- appeareance: { type: "title" },
42
- label: "Title",
43
- localized: false,
44
- position: 99,
45
- hint: "",
46
- validators: { required: {} },
47
- )
48
-
49
- client.fields.create(
50
- article_type[:id],
51
- api_key: "image",
52
- field_type: "image",
53
- appeareance: nil,
54
- label: "Image",
55
- localized: false,
56
- position: 99,
57
- hint: "",
58
- validators: { required: {} },
59
- )
60
-
61
- client.items.create(
62
- item_type: article_type[:id],
63
- title: "First post!",
64
- image: client.upload_image("http://i.giphy.com/NXOF5rlaSXdAc.gif")
65
- )
66
- ```
16
+ This gem can be used in different ways, so the documentation is split up in different files:
17
+
18
+ * [I want to use the content of a DatoCMS site in my static website (Hugo, Jeckyll, etc.)](https://github.com/datocms/ruby-datocms-client/blob/master/docs/dato-cli.md);
19
+ * [I want to edit the contents of an existing DatoCMS site programmatically](https://github.com/datocms/ruby-datocms-client/blob/master/docs/site-api-client.md);
20
+ * [I want to create new DatoCMS sites programmatically](https://github.com/datocms/ruby-datocms-client/blob/master/docs/account-api-client.md).
67
21
 
68
22
  ## Development
69
23
 
@@ -71,11 +25,14 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
71
25
 
72
26
  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).
73
27
 
28
+ ### Updating the client when the API changes
29
+
30
+ The DatoCMS API provides an always up-to-date [JSON Hyperschema](http://json-schema.org/latest/json-schema-hypermedia.html): the code of this gem is generated automatically starting from the schema running `rake regenerate`.
31
+
74
32
  ## Contributing
75
33
 
76
34
  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.
77
35
 
78
-
79
36
  ## License
80
37
 
81
38
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -10,7 +10,7 @@ RSpec::Core::RakeTask.new(:spec)
10
10
  task default: :spec
11
11
 
12
12
  desc 'Regenerates the client starting from the JSON Hyperschema'
13
- task :build_repos do
13
+ task :regenerate do
14
14
  BuildClient.new(
15
15
  open('https://site-api.datocms.com/docs/site-api-hyperschema.json').read,
16
16
  'site',
@@ -0,0 +1,53 @@
1
+ # Create/edit sites within a DatoCMS account
2
+
3
+ With this gem, you can easily create, edit and destroy DatoCMS sites, as well as editing your account settings.
4
+
5
+ # Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'dato'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install dato
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require "dato"
25
+
26
+ # fetch existing sites
27
+ sites = client.sites.all
28
+
29
+ # create a new site
30
+ site = client.sites.create(name: 'Foobar')
31
+
32
+ # update an existing site
33
+ client.sites.update(site[:id], site.merge(name: 'Blog'))
34
+
35
+ # destroy an existing site
36
+ client.sites.destroy(new_site[:id])
37
+ ```
38
+
39
+ ## List of client methods
40
+
41
+ ```ruby
42
+ client.account.find
43
+ client.account.create(resource_attributes)
44
+ client.account.update(resource_attributes)
45
+ client.account.reset_password(resource_attributes)
46
+
47
+ client.sites.find(site_id)
48
+ client.sites.all
49
+ client.sites.create(resource_attributes)
50
+ client.sites.update(site_id, resource_attributes)
51
+ client.sites.destroy(site_id)
52
+ client.sites.duplicate(site_id, resource_attributes)
53
+ ```
@@ -0,0 +1,226 @@
1
+ # Integrating DatoCMS with your static website generator
2
+
3
+ [DatoCMS](https://www.datocms.com) is working hard to provide the easiest way to enable non-technical editors to update a completely static website — without the intervention of a developer — from the comfort of a user-friendly web interface, just like they're used with Wordpress and such.
4
+
5
+ Middleman, Hugo, Jekyll, Hexo, Pelican, Octopress, GatsbyJS... the list of static site generators is [almost endless](https://www.staticgen.com/) and keeps on growing.
6
+
7
+ This gem provides an easy way to integrate content coming from a DatoCMS into virtually any static website generator.
8
+
9
+ # How it works
10
+
11
+ All the websites built with a static website generator are made of:
12
+
13
+ * Static files which represent the actual content of the pages (usually written in Markdown + [front matter](https://jekyllrb.com/docs/frontmatter/), YAML, JSON or Toml);
14
+ * Some HTML templates that use these files to generate the actual static HTML pages you will upload online.
15
+
16
+ That means that, up until now, even the most basic change to a static website could only be performed by a tech-savvy user, as too many things had to be known (Git, Markdown syntax, proper editing of YAML/JSON/Toml files).
17
+
18
+ DatoCMS works differently:
19
+
20
+ 1. You create a web administrative interface for your editors that fits exactly the needs of your static website;
21
+ 2. Editors can make changes to the content of the website from that CMS interface you prepared;
22
+ 3. Using this gem, all the data stored in your DatoCMS administrative interface can be transformed into local Markdown/YAML/JSON/Toml files, so that can be "digested" by the static website generator just as they were written by hand.
23
+
24
+ The process of translating the data coming from the API into static files can be performed both on your machine during the development process of the website, and in your Continuous Deployment service anytime the editors request a new "build" pressing a "Publish" button on the web interface.
25
+
26
+ Now your static website isn't static anymore! Isn't this awesome?! :-)
27
+
28
+ ## Installing the CLI tool
29
+
30
+ Once you have a working Ruby environment, you can use the CLI tool running these commands within your static website directory:
31
+
32
+ ```
33
+ gem install bundler
34
+ bundle init
35
+ echo 'gem "dato"' >> Gemfile
36
+ bundle install
37
+ ```
38
+
39
+ If everything worked correctly, you should now run `bundle exec dato` and see something like this:
40
+
41
+ ```
42
+ $ bundle exec dato
43
+ DatoCMS commands:
44
+ dato dump --token=TOKEN # dumps DatoCMS contents into local files
45
+ dato help [COMMAND] # Describe available commands or one specific command
46
+ ```
47
+
48
+ Hurray!!
49
+
50
+ ## Step-by-step integration guide
51
+
52
+ Now, just to make things more down-to-heart, suppose we're working with a Hugo website with the following structure:
53
+
54
+ ```
55
+ .
56
+ ├── config.toml
57
+ ├── content
58
+ | ├── post
59
+ | | ├── first-post.md
60
+ | | └── ...
61
+ | └── quote
62
+ | | ├── first-quote.md
63
+ | | └── ...
64
+ ├── data
65
+ | └── author
66
+ | ├── mark.toml
67
+ | └── ...
68
+ ├── layouts
69
+ | └── ...
70
+ └── static
71
+ └── ...
72
+ ```
73
+
74
+ Our job is to generate the Markdown files in the `content` directory from the data contained in our DatoCMS site. Also the Toml files contained in the in the `data` directory need the same treatment.
75
+
76
+ ### Set up the site
77
+
78
+ Using the DatoCMS web interface, we first create the following Item types:
79
+
80
+ * post
81
+ - title (string, required, title)
82
+ - publication_date (date, required)
83
+ - body (text, required)
84
+
85
+ * author
86
+ - name (string, required, title)
87
+ - bio (text, required)
88
+
89
+ * quote
90
+ - content (text, required)
91
+ - author (link to author, required)
92
+
93
+ ### Writing the config file
94
+
95
+ We then define how content stored in DatoCMS needs to be translated into local files using a config file placed in your project root folder called `dato.config.rb`.
96
+
97
+ Let's start with posts:
98
+
99
+ ```ruby
100
+ directory "content/post" do
101
+ dato.posts.each do |item|
102
+ create_post "#{item.slug}.md" do
103
+ frontmatter :toml,
104
+ title: item.title,
105
+ date: item.publication_date.to_s
106
+
107
+ content item.body
108
+ end
109
+ end
110
+ end
111
+ ```
112
+
113
+ The DSL is quite terse! Basically we're declaring that:
114
+
115
+ 1. we want to take possess of the directory `content/post` and manage it programmatically from now on;
116
+ 2. we then iterate over each post stored in DatoCMS and...
117
+ 3. for each post, we create a local markdown file:
118
+ - named after the slugified version of the post title;
119
+ - that contains the post body;
120
+ - decorated with a Toml front matter with a `title` and `date` keys;
121
+
122
+ Now we can run the following command:
123
+
124
+ ```
125
+ $ bundle exec dato dump --token=SITE_READONLY_TOKEN
126
+ ```
127
+
128
+ And see the `content/post` directory emptied from previous content and filled with new files:
129
+
130
+ ```
131
+ .
132
+ └── content
133
+ └── post
134
+ ├── lorem-ipsum.md
135
+ ├── dolor-sit-amet.md
136
+ ├── consectetur-adipisci.md
137
+ └── ...
138
+ ```
139
+
140
+ Let's open one of those Markdown files:
141
+
142
+ ```
143
+ +++
144
+ title = "Lorem ipsum"
145
+ date = "2001-02-03"
146
+ +++
147
+
148
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
149
+
150
+ Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
151
+ ```
152
+
153
+ Awesome!! We can now continue using Hugo just like we're used to.
154
+
155
+ ### A more complete config file
156
+
157
+ The config file let us declare different kind of "operations":
158
+
159
+ | Command | Description |
160
+ | --- | --- |
161
+ | `create_post` | Generate Markdown + Frontmatter files |
162
+ | `create_data_file` | Generate plain Toml/YAML files |
163
+ | `add_to_data_file` | Add some keys to an existing Toml/YAML file |
164
+
165
+ In fact, this is a more complete version of our `dato.config.rb` file:
166
+
167
+ ```ruby
168
+ directory "content/post" do
169
+ dato.posts.each do |item|
170
+ create_post "#{item.slug}.md" do
171
+ frontmatter :toml,
172
+ title: item.title,
173
+ date: item.publication_date.to_s
174
+
175
+ content item.body
176
+ end
177
+ end
178
+ end
179
+
180
+ directory "content/quote" do
181
+ dato.quotes.each_with_index do |item, i|
182
+ create_post "#{item.id}.md" do
183
+ frontmatter :toml,
184
+ title: "Quote number #{i}",
185
+ weight: i
186
+
187
+ content item.content
188
+ end
189
+ end
190
+ end
191
+
192
+ directory "data/authors" do
193
+ dato.authors.each do |item|
194
+ create_data_file "#{item.name.slug}.toml", :toml,
195
+ name: item.name,
196
+ bio: item.bio
197
+ end
198
+ end
199
+ end
200
+ ```
201
+
202
+ ### How to fetch data from DatoCMS
203
+
204
+ Using the `dato` method you can access to any item stored in your site grouped by item type. That is, if your site has an Item Type with `post` as API identifier, you can get the complete array of items with `dato.posts` (the pluralized API identifier).
205
+
206
+ If a Item Type is marked as "single instance" (ie. `about_page`) you don't need to pluralize and a call to `dato.about_page` directly returns the item (or `nil`, if still hasn't been created within the CMS).
207
+
208
+ You can query an item's field value with a method called like the field API identifier.
209
+
210
+ An item also features a `.slug` method:
211
+
212
+ * if an item type has a field of type `string` with a "Title" Presentation mode, than the method returns the slugified version of the title itself;
213
+ * otherwise, it just returns the unique identifier of the item;
214
+
215
+ Complex field types (ie. `image`, `file`, `video`, `seo`) implement specific methods you can use as well within the config file:
216
+
217
+ ```
218
+ article = dato.articles.first
219
+
220
+ article.cover_image.file.width(500).fit('crop').to_url
221
+ article.video.iframe_embed(800, 600)
222
+ ```
223
+
224
+ ### Need more help?
225
+
226
+ Just ask! Send us an email to [support@datocms.com](mailto:support@datocms.com) and we'll be happy to guide you.
@@ -0,0 +1,123 @@
1
+ # Edit the contents of an existing DatoCMS site
2
+
3
+ With this gem, you can easily create, edit and destroy any object within a DatoCMS site:
4
+
5
+ * Item types
6
+ * Fields
7
+ * Items
8
+ * Menu items
9
+ * Users
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'dato'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install dato
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ require "dato"
31
+
32
+ # create a DatoCMS client
33
+ client = Dato::Site::Client.new("YOUR_SITE_API_READWRITE_TOKEN")
34
+
35
+ # create a new Article item type
36
+ article_type = client.item_types.create(
37
+ name: "Article",
38
+ singleton: false,
39
+ sortable: false,
40
+ api_key: "article"
41
+ )
42
+
43
+ # add a Title field to the Article item type
44
+ client.fields.create(
45
+ article_type[:id],
46
+ api_key: "title",
47
+ field_type: "string",
48
+ appeareance: { type: "title" },
49
+ label: "Title",
50
+ localized: false,
51
+ position: 99,
52
+ hint: "",
53
+ validators: { required: {} },
54
+ )
55
+
56
+ # add an Image field to the Article item type
57
+ client.fields.create(
58
+ article_type[:id],
59
+ api_key: "image",
60
+ field_type: "image",
61
+ appeareance: nil,
62
+ label: "Image",
63
+ localized: false,
64
+ position: 99,
65
+ hint: "",
66
+ validators: { required: {} },
67
+ )
68
+
69
+ # create a new Article
70
+ client.items.create(
71
+ item_type: article_type[:id],
72
+ title: "My first article!",
73
+ image: client.upload_image("http://i.giphy.com/NXOF5rlaSXdAc.gif")
74
+ )
75
+
76
+ # fetch and edit an existing Article
77
+ article = client.items.find("1234")
78
+ client.items.update("1234", article.merge(title: "New title"))
79
+
80
+ # destroy an existing article
81
+ client.items.destroy("1234")
82
+ ```
83
+
84
+ ## List of client methods
85
+
86
+ ```ruby
87
+ client.fields.create(item_type_id, resource_attributes)
88
+ client.fields.update(field_id, resource_attributes)
89
+ client.fields.all(item_type_id)
90
+ client.fields.find(field_id)
91
+ client.fields.destroy(field_id)
92
+
93
+ client.items.create(resource_attributes)
94
+ client.items.update(item_id, resource_attributes)
95
+ client.items.all(filters = {})
96
+ client.items.find(item_id)
97
+ client.items.destroy(item_id)
98
+
99
+ client.item_types.create(resource_attributes)
100
+ client.item_types.update(item_type_id, resource_attributes)
101
+ client.item_types.all
102
+ client.item_types.find(item_type_id)
103
+ client.item_types.destroy(item_type_id)
104
+
105
+ client.menu_items.create(resource_attributes)
106
+ client.menu_items.update(menu_item_id, resource_attributes)
107
+ client.menu_items.all
108
+ client.menu_items.find(menu_item_id)
109
+ client.menu_items.destroy(menu_item_id)
110
+
111
+ client.site.find
112
+ client.site.update(resource_attributes)
113
+
114
+ client.upload_image(path_or_url)
115
+ client.upload_file(path_or_url)
116
+
117
+ client.users.create(resource_attributes)
118
+ client.users.update(user_id, resource_attributes)
119
+ client.users.all
120
+ client.users.find(user_id)
121
+ client.users.reset_password(resource_attributes)
122
+ client.users.destroy(user_id)
123
+ ```
@@ -7,7 +7,7 @@ module Dato
7
7
  module Format
8
8
  module Yaml
9
9
  def self.dump(value)
10
- YAML.dump(value).chomp.gsub(/^\-+\n/, '')
10
+ YAML.dump(value.deep_stringify_keys).chomp.gsub(/^\-+\n/, '')
11
11
  end
12
12
 
13
13
  def self.frontmatter_dump(value)
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Dato
3
- VERSION = '0.1.8'
3
+ VERSION = '0.1.9'
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dato
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefano Verna
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-23 00:00:00.000000000 Z
11
+ date: 2016-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -296,6 +296,9 @@ files:
296
296
  - bin/console
297
297
  - bin/setup
298
298
  - dato.gemspec
299
+ - docs/account-api-client.md
300
+ - docs/dato-cli.md
301
+ - docs/site-api-client.md
299
302
  - exe/dato
300
303
  - lib/dato.rb
301
304
  - lib/dato/account/client.rb