Sha256: 851484ba95c8eb4b6a398ca8135feaa8379c2618806e971224e2d3edaee28aab

Contents?: true

Size: 1.74 KB

Versions: 6

Compression:

Stored size: 1.74 KB

Contents

require 'thor/core_ext/hash_with_indifferent_access'
require 'contentful_middleman/tools/preview_proxy'

module ContentfulMiddleman
  module Helpers
    def contentful_instances
      ContentfulMiddleman.instances
    end

    def localize_entry(entry, locale, fallback_locale='en-US')
      localized_entry = ::Thor::CoreExt::HashWithIndifferentAccess.new
      entry.each do |field, value|
        localized_entry[field] = localize(entry, field, locale, fallback_locale)
      end
      localized_entry
    end

    def localize(entry, field, locale, fallback_locale='en-US')
      value = entry.fetch(field)

      return localize_array(value, locale, fallback_locale) if value.is_a? ::Array
      localize_value(value, locale, fallback_locale)
    end

    def localize_array(value, locale, fallback_locale='en-US')
      value.map do |v|
        localize_value(v, locale, fallback_locale)
      end
    end

    def localize_value(value, locale, fallback_locale='en-US')
      value = value.fetch(locale) if value.respond_to?(:fetch) && value.respond_to?(:key?) && value.key?(locale)
      value = value.fetch(fallback_locale) if value.respond_to?(:fetch) && value.respond_to?(:key?) && value.key?(fallback_locale)

      return localize_array(value, locale, fallback_locale) if value.is_a? ::Array
      return localize_entry(value, locale, fallback_locale) if value.is_a? ::Hash
      value
    end

    def with_preview(space: '', access_token: '', tries: 3, expires_in: ContentfulMiddleman::Tools::PreviewProxy.hours(2), &block)
      preview_client = ContentfulMiddleman::Tools::PreviewProxy.instance(
        space: space,
        access_token: access_token,
        tries: tries,
        expires_in: expires_in
      )

      block.call(preview_client)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
contentful_middleman-3.0.1 lib/contentful_middleman/helpers.rb
contentful_middleman-4.0.1 lib/contentful_middleman/helpers.rb
contentful_middleman-4.0.0 lib/contentful_middleman/helpers.rb
contentful_middleman-3.0.0 lib/contentful_middleman/helpers.rb
contentful_middleman-2.1.3 lib/contentful_middleman/helpers.rb
contentful_middleman-2.1.2 lib/contentful_middleman/helpers.rb