diff --git a/Gemfile.lock b/Gemfile.lock index fa70d75..01c8fc2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - prismic.io (1.4.4) + prismic.io (1.4.5) hashery (~> 2.1.1) GEM diff --git a/lib/prismic/fragments/slices.rb b/lib/prismic/fragments/slices.rb index 6874884..2f5e493 100644 --- a/lib/prismic/fragments/slices.rb +++ b/lib/prismic/fragments/slices.rb @@ -2,8 +2,57 @@ module Prismic module Fragments - # A fragment of type Slice, an item in a SliceZone - class Slice < Fragment + # A fragment of type CompositeSlice, an item in a SliceZone + class CompositeSlice < Fragment + attr_accessor :slice_type + attr_accessor :slice_label + attr_accessor :non_repeat + attr_accessor :repeat + + def initialize(slice_type, slice_label, non_repeat, repeat) + @slice_type = slice_type + @slice_label = slice_label + @non_repeat = non_repeat + @repeat = repeat + end + + # Generate an text representation of the group + # + # @return [String] the text representation + def as_text + non_repeat_text = '' + @non_repeat.each do |fragment_key, fragment_value| + non_repeat_text += fragment_value.as_text + end + + "#{non_repeat_text}\n#{@repeat.as_text}" + end + + # Generate an HTML representation of the group + # + # @param link_resolver [LinkResolver] The LinkResolver used to build + # application's specific URL + # + # @return [String] the HTML representation + def as_html(link_resolver=nil) + classes = ['slice'] + unless (@slice_label.nil?) + classes.push(@slice_label) + end + + non_repeat_html = '' + @non_repeat.each do |fragment_key, fragment_value| + non_repeat_html += fragment_value.as_html(link_resolver) + end + + repeat_html = repeat.as_html(link_resolver) + + %[
#{non_repeat_html + repeat_html}
] + end + end + + # A fragment of type SimpleSlice, an item in a SliceZone + class SimpleSlice < Fragment attr_accessor :slice_type attr_accessor :slice_label attr_accessor :value diff --git a/lib/prismic/json_parsers.rb b/lib/prismic/json_parsers.rb index 7615a62..a9823b3 100644 --- a/lib/prismic/json_parsers.rb +++ b/lib/prismic/json_parsers.rb @@ -208,8 +208,23 @@ def group_parser(json) def slices_parser(json) slices = [] + json['value'].each do |data| - slices << Prismic::Fragments::Slice.new(data['slice_type'], data['slice_label'], fragment_parser(data['value'])) + slice_type = data['slice_type'] + slice_label = data['slice_label'] + + if data.key?('value') + slices << Prismic::Fragments::SimpleSlice.new(slice_type, slice_label, fragment_parser(data['value'])) + else + non_repeat = {} + data['non-repeat'].each do |fragment_key, fragment_value| + non_repeat[fragment_key] = fragment_parser(fragment_value) + end + + repeat = group_parser({ 'type' => 'Group', 'value' => data['repeat']}) + + slices << Prismic::Fragments::CompositeSlice.new(slice_type, slice_label, non_repeat, repeat) + end end Prismic::Fragments::SliceZone.new(slices) end diff --git a/spec/fragments_spec.rb b/spec/fragments_spec.rb index 8f509a6..54a775d 100644 --- a/spec/fragments_spec.rb +++ b/spec/fragments_spec.rb @@ -720,9 +720,9 @@ def strong(start, stop) end -describe 'Slices' do +describe 'Simple slice' do before do - @raw_json_slices = File.read("#{File.dirname(__FILE__)}/responses_mocks/slices.json") + @raw_json_slices = File.read("#{File.dirname(__FILE__)}/responses_mocks/simple_slice.json") @json_slices = JSON.load(@raw_json_slices) @doc = Prismic::JsonParser.document_parser(@json_slices) @slices = @doc.get_slice_zone("article.blocks") @@ -734,4 +734,34 @@ def strong(start, stop) @slices.as_html(@link_resolver).gsub(''', "'").should == %[
\n
c'est un bloc features
\n

C'est un bloc content

\n

] end + it 'get item correctly' do + expected_url = 'https://wroomdev.s3.amazonaws.com/toto/db3775edb44f9818c54baa72bbfc8d3d6394b6ef_hsf_evilsquall.jpg' + @slices.slices[0].value[0]["illustration"].url.should == expected_url + end +end + +describe 'Composite slice' do + before do + @raw_json_slices = File.read("#{File.dirname(__FILE__)}/responses_mocks/composite_slice.json") + @json_slices = JSON.load(@raw_json_slices) + @doc = Prismic::JsonParser.document_parser(@json_slices) + @slices = @doc.get_slice_zone("nav-nodejs.items") + @link_resolver = Prismic.link_resolver('master'){|doc_link| "http://localhost/#{doc_link.type}/#{doc_link.id}" } + end + + it 'get html' do + @slices.as_html(@link_resolver).gsub(''', "'").should == %[

Getting Started

Start from Scratch

\n
page---nodejs---getting-started-with-the-starter-kit
] + end + + it 'get text' do + @slices.as_text.should == %[Getting Started\nStart from Scratch\n] + end + + it 'get item in non-repeat correctly' do + @slices.slices[0].non_repeat['label'].as_text.should == 'Getting Started' + end + + it 'get item in repeat correctly' do + @slices.slices[0].repeat[0]['label'].as_text.should == 'Start from Scratch' + end end diff --git a/spec/responses_mocks/composite_slice.json b/spec/responses_mocks/composite_slice.json new file mode 100644 index 0000000..3111099 --- /dev/null +++ b/spec/responses_mocks/composite_slice.json @@ -0,0 +1,63 @@ +{ + "id": "WPeLbyoAACsAB1Su", + "uid": null, + "type": "nav-nodejs", + "href": "http://prismicio-docs.prismic.io/api/v1/documents/search?ref=WXnqmigAALs3V7Kx&q=%5B%5B%3Ad+%3D+at%28document.id%2C+%22WPeLbyoAACsAB1Su%22%29+%5D%5D", + "tags": [], + "first_publication_date": "2017-04-19T16:08:43+0000", + "last_publication_date": "2017-07-07T11:26:27+0000", + "slugs": ["navigation---nodejs", "navigation-nodejs"], + "data": { + "nav-nodejs": { + "display-name": { + "type": "StructuredText", + "value": [{ + "type": "paragraph", + "text": "Navigation - NodeJS", + "spans": [] + }] + }, + "items": { + "type": "SliceZone", + "value": [{ + "type": "Slice", + "slice_type": "with-submenu", + "slice_label": null, + "repeat": [{ + "label": { + "type": "StructuredText", + "value": [{ + "type": "paragraph", + "text": "Start from Scratch", + "spans": [] + }] + }, + "link": { + "type": "Link.document", + "value": { + "document": { + "id": "WPeD0SoAACsABzNC", + "type": "page-nodejs", + "tags": [], + "slug": "page---nodejs---getting-started-with-the-starter-kit", + "uid": "prismic-from-scratch-with-nodejs" + }, + "isBroken": false + } + } + }], + "non-repeat": { + "label": { + "type": "StructuredText", + "value": [{ + "type": "paragraph", + "text": "Getting Started", + "spans": [] + }] + } + } + }] + } + } + } +} diff --git a/spec/responses_mocks/slices.json b/spec/responses_mocks/simple_slice.json similarity index 100% rename from spec/responses_mocks/slices.json rename to spec/responses_mocks/simple_slice.json