middleman-dato 0.0.1.rc12 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/.gitlab-ci.yml +7 -0
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +9 -1070
  5. data/Gemfile +8 -8
  6. data/Rakefile +14 -5
  7. data/foo.rb +12 -0
  8. data/lib/dato/client.rb +14 -9
  9. data/lib/dato/entities_repo.rb +43 -0
  10. data/lib/dato/field_type/boolean.rb +9 -0
  11. data/lib/dato/field_type/date.rb +9 -0
  12. data/lib/dato/field_type/date_time.rb +9 -0
  13. data/lib/dato/field_type/file.rb +30 -0
  14. data/lib/dato/field_type/float.rb +9 -0
  15. data/lib/dato/field_type/image.rb +23 -0
  16. data/lib/dato/field_type/integer.rb +9 -0
  17. data/lib/dato/field_type/lat_lon.rb +16 -0
  18. data/lib/dato/field_type/link.rb +9 -0
  19. data/lib/dato/field_type/links.rb +9 -0
  20. data/lib/dato/field_type/seo.rb +21 -0
  21. data/lib/dato/field_type/string.rb +9 -0
  22. data/lib/dato/field_type/text.rb +9 -0
  23. data/lib/dato/field_type/video.rb +48 -0
  24. data/lib/dato/json_api_entity.rb +75 -0
  25. data/lib/dato/meta_tags/article_modified_time.rb +3 -3
  26. data/lib/dato/meta_tags/article_publisher.rb +3 -3
  27. data/lib/dato/meta_tags/base.rb +12 -19
  28. data/lib/dato/meta_tags/description.rb +4 -4
  29. data/lib/dato/meta_tags/image.rb +5 -8
  30. data/lib/dato/meta_tags/og_locale.rb +2 -2
  31. data/lib/dato/meta_tags/og_meta_tag.rb +2 -4
  32. data/lib/dato/meta_tags/og_site_name.rb +2 -2
  33. data/lib/dato/meta_tags/og_type.rb +5 -5
  34. data/lib/dato/meta_tags/robots.rb +2 -4
  35. data/lib/dato/meta_tags/title.rb +5 -4
  36. data/lib/dato/meta_tags/twitter_card.rb +3 -3
  37. data/lib/dato/meta_tags/twitter_meta_tag.rb +2 -4
  38. data/lib/dato/meta_tags/twitter_site.rb +2 -2
  39. data/lib/dato/meta_tags/url.rb +4 -4
  40. data/lib/dato/meta_tags_builder.rb +13 -13
  41. data/lib/dato/middleman_extension.rb +15 -23
  42. data/lib/dato/record.rb +77 -46
  43. data/lib/dato/records_repo.rb +106 -0
  44. data/lib/dato/space.rb +36 -0
  45. data/lib/middleman-dato.rb +5 -2
  46. data/middleman-dato.gemspec +14 -13
  47. data/spec/dato/entities_repo_spec.rb +41 -0
  48. data/spec/dato/field_type/file_spec.rb +20 -0
  49. data/spec/dato/field_type/image_spec.rb +24 -0
  50. data/spec/dato/field_type/lat_lon_spec.rb +18 -0
  51. data/spec/dato/field_type/seo_spec.rb +31 -0
  52. data/spec/dato/field_type/video_spec.rb +32 -0
  53. data/spec/dato/json_api_entity_spec.rb +123 -0
  54. data/spec/dato/meta_tags/article_modified_time_spec.rb +16 -14
  55. data/spec/dato/meta_tags/image_spec.rb +39 -0
  56. data/spec/dato/meta_tags/og_locale_spec.rb +14 -12
  57. data/spec/dato/record_spec.rb +137 -78
  58. data/spec/dato/records_repo_spec.rb +125 -0
  59. data/spec/spec_helper.rb +14 -12
  60. metadata +58 -13
  61. data/lib/dato/field.rb +0 -43
  62. data/lib/dato/fields/belongs_to.rb +0 -21
  63. data/lib/dato/fields/file.rb +0 -22
  64. data/lib/dato/fields/seo.rb +0 -37
  65. data/lib/dato/repo.rb +0 -111
  66. data/lib/middleman_extension.rb +0 -1
  67. data/spec/dato/field_spec.rb +0 -110
@@ -0,0 +1,125 @@
1
+ require 'dato/records_repo'
2
+
3
+ module Dato
4
+ RSpec.describe RecordsRepo do
5
+ subject(:repo) { described_class.new(entities_repo) }
6
+ let(:entities_repo) do
7
+ instance_double('Dato::EntitiesRepo')
8
+ end
9
+ let(:content_type) do
10
+ double('Dato::JsonApiEntity', api_key: 'post', singleton: false)
11
+ end
12
+ let(:singleton_content_type) do
13
+ double('Dato::JsonApiEntity', api_key: 'homepage', singleton: true)
14
+ end
15
+ let(:record_entity) do
16
+ double('Dato::JsonApiEntity', content_type: content_type)
17
+ end
18
+ let(:singleton_record_entity) do
19
+ double('Dato::JsonApiEntity', content_type: singleton_content_type)
20
+ end
21
+ let(:record) do
22
+ instance_double('Dato::Record', id: '14')
23
+ end
24
+ let(:singleton_record) do
25
+ instance_double('Dato::Record', id: '22')
26
+ end
27
+
28
+ before do
29
+ allow(entities_repo).to receive(:find_entities_of_type).with('content_type') do
30
+ [content_type, singleton_content_type]
31
+ end
32
+
33
+ allow(entities_repo).to receive(:find_entities_of_type).with('record') do
34
+ [record_entity, singleton_record_entity]
35
+ end
36
+
37
+ allow(Record).to receive(:new).with(record_entity, anything) do
38
+ record
39
+ end
40
+
41
+ allow(Record).to receive(:new).with(singleton_record_entity, anything) do
42
+ singleton_record
43
+ end
44
+ end
45
+
46
+ describe '#find' do
47
+ it 'returns the specified record' do
48
+ expect(repo.find('14')).to eq record
49
+ expect(repo.find('22')).to eq singleton_record
50
+ end
51
+ end
52
+
53
+ describe 'content_types' do
54
+ describe 'singleton' do
55
+ it 'returns the associated record' do
56
+ expect(repo.respond_to?(:homepage)).to be_truthy
57
+ expect(repo.homepage).to eq singleton_record
58
+ end
59
+ end
60
+
61
+ describe 'non-singleton' do
62
+ it 'returns the associated records' do
63
+ expect(repo.respond_to?(:posts)).to be_truthy
64
+ expect(repo.posts).to eq [record]
65
+ end
66
+ end
67
+
68
+ describe 'non existing content types' do
69
+ it 'returns NoMethodError' do
70
+ expect(repo.respond_to?(:foobars)).to be_falsy
71
+ expect { repo.foobars }.to raise_error NoMethodError
72
+ end
73
+ end
74
+ end
75
+
76
+ describe RecordsRepo::RecordCollection do
77
+ subject(:collection) { described_class.new(items) }
78
+ let(:items) do
79
+ [foo]
80
+ end
81
+
82
+ let(:foo) { double('Dato::Record', id: '1', name: 'Foo') }
83
+
84
+ describe '#[]' do
85
+ it 'returns the record with the specified id or index' do
86
+ expect(collection['1']).to eq foo
87
+ expect(collection[0]).to eq foo
88
+ end
89
+ end
90
+
91
+ describe '#keys' do
92
+ it 'returns the list of ids' do
93
+ expect(collection.keys).to eq ['1']
94
+ end
95
+ end
96
+
97
+ describe '#each' do
98
+ context 'with arity == 2' do
99
+ it 'iterates with id and record' do
100
+ collection.each do |a, b|
101
+ expect(a).to eq '1'
102
+ expect(b).to eq foo
103
+ end
104
+ end
105
+ end
106
+
107
+ context 'with arity != 2' do
108
+ it 'iterates just like a regular array' do
109
+ collection.each do |a|
110
+ expect(a).to eq foo
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ it '#values' do
117
+ expect(collection.values).to eq [foo]
118
+ end
119
+
120
+ it '#sort_by' do
121
+ expect(collection.sort_by(&:name)).to eq [foo]
122
+ end
123
+ end
124
+ end
125
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,18 +1,19 @@
1
- require "simplecov"
2
- SimpleCov.start
1
+ require 'simplecov'
2
+ require 'active_support/dependencies'
3
+ require 'i18n'
3
4
 
4
- require "active_support/dependencies"
5
-
6
- require "i18n"
5
+ SimpleCov.start do
6
+ add_filter '/spec/'
7
+ end
7
8
 
8
- I18n.available_locales = [:it, :en]
9
+ I18n.available_locales = [:it, :en, :ru]
9
10
 
10
11
  ActiveSupport::Dependencies.autoload_paths.unshift(
11
- File.join(__dir__, "../lib")
12
+ File.join(__dir__, '../lib')
12
13
  )
13
14
 
14
- Dir["spec/support/**/*.rb"].each do |f|
15
- require_relative "../" + f
15
+ Dir['spec/support/**/*.rb'].each do |f|
16
+ require_relative '../' + f
16
17
  end
17
18
 
18
19
  RSpec.configure do |config|
@@ -24,11 +25,12 @@ RSpec.configure do |config|
24
25
  mocks.verify_partial_doubles = true
25
26
  end
26
27
 
27
- config.filter_run :focus
28
- config.run_all_when_everything_filtered = true
29
28
  config.disable_monkey_patching!
29
+ config.warnings = false
30
30
 
31
- config.order = :random
31
+ config.default_formatter = 'doc' if config.files_to_run.one?
32
32
 
33
+ config.profile_examples = 10
34
+ config.order = :random
33
35
  Kernel.srand config.seed
34
36
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: middleman-dato
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.rc12
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefano Verna
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-14 00:00:00.000000000 Z
11
+ date: 2016-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: middleman-core
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: 0.3.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: video_embed
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  description: Fetches data from a Dato space
70
84
  email:
71
85
  - s.verna@cantierecreativo.net
@@ -74,14 +88,29 @@ extensions: []
74
88
  extra_rdoc_files: []
75
89
  files:
76
90
  - ".gitignore"
91
+ - ".gitlab-ci.yml"
92
+ - ".rspec"
77
93
  - ".rubocop.yml"
78
94
  - Gemfile
79
95
  - Rakefile
96
+ - foo.rb
80
97
  - lib/dato/client.rb
81
- - lib/dato/field.rb
82
- - lib/dato/fields/belongs_to.rb
83
- - lib/dato/fields/file.rb
84
- - lib/dato/fields/seo.rb
98
+ - lib/dato/entities_repo.rb
99
+ - lib/dato/field_type/boolean.rb
100
+ - lib/dato/field_type/date.rb
101
+ - lib/dato/field_type/date_time.rb
102
+ - lib/dato/field_type/file.rb
103
+ - lib/dato/field_type/float.rb
104
+ - lib/dato/field_type/image.rb
105
+ - lib/dato/field_type/integer.rb
106
+ - lib/dato/field_type/lat_lon.rb
107
+ - lib/dato/field_type/link.rb
108
+ - lib/dato/field_type/links.rb
109
+ - lib/dato/field_type/seo.rb
110
+ - lib/dato/field_type/string.rb
111
+ - lib/dato/field_type/text.rb
112
+ - lib/dato/field_type/video.rb
113
+ - lib/dato/json_api_entity.rb
85
114
  - lib/dato/meta_tags/article_modified_time.rb
86
115
  - lib/dato/meta_tags/article_publisher.rb
87
116
  - lib/dato/meta_tags/base.rb
@@ -100,14 +129,22 @@ files:
100
129
  - lib/dato/meta_tags_builder.rb
101
130
  - lib/dato/middleman_extension.rb
102
131
  - lib/dato/record.rb
103
- - lib/dato/repo.rb
132
+ - lib/dato/records_repo.rb
133
+ - lib/dato/space.rb
104
134
  - lib/middleman-dato.rb
105
- - lib/middleman_extension.rb
106
135
  - middleman-dato.gemspec
107
- - spec/dato/field_spec.rb
136
+ - spec/dato/entities_repo_spec.rb
137
+ - spec/dato/field_type/file_spec.rb
138
+ - spec/dato/field_type/image_spec.rb
139
+ - spec/dato/field_type/lat_lon_spec.rb
140
+ - spec/dato/field_type/seo_spec.rb
141
+ - spec/dato/field_type/video_spec.rb
142
+ - spec/dato/json_api_entity_spec.rb
108
143
  - spec/dato/meta_tags/article_modified_time_spec.rb
144
+ - spec/dato/meta_tags/image_spec.rb
109
145
  - spec/dato/meta_tags/og_locale_spec.rb
110
146
  - spec/dato/record_spec.rb
147
+ - spec/dato/records_repo_spec.rb
111
148
  - spec/spec_helper.rb
112
149
  - spec/support/mock_builder.rb
113
150
  homepage: http://cantierecreativo.net
@@ -124,20 +161,28 @@ required_ruby_version: !ruby/object:Gem::Requirement
124
161
  version: '0'
125
162
  required_rubygems_version: !ruby/object:Gem::Requirement
126
163
  requirements:
127
- - - ">"
164
+ - - ">="
128
165
  - !ruby/object:Gem::Version
129
- version: 1.3.1
166
+ version: '0'
130
167
  requirements: []
131
168
  rubyforge_project:
132
- rubygems_version: 2.2.2
169
+ rubygems_version: 2.5.1
133
170
  signing_key:
134
171
  specification_version: 4
135
172
  summary: Fetches data from a Dato space
136
173
  test_files:
137
- - spec/dato/field_spec.rb
174
+ - spec/dato/entities_repo_spec.rb
175
+ - spec/dato/field_type/file_spec.rb
176
+ - spec/dato/field_type/image_spec.rb
177
+ - spec/dato/field_type/lat_lon_spec.rb
178
+ - spec/dato/field_type/seo_spec.rb
179
+ - spec/dato/field_type/video_spec.rb
180
+ - spec/dato/json_api_entity_spec.rb
138
181
  - spec/dato/meta_tags/article_modified_time_spec.rb
182
+ - spec/dato/meta_tags/image_spec.rb
139
183
  - spec/dato/meta_tags/og_locale_spec.rb
140
184
  - spec/dato/record_spec.rb
185
+ - spec/dato/records_repo_spec.rb
141
186
  - spec/spec_helper.rb
142
187
  - spec/support/mock_builder.rb
143
188
  has_rdoc:
data/lib/dato/field.rb DELETED
@@ -1,43 +0,0 @@
1
- require "active_support/core_ext/hash/indifferent_access"
2
- require "dato/repo"
3
- require "dato/fields/file"
4
- require "dato/fields/seo"
5
- require "time"
6
-
7
- module Dato
8
- class Field
9
- class << self
10
- def value(attribute, field)
11
- attribute = translated_attribute(attribute, field)
12
- converted_attribute(attribute, field) if attribute
13
- end
14
-
15
- private
16
-
17
- def translated_attribute(attribute, field)
18
- field[:localized] ? attribute[I18n.locale.to_sym] : attribute
19
- end
20
-
21
- def converted_attribute(attribute, field)
22
- if factory = field_mappings[field[:field_type].to_sym]
23
- factory.call(attribute)
24
- else
25
- attribute
26
- end
27
- end
28
-
29
- def field_mappings
30
- {
31
- image: Dato::Fields::File.method(:new),
32
- file: Dato::Fields::File.method(:new),
33
- date: Date.method(:parse),
34
- seo: Dato::Fields::Seo.method(:new),
35
- belongs_to_one: Dato::Repo.instance.method(:find),
36
- embeds_one: Dato::Repo.instance.method(:find),
37
- belongs_to_many: Dato::Repo.instance.method(:find_all),
38
- embeds_many: Dato::Repo.instance.method(:find_all)
39
- }
40
- end
41
- end
42
- end
43
- end
@@ -1,21 +0,0 @@
1
- module Dato
2
- module Fields
3
- class BelongsTo
4
- attr_reader :attributes
5
-
6
- def initialize(data)
7
- @record_id = data
8
- field = field.with_indifferent_access
9
- @content_type = field[:record_content_type][:content_type]
10
- end
11
-
12
- def value
13
- Dato::Repo.instance.records_per_content_type
14
- end
15
-
16
- def ==(other)
17
- attributes == other.attributes
18
- end
19
- end
20
- end
21
- end
@@ -1,22 +0,0 @@
1
- require "imgix"
2
-
3
- module Dato
4
- module Fields
5
- class File
6
- attr_reader :attributes
7
-
8
- def initialize(data)
9
- @attributes = data.with_indifferent_access
10
- end
11
-
12
- def file
13
- @file ||= Imgix::Client.new(host: "dato-images.imgix.net", secure: true).
14
- path(attributes[:path])
15
- end
16
-
17
- def ==(other)
18
- attributes == other.attributes
19
- end
20
- end
21
- end
22
- end
@@ -1,37 +0,0 @@
1
- require "imgix"
2
-
3
- module Dato
4
- module Fields
5
- class Seo
6
- attr_reader :attributes
7
-
8
- def initialize(data)
9
- @attributes = data.with_indifferent_access
10
- end
11
-
12
- def image
13
- @attributes[:image] && File.new(@attributes[:image])
14
- end
15
-
16
- def respond_to?(method, include_private = false)
17
- if @attributes.has_key?(method)
18
- true
19
- else
20
- super
21
- end
22
- end
23
-
24
- def method_missing(method, *arguments, &block)
25
- if @attributes.has_key?(method) && arguments.size == 0
26
- @attributes[method.to_sym]
27
- else
28
- super
29
- end
30
- end
31
-
32
- def ==(other)
33
- attributes == other.attributes
34
- end
35
- end
36
- end
37
- end
data/lib/dato/repo.rb DELETED
@@ -1,111 +0,0 @@
1
- require "singleton"
2
- require "active_support/inflector/methods"
3
- require "dato/client"
4
- require "dato/record"
5
-
6
- module Dato
7
- class Repo
8
- include Singleton
9
-
10
- attr_reader :client, :space, :content_types, :records_per_content_type,
11
- :connection_options, :records
12
-
13
- def connection_options=(options)
14
- @connection_options = options
15
- @client = Client.new(
16
- options[:api_host],
17
- options[:domain],
18
- options[:token]
19
- )
20
- end
21
-
22
- def sync!
23
- space_response = client.space.with_indifferent_access
24
- @space = space_response[:data]
25
- @content_types = prepare_content_types(space_response)
26
- @records = client.records.with_indifferent_access
27
- @records_per_content_type = group_by_content_type(@records)
28
- end
29
-
30
- def prepare_content_types(data)
31
- data = data.with_indifferent_access
32
-
33
- Hash[
34
- data[:data][:links][:content_types][:linkage].map do |content_type|
35
- [
36
- content_type[:id],
37
- content_type_data(content_type[:id], data[:included])
38
- ]
39
- end
40
- ]
41
- end
42
-
43
- def content_type_data(content_type, data)
44
- content_type = data.
45
- detect do |item|
46
- item[:type] == "content_type" && item[:id] == content_type
47
- end
48
-
49
- field_ids = content_type[:links][:fields][:linkage].
50
- map { |field| field[:id] }
51
-
52
- fields = Hash[
53
- data.
54
- select do |item|
55
- item[:type] == "field" && field_ids.include?(item[:id])
56
- end.
57
- map do |field|
58
- [field[:attributes][:api_key], field[:attributes]]
59
- end
60
- ]
61
-
62
- {
63
- api_key: content_type[:id],
64
- singleton: content_type[:attributes][:singleton],
65
- fields: fields
66
- }
67
- end
68
-
69
- def find(id)
70
- record = records[:data].detect { |r| r[:id] == id }
71
- normalize_record(record) unless record.nil?
72
- end
73
-
74
- def find_all(ids)
75
- Array(ids).map { |id| find(id) }
76
- end
77
-
78
- def group_by_content_type(data)
79
- Hash[
80
- content_types.map do |id, content_type|
81
- records = data[:data].
82
- select do |record|
83
- record[:links][:content_type][:linkage][:id] == id
84
- end
85
-
86
- if content_type[:singleton]
87
- [id, records.any? ? normalize_record(records.first) : nil]
88
- else
89
- [id.pluralize, group_by_id(records)]
90
- end
91
- end
92
- ]
93
- end
94
-
95
- def group_by_id(records)
96
- Hash[
97
- records.
98
- group_by { |record| record[:id] }.
99
- map { |id, r| [id, normalize_record(r.first)] }
100
- ]
101
- end
102
-
103
- def normalize_record(record)
104
- content_type = record[:links][:content_type][:linkage][:id]
105
- Record.new(
106
- record[:attributes].merge(id: record[:id]),
107
- content_types[content_type]
108
- )
109
- end
110
- end
111
- end