dato 0.1.1 → 0.1.2

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: d8a6c609df22f55d3b16f9b62ea263f8c468d6fc
4
- data.tar.gz: b2e1455cfdea652ba8a7a3804fa689bc9381c200
3
+ metadata.gz: d2cfedd8e15769b9218672800af0046479d30ccc
4
+ data.tar.gz: eb04df8bdb2301f4359dbf138b7a285e42071499
5
5
  SHA512:
6
- metadata.gz: 7836eac085e22543e34ff3b2fe4f1b561b78e78236a3910e1db434221ff00daa59b92227428763dbbe0ed8f044e236374efd1c366d3d0e4e461610457a09da1a
7
- data.tar.gz: 02f5535bd868229c649639e5316a5bae3a25324be31bc2b75131cc2b8a464c9bb1cf0229547d35d1bbf83cc09753659d621dbc9130ff228eccf1b7df2f7d68e4
6
+ metadata.gz: 427934ad134d83a54495b354d8cf7b49b74278f7970b17ba8780eb73e0db49ca8d1c5d52c3df9c69561cf3a476f72b85384da243c3442b510ffc8845b63627cc
7
+ data.tar.gz: 3e5092deff45594a4539c7c757a90d5697273089775cb9436648fadd1d493ac1b10739cf29e28376907953350a1a542a4eb060537fa3d36dac0050138eab3e07
data/.gitignore CHANGED
@@ -6,5 +6,6 @@
6
6
  /doc/
7
7
  /pkg/
8
8
  /spec/reports/
9
+ /coverage/
9
10
  /tmp/
10
11
  build.zip
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
- # Dato
1
+ # DatoCMS Ruby Client
2
+
3
+ [![Build Status](https://travis-ci.org/datocms/ruby-datocms-client.svg?branch=master)](https://travis-ci.org/datocms/ruby-datocms-client)
2
4
 
3
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.
4
6
 
data/Rakefile CHANGED
@@ -8,7 +8,18 @@ RSpec::Core::RakeTask.new(:spec)
8
8
 
9
9
  task :default => :spec
10
10
 
11
- task :build_resources do
12
- schema = DownloadSchema.new.schema
13
- BuildClient.new(schema).build
11
+ task :build_repos do
12
+ downloader = DownloadSchema.new("y32upcTKLupUjXiC9nFW", "182")
13
+
14
+ BuildClient.new(
15
+ downloader.schema("backend-hyperschema.json"),
16
+ "site",
17
+ %w(session item)
18
+ ).build
19
+
20
+ BuildClient.new(
21
+ downloader.schema("frontend-hyperschema.json"),
22
+ "account",
23
+ %w(session item)
24
+ ).build
14
25
  end
data/dato.gemspec CHANGED
@@ -24,6 +24,9 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "rspec", "~> 3.0"
25
25
  spec.add_development_dependency "rubyzip"
26
26
  spec.add_development_dependency "json_schema"
27
+ spec.add_development_dependency "simplecov"
28
+ spec.add_development_dependency "vcr"
29
+ spec.add_development_dependency "webmock"
27
30
 
28
31
  spec.add_runtime_dependency "faraday", [">= 0.9.0"]
29
32
  spec.add_runtime_dependency "faraday_middleware", [">= 0.9.0"]
data/lib/dato.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require "dato/version"
2
- require "dato/client"
2
+
3
+ require "dato/site/client"
4
+ require "dato/account/client"
3
5
 
4
6
  module Dato
5
7
  end
@@ -0,0 +1,67 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+ require 'json'
4
+ require 'active_support/core_ext/hash/indifferent_access'
5
+
6
+ require 'dato/account/repo/account'
7
+ require 'dato/account/repo/site'
8
+
9
+ module Dato
10
+ module Account
11
+ class Client
12
+ REPOS = {
13
+ account: Repo::Account,
14
+ sites: Repo::Site,
15
+ }
16
+
17
+ attr_reader :token, :domain, :schema
18
+
19
+ def initialize(token, domain: 'https://account-api.datocms.com')
20
+ @domain = domain
21
+ @token = token
22
+ end
23
+
24
+ REPOS.each do |method_name, repo_klass|
25
+ define_method method_name do
26
+ instance_variable_set(
27
+ "@#{method_name}",
28
+ instance_variable_get("@#{method_name}") ||
29
+ repo_klass.new(self)
30
+ )
31
+ end
32
+ end
33
+
34
+ def request(*args)
35
+ begin
36
+ connection.send(*args).body.with_indifferent_access
37
+ rescue Faraday::ClientError => e
38
+ puts e.response
39
+ body = JSON.parse(e.response[:body])
40
+ puts JSON.pretty_generate(body)
41
+ raise e
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def connection
48
+ options = {
49
+ url: domain,
50
+ headers: {
51
+ 'Accept' => "application/json",
52
+ 'Content-Type' => "application/json",
53
+ 'Authorization' => "Bearer #{@token}"
54
+ }
55
+ }
56
+
57
+ @connection ||= Faraday.new(options) do |c|
58
+ c.request :json
59
+ c.response :json, content_type: /\bjson$/
60
+ c.response :raise_error
61
+ c.use FaradayMiddleware::FollowRedirects
62
+ c.adapter :net_http
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,44 @@
1
+ require 'dato/account/repo/base'
2
+
3
+ module Dato
4
+ module Account
5
+ module Repo
6
+ class Account < Base
7
+
8
+ def create(resource_attributes)
9
+ body = JsonApiSerializer.new(
10
+ type: :account,
11
+ attributes: %i(email password),
12
+ required_attributes: %i(email password),
13
+ ).serialize(resource_attributes)
14
+
15
+ post_request "/account", body
16
+ end
17
+
18
+ def update(resource_attributes)
19
+ body = JsonApiSerializer.new(
20
+ type: :account,
21
+ attributes: %i(email password),
22
+ ).serialize(resource_attributes)
23
+
24
+ put_request "/account", body
25
+ end
26
+
27
+ def find()
28
+ get_request "/account"
29
+ end
30
+
31
+ def reset_password(resource_attributes)
32
+ body = JsonApiSerializer.new(
33
+ type: :account,
34
+ attributes: %i(email),
35
+ required_attributes: %i(email),
36
+ ).serialize(resource_attributes)
37
+
38
+ post_request "/account/reset_password", body
39
+ end
40
+
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,27 @@
1
+ require "dato/json_api_serializer"
2
+ require "dato/json_api_deserializer"
3
+
4
+ module Dato
5
+ module Account
6
+ module Repo
7
+ class Base
8
+ attr_reader :client
9
+
10
+ def initialize(client)
11
+ @client = client
12
+ end
13
+
14
+ private
15
+
16
+ %i(post put delete get).each do |method|
17
+ define_method "#{method}_request" do |*args|
18
+ JsonApiDeserializer.new.deserialize(
19
+ client.request(method, *args)
20
+ )
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,41 @@
1
+ require 'dato/account/repo/base'
2
+
3
+ module Dato
4
+ module Account
5
+ module Repo
6
+ class Site < Base
7
+
8
+ def find(site_id)
9
+ get_request "/sites/#{site_id}"
10
+ end
11
+
12
+ def all()
13
+ get_request "/sites"
14
+ end
15
+
16
+ def create(resource_attributes)
17
+ body = JsonApiSerializer.new(
18
+ type: :site,
19
+ attributes: %i(domain name notes),
20
+ ).serialize(resource_attributes)
21
+
22
+ post_request "/sites", body
23
+ end
24
+
25
+ def update(site_id, resource_attributes)
26
+ body = JsonApiSerializer.new(
27
+ type: :site,
28
+ attributes: %i(domain name notes),
29
+ ).serialize(resource_attributes, site_id)
30
+
31
+ put_request "/sites/#{site_id}", body
32
+ end
33
+
34
+ def destroy(site_id)
35
+ delete_request "/sites/#{site_id}"
36
+ end
37
+
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,76 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+ require 'json'
4
+ require 'active_support/core_ext/hash/indifferent_access'
5
+
6
+ require 'dato/site/repo/field'
7
+ require 'dato/site/repo/item_type'
8
+ require 'dato/site/repo/menu_item'
9
+ require 'dato/site/repo/site'
10
+ require 'dato/site/repo/upload_request'
11
+ require 'dato/site/repo/user'
12
+ require 'dato/site/repo/item'
13
+
14
+ module Dato
15
+ module Site
16
+ class Client
17
+ REPOS = {
18
+ fields: Repo::Field,
19
+ item_types: Repo::ItemType,
20
+ menu_items: Repo::MenuItem,
21
+ site: Repo::Site,
22
+ upload_requests: Repo::UploadRequest,
23
+ users: Repo::User,
24
+ items: Repo::Item,
25
+ }
26
+
27
+ attr_reader :token, :domain, :schema
28
+
29
+ def initialize(token, domain: 'https://site-api.datocms.com')
30
+ @domain = domain
31
+ @token = token
32
+ end
33
+
34
+ REPOS.each do |method_name, repo_klass|
35
+ define_method method_name do
36
+ instance_variable_set(
37
+ "@#{method_name}",
38
+ instance_variable_get("@#{method_name}") ||
39
+ repo_klass.new(self)
40
+ )
41
+ end
42
+ end
43
+
44
+ def request(*args)
45
+ begin
46
+ connection.send(*args).body.with_indifferent_access
47
+ rescue Faraday::ClientError => e
48
+ body = JSON.parse(e.response[:body])
49
+ puts JSON.pretty_generate(body)
50
+ raise e
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def connection
57
+ options = {
58
+ url: domain,
59
+ headers: {
60
+ 'Accept' => "application/json",
61
+ 'Content-Type' => "application/json",
62
+ 'Authorization' => "Bearer #{@token}"
63
+ }
64
+ }
65
+
66
+ @connection ||= Faraday.new(options) do |c|
67
+ c.request :json
68
+ c.response :json, content_type: /\bjson$/
69
+ c.response :raise_error
70
+ c.use FaradayMiddleware::FollowRedirects
71
+ c.adapter :net_http
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,26 @@
1
+ require "dato/json_api_serializer"
2
+ require "dato/json_api_deserializer"
3
+
4
+ module Dato
5
+ module Site
6
+ module Repo
7
+ class Base
8
+ attr_reader :client
9
+
10
+ def initialize(client)
11
+ @client = client
12
+ end
13
+
14
+ private
15
+
16
+ %i(post put delete get).each do |method|
17
+ define_method "#{method}_request" do |*args|
18
+ JsonApiDeserializer.new.deserialize(
19
+ client.request(method, *args)
20
+ )
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,42 @@
1
+ require 'dato/site/repo/base'
2
+
3
+ module Dato
4
+ module Site
5
+ module Repo
6
+ class Field < Base
7
+
8
+ def create(item_type_id, resource_attributes)
9
+ body = JsonApiSerializer.new(
10
+ type: :field,
11
+ attributes: %i(api_key appeareance field_type hint label localized position validators),
12
+ required_attributes: %i(api_key appeareance field_type hint label localized position validators),
13
+ ).serialize(resource_attributes)
14
+
15
+ post_request "/item-types/#{item_type_id}/fields", body
16
+ end
17
+
18
+ def update(field_id, resource_attributes)
19
+ body = JsonApiSerializer.new(
20
+ type: :field,
21
+ attributes: %i(api_key appeareance hint label localized position validators),
22
+ ).serialize(resource_attributes, field_id)
23
+
24
+ put_request "/fields/#{field_id}", body
25
+ end
26
+
27
+ def all(item_type_id)
28
+ get_request "/item-types/#{item_type_id}/fields"
29
+ end
30
+
31
+ def find(field_id)
32
+ get_request "/fields/#{field_id}"
33
+ end
34
+
35
+ def destroy(field_id)
36
+ delete_request "/fields/#{field_id}"
37
+ end
38
+
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,53 @@
1
+ require 'dato/site/repo/base'
2
+ require 'active_support/core_ext/hash/except'
3
+
4
+ module Dato
5
+ module Site
6
+ module Repo
7
+ class Item < Base
8
+
9
+ def create(resource_attributes)
10
+ body = JsonApiSerializer.new(
11
+ type: :item,
12
+ attributes: resource_attributes.keys - [:item_type, :id],
13
+ relationships: {
14
+ item_type: { collection: false, type: "item_type" }
15
+ },
16
+ required_relationships: %i(item_type),
17
+ ).serialize(resource_attributes)
18
+
19
+ post_request "/items", body
20
+ end
21
+
22
+ def update(item_id, resource_attributes)
23
+ resource_attributes = resource_attributes.except(
24
+ :id,
25
+ :updated_at,
26
+ :is_valid,
27
+ :item_type
28
+ )
29
+
30
+ body = JsonApiSerializer.new(
31
+ type: :item,
32
+ attributes: resource_attributes.keys,
33
+ ).serialize(resource_attributes, item_id)
34
+
35
+ put_request "/items/#{item_id}", body
36
+ end
37
+
38
+ def all(filters)
39
+ get_request "/items", filters
40
+ end
41
+
42
+ def find(item_id)
43
+ get_request "/items/#{item_id}"
44
+ end
45
+
46
+ def destroy(item_id)
47
+ delete_request "/items/#{item_id}"
48
+ end
49
+
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,43 @@
1
+ require 'dato/site/repo/base'
2
+
3
+ module Dato
4
+ module Site
5
+ module Repo
6
+ class ItemType < Base
7
+
8
+ def create(resource_attributes)
9
+ body = JsonApiSerializer.new(
10
+ type: :item_type,
11
+ attributes: %i(api_key name singleton sortable),
12
+ required_attributes: %i(api_key name singleton sortable),
13
+ ).serialize(resource_attributes)
14
+
15
+ post_request "/item-types", body
16
+ end
17
+
18
+ def update(item_type_id, resource_attributes)
19
+ body = JsonApiSerializer.new(
20
+ type: :item_type,
21
+ attributes: %i(api_key name singleton sortable),
22
+ required_attributes: %i(api_key name singleton sortable),
23
+ ).serialize(resource_attributes, item_type_id)
24
+
25
+ put_request "/item-types/#{item_type_id}", body
26
+ end
27
+
28
+ def all()
29
+ get_request "/item-types"
30
+ end
31
+
32
+ def find(item_type_id)
33
+ get_request "/item-types/#{item_type_id}"
34
+ end
35
+
36
+ def destroy(item_type_id)
37
+ delete_request "/item-types/#{item_type_id}"
38
+ end
39
+
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,46 @@
1
+ require 'dato/site/repo/base'
2
+
3
+ module Dato
4
+ module Site
5
+ module Repo
6
+ class MenuItem < Base
7
+
8
+ def create(resource_attributes)
9
+ body = JsonApiSerializer.new(
10
+ type: :menu_item,
11
+ attributes: %i(label position),
12
+ relationships: {:item_type=>{:collection=>false, :type=>:item_type}, :parent=>{:collection=>false, :type=>:menu_item}},
13
+ required_attributes: %i(label position),
14
+ ).serialize(resource_attributes)
15
+
16
+ post_request "/menu-items", body
17
+ end
18
+
19
+ def update(menu_item_id, resource_attributes)
20
+ body = JsonApiSerializer.new(
21
+ type: :menu_item,
22
+ attributes: %i(label position),
23
+ relationships: {:item_type=>{:collection=>false, :type=>:item_type}, :parent=>{:collection=>false, :type=>:menu_item}},
24
+ required_attributes: %i(label position),
25
+ required_relationships: %i(item_type),
26
+ ).serialize(resource_attributes, menu_item_id)
27
+
28
+ put_request "/menu-items/#{menu_item_id}", body
29
+ end
30
+
31
+ def all()
32
+ get_request "/menu-items"
33
+ end
34
+
35
+ def find(menu_item_id)
36
+ get_request "/menu-items/#{menu_item_id}"
37
+ end
38
+
39
+ def destroy(menu_item_id)
40
+ delete_request "/menu-items/#{menu_item_id}"
41
+ end
42
+
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,24 @@
1
+ require 'dato/site/repo/base'
2
+
3
+ module Dato
4
+ module Site
5
+ module Repo
6
+ class Site < Base
7
+
8
+ def find()
9
+ get_request "/site"
10
+ end
11
+
12
+ def update(resource_attributes)
13
+ body = JsonApiSerializer.new(
14
+ type: :site,
15
+ attributes: %i(deploy_adapter deploy_settings favicon global_seo locales name no_index theme_hue timezone),
16
+ ).serialize(resource_attributes)
17
+
18
+ put_request "/site", body
19
+ end
20
+
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ require 'dato/site/repo/base'
2
+
3
+ module Dato
4
+ module Site
5
+ module Repo
6
+ class UploadRequest < Base
7
+
8
+ def create(resource_attributes)
9
+ body = JsonApiSerializer.new(
10
+ type: :upload_request,
11
+ attributes: %i(filename),
12
+ ).serialize(resource_attributes)
13
+
14
+ post_request "/upload-requests", body
15
+ end
16
+
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,52 @@
1
+ require 'dato/site/repo/base'
2
+
3
+ module Dato
4
+ module Site
5
+ module Repo
6
+ class User < Base
7
+
8
+ def create(resource_attributes)
9
+ body = JsonApiSerializer.new(
10
+ type: :user,
11
+ attributes: %i(email first_name is_admin last_name),
12
+ required_attributes: %i(email first_name last_name),
13
+ ).serialize(resource_attributes)
14
+
15
+ post_request "/users", body
16
+ end
17
+
18
+ def update(user_id, resource_attributes)
19
+ body = JsonApiSerializer.new(
20
+ type: :user,
21
+ attributes: %i(email first_name is_admin last_name password),
22
+ ).serialize(resource_attributes, user_id)
23
+
24
+ put_request "/users/#{user_id}", body
25
+ end
26
+
27
+ def all()
28
+ get_request "/users"
29
+ end
30
+
31
+ def find(user_id)
32
+ get_request "/users/#{user_id}"
33
+ end
34
+
35
+ def reset_password(resource_attributes)
36
+ body = JsonApiSerializer.new(
37
+ type: :user,
38
+ attributes: %i(email),
39
+ required_attributes: %i(email),
40
+ ).serialize(resource_attributes)
41
+
42
+ post_request "/users/reset_password", body
43
+ end
44
+
45
+ def destroy(user_id)
46
+ delete_request "/users/#{user_id}"
47
+ end
48
+
49
+ end
50
+ end
51
+ end
52
+ end
data/lib/dato/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dato
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  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.1
4
+ version: 0.1.2
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-07-15 00:00:00.000000000 Z
11
+ date: 2016-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,48 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
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: vcr
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
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: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
83
125
  - !ruby/object:Gem::Dependency
84
126
  name: faraday
85
127
  requirement: !ruby/object:Gem::Requirement
@@ -141,17 +183,21 @@ files:
141
183
  - bin/setup
142
184
  - dato.gemspec
143
185
  - lib/dato.rb
144
- - lib/dato/client.rb
186
+ - lib/dato/account/client.rb
187
+ - lib/dato/account/repo/account.rb
188
+ - lib/dato/account/repo/base.rb
189
+ - lib/dato/account/repo/site.rb
145
190
  - lib/dato/json_api_deserializer.rb
146
191
  - lib/dato/json_api_serializer.rb
147
- - lib/dato/repo/base.rb
148
- - lib/dato/repo/field.rb
149
- - lib/dato/repo/item.rb
150
- - lib/dato/repo/item_type.rb
151
- - lib/dato/repo/menu_item.rb
152
- - lib/dato/repo/site.rb
153
- - lib/dato/repo/upload_request.rb
154
- - lib/dato/repo/user.rb
192
+ - lib/dato/site/client.rb
193
+ - lib/dato/site/repo/base.rb
194
+ - lib/dato/site/repo/field.rb
195
+ - lib/dato/site/repo/item.rb
196
+ - lib/dato/site/repo/item_type.rb
197
+ - lib/dato/site/repo/menu_item.rb
198
+ - lib/dato/site/repo/site.rb
199
+ - lib/dato/site/repo/upload_request.rb
200
+ - lib/dato/site/repo/user.rb
155
201
  - lib/dato/version.rb
156
202
  homepage: https://github.com/datocms/ruby-datocms-client
157
203
  licenses:
@@ -178,3 +224,4 @@ signing_key:
178
224
  specification_version: 4
179
225
  summary: Ruby client for DatoCMS API
180
226
  test_files: []
227
+ has_rdoc:
data/lib/dato/client.rb DELETED
@@ -1,73 +0,0 @@
1
- require 'faraday'
2
- require 'faraday_middleware'
3
- require 'json'
4
- require 'active_support/core_ext/hash/indifferent_access'
5
-
6
- require 'dato/repo/field'
7
- require 'dato/repo/item_type'
8
- require 'dato/repo/menu_item'
9
- require 'dato/repo/site'
10
- require 'dato/repo/upload_request'
11
- require 'dato/repo/user'
12
- require 'dato/repo/item'
13
-
14
- module Dato
15
- class Client
16
- REPOS = {
17
- fields: Repo::Field,
18
- item_types: Repo::ItemType,
19
- menu_items: Repo::MenuItem,
20
- site: Repo::Site,
21
- upload_requests: Repo::UploadRequest,
22
- users: Repo::User,
23
- items: Repo::Item,
24
- }
25
-
26
- attr_reader :token, :domain, :schema
27
-
28
- def initialize(token, domain: 'http://site-api.datocms.com')
29
- @domain = domain
30
- @token = token
31
- end
32
-
33
- REPOS.each do |method_name, repo_klass|
34
- define_method method_name do
35
- instance_variable_set(
36
- "@#{method_name}",
37
- instance_variable_get("@#{method_name}") ||
38
- repo_klass.new(self)
39
- )
40
- end
41
- end
42
-
43
- def request(*args)
44
- begin
45
- connection.send(*args).body.with_indifferent_access
46
- rescue Faraday::ClientError => e
47
- body = JSON.parse(e.response[:body])
48
- puts JSON.pretty_generate(body)
49
- raise e
50
- end
51
- end
52
-
53
- private
54
-
55
- def connection
56
- options = {
57
- url: domain,
58
- headers: {
59
- 'Accept' => "application/json",
60
- 'Content-Type' => "application/json",
61
- 'Authorization' => "Bearer #{@token}"
62
- }
63
- }
64
-
65
- @connection ||= Faraday.new(options) do |c|
66
- c.request :json
67
- c.response :json, content_type: /\bjson$/
68
- c.response :raise_error
69
- c.adapter :net_http
70
- end
71
- end
72
- end
73
- end
@@ -1,24 +0,0 @@
1
- require "dato/json_api_serializer"
2
- require "dato/json_api_deserializer"
3
-
4
- module Dato
5
- module Repo
6
- class Base
7
- attr_reader :client
8
-
9
- def initialize(client)
10
- @client = client
11
- end
12
-
13
- private
14
-
15
- %i(post put delete get).each do |method|
16
- define_method "#{method}_request" do |*args|
17
- JsonApiDeserializer.new.deserialize(
18
- client.request(method, *args)
19
- )
20
- end
21
- end
22
- end
23
- end
24
- end
@@ -1,40 +0,0 @@
1
- require 'dato/repo/base'
2
-
3
- module Dato
4
- module Repo
5
- class Field < Base
6
-
7
- def create(item_type_id, resource_attributes)
8
- body = JsonApiSerializer.new(
9
- type: :field,
10
- attributes: %i(api_key appeareance field_type hint label localized position validators),
11
- required_attributes: %i(api_key appeareance field_type hint label localized position validators),
12
- ).serialize(resource_attributes)
13
-
14
- post_request "/item-types/#{item_type_id}/fields", body
15
- end
16
-
17
- def update(field_id, resource_attributes)
18
- body = JsonApiSerializer.new(
19
- type: :field,
20
- attributes: %i(api_key appeareance hint label localized position validators),
21
- ).serialize(resource_attributes, field_id)
22
-
23
- put_request "/fields/#{field_id}", body
24
- end
25
-
26
- def all(item_type_id)
27
- get_request "/item-types/#{item_type_id}/fields"
28
- end
29
-
30
- def find(field_id)
31
- get_request "/fields/#{field_id}"
32
- end
33
-
34
- def destroy(field_id)
35
- delete_request "/fields/#{field_id}"
36
- end
37
-
38
- end
39
- end
40
- end
@@ -1,44 +0,0 @@
1
- require 'dato/repo/base'
2
-
3
- module Dato
4
- module Repo
5
- class Item < Base
6
-
7
- def create(resource_attributes)
8
- body = JsonApiSerializer.new(
9
- type: :item,
10
- attributes: resource_attributes.keys - [:item_type, :id],
11
- relationships: {
12
- item_type: { collection: false, type: "item_type" }
13
- },
14
- required_relationships: %i(item_type),
15
- ).serialize(resource_attributes)
16
-
17
- post_request "/items", body
18
- end
19
-
20
- def update(item_id, resource_attributes)
21
- body = JsonApiSerializer.new(
22
- type: :item,
23
- attributes: resource_attributes.keys - [:id],
24
- ).serialize(resource_attributes, item_id)
25
- puts body
26
-
27
- put_request "/items/#{item_id}", body
28
- end
29
-
30
- def all(filters)
31
- get_request "/items", filters
32
- end
33
-
34
- def find(item_id)
35
- get_request "/items/#{item_id}"
36
- end
37
-
38
- def destroy(item_id)
39
- delete_request "/items/#{item_id}"
40
- end
41
-
42
- end
43
- end
44
- end
@@ -1,41 +0,0 @@
1
- require 'dato/repo/base'
2
-
3
- module Dato
4
- module Repo
5
- class ItemType < Base
6
-
7
- def create(resource_attributes)
8
- body = JsonApiSerializer.new(
9
- type: :item_type,
10
- attributes: %i(api_key name singleton sortable),
11
- required_attributes: %i(api_key name singleton sortable),
12
- ).serialize(resource_attributes)
13
-
14
- post_request "/item-types", body
15
- end
16
-
17
- def update(item_type_id, resource_attributes)
18
- body = JsonApiSerializer.new(
19
- type: :item_type,
20
- attributes: %i(api_key name singleton sortable),
21
- required_attributes: %i(api_key name singleton sortable),
22
- ).serialize(resource_attributes, item_type_id)
23
-
24
- put_request "/item-types/#{item_type_id}", body
25
- end
26
-
27
- def all()
28
- get_request "/item-types"
29
- end
30
-
31
- def find(item_type_id)
32
- get_request "/item-types/#{item_type_id}"
33
- end
34
-
35
- def destroy(item_type_id)
36
- delete_request "/item-types/#{item_type_id}"
37
- end
38
-
39
- end
40
- end
41
- end
@@ -1,45 +0,0 @@
1
- require 'dato/repo/base'
2
-
3
- module Dato
4
- module Repo
5
- class MenuItem < Base
6
-
7
- def create(resource_attributes)
8
- body = JsonApiSerializer.new(
9
- type: :menu_item,
10
- attributes: %i(label position),
11
- relationships: {:item_type=>{:collection=>false, :type=>:item_type}, :parent=>{:collection=>false, :type=>:menu_item}},
12
- required_attributes: %i(label position),
13
- required_relationships: %i(item_type),
14
- ).serialize(resource_attributes)
15
-
16
- post_request "/menu-items", body
17
- end
18
-
19
- def update(menu_item_id, resource_attributes)
20
- body = JsonApiSerializer.new(
21
- type: :menu_item,
22
- attributes: %i(label position),
23
- relationships: {:item_type=>{:collection=>false, :type=>:item_type}, :parent=>{:collection=>false, :type=>:menu_item}},
24
- required_attributes: %i(label position),
25
- required_relationships: %i(item_type),
26
- ).serialize(resource_attributes, menu_item_id)
27
-
28
- put_request "/menu-items/#{menu_item_id}", body
29
- end
30
-
31
- def all()
32
- get_request "/menu-items"
33
- end
34
-
35
- def find(menu_item_id)
36
- get_request "/menu-items/#{menu_item_id}"
37
- end
38
-
39
- def destroy(menu_item_id)
40
- delete_request "/menu-items/#{menu_item_id}"
41
- end
42
-
43
- end
44
- end
45
- end
@@ -1,22 +0,0 @@
1
- require 'dato/repo/base'
2
-
3
- module Dato
4
- module Repo
5
- class Site < Base
6
-
7
- def find()
8
- get_request "/site"
9
- end
10
-
11
- def update(resource_attributes)
12
- body = JsonApiSerializer.new(
13
- type: :site,
14
- attributes: %i(deploy_adapter deploy_settings favicon global_seo locales name no_index theme_hue timezone),
15
- ).serialize(resource_attributes)
16
-
17
- put_request "/site", body
18
- end
19
-
20
- end
21
- end
22
- end
@@ -1,18 +0,0 @@
1
- require 'dato/repo/base'
2
-
3
- module Dato
4
- module Repo
5
- class UploadRequest < Base
6
-
7
- def create(resource_attributes)
8
- body = JsonApiSerializer.new(
9
- type: :upload_request,
10
- attributes: %i(filename),
11
- ).serialize(resource_attributes)
12
-
13
- post_request "/upload-requests", body
14
- end
15
-
16
- end
17
- end
18
- end
@@ -1,50 +0,0 @@
1
- require 'dato/repo/base'
2
-
3
- module Dato
4
- module Repo
5
- class User < Base
6
-
7
- def create(resource_attributes)
8
- body = JsonApiSerializer.new(
9
- type: :user,
10
- attributes: %i(email first_name is_admin last_name),
11
- required_attributes: %i(email first_name last_name),
12
- ).serialize(resource_attributes)
13
-
14
- post_request "/users", body
15
- end
16
-
17
- def update(user_id, resource_attributes)
18
- body = JsonApiSerializer.new(
19
- type: :user,
20
- attributes: %i(email first_name is_admin last_name password),
21
- ).serialize(resource_attributes, user_id)
22
-
23
- put_request "/users/#{user_id}", body
24
- end
25
-
26
- def all()
27
- get_request "/users"
28
- end
29
-
30
- def find(user_id)
31
- get_request "/users/#{user_id}"
32
- end
33
-
34
- def reset_password(resource_attributes)
35
- body = JsonApiSerializer.new(
36
- type: :user,
37
- attributes: %i(email),
38
- required_attributes: %i(email),
39
- ).serialize(resource_attributes)
40
-
41
- post_request "/users/reset_password", body
42
- end
43
-
44
- def destroy(user_id)
45
- delete_request "/users/#{user_id}"
46
- end
47
-
48
- end
49
- end
50
- end