Sha256: 0af40ae13d4eb0f1d74e5ad6cc15b40aa9f20de5db36f8010b14d322b12f15f1

Contents?: true

Size: 1.88 KB

Versions: 12

Compression:

Stored size: 1.88 KB

Contents

require 'delegate'
require 'active_support/core_ext/object/blank'
require 'active_support/inflector'

require 'link_thumbnailer/parser'
require 'link_thumbnailer/models/website'
require 'link_thumbnailer/scrapers/default/title'
require 'link_thumbnailer/scrapers/opengraph/title'
require 'link_thumbnailer/scrapers/default/description'
require 'link_thumbnailer/scrapers/opengraph/description'
require 'link_thumbnailer/scrapers/default/images'
require 'link_thumbnailer/scrapers/opengraph/images'
require 'link_thumbnailer/scrapers/default/videos'
require 'link_thumbnailer/scrapers/opengraph/videos'
require 'link_thumbnailer/scrapers/default/favicon'
require 'link_thumbnailer/scrapers/opengraph/favicon'

module LinkThumbnailer
  class Scraper < ::SimpleDelegator

    attr_reader :document, :source, :url, :config, :website

    def initialize(source, url)
      @source       = source
      @url          = url
      @config       = ::LinkThumbnailer.page.config
      @document     = parser.call(source)
      @website      = ::LinkThumbnailer::Models::Website.new
      @website.url  = url

      super(config)
    end

    def call
      config.attributes.each do |name|
        scrapers.each do |scraper_prefix|
          scraper_class(scraper_prefix, name).new(document, website).call(name.to_s)
          break unless website.send(name).blank?
        end
      end

      website
    end

    private

    def scrapers
      [
        '::LinkThumbnailer::Scrapers::Opengraph',
        '::LinkThumbnailer::Scrapers::Default'
      ]
    end

    def scraper_class(prefix, name)
      name = name.to_s.camelize
      "#{prefix}::#{name}".constantize
    rescue NameError
      raise ::LinkThumbnailer::ScraperInvalid, "scraper named '#{prefix}::#{name}' does not exists."
    end

    def parser
      ::LinkThumbnailer::Parser.new
    end

  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
link_thumbnailer-3.1.2 lib/link_thumbnailer/scraper.rb
link_thumbnailer-3.1.1 lib/link_thumbnailer/scraper.rb
link_thumbnailer-3.1.0 lib/link_thumbnailer/scraper.rb
link_thumbnailer-3.0.3 lib/link_thumbnailer/scraper.rb
link_thumbnailer-3.0.2 lib/link_thumbnailer/scraper.rb
link_thumbnailer-3.0.1 lib/link_thumbnailer/scraper.rb
link_thumbnailer-3.0.0 lib/link_thumbnailer/scraper.rb
link_thumbnailer-2.6.1 lib/link_thumbnailer/scraper.rb
link_thumbnailer-2.6.0 lib/link_thumbnailer/scraper.rb
link_thumbnailer-2.5.2 lib/link_thumbnailer/scraper.rb
link_thumbnailer-2.5.1 lib/link_thumbnailer/scraper.rb
link_thumbnailer-2.5.0 lib/link_thumbnailer/scraper.rb