dato 0.7.5 → 0.7.6

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
  SHA256:
3
- metadata.gz: 7b83ae27c74e6a173bdde5b26ce31b9d71a70969f7630d0ffee199ed5a7f45ff
4
- data.tar.gz: eadb0a8bf60f1364345e57cfc1c8ffc314f84310482157129b37e2c73fe67ddf
3
+ metadata.gz: 118ea91dc3caa834412e342a2350b594c94880bbbafba6f37d2b6041b5570acc
4
+ data.tar.gz: 64e1a53c6292a41ad4e361e4b01bdff416d09e3acebbd133afc123c2f80fe74b
5
5
  SHA512:
6
- metadata.gz: 57ae67081408c41489e5f9d44cc4548f89decb247db797223f2e6582f615fed19acab7ced73e62006f1d5e50df2a6c47f61717f3ee866f9e184b51952c247f92
7
- data.tar.gz: 65996e97224be9504d239166b40a0f363e758236f31347a9b2f6acc39556935725ec73d1b1b2edad1fee1a109d0299ab040ffca6ff53b618a87c09c586e83800
6
+ metadata.gz: a93f7dccda38b47237a585a7274754b2a3c370918c476e5556bc42ec13a2326a53e594f25b48fdc240a456f707d71dedfe9d9587a978ff38a177c398d5627ffa
7
+ data.tar.gz: b74c22160ba51d703705213b12c60d795eb499855fc6179a8b2c576ff8e32d1cd7bb94747a7e3a6a5bd957ff527e34964ca01eb47dbfdc078f16519272dfb6a4
@@ -1,8 +1,8 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2.2
4
- - 2.3.0
5
3
  - 2.4.0
4
+ - 2.5.0
5
+ - 2.6.0
6
6
  before_install:
7
7
  - gem install bundler -v 1.13.5
8
8
  - echo 'puts "ruby \"#{RUBY_VERSION}\""' | ruby >> Gemfile
@@ -20,9 +20,9 @@ Gem::Specification.new do |spec|
20
20
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
21
  spec.require_paths = ['lib']
22
22
 
23
- spec.add_development_dependency 'bundler', '~> 1.11'
24
- spec.add_development_dependency 'rake', '~> 10.0'
25
- spec.add_development_dependency 'rspec', '~> 3.0'
23
+ spec.add_development_dependency 'bundler'
24
+ spec.add_development_dependency 'rake'
25
+ spec.add_development_dependency 'rspec'
26
26
  spec.add_development_dependency 'rubyzip'
27
27
  spec.add_development_dependency 'simplecov'
28
28
  spec.add_development_dependency 'vcr'
@@ -43,6 +43,6 @@ Gem::Specification.new do |spec|
43
43
  spec.add_runtime_dependency 'dotenv'
44
44
  spec.add_runtime_dependency 'pusher-client'
45
45
  spec.add_runtime_dependency 'listen'
46
- spec.add_runtime_dependency 'json_schema'
46
+ spec.add_runtime_dependency 'dato_json_schema'
47
47
  spec.add_runtime_dependency 'mime-types'
48
48
  end
@@ -56,8 +56,31 @@ module Dato
56
56
  end
57
57
  end
58
58
 
59
+ def put(absolute_path, body = {}, params = {})
60
+ request(:put, absolute_path, body, params)
61
+ end
62
+
63
+ def post(absolute_path, body = {}, params = {})
64
+ request(:post, absolute_path, body, params)
65
+ end
66
+
67
+ def get(absolute_path, params = {})
68
+ request(:get, absolute_path, nil, params)
69
+ end
70
+
71
+ def delete(absolute_path, params = {})
72
+ request(:delete, absolute_path, nil, params)
73
+ end
74
+
59
75
  def request(*args)
60
- response = connection.send(*args)
76
+ method, absolute_path, body, params = args
77
+
78
+ response = connection.send(method, absolute_path, body) do |c|
79
+ if params
80
+ c.params = params
81
+ end
82
+ end
83
+
61
84
  if response.body.is_a?(Hash)
62
85
  response.body.with_indifferent_access
63
86
  else
@@ -82,7 +105,7 @@ module Dato
82
105
  sleep(1)
83
106
  request(*args)
84
107
  else
85
- error = ApiError.new(e)
108
+ error = ApiError.new(e.response)
86
109
  puts "===="
87
110
  puts error.message
88
111
  puts "===="
@@ -115,12 +138,12 @@ module Dato
115
138
  'Content-Type' => 'application/json',
116
139
  'Authorization' => "Bearer #{@token}",
117
140
  'User-Agent' => "ruby-client v#{Dato::VERSION}",
118
- 'X-Api-Version' => '2'
141
+ 'X-Api-Version' => '3'
119
142
  }
120
143
 
121
144
  options = {
122
145
  url: base_url,
123
- headers: default_headers.merge(extra_headers)
146
+ headers: default_headers.merge(extra_headers),
124
147
  }
125
148
 
126
149
  @connection ||= Faraday.new(options) do |c|
@@ -2,23 +2,25 @@
2
2
 
3
3
  module Dato
4
4
  class ApiError < StandardError
5
- attr_reader :faraday_error
5
+ attr_reader :response
6
6
 
7
- def initialize(faraday_error)
8
- @faraday_error = faraday_error
7
+ def initialize(response)
8
+ @response = response
9
9
  end
10
10
 
11
11
  def message
12
12
  [
13
13
  'DatoCMS API Error',
14
- "Status: #{faraday_error.response[:status]}",
14
+ "Status: #{response[:status]}",
15
15
  'Response:',
16
16
  JSON.pretty_generate(body)
17
17
  ].join("\n")
18
18
  end
19
19
 
20
20
  def body
21
- JSON.parse(faraday_error.response[:body])
21
+ if response[:body]
22
+ JSON.parse(response[:body])
23
+ end
22
24
  end
23
25
  end
24
26
  end
@@ -3,10 +3,10 @@ require 'dato/json_schema_relationships'
3
3
 
4
4
  module Dato
5
5
  class JsonApiDeserializer
6
- attr_reader :link
6
+ attr_reader :schema
7
7
 
8
- def initialize(link)
9
- @link = link
8
+ def initialize(schema)
9
+ @schema = schema
10
10
  end
11
11
 
12
12
  def deserialize(data)
@@ -23,7 +23,6 @@ module Dato
23
23
 
24
24
  def deserialize_resource(data)
25
25
  result = { id: data[:id] }
26
-
27
26
  result[:meta] = data[:meta] if data[:meta]
28
27
  result.merge!(data[:attributes]) if data[:attributes]
29
28
 
@@ -51,7 +50,7 @@ module Dato
51
50
  end
52
51
 
53
52
  def relationships
54
- @relationships ||= JsonSchemaRelationships.new(link.target_schema).relationships
53
+ @relationships ||= JsonSchemaRelationships.new(schema).relationships
55
54
  end
56
55
  end
57
56
  end
@@ -6,21 +6,18 @@ module Dato
6
6
  module Local
7
7
  module FieldType
8
8
  class File
9
- attr_reader :path, :format, :size, :width, :height, :title, :alt
9
+ def self.parse(value, repo)
10
+ if value
11
+ v = value.with_indifferent_access
10
12
 
11
- def self.parse(upload_id, repo)
12
- if upload_id
13
- upload = repo.entities_repo.find_entity('upload', upload_id)
13
+ upload = repo.entities_repo.find_entity('upload', v[:upload_id])
14
14
 
15
15
  if upload
16
16
  new(
17
- upload.path,
18
- upload.format,
19
- upload.size,
20
- upload.width,
21
- upload.height,
22
- upload.alt,
23
- upload.title,
17
+ upload,
18
+ v[:alt],
19
+ v[:title],
20
+ v[:custom_data],
24
21
  repo.site.entity.imgix_host
25
22
  )
26
23
  end
@@ -28,23 +25,64 @@ module Dato
28
25
  end
29
26
 
30
27
  def initialize(
31
- path,
32
- format,
33
- size,
34
- width,
35
- height,
28
+ upload,
36
29
  alt,
37
30
  title,
31
+ custom_data,
38
32
  imgix_host
39
33
  )
40
- @path = path
41
- @format = format
42
- @size = size
43
- @imgix_host = imgix_host
44
- @width = width
45
- @height = height
34
+ @upload = upload
46
35
  @alt = alt
47
36
  @title = title
37
+ @custom_data = custom_data
38
+ @imgix_host = imgix_host
39
+ end
40
+
41
+ def path
42
+ @upload.path
43
+ end
44
+
45
+ def format
46
+ @upload.format
47
+ end
48
+
49
+ def size
50
+ @upload.size
51
+ end
52
+
53
+ def width
54
+ @upload.width
55
+ end
56
+
57
+ def height
58
+ @upload.height
59
+ end
60
+
61
+ def author
62
+ @upload.author
63
+ end
64
+
65
+ def notes
66
+ @upload.notes
67
+ end
68
+
69
+ def copyright
70
+ @upload.copyright
71
+ end
72
+
73
+ def alt
74
+ default_metadata = @upload.default_field_metadata.deep_stringify_keys.fetch(I18n.locale.to_s, {})
75
+ @alt || default_metadata["alt"]
76
+ end
77
+
78
+ def title
79
+ default_metadata = @upload.default_field_metadata.deep_stringify_keys.fetch(I18n.locale.to_s, {})
80
+ @title || default_metadata["title"]
81
+ end
82
+
83
+ def custom_data
84
+ default_metadata = @upload.default_field_metadata.deep_stringify_keys.fetch(I18n.locale.to_s, {})
85
+ @custom_data.merge(default_metadata.fetch("custom_data", {}))
48
86
  end
49
87
 
50
88
  def file
@@ -67,6 +105,7 @@ module Dato
67
105
  height: height,
68
106
  alt: alt,
69
107
  title: title,
108
+ custom_data: custom_data,
70
109
  url: url
71
110
  }
72
111
  end
@@ -19,7 +19,7 @@ module Dato
19
19
  end
20
20
 
21
21
  def image
22
- @image && File.parse(@image, @repo)
22
+ @image && UploadId.parse(@image, @repo)
23
23
  end
24
24
 
25
25
  def to_hash(*args)
@@ -27,7 +27,7 @@ module Dato
27
27
  end
28
28
 
29
29
  def logo
30
- @logo && File.parse(@logo, @repo)
30
+ @logo && UploadId.parse(@logo, @repo)
31
31
  end
32
32
 
33
33
  def to_hash(*args)
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'imgix'
4
+
5
+ module Dato
6
+ module Local
7
+ module FieldType
8
+ class UploadId
9
+ attr_reader :path, :format, :size, :width, :height
10
+
11
+ def self.parse(upload_id, repo)
12
+ if upload_id
13
+ upload = repo.entities_repo.find_entity('upload', upload_id)
14
+
15
+ if upload
16
+ new(
17
+ upload.path,
18
+ upload.format,
19
+ upload.size,
20
+ upload.width,
21
+ upload.height,
22
+ repo.site.entity.imgix_host
23
+ )
24
+ end
25
+ end
26
+ end
27
+
28
+ def initialize(
29
+ path,
30
+ format,
31
+ size,
32
+ width,
33
+ height,
34
+ imgix_host
35
+ )
36
+ @path = path
37
+ @format = format
38
+ @size = size
39
+ @imgix_host = imgix_host
40
+ @width = width
41
+ @height = height
42
+ end
43
+
44
+ def file
45
+ Imgix::Client.new(
46
+ host: @imgix_host,
47
+ secure: true,
48
+ include_library_param: false
49
+ ).path(path)
50
+ end
51
+
52
+ def url(opts = {})
53
+ file.to_url(opts)
54
+ end
55
+
56
+ def to_hash(*_args)
57
+ {
58
+ format: format,
59
+ size: size,
60
+ width: width,
61
+ height: height,
62
+ url: url
63
+ }
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/core_ext/hash/compact'
4
-
5
3
  module Dato
6
4
  module Local
7
5
  module FieldType
@@ -39,7 +39,7 @@ module Dato
39
39
  end
40
40
 
41
41
  def watch(&block)
42
- site_id = client.request(:get, '/site')['data']['id']
42
+ site_id = client.get('/site')['data']['id']
43
43
 
44
44
  return if pusher && pusher.connected
45
45
 
@@ -172,8 +172,7 @@ module Dato
172
172
  end
173
173
 
174
174
  def site
175
- include = ['item_types', 'item_types.fields']
176
- client.request(:get, '/site', include: include)
175
+ client.get('/site', include: ['item_types', 'item_types.fields'])
177
176
  end
178
177
 
179
178
  def all_items
@@ -28,7 +28,7 @@ module Dato
28
28
  end
29
29
 
30
30
  def favicon
31
- read_attribute(:favicon, FieldType::File, false)
31
+ read_attribute(:favicon, FieldType::UploadId, false)
32
32
  end
33
33
 
34
34
  def to_s
@@ -11,8 +11,8 @@ module Dato
11
11
  def response
12
12
  items_per_page = 100
13
13
 
14
- base_response = @client.request(
15
- :get, @base_endpoint, @filters.dup.merge('page[limit]' => items_per_page)
14
+ base_response = @client.get(
15
+ @base_endpoint, @filters.dup.merge('page[limit]' => items_per_page)
16
16
  )
17
17
 
18
18
  extra_pages = (
@@ -20,8 +20,7 @@ module Dato
20
20
  ).ceil - 1
21
21
 
22
22
  extra_pages.times do |page|
23
- base_response[:data] += @client.request(
24
- :get,
23
+ base_response[:data] += @client.get(
25
24
  @base_endpoint,
26
25
  @filters.dup.merge(
27
26
  'page[offset]' => items_per_page * (page + 1),
@@ -54,47 +54,79 @@ module Dato
54
54
  placeholder
55
55
  end
56
56
 
57
- response = if %i[post put].include?(link.method)
58
- body = if link.schema
59
- unserialized_body = args.shift
57
+ body = nil
58
+ query_string = nil
60
59
 
61
- JsonApiSerializer.new(type, link).serialize(
62
- unserialized_body,
63
- link.method == :post ? nil : placeholders.last
64
- )
65
- else
66
- {}
67
- end
60
+ if %i[post put].include?(link.method)
61
+ body = link.schema ? args.shift : {}
62
+ query_string = args.shift || {}
68
63
 
69
- client.request(link.method, url, body)
64
+ elsif link.method == :delete
65
+ query_string = args.shift || {}
70
66
 
71
- elsif link.method == :delete
72
- client.request(:delete, url)
67
+ elsif link.method == :get
68
+ query_string = args.shift || {}
69
+ end
73
70
 
74
- elsif link.method == :get
75
- query_string = args.shift
71
+ options = args.any? ? args.shift.symbolize_keys : {}
76
72
 
77
- all_pages = (args[0] || {})
78
- .symbolize_keys
79
- .fetch(:all_pages, false)
73
+ if link.schema && %i[post put].include?(link.method) && options.fetch(:serialize_response, true)
74
+ body = JsonApiSerializer.new(type, link).serialize(
75
+ body,
76
+ link.method == :post ? nil : placeholders.last
77
+ )
78
+ end
80
79
 
81
- if all_pages
80
+ response = if %i[post put].include?(link.method)
81
+ client.send(link.method, url, body, query_string)
82
+ elsif link.method == :delete
83
+ client.delete(url, query_string)
84
+ elsif link.method == :get
85
+ if options.fetch(:all_pages, false)
82
86
  Paginator.new(client, url, query_string).response
83
87
  else
84
- client.request(:get, url, query_string)
88
+ client.get(url, query_string)
85
89
  end
86
90
  end
87
91
 
88
- options = if args.any?
89
- args.shift.symbolize_keys
90
- else
91
- {}
92
- end
93
-
94
- if options.fetch(:deserialize_response, true)
95
- JsonApiDeserializer.new(link).deserialize(response)
92
+ if response && response[:data] && response[:data].is_a?(Hash) && response[:data][:type] == "job"
93
+ job_result = nil
94
+
95
+ while !job_result do
96
+ begin
97
+ sleep(1)
98
+ job_result = client.job_result.find(response[:data][:id])
99
+ rescue ApiError => error
100
+ if error.response[:status] != 404
101
+ raise error
102
+ end
103
+ end
104
+ end
105
+
106
+ if job_result[:status] < 200 || job_result[:status] >= 300
107
+ error = ApiError.new(
108
+ status: job_result[:status],
109
+ body: JSON.dump(job_result[:payload])
110
+ )
111
+
112
+ puts "===="
113
+ puts error.message
114
+ puts "===="
115
+
116
+ raise error
117
+ end
118
+
119
+ if options.fetch(:deserialize_response, true)
120
+ JsonApiDeserializer.new(link.job_schema).deserialize(job_result[:payload])
121
+ else
122
+ job_result.payload
123
+ end
96
124
  else
97
- response
125
+ if options.fetch(:deserialize_response, true)
126
+ JsonApiDeserializer.new(link.target_schema).deserialize(response)
127
+ else
128
+ response
129
+ end
98
130
  end
99
131
  end
100
132
  end
@@ -63,9 +63,14 @@ module Dato
63
63
 
64
64
  http.request(request)
65
65
 
66
- uploads = client.uploads.create(path: upload_request[:id])
66
+ upload = client.uploads.create(path: upload_request[:id])
67
67
 
68
- uploads['id']
68
+ {
69
+ upload_id: upload['id'],
70
+ alt: nil,
71
+ title: nil,
72
+ custom_data: {},
73
+ }
69
74
  end
70
75
 
71
76
  def download_file(url)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dato
4
- VERSION = '0.7.5'
4
+ VERSION = '0.7.6'
5
5
  end
metadata CHANGED
@@ -1,57 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dato
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.5
4
+ version: 0.7.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefano Verna
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-19 00:00:00.000000000 Z
11
+ date: 2019-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.11'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.11'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '3.0'
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '3.0'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rubyzip
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -319,7 +319,7 @@ dependencies:
319
319
  - !ruby/object:Gem::Version
320
320
  version: '0'
321
321
  - !ruby/object:Gem::Dependency
322
- name: json_schema
322
+ name: dato_json_schema
323
323
  requirement: !ruby/object:Gem::Requirement
324
324
  requirements:
325
325
  - - ">="
@@ -414,6 +414,7 @@ files:
414
414
  - lib/dato/local/field_type/string.rb
415
415
  - lib/dato/local/field_type/text.rb
416
416
  - lib/dato/local/field_type/theme.rb
417
+ - lib/dato/local/field_type/upload_id.rb
417
418
  - lib/dato/local/field_type/video.rb
418
419
  - lib/dato/local/item.rb
419
420
  - lib/dato/local/items_repo.rb