Module: PrismicHelper

Defined in:
app/helpers/prismic_helper.rb

Instance Method Summary collapse

Instance Method Details

#prismic_apiObject

Access and initialization of the Prismic::API object.



23
24
25
26
27
28
29
30
31
32
# File 'app/helpers/prismic_helper.rb', line 23

def prismic_api
  prismic_url = ENV['PRISMIC_API_URL']
  @api ||= Prismic.api(prismic_url, ENV['PRISMIC_ACCESS_TOKEN'])
rescue Prismic::API::PrismicWSConnectionError,
       Prismic::API::BadPrismicResponseError,
       Prismic::API::PrismicWSAuthError,
       Net::OpenTimeout => e
  Rails.logger.error e
  @api = nil
end

#prismic_refObject

Setting @ref as the actual ref id being queried, even if it’s the master ref. To be used to call the API, for instance: api.form(‘everything’).submit(ref) If we fail to initialize the Prismic API we return the last successful ref stored in Rails cache



10
11
12
13
14
15
16
17
18
# File 'app/helpers/prismic_helper.rb', line 10

def prismic_ref
  if api.nil?
    @ref = Rails.cache.fetch('prismic_ref')
  else
    master_ref = api.master_ref.ref
    Rails.cache.write('prismic_ref', master_ref)
    @ref ||= preview_ref || experiment_ref || master_ref
  end
end