Sha256: 188ab4a10ff8e4f011a33e4b1c2aa6256fd79fc4dcdfb267fdbba85e4245dd09

Contents?: true

Size: 1.23 KB

Versions: 6

Compression:

Stored size: 1.23 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
      return record[:domain] if record[:domain]
      URI(link || '').host
    end

    def details
      {
        link: record[:link],
        title: record[:title],
        domain: domain,
        subname: view.template_name,
        view: view.to_html
      }
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
onebox-1.5.25 lib/onebox/layout.rb
onebox-1.5.24 lib/onebox/layout.rb
onebox-1.5.23 lib/onebox/layout.rb
onebox-1.5.22 lib/onebox/layout.rb
onebox-1.5.21 lib/onebox/layout.rb
onebox-1.5.20 lib/onebox/layout.rb