middleman-dato 0.5.16.3 → 0.5.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +3 -0
  3. data/.travis.yml +0 -1
  4. data/Gemfile +1 -0
  5. data/Rakefile +1 -0
  6. data/lib/middleman-dato.rb +2 -1
  7. data/lib/middleman_dato/meta_tags/article_modified_time.rb +1 -0
  8. data/lib/middleman_dato/meta_tags/article_publisher.rb +1 -0
  9. data/lib/middleman_dato/meta_tags/base.rb +3 -2
  10. data/lib/middleman_dato/meta_tags/description.rb +1 -0
  11. data/lib/middleman_dato/meta_tags/favicon.rb +7 -11
  12. data/lib/middleman_dato/meta_tags/image.rb +1 -0
  13. data/lib/middleman_dato/meta_tags/og_locale.rb +1 -0
  14. data/lib/middleman_dato/meta_tags/og_meta_tag.rb +1 -0
  15. data/lib/middleman_dato/meta_tags/og_site_name.rb +1 -0
  16. data/lib/middleman_dato/meta_tags/og_type.rb +1 -0
  17. data/lib/middleman_dato/meta_tags/robots.rb +1 -0
  18. data/lib/middleman_dato/meta_tags/title.rb +1 -0
  19. data/lib/middleman_dato/meta_tags/twitter_card.rb +1 -0
  20. data/lib/middleman_dato/meta_tags/twitter_meta_tag.rb +1 -0
  21. data/lib/middleman_dato/meta_tags/twitter_site.rb +1 -0
  22. data/lib/middleman_dato/meta_tags/url.rb +1 -0
  23. data/lib/middleman_dato/meta_tags_builder.rb +1 -0
  24. data/lib/middleman_dato/middleman_extension.rb +14 -5
  25. data/lib/middleman_dato/version.rb +2 -2
  26. data/middleman-dato.gemspec +3 -4
  27. data/spec/middleman_dato/meta_tags/article_modified_time_spec.rb +1 -0
  28. data/spec/middleman_dato/meta_tags/favicon_spec.rb +8 -7
  29. data/spec/middleman_dato/meta_tags/image_spec.rb +1 -0
  30. data/spec/middleman_dato/meta_tags/og_locale_spec.rb +1 -0
  31. data/spec/spec_helper.rb +7 -7
  32. data/spec/support/mock_builder.rb +1 -0
  33. metadata +8 -73
  34. data/lib/middleman_dato/client.rb +0 -68
  35. data/lib/middleman_dato/entities_repo.rb +0 -43
  36. data/lib/middleman_dato/field_type/boolean.rb +0 -9
  37. data/lib/middleman_dato/field_type/date.rb +0 -9
  38. data/lib/middleman_dato/field_type/date_time.rb +0 -9
  39. data/lib/middleman_dato/field_type/file.rb +0 -30
  40. data/lib/middleman_dato/field_type/float.rb +0 -9
  41. data/lib/middleman_dato/field_type/image.rb +0 -29
  42. data/lib/middleman_dato/field_type/integer.rb +0 -9
  43. data/lib/middleman_dato/field_type/lat_lon.rb +0 -20
  44. data/lib/middleman_dato/field_type/link.rb +0 -9
  45. data/lib/middleman_dato/field_type/links.rb +0 -9
  46. data/lib/middleman_dato/field_type/seo.rb +0 -21
  47. data/lib/middleman_dato/field_type/string.rb +0 -9
  48. data/lib/middleman_dato/field_type/text.rb +0 -9
  49. data/lib/middleman_dato/field_type/video.rb +0 -52
  50. data/lib/middleman_dato/item.rb +0 -121
  51. data/lib/middleman_dato/items_repo.rb +0 -107
  52. data/lib/middleman_dato/json_api_entity.rb +0 -75
  53. data/lib/middleman_dato/site.rb +0 -36
  54. data/spec/middleman_dato/entities_repo_spec.rb +0 -41
  55. data/spec/middleman_dato/field_type/file_spec.rb +0 -20
  56. data/spec/middleman_dato/field_type/image_spec.rb +0 -24
  57. data/spec/middleman_dato/field_type/lat_lon_spec.rb +0 -18
  58. data/spec/middleman_dato/field_type/seo_spec.rb +0 -31
  59. data/spec/middleman_dato/field_type/video_spec.rb +0 -32
  60. data/spec/middleman_dato/item_spec.rb +0 -188
  61. data/spec/middleman_dato/items_repo_spec.rb +0 -125
  62. data/spec/middleman_dato/json_api_entity_spec.rb +0 -123
@@ -1,107 +0,0 @@
1
- require 'active_support/core_ext/string'
2
- require 'middleman_dato/item'
3
-
4
- module MiddlemanDato
5
- class ItemsRepo
6
- attr_reader :entities_repo, :collections_by_type
7
-
8
- def initialize(entities_repo)
9
- @entities_repo = entities_repo
10
- @collections_by_type = {}
11
- @items_by_id = {}
12
-
13
- build_cache!
14
- end
15
-
16
- def find(id)
17
- @items_by_id[id]
18
- end
19
-
20
- def respond_to?(method, include_private = false)
21
- if collections_by_type.key?(method)
22
- true
23
- else
24
- super
25
- end
26
- end
27
-
28
- private
29
-
30
- def item_type_key(item_type)
31
- api_key = item_type.api_key
32
- if item_type.singleton
33
- [api_key.to_sym, true]
34
- else
35
- [api_key.pluralize.to_sym, false]
36
- end
37
- end
38
-
39
- def build_cache!
40
- item_type_entities.each do |item_type|
41
- key, singleton = item_type_key(item_type)
42
- @collections_by_type[key] = if singleton
43
- nil
44
- else
45
- ItemCollection.new
46
- end
47
- end
48
-
49
- item_entities.each do |item_entity|
50
- item = Item.new(item_entity, self)
51
-
52
- key, singleton = item_type_key(item_entity.item_type)
53
- if singleton
54
- @collections_by_type[key] = item
55
- else
56
- @collections_by_type[key].push item
57
- end
58
-
59
- @items_by_id[item.id] = item
60
- end
61
- end
62
-
63
- def item_type_entities
64
- entities_repo.find_entities_of_type('item_type')
65
- end
66
-
67
- def item_entities
68
- entities_repo.find_entities_of_type('item')
69
- end
70
-
71
- def method_missing(method, *arguments, &block)
72
- if collections_by_type.key?(method) && arguments.empty?
73
- collections_by_type[method]
74
- else
75
- super
76
- end
77
- end
78
-
79
- class ItemCollection < Array
80
- def each(&block)
81
- if block && block.arity == 2
82
- each_with_object({}) do |item, acc|
83
- acc[item.id] = item
84
- end.each(&block)
85
- else
86
- super(&block)
87
- end
88
- end
89
-
90
- def [](id)
91
- if id.is_a? String
92
- find { |item| item.id == id }
93
- else
94
- super(id)
95
- end
96
- end
97
-
98
- def keys
99
- map(&:id)
100
- end
101
-
102
- def values
103
- to_a
104
- end
105
- end
106
- end
107
- end
@@ -1,75 +0,0 @@
1
- module MiddlemanDato
2
- class JsonApiEntity
3
- attr_reader :payload, :data_source
4
-
5
- def initialize(payload, data_source)
6
- @payload = payload
7
- @data_source = data_source
8
- end
9
-
10
- def id
11
- @payload[:id]
12
- end
13
-
14
- def type
15
- @payload[:type]
16
- end
17
-
18
- def ==(other)
19
- if other.is_a? JsonApiEntity
20
- id == other.id && type == other.type
21
- else
22
- false
23
- end
24
- end
25
-
26
- def to_s
27
- "#<JsonApiEntity id=#{id} type=#{type} payload=#{payload}>"
28
- end
29
- alias inspect to_s
30
-
31
- def [](key)
32
- attributes[key]
33
- end
34
-
35
- def respond_to?(method, include_private = false)
36
- if attributes.key?(method) || relationships.key?(method)
37
- true
38
- else
39
- super
40
- end
41
- end
42
-
43
- private
44
-
45
- def attributes
46
- @payload.fetch(:attributes, {})
47
- end
48
-
49
- def relationships
50
- @payload.fetch(:relationships, {})
51
- end
52
-
53
- def dereference_linkage(linkage)
54
- if linkage.is_a? Array
55
- linkage.map do |item|
56
- data_source.find_entity(item[:type], item[:id])
57
- end
58
- else
59
- data_source.find_entity(linkage[:type], linkage[:id])
60
- end
61
- end
62
-
63
- def method_missing(method, *arguments, &block)
64
- return super unless arguments.empty?
65
-
66
- if attributes.key?(method)
67
- attributes[method]
68
- elsif relationships.key?(method)
69
- dereference_linkage(relationships[method][:data])
70
- else
71
- super
72
- end
73
- end
74
- end
75
- end
@@ -1,36 +0,0 @@
1
- # coding: utf-8
2
- require 'middleman_dato/client'
3
- require 'middleman_dato/entities_repo'
4
- require 'middleman_dato/items_repo'
5
-
6
- module MiddlemanDato
7
- class Site
8
- attr_reader :options
9
- attr_reader :entities_repo
10
- attr_reader :items_repo
11
-
12
- def initialize(options)
13
- @options = options
14
- @entities_repo = EntitiesRepo.new
15
- @items_repo = ItemsRepo.new(@entities_repo)
16
- end
17
-
18
- def refresh!
19
- @entities_repo = EntitiesRepo.new(client.site, client.items)
20
- @items_repo = ItemsRepo.new(@entities_repo)
21
- end
22
-
23
- def entity
24
- @entities_repo.find_entities_of_type('site').first
25
- end
26
-
27
- private
28
-
29
- def client
30
- @client ||= Client.new(
31
- options[:api_host],
32
- options[:token]
33
- )
34
- end
35
- end
36
- end
@@ -1,41 +0,0 @@
1
- require 'middleman_dato/json_api_entity'
2
-
3
- module MiddlemanDato
4
- RSpec.describe EntitiesRepo do
5
- subject(:source) { described_class.new(payload) }
6
-
7
- describe '#entities' do
8
- context 'a payload with a data key' do
9
- context 'object' do
10
- let(:payload) do
11
- { data: { id: 'bar', type: 'item' } }
12
- end
13
-
14
- it 'inserts the object into entities' do
15
- expect(source.entities['item']['bar']).to be_a JsonApiEntity
16
- end
17
- end
18
-
19
- context 'array' do
20
- let(:payload) do
21
- { data: [{ id: 'bar', type: 'item' }] }
22
- end
23
-
24
- it 'inserts the objects into entities' do
25
- expect(source.entities['item']['bar']).to be_a JsonApiEntity
26
- end
27
- end
28
- end
29
-
30
- context 'a payload with an included key' do
31
- let(:payload) do
32
- { included: [{ id: 'bar', type: 'item' }] }
33
- end
34
-
35
- it 'inserts the objects into entities' do
36
- expect(source.entities['item']['bar']).to be_a JsonApiEntity
37
- end
38
- end
39
- end
40
- end
41
- end
@@ -1,20 +0,0 @@
1
- module MiddlemanDato
2
- module FieldType
3
- RSpec.describe MiddlemanDato::FieldType::File do
4
- subject(:file) { described_class.parse(attributes, nil) }
5
- let(:attributes) do
6
- {
7
- path: '/foo.png',
8
- format: 'jpg',
9
- size: 4000
10
- }
11
- end
12
-
13
- it 'responds to path, format and size methods' do
14
- expect(file.path).to eq '/foo.png'
15
- expect(file.format).to eq 'jpg'
16
- expect(file.size).to eq 4000
17
- end
18
- end
19
- end
20
- end
@@ -1,24 +0,0 @@
1
- module MiddlemanDato
2
- module FieldType
3
- RSpec.describe Image do
4
- subject(:image) { described_class.parse(attributes, nil) }
5
- let(:attributes) do
6
- {
7
- path: '/foo.png',
8
- format: 'jpg',
9
- size: 4000,
10
- width: 20,
11
- height: 20
12
- }
13
- end
14
-
15
- it 'responds to path, format, size, width and height' do
16
- expect(image.path).to eq '/foo.png'
17
- expect(image.format).to eq 'jpg'
18
- expect(image.size).to eq 4000
19
- expect(image.width).to eq 20
20
- expect(image.height).to eq 20
21
- end
22
- end
23
- end
24
- end
@@ -1,18 +0,0 @@
1
- module MiddlemanDato
2
- module FieldType
3
- RSpec.describe LatLon do
4
- subject(:latlon) { described_class.parse(attributes, nil) }
5
- let(:attributes) do
6
- {
7
- latitude: 12,
8
- longitude: 10
9
- }
10
- end
11
-
12
- it 'responds to latitude and longitude methods' do
13
- expect(latlon.latitude).to eq 12
14
- expect(latlon.longitude).to eq 10
15
- end
16
- end
17
- end
18
- end
@@ -1,31 +0,0 @@
1
- module MiddlemanDato
2
- module FieldType
3
- RSpec.describe Seo do
4
- subject(:seo) { described_class.parse(attributes, nil) }
5
- let(:attributes) do
6
- {
7
- title: 'title',
8
- description: 'description',
9
- image: {
10
- path: '/foo.png',
11
- format: 'jpg',
12
- size: 4000,
13
- width: 20,
14
- height: 20
15
- }
16
- }
17
- end
18
-
19
- it 'responds to title, description and image methods' do
20
- expect(seo.title).to eq 'title'
21
- expect(seo.description).to eq 'description'
22
- expect(seo.image).to be_a MiddlemanDato::FieldType::Image
23
- expect(seo.image.path).to eq '/foo.png'
24
- expect(seo.image.format).to eq 'jpg'
25
- expect(seo.image.size).to eq 4000
26
- expect(seo.image.width).to eq 20
27
- expect(seo.image.height).to eq 20
28
- end
29
- end
30
- end
31
- end
@@ -1,32 +0,0 @@
1
- module MiddlemanDato
2
- module FieldType
3
- RSpec.describe Video do
4
- subject(:video) { described_class.parse(attributes, nil) }
5
- let(:attributes) do
6
- {
7
- url: 'https://www.youtube.com/watch?v=oHg5SJYRHA0',
8
- provider_uid: '123123',
9
- thumbnail_url: 'http://i3.ytimg.com/vi/oHg5SJYRHA0/hqdefault.jpg',
10
- title: "RickRoll'D",
11
- width: 640,
12
- height: 480
13
- }
14
- end
15
-
16
- it 'responds to path, format, size, width and height' do
17
- expect(video.url).to eq 'https://www.youtube.com/watch?v=oHg5SJYRHA0'
18
- expect(video.provider_uid).to eq '123123'
19
- expect(video.thumbnail_url).to eq 'http://i3.ytimg.com/vi/oHg5SJYRHA0/hqdefault.jpg'
20
- expect(video.title).to eq "RickRoll'D"
21
- expect(video.width).to eq 640
22
- expect(video.height).to eq 480
23
- end
24
-
25
- describe 'iframe_embed' do
26
- it 'returns a iframe embed HTML fragment' do
27
- expect(video.iframe_embed).to eq '<iframe width="560" height="315" src="http://www.youtube.com/embed/oHg5SJYRHA0?rel=0" frameborder="0" allowfullscreen></iframe>'
28
- end
29
- end
30
- end
31
- end
32
- end
@@ -1,188 +0,0 @@
1
- module MiddlemanDato
2
- RSpec.describe Item do
3
- subject(:item) { described_class.new(entity, repo) }
4
- let(:entity) do
5
- double(
6
- 'MiddlemanDato::JsonApiEntity(Item)',
7
- id: '14',
8
- item_type: item_type,
9
- title: "My titlè with àccents",
10
- body: 'Hi there',
11
- position: 2,
12
- updated_at: '2010-01-01T00:00'
13
- )
14
- end
15
- let(:repo) do
16
- instance_double('MiddlemanDato::ItemsRepo')
17
- end
18
- let(:item_type) do
19
- double(
20
- 'MiddlemanDato::JsonApiEntity(Content Type)',
21
- singleton: is_singleton,
22
- api_key: 'work_item',
23
- fields: fields
24
- )
25
- end
26
- let(:is_singleton) { false }
27
- let(:fields) do
28
- [
29
- double(
30
- 'MiddlemanDato::JsonApiEntity(Field)',
31
- position: 1,
32
- api_key: 'title',
33
- localized: false,
34
- field_type: 'string',
35
- appeareance: { type: 'title' }
36
- ),
37
- double(
38
- 'MiddlemanDato::JsonApiEntity(Field)',
39
- position: 1,
40
- api_key: 'body',
41
- localized: false,
42
- field_type: 'text',
43
- appeareance: { type: 'plain' }
44
- )
45
- ]
46
- end
47
-
48
- describe '#slug' do
49
- context 'singleton' do
50
- let(:is_singleton) { true }
51
-
52
- it 'returns the parameterized content type api key' do
53
- expect(item.slug).to eq 'work-item'
54
- end
55
- end
56
-
57
- context 'non singleton, no title field' do
58
- let(:fields) { [] }
59
-
60
- it 'returns the ID' do
61
- expect(item.slug).to eq '14'
62
- end
63
- end
64
-
65
- context 'non singleton, title field' do
66
- it 'returns the ID + title' do
67
- expect(item.slug).to eq '14-my-title-with-accents'
68
- end
69
- end
70
- end
71
-
72
- describe '#attributes' do
73
- it 'returns an hash of the field values' do
74
- expected_attributes = {
75
- 'title' => "My titlè with àccents",
76
- 'body' => 'Hi there'
77
- }
78
- expect(item.attributes).to eq expected_attributes
79
- end
80
- end
81
-
82
- describe 'position' do
83
- it 'returns the entity position field' do
84
- expect(item.position).to eq 2
85
- end
86
- end
87
-
88
- describe 'updated_at' do
89
- it 'returns the entity updated_at field' do
90
- expect(item.updated_at).to be_a Time
91
- end
92
- end
93
-
94
- describe 'dynamic methods' do
95
- context 'existing field' do
96
- it 'returns the field value' do
97
- expect(item.respond_to?(:body)).to be_truthy
98
- expect(item.body).to eq 'Hi there'
99
- end
100
-
101
- context 'localized field' do
102
- let(:entity) do
103
- double(
104
- 'MiddlemanDato::JsonApiEntity(Item)',
105
- id: '14',
106
- item_type: item_type,
107
- title: { it: 'Foo', en: 'Bar' }
108
- )
109
- end
110
-
111
- let(:fields) do
112
- [
113
- double(
114
- 'MiddlemanDato::JsonApiEntity(Field)',
115
- position: 1,
116
- api_key: 'title',
117
- localized: true,
118
- field_type: 'string',
119
- appeareance: { type: 'plain' }
120
- )
121
- ]
122
- end
123
-
124
- it 'returns the value for the current locale' do
125
- I18n.with_locale(:it) do
126
- expect(item.title).to eq 'Foo'
127
- end
128
- end
129
-
130
- context 'non existing value' do
131
- it 'raises nil' do
132
- I18n.with_locale(:ru) do
133
- expect(item.title).to eq nil
134
- end
135
- end
136
- end
137
- end
138
- end
139
-
140
- context 'non existing field' do
141
- it 'raises NoMethodError' do
142
- expect(item.respond_to?(:qux)).to be_falsy
143
- expect { item.qux }.to raise_error NoMethodError
144
- end
145
- end
146
-
147
- context 'non existing field type' do
148
- let(:fields) do
149
- [
150
- double(
151
- 'MiddlemanDato::JsonApiEntity(Field)',
152
- position: 1,
153
- api_key: 'title',
154
- localized: true,
155
- field_type: 'rotfl'
156
- )
157
- ]
158
- end
159
-
160
- it 'raises RuntimeError' do
161
- expect { item.title }.to raise_error RuntimeError
162
- end
163
- end
164
- end
165
-
166
- context 'equality' do
167
- subject(:same_item) { described_class.new(entity, repo) }
168
-
169
- subject(:another_item) { described_class.new(another_entity, repo) }
170
- let(:another_entity) do
171
- double(
172
- 'MiddlemanDato::JsonApiEntity(Item)',
173
- id: '15'
174
- )
175
- end
176
-
177
-
178
- it 'two items are equal if their id is the same' do
179
- expect(item).to eq same_item
180
- end
181
-
182
- it 'else they\'re not' do
183
- expect(item).not_to eq another_item
184
- expect(item).not_to eq "foobar"
185
- end
186
- end
187
- end
188
- end