dato 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +24 -0
  3. data/Gemfile +1 -0
  4. data/Rakefile +25 -9
  5. data/bin/console +4 -3
  6. data/dato.gemspec +28 -23
  7. data/lib/dato.rb +5 -3
  8. data/lib/dato/account/client.rb +6 -5
  9. data/lib/dato/account/repo/account.rb +9 -10
  10. data/lib/dato/account/repo/base.rb +3 -3
  11. data/lib/dato/account/repo/site.rb +7 -8
  12. data/lib/dato/api_error.rb +3 -2
  13. data/lib/dato/json_api_deserializer.rb +7 -9
  14. data/lib/dato/json_api_serializer.rb +4 -5
  15. data/lib/dato/local/entities_repo.rb +46 -0
  16. data/lib/dato/local/field_type/boolean.rb +12 -0
  17. data/lib/dato/local/field_type/date.rb +12 -0
  18. data/lib/dato/local/field_type/date_time.rb +12 -0
  19. data/lib/dato/local/field_type/file.rb +33 -0
  20. data/lib/dato/local/field_type/float.rb +12 -0
  21. data/lib/dato/local/field_type/image.rb +32 -0
  22. data/lib/dato/local/field_type/integer.rb +12 -0
  23. data/lib/dato/local/field_type/lat_lon.rb +23 -0
  24. data/lib/dato/local/field_type/link.rb +12 -0
  25. data/lib/dato/local/field_type/links.rb +12 -0
  26. data/lib/dato/local/field_type/seo.rb +24 -0
  27. data/lib/dato/local/field_type/string.rb +12 -0
  28. data/lib/dato/local/field_type/text.rb +12 -0
  29. data/lib/dato/local/field_type/video.rb +55 -0
  30. data/lib/dato/local/item.rb +121 -0
  31. data/lib/dato/local/items_repo.rb +110 -0
  32. data/lib/dato/local/json_api_entity.rb +78 -0
  33. data/lib/dato/local/site.rb +57 -0
  34. data/lib/dato/site/client.rb +6 -5
  35. data/lib/dato/site/repo/base.rb +3 -2
  36. data/lib/dato/site/repo/field.rb +3 -4
  37. data/lib/dato/site/repo/item.rb +6 -7
  38. data/lib/dato/site/repo/item_type.rb +6 -7
  39. data/lib/dato/site/repo/menu_item.rb +8 -9
  40. data/lib/dato/site/repo/site.rb +5 -6
  41. data/lib/dato/site/repo/upload_request.rb +3 -4
  42. data/lib/dato/site/repo/user.rb +8 -9
  43. data/lib/dato/upload/file.rb +6 -5
  44. data/lib/dato/upload/image.rb +3 -3
  45. data/lib/dato/version.rb +2 -1
  46. metadata +77 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 138058c0385600db6eb9706feb3e7a6c89adf03d
4
- data.tar.gz: 4d8838258e67a46fee3ba8c6becf709922471a63
3
+ metadata.gz: 8281ddd92777978a05824eeb518a05811ae762d4
4
+ data.tar.gz: 23f0dab9c01750089eb400879034f44cfdced2bd
5
5
  SHA512:
6
- metadata.gz: b12813a3daf777d057d1ad7aa31f2151df7e98975ae1559eb89946cb98f9b585395fdbcb11ce6490bc7bd99b183a088e908fb8fbcc6b55401b23b1d5cdbae126
7
- data.tar.gz: 25ff7792b0b1dbeb87d1ebd3823960761947748ed5f65cb39743562fb5274ed924203dfefab20ef4561ed06b01450c3f959185c0c31d1445eb91121021e17754
6
+ metadata.gz: 35bf49bfe358c1f1975e3cafd6a35038372fa398d7ff9d3e41fec91e32d038050e21ebcec2921b41c08f0e67e4c78d9c650253af7f495910c2b5c73e62e8eae9
7
+ data.tar.gz: 193ce665fb9027bdff18ee3147df90e0b5159117bf676a65ecdd9e11cf6d97b940f9ade791ab6be38f26d0d72903221256ab4b94b55d884bd8483ca2247d6089
@@ -0,0 +1,24 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+
4
+ Style/Documentation:
5
+ Enabled: false
6
+
7
+ Metrics/MethodLength:
8
+ Enabled: false
9
+
10
+ Metrics/ClassLength:
11
+ Enabled: false
12
+
13
+ Metrics/ModuleLength:
14
+ Enabled: false
15
+
16
+ Metrics/AbcSize:
17
+ Max: 18
18
+
19
+ Metrics/LineLength:
20
+ Exclude:
21
+ - "spec/**/*"
22
+ - "lib/dato/site/repo/*"
23
+ - "lib/dato/account/repo/*"
24
+ - "dato.gemspec"
data/Gemfile CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  source 'https://rubygems.org'
2
3
 
3
4
  # Specify your gem's dependencies in dato.gemspec
data/Rakefile CHANGED
@@ -1,23 +1,39 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
- require "open-uri"
1
+ # frozen_string_literal: true
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
4
+ require 'open-uri'
4
5
 
5
- require_relative "./build/build_client"
6
+ require_relative './build/build_client'
6
7
 
7
8
  RSpec::Core::RakeTask.new(:spec)
8
9
 
9
- task :default => :spec
10
+ task default: :spec
10
11
 
12
+ desc 'Regenerates the client starting from the JSON Hyperschema'
11
13
  task :build_repos do
12
14
  BuildClient.new(
13
- open("https://site-api.datocms.com/docs/site-api-hyperschema.json").read,
14
- "site",
15
+ open('https://site-api.datocms.com/docs/site-api-hyperschema.json').read,
16
+ 'site',
15
17
  %w(session item)
16
18
  ).build
17
19
 
18
20
  BuildClient.new(
19
- open("https://site-api.datocms.com/docs/account-api-hyperschema.json").read,
20
- "account",
21
+ open('https://site-api.datocms.com/docs/account-api-hyperschema.json').read,
22
+ 'account',
21
23
  %w(session item)
22
24
  ).build
23
25
  end
26
+
27
+ desc 'Open an irb (or pry) session preloaded with this gem'
28
+ task :console do
29
+ begin
30
+ require 'pry'
31
+ gem_name = File.basename(Dir.pwd)
32
+ sh %(pry -I lib -r #{gem_name}.rb)
33
+ rescue LoadError => _
34
+ sh %(irb -rubygems -I lib -r #{gem_name}.rb)
35
+ end
36
+ end
37
+
38
+ require 'rubocop/rake_task'
39
+ RuboCop::RakeTask.new
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- require "bundler/setup"
4
- require "dato"
4
+ require 'bundler/setup'
5
+ require 'dato'
5
6
 
6
7
  # You can add fixtures and/or initialization code here to make experimenting
7
8
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +11,5 @@ require "dato"
10
11
  # require "pry"
11
12
  # Pry.start
12
13
 
13
- require "irb"
14
+ require 'irb'
14
15
  IRB.start
@@ -1,37 +1,42 @@
1
1
  # coding: utf-8
2
+ # frozen_string_literal: true
2
3
  lib = File.expand_path('../lib', __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'dato/version'
5
6
 
6
7
  Gem::Specification.new do |spec|
7
- spec.name = "dato"
8
+ spec.name = 'dato'
8
9
  spec.version = Dato::VERSION
9
- spec.authors = ["Stefano Verna"]
10
- spec.email = ["s.verna@cantierecreativo.net"]
10
+ spec.authors = ['Stefano Verna']
11
+ spec.email = ['s.verna@cantierecreativo.net']
11
12
 
12
- spec.summary = %q{Ruby client for DatoCMS API}
13
- spec.description = %q{Ruby client for DatoCMS API}
14
- spec.homepage = "https://github.com/datocms/ruby-datocms-client"
15
- spec.license = "MIT"
13
+ spec.summary = 'Ruby client for DatoCMS API'
14
+ spec.description = 'Ruby client for DatoCMS API'
15
+ spec.homepage = 'https://github.com/datocms/ruby-datocms-client'
16
+ spec.license = 'MIT'
16
17
 
17
18
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|build|spec|features)/}) }
18
- spec.bindir = "exe"
19
+ spec.bindir = 'exe'
19
20
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
- spec.require_paths = ["lib"]
21
+ spec.require_paths = ['lib']
21
22
 
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
- spec.add_development_dependency "rubyzip"
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"
23
+ spec.add_development_dependency 'bundler', '~> 1.11'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rspec', '~> 3.0'
26
+ spec.add_development_dependency 'rubyzip'
27
+ spec.add_development_dependency 'json_schema'
28
+ spec.add_development_dependency 'simplecov'
29
+ spec.add_development_dependency 'vcr'
30
+ spec.add_development_dependency 'webmock'
31
+ spec.add_development_dependency 'rubocop'
30
32
 
31
- spec.add_runtime_dependency "faraday", [">= 0.9.0"]
32
- spec.add_runtime_dependency "faraday_middleware", [">= 0.9.0"]
33
- spec.add_runtime_dependency "activesupport"
34
- spec.add_runtime_dependency "fastimage"
35
- spec.add_runtime_dependency "downloadr"
36
- spec.add_runtime_dependency "addressable"
33
+ spec.add_runtime_dependency 'faraday', ['>= 0.9.0']
34
+ spec.add_runtime_dependency 'faraday_middleware', ['>= 0.9.0']
35
+ spec.add_runtime_dependency 'activesupport'
36
+ spec.add_runtime_dependency 'fastimage'
37
+ spec.add_runtime_dependency 'downloadr'
38
+ spec.add_runtime_dependency 'addressable'
39
+ spec.add_runtime_dependency('imgix', ['>= 0.3.1'])
40
+ spec.add_runtime_dependency('video_embed')
41
+ spec.add_runtime_dependency('semantic')
37
42
  end
@@ -1,7 +1,9 @@
1
- require "dato/version"
1
+ # frozen_string_literal: true
2
+ require 'dato/version'
2
3
 
3
- require "dato/site/client"
4
- require "dato/account/client"
4
+ require 'dato/site/client'
5
+ require 'dato/account/client'
6
+ require 'dato/local/site'
5
7
 
6
8
  module Dato
7
9
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'faraday'
2
3
  require 'faraday_middleware'
3
4
  require 'json'
@@ -12,8 +13,8 @@ module Dato
12
13
  class Client
13
14
  REPOS = {
14
15
  account: Repo::Account,
15
- sites: Repo::Site,
16
- }
16
+ sites: Repo::Site
17
+ }.freeze
17
18
 
18
19
  attr_reader :token, :base_url, :schema
19
20
 
@@ -35,7 +36,7 @@ module Dato
35
36
  def request(*args)
36
37
  connection.send(*args).body.with_indifferent_access
37
38
  rescue Faraday::ClientError => e
38
- raise ApiError.new(e)
39
+ raise ApiError, e
39
40
  end
40
41
 
41
42
  private
@@ -44,8 +45,8 @@ module Dato
44
45
  options = {
45
46
  url: base_url,
46
47
  headers: {
47
- 'Accept' => "application/json",
48
- 'Content-Type' => "application/json",
48
+ 'Accept' => 'application/json',
49
+ 'Content-Type' => 'application/json',
49
50
  'Authorization' => "Bearer #{@token}"
50
51
  }
51
52
  }
@@ -1,43 +1,42 @@
1
+ # frozen_string_literal: true
1
2
  require 'dato/account/repo/base'
2
3
 
3
4
  module Dato
4
5
  module Account
5
6
  module Repo
6
7
  class Account < Base
7
-
8
8
  def create(resource_attributes)
9
9
  body = JsonApiSerializer.new(
10
10
  type: :account,
11
11
  attributes: %i(email password),
12
- required_attributes: %i(email password),
12
+ required_attributes: %i(email password)
13
13
  ).serialize(resource_attributes)
14
14
 
15
- post_request "/account", body
15
+ post_request '/account', body
16
16
  end
17
17
 
18
18
  def update(resource_attributes)
19
19
  body = JsonApiSerializer.new(
20
20
  type: :account,
21
- attributes: %i(email password),
21
+ attributes: %i(email password)
22
22
  ).serialize(resource_attributes)
23
23
 
24
- put_request "/account", body
24
+ put_request '/account', body
25
25
  end
26
26
 
27
- def find()
28
- get_request "/account"
27
+ def find
28
+ get_request '/account'
29
29
  end
30
30
 
31
31
  def reset_password(resource_attributes)
32
32
  body = JsonApiSerializer.new(
33
33
  type: :account,
34
34
  attributes: %i(email),
35
- required_attributes: %i(email),
35
+ required_attributes: %i(email)
36
36
  ).serialize(resource_attributes)
37
37
 
38
- post_request "/account/reset_password", body
38
+ post_request '/account/reset_password', body
39
39
  end
40
-
41
40
  end
42
41
  end
43
42
  end
@@ -1,5 +1,6 @@
1
- require "dato/json_api_serializer"
2
- require "dato/json_api_deserializer"
1
+ # frozen_string_literal: true
2
+ require 'dato/json_api_serializer'
3
+ require 'dato/json_api_deserializer'
3
4
 
4
5
  module Dato
5
6
  module Account
@@ -24,4 +25,3 @@ module Dato
24
25
  end
25
26
  end
26
27
  end
27
-
@@ -1,31 +1,31 @@
1
+ # frozen_string_literal: true
1
2
  require 'dato/account/repo/base'
2
3
 
3
4
  module Dato
4
5
  module Account
5
6
  module Repo
6
7
  class Site < Base
7
-
8
8
  def find(site_id)
9
9
  get_request "/sites/#{site_id}"
10
10
  end
11
11
 
12
- def all()
13
- get_request "/sites"
12
+ def all
13
+ get_request '/sites'
14
14
  end
15
15
 
16
16
  def create(resource_attributes)
17
17
  body = JsonApiSerializer.new(
18
18
  type: :site,
19
- attributes: %i(domain name notes template),
19
+ attributes: %i(domain name notes template)
20
20
  ).serialize(resource_attributes)
21
21
 
22
- post_request "/sites", body
22
+ post_request '/sites', body
23
23
  end
24
24
 
25
25
  def update(site_id, resource_attributes)
26
26
  body = JsonApiSerializer.new(
27
27
  type: :site,
28
- attributes: %i(domain name notes),
28
+ attributes: %i(domain name notes)
29
29
  ).serialize(resource_attributes, site_id)
30
30
 
31
31
  put_request "/sites/#{site_id}", body
@@ -38,12 +38,11 @@ module Dato
38
38
  def duplicate(site_id, resource_attributes)
39
39
  body = JsonApiSerializer.new(
40
40
  type: :site,
41
- attributes: %i(name),
41
+ attributes: %i(name)
42
42
  ).serialize(resource_attributes)
43
43
 
44
44
  post_request "/sites/#{site_id}/duplicate", body
45
45
  end
46
-
47
46
  end
48
47
  end
49
48
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Dato
2
3
  class ApiError < StandardError
3
4
  attr_reader :faraday_error
@@ -8,9 +9,9 @@ module Dato
8
9
 
9
10
  def message
10
11
  [
11
- "DatoCMS API Error",
12
+ 'DatoCMS API Error',
12
13
  "Status: #{faraday_error.response[:status]}",
13
- "Response:",
14
+ 'Response:',
14
15
  JSON.pretty_generate(JSON.load(faraday_error.response[:body]))
15
16
  ].join("\n")
16
17
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Dato
2
3
  class JsonApiDeserializer
3
4
  def deserialize(data)
@@ -17,14 +18,12 @@ module Dato
17
18
  relationships = data.delete(:relationships)
18
19
 
19
20
  if relationships
20
- relationships.each do |key, data|
21
- data = data["data"]
22
- value = if data.is_a? Array
23
- data.map { |ref| ref["id"] }
24
- elsif data.is_a? Hash
25
- data[:id]
26
- else
27
- nil
21
+ relationships.each do |key, handle|
22
+ handle_data = handle['data']
23
+ value = if handle_data.is_a? Array
24
+ handle_data.map { |ref| ref['id'] }
25
+ elsif handle_data.is_a? Hash
26
+ handle_data[:id]
28
27
  end
29
28
  result[key] = value
30
29
  end
@@ -34,4 +33,3 @@ module Dato
34
33
  end
35
34
  end
36
35
  end
37
-
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Dato
2
3
  class JsonApiSerializer
3
4
  attr_reader :type, :attributes, :relationships
@@ -21,9 +22,7 @@ module Dato
21
22
  resource = resource.with_indifferent_access
22
23
  data = {}
23
24
 
24
- if id || resource[:id]
25
- data[:id] = id || resource[:id]
26
- end
25
+ data[:id] = id || resource[:id] if id || resource[:id]
27
26
 
28
27
  data[:type] = type
29
28
  data[:attributes] = serialized_attributes(resource)
@@ -39,7 +38,7 @@ module Dato
39
38
  result = {}
40
39
 
41
40
  attributes.each do |attribute|
42
- if resource.has_key? attribute
41
+ if resource.key? attribute
43
42
  result[attribute] = resource[attribute]
44
43
  elsif required_attributes.include? attribute
45
44
  throw "Required attribute: #{attribute}"
@@ -53,7 +52,7 @@ module Dato
53
52
  result = {}
54
53
 
55
54
  relationships.each do |relationship, meta|
56
- if resource.has_key? relationship
55
+ if resource.key? relationship
57
56
  value = resource[relationship]
58
57
 
59
58
  data = if value
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+ require 'dato/local/json_api_entity'
3
+
4
+ module Dato
5
+ module Local
6
+ class EntitiesRepo
7
+ attr_reader :entities
8
+
9
+ def initialize(*payloads)
10
+ @entities = {}
11
+
12
+ payloads.each do |payload|
13
+ EntitiesRepo.payload_entities(payload).each do |entity_payload|
14
+ object = JsonApiEntity.new(entity_payload, self)
15
+ @entities[object.type] ||= {}
16
+ @entities[object.type][object.id] = object
17
+ end
18
+ end
19
+ end
20
+
21
+ def find_entities_of_type(type)
22
+ entities.fetch(type, {}).values
23
+ end
24
+
25
+ def find_entity(type, id)
26
+ entities.fetch(type, {}).fetch(id, nil)
27
+ end
28
+
29
+ def self.payload_entities(payload)
30
+ acc = []
31
+
32
+ if payload[:data]
33
+ acc = if payload[:data].is_a? Array
34
+ acc + payload[:data]
35
+ else
36
+ acc + [payload[:data]]
37
+ end
38
+ end
39
+
40
+ acc += payload[:included] if payload[:included]
41
+
42
+ acc
43
+ end
44
+ end
45
+ end
46
+ end