prismic.io 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +1 -1
- data/Gemfile.lock +1 -1
- data/lib/prismic.rb +53 -108
- data/lib/prismic/api.rb +25 -13
- data/lib/prismic/experiments.rb +21 -3
- data/lib/prismic/fragments/group.rb +14 -61
- data/lib/prismic/fragments/structured_text.rb +15 -2
- data/lib/prismic/json_parsers.rb +1 -1
- data/lib/prismic/version.rb +1 -1
- data/lib/prismic/with_fragments.rb +140 -0
- data/spec/doc_spec.rb +16 -0
- data/spec/lesbonneschoses_spec.rb +25 -5
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fd49e594dff30e85b1dce2393707441e716c88f6
|
4
|
+
data.tar.gz: 18838f06eb596494d16fcac5bc461cbef642a12b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c2da8f9c018d2cb305d139d4a176a3d20efcb4024a40c0da9add1d8d1c60118840e283c368b2d880b6095e54aa1437c0a1e3fdf6b928aba6374bade151f1fff9
|
7
|
+
data.tar.gz: a5aa83e595426ab090d6dfc97409b3d1705f2e8eb0d9a0a2854a79e2da9bbf60eca38e22ae947213cf99dbfa1ed360474e01d1a468edeaedcd1631265b7ab732
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
data/lib/prismic.rb
CHANGED
@@ -5,6 +5,8 @@ require 'uri'
|
|
5
5
|
|
6
6
|
require 'json' unless defined?(JSON)
|
7
7
|
|
8
|
+
require 'prismic/with_fragments'
|
9
|
+
|
8
10
|
module Prismic
|
9
11
|
|
10
12
|
# These exception can contains an error cause and is able to show them
|
@@ -306,11 +308,13 @@ module Prismic
|
|
306
308
|
# @return [SearchForm] self
|
307
309
|
def set(field_name, value)
|
308
310
|
field = @form.fields[field_name]
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
311
|
+
unless value == nil
|
312
|
+
if field && field.repeatable?
|
313
|
+
data[field_name] = [] unless data.include? field_name
|
314
|
+
data[field_name] << value.to_s
|
315
|
+
else
|
316
|
+
data[field_name] = value.to_s
|
317
|
+
end
|
314
318
|
end
|
315
319
|
self
|
316
320
|
end
|
@@ -345,8 +349,25 @@ module Prismic
|
|
345
349
|
alias :repeatable? :repeatable
|
346
350
|
end
|
347
351
|
|
352
|
+
# Paginated response to a Prismic.io query. Note that you may not get all documents in the first page,
|
353
|
+
# and may need to retrieve more pages or increase the page size.
|
348
354
|
class Response
|
349
|
-
|
355
|
+
# @return [Number] current page, starting at 1
|
356
|
+
attr_accessor :page
|
357
|
+
# @return [Number]
|
358
|
+
attr_accessor :results_per_page
|
359
|
+
# @return [Number]
|
360
|
+
attr_accessor :results_size
|
361
|
+
# @return [Number]
|
362
|
+
attr_accessor :total_results_size
|
363
|
+
# @return [Number]
|
364
|
+
attr_accessor :total_pages
|
365
|
+
# @return [String] URL to the next page - nil if current page is the last page
|
366
|
+
attr_accessor :next_page
|
367
|
+
# @return [String] URL to the previous page - nil if current page is the first page
|
368
|
+
attr_accessor :prev_page
|
369
|
+
# @return [Array<Document>] Documents of the current page
|
370
|
+
attr_accessor :results
|
350
371
|
|
351
372
|
# To be able to use Kaminari as a paginator in Rails out of the box
|
352
373
|
alias :current_page :page
|
@@ -364,6 +385,7 @@ module Prismic
|
|
364
385
|
end
|
365
386
|
|
366
387
|
# Accessing the i-th document in the results
|
388
|
+
# @return [Document]
|
367
389
|
def [](i)
|
368
390
|
@results[i]
|
369
391
|
end
|
@@ -400,62 +422,32 @@ module Prismic
|
|
400
422
|
end
|
401
423
|
end
|
402
424
|
|
403
|
-
class Document
|
404
|
-
|
425
|
+
class Document < Prismic::WithFragments
|
426
|
+
# @return [String]
|
427
|
+
attr_accessor :id
|
428
|
+
# @return [String]
|
429
|
+
attr_accessor :type
|
430
|
+
# @return [String]
|
431
|
+
attr_accessor :href
|
432
|
+
# @return [Array<String>]
|
433
|
+
attr_accessor :tags
|
434
|
+
# @return [Array<String>]
|
435
|
+
attr_accessor :slugs
|
436
|
+
# @return [Array<LinkedDocument>]
|
437
|
+
attr_accessor :linked_documents
|
405
438
|
|
406
439
|
def initialize(id, type, href, tags, slugs, linked_documents, fragments)
|
440
|
+
super(fragments)
|
407
441
|
@id = id
|
408
442
|
@type = type
|
409
443
|
@href = href
|
410
444
|
@tags = tags
|
411
445
|
@slugs = slugs
|
412
446
|
@linked_documents = linked_documents
|
413
|
-
@fragments = (fragments.is_a? Hash) ? parse_fragments(fragments) : fragments
|
414
|
-
end
|
415
|
-
|
416
|
-
# Returns the document's slug
|
417
|
-
#
|
418
|
-
# @return [String]
|
419
|
-
def slug
|
420
|
-
slugs.empty? ? '-' : slugs.first
|
421
|
-
end
|
422
|
-
|
423
|
-
# Generate an HTML representation of the entire document
|
424
|
-
#
|
425
|
-
# @param link_resolver [LinkResolver] The LinkResolver used to build
|
426
|
-
# application's specific URL
|
427
|
-
#
|
428
|
-
# @return [String] the HTML representation
|
429
|
-
def as_html(link_resolver)
|
430
|
-
fragments.map { |field, fragment|
|
431
|
-
%(<section data-field="#{field}">#{fragment.as_html(link_resolver)}</section>)
|
432
|
-
}.join("\n")
|
433
|
-
end
|
434
|
-
|
435
|
-
# Finds the first highest title in a document (if any)
|
436
|
-
#
|
437
|
-
# @return [String]
|
438
|
-
def first_title
|
439
|
-
# It is impossible to reuse the StructuredText.first_title method, since
|
440
|
-
# we need to test the highest title across the whole document
|
441
|
-
title = false
|
442
|
-
max_level = 6 # any title with a higher level kicks the current one out
|
443
|
-
@fragments.each do |_, fragment|
|
444
|
-
if fragment.is_a? Prismic::Fragments::StructuredText
|
445
|
-
fragment.blocks.each do |block|
|
446
|
-
if block.is_a?(Prismic::Fragments::StructuredText::Block::Heading)
|
447
|
-
if block.level < max_level
|
448
|
-
title = block.text
|
449
|
-
max_level = block.level # new maximum
|
450
|
-
end
|
451
|
-
end
|
452
|
-
end
|
453
|
-
end
|
454
|
-
end
|
455
|
-
title
|
456
447
|
end
|
457
448
|
|
458
449
|
# Get a document's field
|
450
|
+
# @return [Fragments::Fragment]
|
459
451
|
def [](field)
|
460
452
|
array = field.split('.')
|
461
453
|
if array.length != 2
|
@@ -466,65 +458,13 @@ module Prismic
|
|
466
458
|
end
|
467
459
|
alias :get :[]
|
468
460
|
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
def get_number(field)
|
476
|
-
fragment = self[field]
|
477
|
-
return nil unless fragment.is_a? Prismic::Fragments::Number
|
478
|
-
fragment
|
479
|
-
end
|
480
|
-
|
481
|
-
def get_date(field)
|
482
|
-
fragment = self[field]
|
483
|
-
return nil unless fragment.is_a? Prismic::Fragments::Date
|
484
|
-
fragment
|
485
|
-
end
|
486
|
-
|
487
|
-
def get_timestamp(field)
|
488
|
-
fragment = self[field]
|
489
|
-
return nil unless fragment.is_a? Prismic::Fragments::Timestamp
|
490
|
-
fragment
|
491
|
-
end
|
492
|
-
|
493
|
-
def get_group(field)
|
494
|
-
fragment = self[field]
|
495
|
-
return nil unless fragment.is_a? Prismic::Fragments::Group
|
496
|
-
fragment
|
497
|
-
end
|
498
|
-
|
499
|
-
def get_link(field)
|
500
|
-
fragment = self[field]
|
501
|
-
return nil unless fragment.is_a? Prismic::Fragments::Link
|
502
|
-
fragment
|
503
|
-
end
|
504
|
-
|
505
|
-
def get_embed(field)
|
506
|
-
fragment = self[field]
|
507
|
-
return nil unless fragment.is_a? Prismic::Fragments::Embed
|
508
|
-
fragment
|
509
|
-
end
|
510
|
-
|
511
|
-
def get_color(field)
|
512
|
-
fragment = self[field]
|
513
|
-
return nil unless fragment.is_a? Prismic::Fragments::Color
|
514
|
-
fragment
|
515
|
-
end
|
516
|
-
|
517
|
-
def get_geopoint(field)
|
518
|
-
fragment = self[field]
|
519
|
-
return nil unless fragment.is_a? Prismic::Fragments::GeoPoint
|
520
|
-
fragment
|
461
|
+
# Returns the document's slug
|
462
|
+
#
|
463
|
+
# @return [String]
|
464
|
+
def slug
|
465
|
+
slugs.empty? ? '-' : slugs.first
|
521
466
|
end
|
522
467
|
|
523
|
-
private
|
524
|
-
|
525
|
-
def parse_fragments(fragments)
|
526
|
-
fragments
|
527
|
-
end
|
528
468
|
end
|
529
469
|
|
530
470
|
|
@@ -573,6 +513,8 @@ module Prismic
|
|
573
513
|
|
574
514
|
# The LinkResolver will help to build URL specific to an application, based
|
575
515
|
# on a generic prismic.io's {Fragments::DocumentLink Document link}.
|
516
|
+
#
|
517
|
+
# The {Prismic.link_resolver} function is the recommended way to create a LinkResolver.
|
576
518
|
class LinkResolver
|
577
519
|
attr_reader :ref
|
578
520
|
|
@@ -592,6 +534,9 @@ module Prismic
|
|
592
534
|
end
|
593
535
|
end
|
594
536
|
|
537
|
+
# A class to override the default was to serialize HTML. Only needed if you want to override the default HTML serialization.
|
538
|
+
#
|
539
|
+
# The {Prismic.html_serializer} function is the recommended way to create an HtmlSerializer.
|
595
540
|
class HtmlSerializer
|
596
541
|
def initialize(&blk)
|
597
542
|
@blk = blk
|
data/lib/prismic/api.rb
CHANGED
@@ -6,8 +6,19 @@ module Prismic
|
|
6
6
|
@@warned_create_search_form = false
|
7
7
|
@@warned_oauth_initiate_url = false
|
8
8
|
@@warned_oauth_check_token = false
|
9
|
-
attr_reader :json
|
10
|
-
|
9
|
+
attr_reader :json
|
10
|
+
# @return [String]
|
11
|
+
attr_reader :access_token
|
12
|
+
attr_reader :http_client
|
13
|
+
# @return [Hash{String => Ref}] list of references, as label -> reference
|
14
|
+
attr_accessor :refs
|
15
|
+
# @return [Hash{String => String}] list of bookmarks, as name -> documentId
|
16
|
+
attr_accessor :bookmarks
|
17
|
+
# @return [Hash{String => SearchForm}] list of bookmarks, as name -> documentId
|
18
|
+
attr_accessor :forms
|
19
|
+
attr_accessor :tags, :types, :oauth, :cache
|
20
|
+
# @return [Experiments] list of all experiments from Prismic
|
21
|
+
attr_accessor :experiments
|
11
22
|
|
12
23
|
# Is the cache enabled on this API object?
|
13
24
|
#
|
@@ -47,9 +58,9 @@ module Prismic
|
|
47
58
|
|
48
59
|
# Get a bookmark by its name
|
49
60
|
# @api
|
50
|
-
# @param
|
61
|
+
# @param name [String] The bookmark's name
|
51
62
|
#
|
52
|
-
# @return [
|
63
|
+
# @return [String] The bookmark document id
|
53
64
|
def bookmark(name)
|
54
65
|
bookmarks[name]
|
55
66
|
end
|
@@ -63,7 +74,7 @@ module Prismic
|
|
63
74
|
refs[name.downcase]
|
64
75
|
end
|
65
76
|
|
66
|
-
# Returns a {Prismic::SearchForm search form} by its name
|
77
|
+
# Returns a {Prismic::SearchForm search form} by its name. This is where you start to query a repository.
|
67
78
|
# @api
|
68
79
|
# @param name [String] The name of the form
|
69
80
|
# @param data [Hash] Default values
|
@@ -77,7 +88,7 @@ module Prismic
|
|
77
88
|
|
78
89
|
# @deprecated Use {#form} instead.
|
79
90
|
def create_search_form(name, data={}, ref={})
|
80
|
-
|
91
|
+
unless @@warned_create_search_form
|
81
92
|
warn "[DEPRECATION] `create_search_form` is deprecated. Please use `form` instead."
|
82
93
|
@@warned_create_search_form = true
|
83
94
|
end
|
@@ -88,10 +99,11 @@ module Prismic
|
|
88
99
|
@json
|
89
100
|
end
|
90
101
|
|
102
|
+
# Fetch the API information from the Prismic.io server
|
91
103
|
def self.get(url, access_token=nil, http_client=Prismic::DefaultHTTPClient, api_cache=Prismic::DefaultApiCache)
|
92
104
|
data = {}
|
93
|
-
data[
|
94
|
-
cache_key = url + (access_token ? (
|
105
|
+
data['access_token'] = access_token if access_token
|
106
|
+
cache_key = url + (access_token ? ('#' + access_token) : '')
|
95
107
|
api_cache.get_or_set(cache_key, nil, 5) {
|
96
108
|
res = http_client.get(url, data, 'Accept' => 'application/json')
|
97
109
|
case res.code
|
@@ -221,11 +233,11 @@ module Prismic
|
|
221
233
|
@token = token
|
222
234
|
end
|
223
235
|
def initiate_url(opts)
|
224
|
-
initiate +
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
}.map{|kv| kv.map{|e| CGI.escape(e) }.join(
|
236
|
+
initiate + '?' + {
|
237
|
+
'client_id' => opts.fetch(:client_id),
|
238
|
+
'redirect_uri' => opts.fetch(:redirect_uri),
|
239
|
+
'scope' => opts.fetch(:scope),
|
240
|
+
}.map{|kv| kv.map{|e| CGI.escape(e) }.join('=') }.join('&')
|
229
241
|
end
|
230
242
|
def check_token(params)
|
231
243
|
res = http_client.post(token, params)
|
data/lib/prismic/experiments.rb
CHANGED
@@ -2,7 +2,10 @@
|
|
2
2
|
module Prismic
|
3
3
|
|
4
4
|
class Experiments
|
5
|
-
|
5
|
+
# @return [Array<Experiment>] list of all experiments from Prismic
|
6
|
+
attr_reader :draft
|
7
|
+
# @return [Array<Experiments>] list of all experiments from Prismic
|
8
|
+
attr_reader :running
|
6
9
|
|
7
10
|
def initialize(draft, running)
|
8
11
|
@draft = draft
|
@@ -31,6 +34,7 @@ module Prismic
|
|
31
34
|
nil
|
32
35
|
end
|
33
36
|
|
37
|
+
# @return [Experiments]
|
34
38
|
def self.parse(data)
|
35
39
|
draft = []
|
36
40
|
running = []
|
@@ -48,7 +52,14 @@ module Prismic
|
|
48
52
|
end
|
49
53
|
|
50
54
|
class Experiment
|
51
|
-
|
55
|
+
# @return [String]
|
56
|
+
attr_reader :id
|
57
|
+
# @return [String]
|
58
|
+
attr_reader :google_id
|
59
|
+
# @return [String]
|
60
|
+
attr_reader :name
|
61
|
+
# @return [Array<Variation>]
|
62
|
+
attr_reader :variations
|
52
63
|
|
53
64
|
def initialize(id, google_id, name, variations)
|
54
65
|
@id = id
|
@@ -57,6 +68,7 @@ module Prismic
|
|
57
68
|
@variations = variations
|
58
69
|
end
|
59
70
|
|
71
|
+
# @return [Experiment]
|
60
72
|
def self.parse(data)
|
61
73
|
new(data['id'], data['googleId'], data['name'], data['variations'].map { |variation|
|
62
74
|
Variation.parse(variation)
|
@@ -65,7 +77,12 @@ module Prismic
|
|
65
77
|
end
|
66
78
|
|
67
79
|
class Variation
|
68
|
-
|
80
|
+
# @return [String]
|
81
|
+
attr_reader :id
|
82
|
+
# @return [String]
|
83
|
+
attr_reader :ref
|
84
|
+
# @return [String]
|
85
|
+
attr_reader :label
|
69
86
|
|
70
87
|
def initialize(id, ref, label)
|
71
88
|
@id = id
|
@@ -73,6 +90,7 @@ module Prismic
|
|
73
90
|
@label = label
|
74
91
|
end
|
75
92
|
|
93
|
+
# @return [Variation]
|
76
94
|
def self.parse(data)
|
77
95
|
new(data['id'], data['ref'], data['label'])
|
78
96
|
end
|
@@ -10,27 +10,28 @@ module Prismic
|
|
10
10
|
# the first image will look like this: `group[0]['image']`.
|
11
11
|
class Group < Fragment
|
12
12
|
|
13
|
-
# The array of
|
14
|
-
attr_accessor :
|
13
|
+
# The array of group documents
|
14
|
+
attr_accessor :group_documents
|
15
15
|
|
16
|
-
def initialize(
|
17
|
-
@
|
16
|
+
def initialize(group_documents)
|
17
|
+
@group_documents = group_documents
|
18
18
|
end
|
19
19
|
|
20
|
-
#
|
21
|
-
|
22
|
-
|
20
|
+
# Get the group document corresponding to index
|
21
|
+
# @return [Prismic::WithFragments]
|
22
|
+
def [](index)
|
23
|
+
@group_documents[index]
|
23
24
|
end
|
24
|
-
alias :get :[]
|
25
25
|
|
26
|
-
|
26
|
+
alias :get :[]
|
27
|
+
# @yieldparam group_doc [WithFragment]
|
27
28
|
def each(&blk)
|
28
|
-
@
|
29
|
+
@group_documents.each(&blk)
|
29
30
|
end
|
30
31
|
include Enumerable # adds map, select, etc
|
31
32
|
|
32
33
|
def length
|
33
|
-
@
|
34
|
+
@group_documents.length
|
34
35
|
end
|
35
36
|
alias :size :length
|
36
37
|
|
@@ -41,64 +42,16 @@ module Prismic
|
|
41
42
|
#
|
42
43
|
# @return [String] the HTML representation
|
43
44
|
def as_html(link_resolver = nil)
|
44
|
-
@
|
45
|
+
@group_documents.map { |doc| doc.as_html(link_resolver) }.join("\n")
|
45
46
|
end
|
46
47
|
|
47
48
|
# Generate an text representation of the group
|
48
49
|
#
|
49
50
|
# @return [String] the text representation
|
50
51
|
def as_text
|
51
|
-
@
|
52
|
+
@group_documents.map { |doc| doc.as_text }.join("\n")
|
52
53
|
end
|
53
54
|
|
54
|
-
|
55
|
-
class FragmentMapping
|
56
|
-
|
57
|
-
# a hash containing all the fragments in the fragment list
|
58
|
-
attr_accessor :fragments
|
59
|
-
|
60
|
-
def initialize(fragments)
|
61
|
-
@fragments = fragments
|
62
|
-
end
|
63
|
-
|
64
|
-
# Accessing the right fragment of the fragment list: `fl['caption']`
|
65
|
-
def [](name)
|
66
|
-
@fragments[name]
|
67
|
-
end
|
68
|
-
alias :get :[]
|
69
|
-
|
70
|
-
# @yieldparam name [String]
|
71
|
-
# @yieldparam fragment [Fragment]
|
72
|
-
def each(&blk)
|
73
|
-
@fragments.each(&blk)
|
74
|
-
end
|
75
|
-
include Enumerable # adds map, select, etc
|
76
|
-
|
77
|
-
# @return [Fixum]
|
78
|
-
def length
|
79
|
-
@fragments.length
|
80
|
-
end
|
81
|
-
alias :size :length
|
82
|
-
|
83
|
-
# Generate an HTML representation of the fragments
|
84
|
-
#
|
85
|
-
# @param link_resolver [LinkResolver] The LinkResolver used to build
|
86
|
-
# application's specific URL
|
87
|
-
#
|
88
|
-
# @return [String] the HTML representation
|
89
|
-
def as_html(link_resolver = nil)
|
90
|
-
@fragments.map { |name, fragment|
|
91
|
-
%(<section data-field="#{name}">#{fragment.as_html(link_resolver)}</section>)
|
92
|
-
}.join("\n")
|
93
|
-
end
|
94
|
-
|
95
|
-
# Generate a text representation of the fragment
|
96
|
-
#
|
97
|
-
# @return [String] the text representation
|
98
|
-
def as_text
|
99
|
-
@fragments.values.map { |fragment| fragment.as_text }.join("\n")
|
100
|
-
end
|
101
|
-
end
|
102
55
|
end
|
103
56
|
end
|
104
57
|
end
|
@@ -31,6 +31,10 @@ module Prismic
|
|
31
31
|
# it is not advised to override this method if you want to change the HTML output, you should
|
32
32
|
# override the as_html method at the block level (like {Heading.as_html}, or {Preformatted.as_html},
|
33
33
|
# for instance).
|
34
|
+
# @param link_resolver [LinkResolver]
|
35
|
+
# @param html_serializer [HtmlSerializer]
|
36
|
+
# @return [String] the resulting html snippet
|
37
|
+
|
34
38
|
def as_html(link_resolver, html_serializer=nil)
|
35
39
|
# Defining blocks that deserve grouping, assigning them "group kind" names
|
36
40
|
block_group = ->(block){
|
@@ -91,7 +95,10 @@ module Prismic
|
|
91
95
|
end
|
92
96
|
|
93
97
|
class Span
|
94
|
-
|
98
|
+
# @return [Number]
|
99
|
+
attr_accessor :start
|
100
|
+
# @return [Number]
|
101
|
+
attr_accessor :end
|
95
102
|
|
96
103
|
def initialize(start, finish)
|
97
104
|
@start = start
|
@@ -99,6 +106,7 @@ module Prismic
|
|
99
106
|
end
|
100
107
|
|
101
108
|
class Label < Span
|
109
|
+
# @return [String]
|
102
110
|
attr_accessor :label
|
103
111
|
def initialize(start, finish, label)
|
104
112
|
super(start, finish)
|
@@ -149,7 +157,12 @@ module Prismic
|
|
149
157
|
end
|
150
158
|
|
151
159
|
class Text
|
152
|
-
|
160
|
+
# @return [String]
|
161
|
+
attr_accessor :text
|
162
|
+
# @return [Array<Span>]
|
163
|
+
attr_accessor :spans
|
164
|
+
# @return [String] may be nil
|
165
|
+
attr_accessor :label
|
153
166
|
|
154
167
|
def initialize(text, spans, label = nil)
|
155
168
|
@text = text
|
data/lib/prismic/json_parsers.rb
CHANGED
@@ -192,7 +192,7 @@ module Prismic
|
|
192
192
|
fragment_list_array = []
|
193
193
|
json['value'].each do |group|
|
194
194
|
fragments = Hash[ group.map {|name, fragment| [name, parsers[fragment['type']].call(fragment)] }]
|
195
|
-
fragment_list_array << Prismic::
|
195
|
+
fragment_list_array << Prismic::WithFragments.new(fragments)
|
196
196
|
end
|
197
197
|
Prismic::Fragments::Group.new(fragment_list_array)
|
198
198
|
end
|
data/lib/prismic/version.rb
CHANGED
@@ -0,0 +1,140 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Prismic
|
3
|
+
|
4
|
+
# A document with Fragments: usually a Prismic.io Document, or a Document within a Group
|
5
|
+
class WithFragments
|
6
|
+
# @return [Hash{String => Fragment}]
|
7
|
+
attr_accessor :fragments
|
8
|
+
|
9
|
+
def initialize(fragments)
|
10
|
+
@fragments = fragments
|
11
|
+
end
|
12
|
+
|
13
|
+
# Generate an HTML representation of the entire document
|
14
|
+
#
|
15
|
+
# @param link_resolver [LinkResolver] The LinkResolver used to build
|
16
|
+
# application's specific URL
|
17
|
+
#
|
18
|
+
# @return [String] the HTML representation
|
19
|
+
def as_html(link_resolver)
|
20
|
+
fragments.map { |field, fragment|
|
21
|
+
%(<section data-field="#{field}">#{fragment.as_html(link_resolver)}</section>)
|
22
|
+
}.join("\n")
|
23
|
+
end
|
24
|
+
|
25
|
+
# Finds the first highest title in a document (if any)
|
26
|
+
#
|
27
|
+
# @return [String]
|
28
|
+
def first_title
|
29
|
+
# It is impossible to reuse the StructuredText.first_title method, since
|
30
|
+
# we need to test the highest title across the whole document
|
31
|
+
title = false
|
32
|
+
max_level = 6 # any title with a higher level kicks the current one out
|
33
|
+
@fragments.each do |_, fragment|
|
34
|
+
if fragment.is_a? Prismic::Fragments::StructuredText
|
35
|
+
fragment.blocks.each do |block|
|
36
|
+
if block.is_a?(Prismic::Fragments::StructuredText::Block::Heading)
|
37
|
+
if block.level < max_level
|
38
|
+
title = block.text
|
39
|
+
max_level = block.level # new maximum
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
title
|
46
|
+
end
|
47
|
+
|
48
|
+
# Get a document's field
|
49
|
+
# @return [Fragments::Fragment]
|
50
|
+
def [](field)
|
51
|
+
@fragments[field]
|
52
|
+
end
|
53
|
+
alias :get :[]
|
54
|
+
|
55
|
+
# @yieldparam name [String]
|
56
|
+
# @yieldparam fragment [Fragment]
|
57
|
+
def each(&blk)
|
58
|
+
@fragments.each(&blk)
|
59
|
+
end
|
60
|
+
include Enumerable # adds map, select, etc
|
61
|
+
|
62
|
+
# @return [Fixum]
|
63
|
+
def length
|
64
|
+
@fragments.length
|
65
|
+
end
|
66
|
+
alias :size :length
|
67
|
+
|
68
|
+
# @return [Fragments::Text]
|
69
|
+
def get_text(field)
|
70
|
+
fragment = self[field]
|
71
|
+
return nil unless fragment.is_a? Prismic::Fragments::Text
|
72
|
+
fragment
|
73
|
+
end
|
74
|
+
|
75
|
+
# @return [Fragments::Number]
|
76
|
+
def get_number(field)
|
77
|
+
fragment = self[field]
|
78
|
+
return nil unless fragment.is_a? Prismic::Fragments::Number
|
79
|
+
fragment
|
80
|
+
end
|
81
|
+
|
82
|
+
# @return [Fragments::Image]
|
83
|
+
def get_image(field)
|
84
|
+
fragment = self[field]
|
85
|
+
return nil unless fragment.is_a? Prismic::Fragments::Image
|
86
|
+
fragment
|
87
|
+
end
|
88
|
+
|
89
|
+
# @return [Fragments::Date]
|
90
|
+
def get_date(field)
|
91
|
+
fragment = self[field]
|
92
|
+
return nil unless fragment.is_a? Prismic::Fragments::Date
|
93
|
+
fragment
|
94
|
+
end
|
95
|
+
|
96
|
+
# @return [Fragments::Timestamp]
|
97
|
+
def get_timestamp(field)
|
98
|
+
fragment = self[field]
|
99
|
+
return nil unless fragment.is_a? Prismic::Fragments::Timestamp
|
100
|
+
fragment
|
101
|
+
end
|
102
|
+
|
103
|
+
# @return [Fragments::Group]
|
104
|
+
def get_group(field)
|
105
|
+
fragment = self[field]
|
106
|
+
return nil unless fragment.is_a? Prismic::Fragments::Group
|
107
|
+
fragment
|
108
|
+
end
|
109
|
+
|
110
|
+
# @return [Fragments::Link]
|
111
|
+
def get_link(field)
|
112
|
+
fragment = self[field]
|
113
|
+
return nil unless fragment.is_a? Prismic::Fragments::Link
|
114
|
+
fragment
|
115
|
+
end
|
116
|
+
|
117
|
+
# @return [Fragments::Embed]
|
118
|
+
def get_embed(field)
|
119
|
+
fragment = self[field]
|
120
|
+
return nil unless fragment.is_a? Prismic::Fragments::Embed
|
121
|
+
fragment
|
122
|
+
end
|
123
|
+
|
124
|
+
# @return [Fragments::Color]
|
125
|
+
def get_color(field)
|
126
|
+
fragment = self[field]
|
127
|
+
return nil unless fragment.is_a? Prismic::Fragments::Color
|
128
|
+
fragment
|
129
|
+
end
|
130
|
+
|
131
|
+
# @return [Fragments::GeoPoint]
|
132
|
+
def get_geopoint(field)
|
133
|
+
fragment = self[field]
|
134
|
+
return nil unless fragment.is_a? Prismic::Fragments::GeoPoint
|
135
|
+
fragment
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
data/spec/doc_spec.rb
CHANGED
@@ -140,6 +140,22 @@ describe 'Documentation' do
|
|
140
140
|
price.should == 2.5
|
141
141
|
end
|
142
142
|
|
143
|
+
it 'images' do
|
144
|
+
api = Prismic::api('https://lesbonneschoses.prismic.io/api')
|
145
|
+
response = api.form('everything')
|
146
|
+
.query(Predicates::at('document.id', 'UlfoxUnM0wkXYXbO'))
|
147
|
+
.ref(api.master_ref)
|
148
|
+
.submit
|
149
|
+
doc = response[0]
|
150
|
+
# startgist:825fa25b66355ce758fe:prismic-images.rb
|
151
|
+
# Accessing image fields
|
152
|
+
image = doc.get_image('product.image')
|
153
|
+
# Most of the time you will be using the "main" view
|
154
|
+
url = image.main.url
|
155
|
+
# endgist
|
156
|
+
url.should == 'https://prismic-io.s3.amazonaws.com/lesbonneschoses/f606ad513fcc2a73b909817119b84d6fd0d61a6d.png'
|
157
|
+
end
|
158
|
+
|
143
159
|
it 'date and timestamp' do
|
144
160
|
api = Prismic::api('https://lesbonneschoses.prismic.io/api')
|
145
161
|
response = api.form('everything')
|
@@ -3,20 +3,20 @@ require 'spec_helper'
|
|
3
3
|
|
4
4
|
describe 'LesBonnesChoses' do
|
5
5
|
before do
|
6
|
-
@api = Prismic.api(
|
6
|
+
@api = Prismic.api('https://lesbonneschoses.prismic.io/api', nil)
|
7
7
|
@master_ref = @api.master_ref
|
8
8
|
end
|
9
9
|
|
10
10
|
describe '/api' do
|
11
|
-
it
|
11
|
+
it 'API works' do
|
12
12
|
@api.should_not be_nil
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
16
|
describe 'query' do
|
17
|
-
it
|
18
|
-
@api.form(
|
19
|
-
@api.form(
|
17
|
+
it 'queries everything and returns 20 documents' do
|
18
|
+
@api.form('everything').submit(@master_ref).size.should == 20
|
19
|
+
@api.form('everything').submit(@master_ref).results.size.should == 20
|
20
20
|
end
|
21
21
|
|
22
22
|
it "queries macarons (using a predicate) and returns 7 documents" do
|
@@ -54,6 +54,16 @@ describe 'LesBonnesChoses' do
|
|
54
54
|
documents.next_page.should == "https://lesbonneschoses.prismic.io/api/documents/search?ref=UlfoxUnM08QWYXdl&page=2&pageSize=20"
|
55
55
|
documents.prev_page.should == nil
|
56
56
|
end
|
57
|
+
it "works when passing nil" do
|
58
|
+
documents = @api.form("everything").page(nil).submit(@master_ref)
|
59
|
+
documents.page.should == 1
|
60
|
+
documents.results_per_page.should == 20
|
61
|
+
documents.results_size.should == 20
|
62
|
+
documents.total_results_size.should == 40
|
63
|
+
documents.total_pages.should == 2
|
64
|
+
documents.next_page.should == "https://lesbonneschoses.prismic.io/api/documents/search?ref=UlfoxUnM08QWYXdl&page=2&pageSize=20"
|
65
|
+
documents.prev_page.should == nil
|
66
|
+
end
|
57
67
|
it "works on page 2" do
|
58
68
|
documents = @api.form("everything").page("2").submit(@master_ref)
|
59
69
|
documents.page.should == 2
|
@@ -74,6 +84,16 @@ describe 'LesBonnesChoses' do
|
|
74
84
|
documents.next_page.should == "https://lesbonneschoses.prismic.io/api/documents/search?ref=UlfoxUnM08QWYXdl&page=3&pageSize=10"
|
75
85
|
documents.prev_page.should == "https://lesbonneschoses.prismic.io/api/documents/search?ref=UlfoxUnM08QWYXdl&page=1&pageSize=10"
|
76
86
|
end
|
87
|
+
it "works when passing nil" do
|
88
|
+
documents = @api.form("everything").page(nil).submit(@master_ref)
|
89
|
+
documents.page.should == 1
|
90
|
+
documents.results_per_page.should == 20
|
91
|
+
documents.results_size.should == 20
|
92
|
+
documents.total_results_size.should == 40
|
93
|
+
documents.total_pages.should == 2
|
94
|
+
documents.next_page.should == "https://lesbonneschoses.prismic.io/api/documents/search?ref=UlfoxUnM08QWYXdl&page=2&pageSize=20"
|
95
|
+
documents.prev_page.should == nil
|
96
|
+
end
|
77
97
|
end
|
78
98
|
|
79
99
|
describe 'API::Document' do
|
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.0.
|
4
|
+
version: 1.0.5
|
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: 2014-10-
|
14
|
+
date: 2014-10-31 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: bundler
|
@@ -109,6 +109,7 @@ files:
|
|
109
109
|
- lib/prismic/json_parsers.rb
|
110
110
|
- lib/prismic/predicates.rb
|
111
111
|
- lib/prismic/version.rb
|
112
|
+
- lib/prismic/with_fragments.rb
|
112
113
|
- package.json
|
113
114
|
- prismic.gemspec
|
114
115
|
- spec/cache_spec.rb
|