Sha256: a0bd31d6e86e31ddf4fc06df497912bccd2cc78ef7ad4048d15b3033e6dd110c

Contents?: true

Size: 1.55 KB

Versions: 82

Compression:

Stored size: 1.55 KB

Contents

module Onebox
  class Preview
    attr_reader :cache

    def initialize(link, parameters = Onebox.options)
      @url = link
      @options = parameters
      @cache = options.cache
      @engine_class = Matcher.new(@url).oneboxed
    end

    def to_s
      return "" unless engine
      process_html(engine_html)
    rescue *Onebox::Preview.web_exceptions
      ""
    end

    def placeholder_html
      return "" unless engine
      process_html(engine.placeholder_html)
    rescue *Onebox::Preview.web_exceptions
      ""
    end

    def options
      OpenStruct.new(@options)
    end

    def self.web_exceptions
     [Net::HTTPServerException, OpenURI::HTTPError, Timeout::Error, Net::HTTPError, Errno::ECONNREFUSED]
    end

    private

    def engine_html
      engine.to_html
    end

    def process_html(html)
      return "" unless html

      if @options[:max_width]
        doc = Nokogiri::HTML::fragment(html)
        if doc
          doc.css('[width]').each do |e|
            width = e['width'].to_i

            if width > @options[:max_width]
              height = e['height'].to_i
              if (height > 0)
                ratio = (height.to_f / width.to_f)
                e['height'] = (@options[:max_width] * ratio).floor
              end
              e['width'] = @options[:max_width]
            end
          end
          return doc.to_html
        end
      end

      html
    end

    def engine
      return nil unless @engine_class
      @engine ||= @engine_class.new(@url, cache)
    end

    class InvalidURI < StandardError
    end
  end
end

Version data entries

82 entries across 82 versions & 1 rubygems

Version Path
onebox-1.7.3 lib/onebox/preview.rb
onebox-1.7.2 lib/onebox/preview.rb
onebox-1.7.1 lib/onebox/preview.rb
onebox-1.7.0 lib/onebox/preview.rb
onebox-1.6.9 lib/onebox/preview.rb
onebox-1.6.8 lib/onebox/preview.rb
onebox-1.6.7 lib/onebox/preview.rb
onebox-1.6.6 lib/onebox/preview.rb
onebox-1.6.5 lib/onebox/preview.rb
onebox-1.6.4 lib/onebox/preview.rb
onebox-1.6.3 lib/onebox/preview.rb
onebox-1.6.2 lib/onebox/preview.rb
onebox-1.6.1 lib/onebox/preview.rb
onebox-1.6.0 lib/onebox/preview.rb
onebox-1.5.65 lib/onebox/preview.rb
onebox-1.5.64 lib/onebox/preview.rb
onebox-1.5.63 lib/onebox/preview.rb
onebox-1.5.62 lib/onebox/preview.rb
onebox-1.5.61 lib/onebox/preview.rb
onebox-1.5.60 lib/onebox/preview.rb