Sha256: 3212b883ae3bbe18a6fcf41fa979312a6054be697ec64d745b78bfe087f554b9

Contents?: true

Size: 1.88 KB

Versions: 4

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).call(website, 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

4 entries across 4 versions & 1 rubygems

Version Path
link_thumbnailer-2.4.0 lib/link_thumbnailer/scraper.rb
link_thumbnailer-2.3.2 lib/link_thumbnailer/scraper.rb
link_thumbnailer-2.3.1 lib/link_thumbnailer/scraper.rb
link_thumbnailer-2.3.0 lib/link_thumbnailer/scraper.rb