Sha256: 96dc8fc21afce798344e7650bf2cbcc2e822b3f80d86f6dd0f5ced4822eaefb6

Contents?: true

Size: 1.7 KB

Versions: 40

Compression:

Stored size: 1.7 KB

Contents

require_relative "template_support"

module Onebox
  class Layout < Mustache
    include TemplateSupport

    VERSION = "1.0.0"

    attr_reader :cache
    attr_reader :record
    attr_reader :view

    def initialize(name, record, cache)
      @cache = cache
      @record = Onebox::Helpers.symbolize_keys(record)

      # Fix any relative paths
      if @record[:image] && @record[:image] =~ /^\/[^\/]/
        @record[:image] = "#{uri.scheme}://#{uri.host}/#{@record[:image]}"
      end

      @md5 = Digest::MD5.new
      @view = View.new(name, @record)
      @template_name = "_layout"
      @template_path = load_paths.last
    end

    def to_html
      result = cache.fetch(checksum) { render(details) }
      cache[checksum] = result if cache.respond_to?(:key?)
      result
    end

    private

      def uri
        @uri = URI(link)
      end

      def checksum
        @md5.hexdigest("#{VERSION}:#{link}")
      end

      def link
        ::Onebox::Helpers.normalize_url_for_output(record[:link])
      end

      def domain
        record[:domain] || URI(link || '').host.to_s.sub(/^www\./, '')
      end

      def details
        {
          link: record[:link],
          title: record[:title],
          favicon: record[:favicon],
          domain: domain,
          article_published_time: record[:article_published_time],
          article_published_time_title: record[:article_published_time_title],
          metadata_1_label: record[:metadata_1_label],
          metadata_1_value: record[:metadata_1_value],
          metadata_2_label: record[:metadata_2_label],
          metadata_2_value: record[:metadata_2_value],
          subname: view.template_name,
          view: view.to_html
        }
      end
  end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
onebox-1.8.47 lib/onebox/layout.rb
onebox-1.8.46 lib/onebox/layout.rb
onebox-1.8.45 lib/onebox/layout.rb
onebox-1.8.44 lib/onebox/layout.rb
onebox-1.8.43 lib/onebox/layout.rb
onebox-1.8.42 lib/onebox/layout.rb
onebox-1.8.41 lib/onebox/layout.rb
onebox-1.8.40 lib/onebox/layout.rb
onebox-1.8.39 lib/onebox/layout.rb
onebox-1.8.38 lib/onebox/layout.rb
onebox-1.8.36 lib/onebox/layout.rb
onebox-1.8.35 lib/onebox/layout.rb
onebox-1.8.34 lib/onebox/layout.rb
onebox-1.8.33 lib/onebox/layout.rb
onebox-1.8.32 lib/onebox/layout.rb
onebox-1.8.31 lib/onebox/layout.rb
onebox-1.8.30 lib/onebox/layout.rb
onebox-1.8.29 lib/onebox/layout.rb
onebox-1.8.28 lib/onebox/layout.rb
onebox-1.8.27 lib/onebox/layout.rb