Sha256: 22c9f2be0602d37c0a1d666d7564f660b12097ee8c04f555af90c26d317c45c0

Contents?: true

Size: 1.74 KB

Versions: 15

Compression:

Stored size: 1.74 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
        record[:link]
      end

      def domain
        record[:domain] || URI(link || '').host
      end

      def metadata_1_label
        record[:metadata_1_label]
      end

      def metadata_1_value
        record[:metadata_1_value]
      end

      def metadata_2_label
        record[:metadata_2_label]
      end

      def metadata_2_value
        record[:metadata_2_value]
      end

      def details
        {
          link: record[:link],
          title: record[:title],
          domain: domain,
          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

15 entries across 15 versions & 1 rubygems

Version Path
onebox-1.6.8 lib/onebox/layout.rb
onebox-1.6.7 lib/onebox/layout.rb
onebox-1.6.6 lib/onebox/layout.rb
onebox-1.6.5 lib/onebox/layout.rb
onebox-1.6.4 lib/onebox/layout.rb
onebox-1.6.3 lib/onebox/layout.rb
onebox-1.6.2 lib/onebox/layout.rb
onebox-1.6.1 lib/onebox/layout.rb
onebox-1.6.0 lib/onebox/layout.rb
onebox-1.5.65 lib/onebox/layout.rb
onebox-1.5.64 lib/onebox/layout.rb
onebox-1.5.63 lib/onebox/layout.rb
onebox-1.5.62 lib/onebox/layout.rb
onebox-1.5.61 lib/onebox/layout.rb
onebox-1.5.60 lib/onebox/layout.rb