Sha256: 6c9f66ed4c59fb15052242d58db4774c9967c0ada1d84d1401bb245bca9f48a8

Contents?: true

Size: 1.33 KB

Versions: 5

Compression:

Stored size: 1.33 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 repository_path
      record[:repository_path]
    end

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
onebox-1.5.30 lib/onebox/layout.rb
onebox-1.5.29 lib/onebox/layout.rb
onebox-1.5.28 lib/onebox/layout.rb
onebox-1.5.27 lib/onebox/layout.rb
onebox-1.5.26 lib/onebox/layout.rb