Sha256: 72c9801e6139faf2024a41df6027937c3ffd2d5a505591390e8dd8f342f5204c

Contents?: true

Size: 1.36 KB

Versions: 41

Compression:

Stored size: 1.36 KB

Contents

module Onebox
  class Layout < Mustache
    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 load_paths
      Onebox.options.load_paths.select(&method(:template?))
    end

    def template?(path)
      File.exist?(File.join(path, "#{template_name}.#{template_extension}"))
    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

41 entries across 41 versions & 1 rubygems

Version Path
onebox-1.5.19 lib/onebox/layout.rb
onebox-1.5.18 lib/onebox/layout.rb
onebox-1.5.17 lib/onebox/layout.rb
onebox-1.5.16 lib/onebox/layout.rb
onebox-1.5.14 lib/onebox/layout.rb
onebox-1.5.13 lib/onebox/layout.rb
onebox-1.5.12 lib/onebox/layout.rb
onebox-1.5.11 lib/onebox/layout.rb
onebox-1.5.10 lib/onebox/layout.rb
onebox-1.5.9 lib/onebox/layout.rb
onebox-1.5.8 lib/onebox/layout.rb
onebox-1.5.7 lib/onebox/layout.rb
onebox-1.5.6 lib/onebox/layout.rb
onebox-1.5.5 lib/onebox/layout.rb
onebox-1.5.3 lib/onebox/layout.rb
onebox-1.5.2 lib/onebox/layout.rb
onebox-1.5.1 lib/onebox/layout.rb
onebox-1.5.0 lib/onebox/layout.rb
onebox-1.4.9 lib/onebox/layout.rb
onebox-1.4.8 lib/onebox/layout.rb