prismic.io 1.2.1 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/prismic/fragments.rb +1 -0
- data/lib/prismic/fragments/image.rb +4 -0
- data/lib/prismic/fragments/slices.rb +59 -0
- data/lib/prismic/json_parsers.rb +21 -6
- data/lib/prismic/version.rb +1 -1
- data/lib/prismic/with_fragments.rb +13 -0
- data/spec/fragments_spec.rb +18 -2
- data/spec/responses_mocks/slices.json +96 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ddebf7e0c8efd6eb548e0c2311e5021611320ef
|
4
|
+
data.tar.gz: 396c64eadc4b74c2355a70cc60723c9a1a7252af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9781e89122d946b0ee9d4c1a11869ec6887e00e8b5c9921e996c537607ce483d9723c27cf789e258ce99cf7f5c837b46956d5a3309883d11b2eefaadad472787
|
7
|
+
data.tar.gz: 97402b3c5ffad257575f73f2583d615028646dcf7d106093e4ecdeb38c2897b7d27022c0d815432c0177370077d189e99d6c41a1115d495c5f45a3269ab48e40
|
data/Gemfile.lock
CHANGED
data/lib/prismic/fragments.rb
CHANGED
@@ -0,0 +1,59 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Prismic
|
3
|
+
module Fragments
|
4
|
+
|
5
|
+
# A fragment of type Slice, an item in a SliceZone
|
6
|
+
class Slice < Fragment
|
7
|
+
attr_accessor :slice_type
|
8
|
+
attr_accessor :value
|
9
|
+
|
10
|
+
def initialize(slice_type, value)
|
11
|
+
@slice_type = slice_type
|
12
|
+
@value = value
|
13
|
+
end
|
14
|
+
|
15
|
+
# Generate an text representation of the group
|
16
|
+
#
|
17
|
+
# @return [String] the text representation
|
18
|
+
def as_text
|
19
|
+
@value.as_text
|
20
|
+
end
|
21
|
+
|
22
|
+
# Generate an HTML representation of the group
|
23
|
+
#
|
24
|
+
# @param link_resolver [LinkResolver] The LinkResolver used to build
|
25
|
+
# application's specific URL
|
26
|
+
#
|
27
|
+
# @return [String] the HTML representation
|
28
|
+
def as_html(link_resolver=nil)
|
29
|
+
@value.as_html(link_resolver)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class SliceZone < Fragment
|
34
|
+
attr_accessor :slices
|
35
|
+
|
36
|
+
def initialize(slices)
|
37
|
+
@slices = slices
|
38
|
+
end
|
39
|
+
|
40
|
+
# Generate an text representation of the group
|
41
|
+
#
|
42
|
+
# @return [String] the text representation
|
43
|
+
def as_text
|
44
|
+
@slices.map { |slice| slice.as_text }.join("\n")
|
45
|
+
end
|
46
|
+
|
47
|
+
# Generate an HTML representation of the group
|
48
|
+
#
|
49
|
+
# @param link_resolver [LinkResolver] The LinkResolver used to build
|
50
|
+
# application's specific URL
|
51
|
+
#
|
52
|
+
# @return [String] the HTML representation
|
53
|
+
def as_html(link_resolver=nil)
|
54
|
+
@slices.map { |slice| slice.as_html(link_resolver) }.join("\n")
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
data/lib/prismic/json_parsers.rb
CHANGED
@@ -23,7 +23,8 @@ module Prismic
|
|
23
23
|
'StructuredText' => method(:structured_text_parser),
|
24
24
|
'Select' => method(:select_parser),
|
25
25
|
'Multiple' => method(:multiple_parser),
|
26
|
-
'Group' => method(:group_parser)
|
26
|
+
'Group' => method(:group_parser),
|
27
|
+
'SliceZone' => method(:slices_parser)
|
27
28
|
}
|
28
29
|
end
|
29
30
|
|
@@ -199,6 +200,24 @@ module Prismic
|
|
199
200
|
Prismic::Fragments::Group.new(fragment_list_array)
|
200
201
|
end
|
201
202
|
|
203
|
+
def slices_parser(json)
|
204
|
+
slices = []
|
205
|
+
json['value'].each do |data|
|
206
|
+
slice_type = data['slice_type']
|
207
|
+
fragment = data['value']
|
208
|
+
slices << Prismic::Fragments::Slice.new(slice_type, fragment_parser(fragment))
|
209
|
+
end
|
210
|
+
Prismic::Fragments::SliceZone.new(slices)
|
211
|
+
end
|
212
|
+
|
213
|
+
def fragment_parser(fragment)
|
214
|
+
if fragment.is_a? Array
|
215
|
+
multiple_parser(fragment)
|
216
|
+
else
|
217
|
+
parsers[fragment['type']].call(fragment)
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
202
221
|
def document_parser(json)
|
203
222
|
data_json = json['data'].values.first # {"doc_type": data}
|
204
223
|
|
@@ -213,11 +232,7 @@ module Prismic
|
|
213
232
|
}
|
214
233
|
|
215
234
|
fragments = Hash[data_json.map { |name, fragment|
|
216
|
-
|
217
|
-
[name, multiple_parser(fragment)]
|
218
|
-
else
|
219
|
-
[name, parsers[fragment['type']].call(fragment)]
|
220
|
-
end
|
235
|
+
[name, fragment_parser(fragment)]
|
221
236
|
}]
|
222
237
|
|
223
238
|
Prismic::Document.new(
|
data/lib/prismic/version.rb
CHANGED
@@ -6,6 +6,13 @@ module Prismic
|
|
6
6
|
# @return [Hash{String => Fragment}]
|
7
7
|
attr_accessor :fragments
|
8
8
|
|
9
|
+
# Generate a Text representation of the entire document
|
10
|
+
#
|
11
|
+
# @return [String] the text representation
|
12
|
+
def as_text
|
13
|
+
fragments.map { |field, fragment| fragment.as_text }.join("\n")
|
14
|
+
end
|
15
|
+
|
9
16
|
# Generate an HTML representation of the entire document
|
10
17
|
#
|
11
18
|
# @param link_resolver [LinkResolver] The LinkResolver used to build
|
@@ -143,6 +150,12 @@ module Prismic
|
|
143
150
|
fragment
|
144
151
|
end
|
145
152
|
|
153
|
+
def get_slice_zone(field)
|
154
|
+
fragment = self[field]
|
155
|
+
return nil unless fragment.is_a? Prismic::Fragments::SliceZone
|
156
|
+
fragment
|
157
|
+
end
|
158
|
+
|
146
159
|
# @return [Array<LinkedDocument>]
|
147
160
|
def linked_documents
|
148
161
|
self.fragments.map { |name, fragment|
|
data/spec/fragments_spec.rb
CHANGED
@@ -485,8 +485,8 @@ describe 'Image' do
|
|
485
485
|
end
|
486
486
|
|
487
487
|
describe 'as_text' do
|
488
|
-
it '
|
489
|
-
|
488
|
+
it 'is empty' do
|
489
|
+
@image.as_text.should == ""
|
490
490
|
end
|
491
491
|
end
|
492
492
|
end
|
@@ -701,3 +701,19 @@ describe 'Group' do
|
|
701
701
|
end
|
702
702
|
|
703
703
|
end
|
704
|
+
|
705
|
+
describe 'Slices' do
|
706
|
+
before do
|
707
|
+
@raw_json_slices = File.read("#{File.dirname(__FILE__)}/responses_mocks/slices.json")
|
708
|
+
@json_slices = JSON.load(@raw_json_slices)
|
709
|
+
@doc = Prismic::JsonParser.document_parser(@json_slices)
|
710
|
+
@slices = @doc.get_slice_zone("article.blocks")
|
711
|
+
@link_resolver = Prismic.link_resolver('master'){|doc_link| "http://localhost/#{doc_link.link_type}/#{doc_link.id}" }
|
712
|
+
end
|
713
|
+
|
714
|
+
it 'parses correctly' do
|
715
|
+
@slices.as_text.should == "\nc'est un bloc features\nC'est un bloc content"
|
716
|
+
@slices.as_html(@link_resolver).should == %[<section data-field="illustration"><img src="https://wroomdev.s3.amazonaws.com/toto/db3775edb44f9818c54baa72bbfc8d3d6394b6ef_hsf_evilsquall.jpg" alt="" width="4285" height="709" /></section>\n<section data-field="title"><span class="text">c'est un bloc features</span></section>\n<p>C'est un bloc content</p>]
|
717
|
+
end
|
718
|
+
|
719
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
{
|
2
|
+
"id":"VQ_hV31Za5EAy02H",
|
3
|
+
"uid":null,
|
4
|
+
"type":"article",
|
5
|
+
"href":"http://toto.wroom.dev/api/documents/search?ref=VQ_uWX1Za0oCy46m&q=%5B%5B%3Ad+%3D+at%28document.id%2C+%22VQ_hV31Za5EAy02H%22%29+%5D%5D",
|
6
|
+
"tags":[],
|
7
|
+
"slugs":["une-activite"],
|
8
|
+
"linked_documents":[],
|
9
|
+
"data":{
|
10
|
+
"article":{
|
11
|
+
"activities":{
|
12
|
+
"type":"Group",
|
13
|
+
"value":[{
|
14
|
+
"title":{
|
15
|
+
"type":"StructuredText",
|
16
|
+
"value":[{
|
17
|
+
"type":"paragraph",
|
18
|
+
"text":"Une activité",
|
19
|
+
"spans":[]
|
20
|
+
}]
|
21
|
+
},
|
22
|
+
"image":{
|
23
|
+
"type":"Image",
|
24
|
+
"value":{
|
25
|
+
"main":{
|
26
|
+
"url":"https://wroomdev.s3.amazonaws.com/toto/ce3f52b933c4934a13422e09ed0ff6ad03a29621_hsf_evilsquall.jpg",
|
27
|
+
"alt":"",
|
28
|
+
"copyright":"",
|
29
|
+
"dimensions":{"width":860,"height":640}
|
30
|
+
},
|
31
|
+
"views":{
|
32
|
+
"headline":{
|
33
|
+
"url":"https://wroomdev.s3.amazonaws.com/toto/5445d2dcd2b0c541b0406ca867ab3d07b309c944_hsf_evilsquall.jpg",
|
34
|
+
"alt":"",
|
35
|
+
"copyright":"",
|
36
|
+
"dimensions":{"width":570,"height":400}
|
37
|
+
}
|
38
|
+
}
|
39
|
+
}
|
40
|
+
},
|
41
|
+
"body":{
|
42
|
+
"type":"StructuredText",
|
43
|
+
"value":[{
|
44
|
+
"type":"paragraph",
|
45
|
+
"text":"elle est bien",
|
46
|
+
"spans":[]
|
47
|
+
}]
|
48
|
+
}
|
49
|
+
}]
|
50
|
+
},
|
51
|
+
"un_champ_texte":{
|
52
|
+
"type":"Text",
|
53
|
+
"value":"stuffgg"
|
54
|
+
},
|
55
|
+
"blocks":{
|
56
|
+
"type":"SliceZone",
|
57
|
+
"value":[{
|
58
|
+
"type":"Slice",
|
59
|
+
"slice_type": "features",
|
60
|
+
"value":{
|
61
|
+
"type":"Group",
|
62
|
+
"value":[{
|
63
|
+
"illustration":{
|
64
|
+
"type":"Image",
|
65
|
+
"value":{
|
66
|
+
"main":{
|
67
|
+
"url":"https://wroomdev.s3.amazonaws.com/toto/db3775edb44f9818c54baa72bbfc8d3d6394b6ef_hsf_evilsquall.jpg",
|
68
|
+
"alt":"",
|
69
|
+
"copyright":"",
|
70
|
+
"dimensions":{"width":4285,"height":709}
|
71
|
+
},
|
72
|
+
"views":{}
|
73
|
+
}
|
74
|
+
},
|
75
|
+
"title":{
|
76
|
+
"type":"Text",
|
77
|
+
"value":"c'est un bloc features"
|
78
|
+
}
|
79
|
+
}]
|
80
|
+
}
|
81
|
+
},{
|
82
|
+
"type":"Slice",
|
83
|
+
"slice_type":"text",
|
84
|
+
"value":{
|
85
|
+
"type":"StructuredText",
|
86
|
+
"value":[{
|
87
|
+
"type":"paragraph",
|
88
|
+
"text":"C'est un bloc content",
|
89
|
+
"spans":[]
|
90
|
+
}]
|
91
|
+
}
|
92
|
+
}]
|
93
|
+
}
|
94
|
+
}
|
95
|
+
}
|
96
|
+
}
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prismic.io
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Étienne Vallette d'Osia
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2015-
|
14
|
+
date: 2015-07-09 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: bundler
|
@@ -116,6 +116,7 @@ files:
|
|
116
116
|
- lib/prismic/fragments/multiple.rb
|
117
117
|
- lib/prismic/fragments/number.rb
|
118
118
|
- lib/prismic/fragments/select.rb
|
119
|
+
- lib/prismic/fragments/slices.rb
|
119
120
|
- lib/prismic/fragments/structured_text.rb
|
120
121
|
- lib/prismic/fragments/text.rb
|
121
122
|
- lib/prismic/fragments/timestamp.rb
|
@@ -140,6 +141,7 @@ files:
|
|
140
141
|
- spec/responses_mocks/document_with_unknown_fragment.json
|
141
142
|
- spec/responses_mocks/experiments.json
|
142
143
|
- spec/responses_mocks/fragments.json
|
144
|
+
- spec/responses_mocks/slices.json
|
143
145
|
- spec/responses_mocks/structured_text_heading.json
|
144
146
|
- spec/responses_mocks/structured_text_linkfile.json
|
145
147
|
- spec/responses_mocks/structured_text_paragraph.json
|
@@ -186,6 +188,7 @@ test_files:
|
|
186
188
|
- spec/responses_mocks/document_with_unknown_fragment.json
|
187
189
|
- spec/responses_mocks/experiments.json
|
188
190
|
- spec/responses_mocks/fragments.json
|
191
|
+
- spec/responses_mocks/slices.json
|
189
192
|
- spec/responses_mocks/structured_text_heading.json
|
190
193
|
- spec/responses_mocks/structured_text_linkfile.json
|
191
194
|
- spec/responses_mocks/structured_text_paragraph.json
|