Module: Prismic::JsonParser
- Defined in:
- lib/prismic/json_parsers.rb
Constant Summary
- @@warned_unknown_type =
[]
Class Method Summary collapse
- .color_parser(json) ⇒ Object
- .date_parser(json) ⇒ Object
- .document_link_parser(json) ⇒ Object
- .document_parser(json) ⇒ Object
- .embed_parser(json) ⇒ Object
- .file_link_parser(json) ⇒ Object
- .fragment_parser(fragment) ⇒ Object
- .geo_point_parser(json) ⇒ Object
- .group_parser(json) ⇒ Object
- .image_link_parser(json) ⇒ Object
- .image_parser(json) ⇒ Object
- .multiple_parser(json) ⇒ Object
- .number_parser(json) ⇒ Object
- .parsers ⇒ Object
- .response_parser(documents) ⇒ Object
- .results_parser(results) ⇒ Object
- .select_parser(json) ⇒ Object
- .separator_parser(json) ⇒ Object
- .slices_parser(json) ⇒ Object
- .span_parser(span) ⇒ Object
- .structured_text_parser(json) ⇒ Object
- .text_parser(json) ⇒ Object
- .timestamp_parser(json) ⇒ Object
- .web_link_parser(json) ⇒ Object
Class Method Details
.color_parser(json) ⇒ Object
119 120 121 |
# File 'lib/prismic/json_parsers.rb', line 119 def color_parser(json) Prismic::Fragments::Color.new(json['value'][1..6]) end |
.date_parser(json) ⇒ Object
79 80 81 |
# File 'lib/prismic/json_parsers.rb', line 79 def date_parser(json) Prismic::Fragments::Date.new(Time.parse(json['value'])) end |
.document_link_parser(json) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/prismic/json_parsers.rb', line 32 def document_link_parser(json) doc = json['value']['document'] type = doc['type'] fragments = {} if doc['data'] and doc['data'][type] fragments = Hash[doc['data'][type].map { |name, fragment| if fragment.is_a? Array [name, multiple_parser(fragment)] else [name, parsers[fragment['type']].call(fragment)] end }] end Prismic::Fragments::DocumentLink.new( doc['id'], doc['uid'], type, doc['tags'], URI.unescape(doc['slug']), fragments, json['value']['isBroken']) end |
.document_parser(json) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/prismic/json_parsers.rb', line 224 def document_parser(json) data_json = json['data'].values.first # {"doc_type": data} # Removing the unknown types + sending a warning, once data_json.select!{ |_, fragment| known_type = fragment.is_a?(Array) || parsers.include?(fragment['type']) if !known_type && !@@warned_unknown_type.include?(fragment['type']) warn "Type #{fragment['type']} is unknown, fragment was skipped; perhaps you should update your prismic.io gem?" @@warned_unknown_type << fragment['type'] end known_type } fragments = Hash[data_json.map { |name, fragment| [name, fragment_parser(fragment)] }] Prismic::Document.new( json['id'], json['uid'], json['type'], json['href'], json['tags'], json['slugs'].map { |slug| URI.unescape(slug) }, json['first_publication_date'] && Time.parse(json['first_publication_date']), json['last_publication_date'] && Time.parse(json['last_publication_date']), fragments) end |
.embed_parser(json) ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/prismic/json_parsers.rb', line 91 def (json) = json['value']['oembed'] Prismic::Fragments::Embed.new( ['type'], ['provider_name'], ['provider_url'], ['html'], ) end |
.file_link_parser(json) ⇒ Object
59 60 61 |
# File 'lib/prismic/json_parsers.rb', line 59 def file_link_parser(json) Prismic::Fragments::FileLink.new(json['value']['file']['url'], json['value']['file']['name'], json['value']['file']['kind'], json['value']['file']['size']) end |
.fragment_parser(fragment) ⇒ Object
216 217 218 219 220 221 222 |
# File 'lib/prismic/json_parsers.rb', line 216 def fragment_parser(fragment) if fragment.is_a? Array multiple_parser(fragment) else parsers[fragment['type']].call(fragment) end end |
.geo_point_parser(json) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/prismic/json_parsers.rb', line 102 def geo_point_parser(json) Prismic::Fragments::GeoPoint.new( json['value']['longitude'], json['value']['latitude'] ) end |
.group_parser(json) ⇒ Object
199 200 201 202 203 204 205 206 |
# File 'lib/prismic/json_parsers.rb', line 199 def group_parser(json) fragment_list_array = [] json['value'].each do |group| fragments = Hash[ group.map {|name, fragment| [name, parsers[fragment['type']].call(fragment)] }] fragment_list_array << Prismic::Fragments::GroupDocument.new(fragments) end Prismic::Fragments::Group.new(fragment_list_array) end |
.image_link_parser(json) ⇒ Object
55 56 57 |
# File 'lib/prismic/json_parsers.rb', line 55 def image_link_parser(json) Prismic::Fragments::ImageLink.new(json['value']['image']['url']) end |
.image_parser(json) ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'lib/prismic/json_parsers.rb', line 109 def image_parser(json) main = view_parser(json['value']['main']) views = {} json['value']['views'].each do |name, view| views[name] = view_parser(view) end Prismic::Fragments::Image.new(main, views) end |
.multiple_parser(json) ⇒ Object
192 193 194 195 196 197 |
# File 'lib/prismic/json_parsers.rb', line 192 def multiple_parser(json) fragments = json.map do |fragment| parsers[fragment['type']].call(fragment) end Prismic::Fragments::Multiple.new(fragments) end |
.number_parser(json) ⇒ Object
87 88 89 |
# File 'lib/prismic/json_parsers.rb', line 87 def number_parser(json) Prismic::Fragments::Number.new(json['value']) end |
.parsers ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/prismic/json_parsers.rb', line 9 def parsers @parsers ||= { 'Link.document' => method(:document_link_parser), 'Text' => method(:text_parser), 'Link.web' => method(:web_link_parser), 'Link.image' => method(:image_link_parser), 'Link.file' => method(:file_link_parser), 'Date' => method(:date_parser), 'Timestamp' => method(:timestamp_parser), 'Number' => method(:number_parser), 'Embed' => method(:embed_parser), 'GeoPoint' => method(:geo_point_parser), 'Image' => method(:image_parser), 'Color' => method(:color_parser), 'StructuredText' => method(:structured_text_parser), 'Select' => method(:select_parser), 'Multiple' => method(:multiple_parser), 'Group' => method(:group_parser), 'SliceZone' => method(:slices_parser), 'Separator' => method(:separator_parser) } end |
.response_parser(documents) ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/prismic/json_parsers.rb', line 259 def response_parser(documents) raise FormSearchException, "Error : #{documents['error']}" if documents['error'] Prismic::Response.new( documents['page'], documents['results_per_page'], documents['results_size'], documents['total_results_size'], documents['total_pages'], documents['next_page'], documents['prev_page'], results_parser(documents['results']) ) end |
.results_parser(results) ⇒ Object
253 254 255 256 257 |
# File 'lib/prismic/json_parsers.rb', line 253 def results_parser(results) results.map do |doc| document_parser(doc) end end |
.select_parser(json) ⇒ Object
71 72 73 |
# File 'lib/prismic/json_parsers.rb', line 71 def select_parser(json) Prismic::Fragments::Text.new(json['value']) end |
.separator_parser(json) ⇒ Object
67 68 69 |
# File 'lib/prismic/json_parsers.rb', line 67 def separator_parser(json) Prismic::Fragments::Separator.new('') end |
.slices_parser(json) ⇒ Object
208 209 210 211 212 213 214 |
# File 'lib/prismic/json_parsers.rb', line 208 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'])) end Prismic::Fragments::SliceZone.new(slices) end |
.span_parser(span) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/prismic/json_parsers.rb', line 124 def self.span_parser(span) case span['type'] when 'em' Prismic::Fragments::StructuredText::Span::Em.new(span['start'], span['end']) when 'strong' Prismic::Fragments::StructuredText::Span::Strong.new(span['start'], span['end']) when 'hyperlink' Prismic::Fragments::StructuredText::Span::Hyperlink.new(span['start'], span['end'], link_parser(span['data'])) else label = span['data'] && span['data']['label'] Prismic::Fragments::StructuredText::Span::Label.new(span['start'], span['end'], label) end end |
.structured_text_parser(json) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/prismic/json_parsers.rb', line 123 def structured_text_parser(json) def self.span_parser(span) case span['type'] when 'em' Prismic::Fragments::StructuredText::Span::Em.new(span['start'], span['end']) when 'strong' Prismic::Fragments::StructuredText::Span::Strong.new(span['start'], span['end']) when 'hyperlink' Prismic::Fragments::StructuredText::Span::Hyperlink.new(span['start'], span['end'], link_parser(span['data'])) else label = span['data'] && span['data']['label'] Prismic::Fragments::StructuredText::Span::Label.new(span['start'], span['end'], label) end end blocks = json['value'].map do |block| case block['type'] when 'paragraph' spans = block['spans'].map {|span| span_parser(span) } Prismic::Fragments::StructuredText::Block::Paragraph.new(block['text'], spans, block['label']) when 'preformatted' spans = block['spans'].map {|span| span_parser(span) } Prismic::Fragments::StructuredText::Block::Preformatted.new(block['text'], spans, block['label']) when /^heading(\d+)$/ heading = $1 spans = block['spans'].map {|span| span_parser(span) } Prismic::Fragments::StructuredText::Block::Heading.new( block['text'], spans, heading.to_i, block['label'] ) when 'o-list-item' spans = block['spans'].map {|span| span_parser(span) } Prismic::Fragments::StructuredText::Block::ListItem.new( block['text'], spans, true, # ordered block['label'] ) when 'list-item' spans = block['spans'].map {|span| span_parser(span) } Prismic::Fragments::StructuredText::Block::ListItem.new( block['text'], spans, false, # unordered block['label'] ) when 'image' Prismic::Fragments::StructuredText::Block::Image.new( view_parser(block), block['label'] ) when 'embed' = block['oembed'] Prismic::Fragments::Embed.new( ['type'], ['provider_name'], ['provider_url'], ['html'], ) else puts "Unknown bloc type: #{block['type']}" end end Prismic::Fragments::StructuredText.new(blocks) end |
.text_parser(json) ⇒ Object
63 64 65 |
# File 'lib/prismic/json_parsers.rb', line 63 def text_parser(json) Prismic::Fragments::Text.new(json['value']) end |