Sha256: 6b68acdf424577ba006dc944a5c4d81fcbec5941d7fd98e3c8d1a85b663649aa

Contents?: true

Size: 1.52 KB

Versions: 41

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

require_relative "template_support"

module Onebox
  class Layout < Mustache
    include TemplateSupport

    VERSION = "1.0.0"

    attr_reader :record
    attr_reader :view

    def initialize(name, record)
      @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
      render(details)
    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

41 entries across 41 versions & 1 rubygems

Version Path
onebox-2.2.14 lib/onebox/layout.rb
onebox-2.2.13 lib/onebox/layout.rb
onebox-2.2.12 lib/onebox/layout.rb
onebox-2.2.11 lib/onebox/layout.rb
onebox-2.2.10 lib/onebox/layout.rb
onebox-2.2.9 lib/onebox/layout.rb
onebox-2.2.8 lib/onebox/layout.rb
onebox-2.2.6 lib/onebox/layout.rb
onebox-2.2.5 lib/onebox/layout.rb
onebox-2.2.4 lib/onebox/layout.rb
onebox-2.2.2 lib/onebox/layout.rb
onebox-2.2.1 lib/onebox/layout.rb
onebox-2.2.0 lib/onebox/layout.rb
onebox-2.1.9 lib/onebox/layout.rb
onebox-2.1.8 lib/onebox/layout.rb
onebox-2.1.7 lib/onebox/layout.rb
onebox-2.1.6 lib/onebox/layout.rb
onebox-2.1.5 lib/onebox/layout.rb
onebox-2.1.4 lib/onebox/layout.rb
onebox-2.1.3 lib/onebox/layout.rb