Skip to content

Instantly share code, notes, and snippets.

View prismicdeveloper's full-sized avatar

prismicdeveloper

View GitHub Profile
group = document.get_group('article.documents')
docs = group ? group : []
docs.each do |doc|
# Desc and Link are Fragments, their type depending on what's declared in the Document Mask
desc = doc['desc']
link = doc['linktodoc']
end
video = document.get_embed('article.video')
# Html is the code to include to embed the object, and depends on the embedded service
html = video ? video.as_html : ''
# Date and Timestamp predicates
dateBefore = Predicates::date_before('my.product.releaseDate', Time.new(2014, 6, 1))
dateAfter = Predicates::date_after('my.product.releaseDate', Time.new(2014, 1, 1))
dateBetween = Predicates::date_between('my.product.releaseDate', Time.new(2014, 1, 1), Time.new(2014, 6, 1))
dayOfMonth = Predicates::day_of_month('my.product.releaseDate', 14)
dayOfMonthAfter = Predicates::day_of_month_after('my.product.releaseDate', 14)
dayOfMonthBefore = Predicates::day_of_month_before('my.product.releaseDate', 14)
dayOfWeek = Predicates::day_of_week('my.product.releaseDate', 'Tuesday')
dayOfWeekAfter = Predicates::day_of_week_after('my.product.releaseDate', 'Wednesday')
dayOfWeekBefore = Predicates::day_of_week_before('my.product.releaseDate', 'Wednesday')
# Number predicates
gt = Predicates::gt('my.product.price', 10)
lt = Predicates::lt('my.product.price', 20)
in_range = Predicates::in_range('my.product.price', 10, 20)
# Accessing number fields
price = doc.get_number('product.price').value
# 'at' predicate: equality of a fragment to a value.
at = Predicates::at('document.type', 'article')
# 'any' predicate: equality of a fragment to a value.
any = Predicates::any('document.type', ['article', 'blog-post'])
# 'fulltext' predicate: fulltext search in a fragment.
fulltext = Predicates::fulltext('my.article.body', 'sausage')
# 'similar' predicate, with a document id as reference
similar = Predicates::similar('UXasdFwe42D', 10)
author = doc.get_text('blog-post.author')
if author == nil
name = 'Anonymous'
else
name = author.value
end
api = Prismic.api('https://lesbonneschoses.cdn.prismic.io/api')
response = api
.form('everything')
.query(
Prismic::Predicates::at('document.type', 'product'),
Prismic::Predicates::date_after('my.blog-post.date', 1401580800000))
.submit(api.master_ref)
api = Prismic.api('https://lesbonneschoses.cdn.prismic.io/api')
response = api.form('everything')
.ref(api.master_ref)
.query(Prismic::Predicates::at('document.type', 'product'))
.page_size(100)
.orderings('[my.product.price desc]')
.submit
# The products are now ordered by price, highest first
results = response.results
# This will fail because the token is invalid, but this is how to access a private API
api = Prismic.api('https://lesbonneschoses.cdn.prismic.io/api', 'MC5-XXXXXXX-vRfvv70')