dato 0.1.3 → 0.1.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 82c1636f441fbd46c8331b4a3a17d005560de0e8
4
- data.tar.gz: c37302cbe0003a7188dcc6c4344876eab27f1b23
3
+ metadata.gz: 625bd39b3b678607cd376d9ba1edc2e725bc2b86
4
+ data.tar.gz: 272fee3a00041493b597d137697b10f8a4fde371
5
5
  SHA512:
6
- metadata.gz: daa7cca64ddccfedde2a6fa675111c2b11f92f944c61aa0ebdc69114f2d84c4701e1357d92b164a25b10a7db373b2735c0a7377bef2aec98eba168b2a0b2481b
7
- data.tar.gz: b9c65378b99fda7b554f62c9191a5dddea274847bf09cd05f7968bec8074be49e4fcb5017fa3af572f13e7d30f937067d3d00250dcc527d9cfac0e50906093f7
6
+ metadata.gz: 8d0351d838f7799d1bf129b9dd7e81848cadc4ff638e252f8a3e6b6f636e53ac79fb2f8db1c75b3b66130c93d2a50f4b4ee7b4fd39551a0299403d688c0b4121
7
+ data.tar.gz: 0f74932dbec7b9f7bdf326f904f99cab5233e33d586ad3118dff08954fc818154d8a628ea32db475262c7f5652ddd0bb53a5192f12e4737b3229e83519be906c
data/README.md CHANGED
@@ -24,7 +24,46 @@ Or install it yourself as:
24
24
 
25
25
  ## Usage
26
26
 
27
- TODO: Write usage instructions here
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
+ ```
28
67
 
29
68
  ## Development
30
69
 
@@ -31,4 +31,7 @@ Gem::Specification.new do |spec|
31
31
  spec.add_runtime_dependency "faraday", [">= 0.9.0"]
32
32
  spec.add_runtime_dependency "faraday_middleware", [">= 0.9.0"]
33
33
  spec.add_runtime_dependency "activesupport"
34
+ spec.add_runtime_dependency "fastimage"
35
+ spec.add_runtime_dependency "downloadr"
36
+ spec.add_runtime_dependency "addressable"
34
37
  end
@@ -5,6 +5,7 @@ require 'active_support/core_ext/hash/indifferent_access'
5
5
 
6
6
  require 'dato/account/repo/account'
7
7
  require 'dato/account/repo/site'
8
+ require 'dato/api_error'
8
9
 
9
10
  module Dato
10
11
  module Account
@@ -14,10 +15,10 @@ module Dato
14
15
  sites: Repo::Site,
15
16
  }
16
17
 
17
- attr_reader :token, :domain, :schema
18
+ attr_reader :token, :base_url, :schema
18
19
 
19
- def initialize(token, domain: 'https://account-api.datocms.com')
20
- @domain = domain
20
+ def initialize(token, base_url: 'https://account-api.datocms.com')
21
+ @base_url = base_url
21
22
  @token = token
22
23
  end
23
24
 
@@ -32,21 +33,16 @@ module Dato
32
33
  end
33
34
 
34
35
  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
36
+ connection.send(*args).body.with_indifferent_access
37
+ rescue Faraday::ClientError => e
38
+ raise ApiError.new(e)
43
39
  end
44
40
 
45
41
  private
46
42
 
47
43
  def connection
48
44
  options = {
49
- url: domain,
45
+ url: base_url,
50
46
  headers: {
51
47
  'Accept' => "application/json",
52
48
  'Content-Type' => "application/json",
@@ -0,0 +1,18 @@
1
+ module Dato
2
+ class ApiError < StandardError
3
+ attr_reader :faraday_error
4
+
5
+ def initialize(faraday_error)
6
+ @faraday_error = faraday_error
7
+ end
8
+
9
+ def message
10
+ [
11
+ "DatoCMS API Error",
12
+ "Status: #{faraday_error.response[:status]}",
13
+ "Response:",
14
+ JSON.pretty_generate(JSON.load(faraday_error.response[:body]))
15
+ ].join("\n")
16
+ end
17
+ end
18
+ end
@@ -11,6 +11,10 @@ require 'dato/site/repo/upload_request'
11
11
  require 'dato/site/repo/user'
12
12
  require 'dato/site/repo/item'
13
13
 
14
+ require 'dato/upload/file'
15
+ require 'dato/upload/image'
16
+ require 'dato/api_error'
17
+
14
18
  module Dato
15
19
  module Site
16
20
  class Client
@@ -24,13 +28,23 @@ module Dato
24
28
  items: Repo::Item,
25
29
  }
26
30
 
27
- attr_reader :token, :domain, :schema
31
+ attr_reader :token, :base_url, :schema
28
32
 
29
- def initialize(token, domain: 'https://site-api.datocms.com')
30
- @domain = domain
33
+ def initialize(token, base_url: 'https://site-api.datocms.com')
34
+ @base_url = base_url
31
35
  @token = token
32
36
  end
33
37
 
38
+ def upload_file(path_or_url)
39
+ file = Upload::File.new(self, path_or_url)
40
+ file.upload
41
+ end
42
+
43
+ def upload_image(path_or_url)
44
+ file = Upload::Image.new(self, path_or_url)
45
+ file.upload
46
+ end
47
+
34
48
  REPOS.each do |method_name, repo_klass|
35
49
  define_method method_name do
36
50
  instance_variable_set(
@@ -42,20 +56,16 @@ module Dato
42
56
  end
43
57
 
44
58
  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
59
+ connection.send(*args).body.with_indifferent_access
60
+ rescue Faraday::ClientError => e
61
+ raise ApiError.new(e)
52
62
  end
53
63
 
54
64
  private
55
65
 
56
66
  def connection
57
67
  options = {
58
- url: domain,
68
+ url: base_url,
59
69
  headers: {
60
70
  'Accept' => "application/json",
61
71
  'Content-Type' => "application/json",
@@ -0,0 +1,68 @@
1
+ require "downloadr"
2
+ require "tempfile"
3
+ require "addressable"
4
+ require "net/http"
5
+
6
+ module Dato
7
+ module Upload
8
+ class File
9
+ attr_reader :client, :source
10
+
11
+ def initialize(client, source)
12
+ @client = client
13
+ @source = source
14
+ end
15
+
16
+ def file
17
+ @file ||= if http_source?
18
+ Tempfile.new('file').tap do |file|
19
+ Downloadr::HTTP.new(source, file).download
20
+ end
21
+ else
22
+ ::File.new(::File.expand_path(source))
23
+ end
24
+ end
25
+
26
+ def http_source?
27
+ uri = Addressable::URI.parse(source)
28
+ uri.scheme == "http" || uri.scheme == "https"
29
+ rescue Addressable::URI::InvalidURIError
30
+ false
31
+ end
32
+
33
+ def filename
34
+ if http_source?
35
+ ::File.basename(source)
36
+ else
37
+ ::File.basename(file.path)
38
+ end
39
+ end
40
+
41
+ def upload
42
+ upload_request = client.upload_requests.create(filename: filename)
43
+ uri = URI.parse(upload_request[:url])
44
+
45
+ request = Net::HTTP::Put.new(
46
+ uri,
47
+ 'x-amz-acl' => 'public-read'
48
+ )
49
+ request.body = file.read
50
+
51
+ http = Net::HTTP.new(uri.host, uri.port)
52
+ http.use_ssl = true
53
+
54
+ http.request(request)
55
+
56
+ format_resource(upload_request)
57
+ end
58
+
59
+ def format_resource(upload_request)
60
+ {
61
+ path: upload_request[:id],
62
+ size: ::File.size(file.path),
63
+ format: ::File.extname(::File.basename(file.path)).delete('.')
64
+ }
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,19 @@
1
+ require "dato/upload/file"
2
+ require "fastimage"
3
+
4
+ module Dato
5
+ module Upload
6
+ class Image < Dato::Upload::File
7
+ def format_resource(upload_request)
8
+ width, height = FastImage.size(file.path)
9
+
10
+ super(upload_request).merge(
11
+ width: width,
12
+ height: height,
13
+ format: FastImage.type(file.path).to_s
14
+ )
15
+ end
16
+ end
17
+ end
18
+ end
19
+
@@ -1,3 +1,3 @@
1
1
  module Dato
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
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.3
4
+ version: 0.1.4
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-06 00:00:00.000000000 Z
11
+ date: 2016-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -164,6 +164,48 @@ dependencies:
164
164
  - - ">="
165
165
  - !ruby/object:Gem::Version
166
166
  version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: fastimage
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :runtime
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: downloadr
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :runtime
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: addressable
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ type: :runtime
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
167
209
  description: Ruby client for DatoCMS API
168
210
  email:
169
211
  - s.verna@cantierecreativo.net
@@ -187,6 +229,7 @@ files:
187
229
  - lib/dato/account/repo/account.rb
188
230
  - lib/dato/account/repo/base.rb
189
231
  - lib/dato/account/repo/site.rb
232
+ - lib/dato/api_error.rb
190
233
  - lib/dato/json_api_deserializer.rb
191
234
  - lib/dato/json_api_serializer.rb
192
235
  - lib/dato/site/client.rb
@@ -198,6 +241,8 @@ files:
198
241
  - lib/dato/site/repo/site.rb
199
242
  - lib/dato/site/repo/upload_request.rb
200
243
  - lib/dato/site/repo/user.rb
244
+ - lib/dato/upload/file.rb
245
+ - lib/dato/upload/image.rb
201
246
  - lib/dato/version.rb
202
247
  homepage: https://github.com/datocms/ruby-datocms-client
203
248
  licenses:
@@ -224,4 +269,3 @@ signing_key:
224
269
  specification_version: 4
225
270
  summary: Ruby client for DatoCMS API
226
271
  test_files: []
227
- has_rdoc: