Sha256: aebdd5dd691345ef8f4559665daacaed1867af442f1ceeabfb07d123c6ed92d7

Contents?: true

Size: 1.45 KB

Versions: 5

Compression:

Stored size: 1.45 KB

Contents

require 'delegate'
require 'link_thumbnailer/models/title'
require 'link_thumbnailer/models/description'
require 'link_thumbnailer/models/image'

module LinkThumbnailer
  module Scrapers
    class Base < ::SimpleDelegator

      attr_reader :config, :document, :website, :attribute_name

      def initialize(document)
        @config   = ::LinkThumbnailer.page.config
        @document = document

        super(config)
      end

      def call(website, attribute_name)
        @website        = website
        @attribute_name = attribute_name

        website.send("#{attribute_name}=", value)
        website
      end

      def applicable?
        true
      end

      private

      def value
        raise 'must implement'
      end

      def meta_xpath(options = {})
        meta_xpaths(options).first
      end

      def meta_xpaths(options = {})
        key       = options.fetch(:key, :property)
        value     = options.fetch(:value, :content)
        attribute = options.fetch(:attribute, attribute_name)

        document.xpath("//meta[translate(@#{key},'#{abc.upcase}','#{abc}') = '#{attribute}' and @#{value}]")
      end

      def abc
        'abcdefghijklmnopqrstuvwxyz'
      end

      def model_class
        "::LinkThumbnailer::Models::#{attribute_name.to_s.camelize}".constantize
      end

      def modelize(node, text = nil)
        model_class.new(node, text)
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
link_thumbnailer-2.0.4 lib/link_thumbnailer/scrapers/base.rb
link_thumbnailer-2.0.3 lib/link_thumbnailer/scrapers/base.rb
link_thumbnailer-2.0.2 lib/link_thumbnailer/scrapers/base.rb
link_thumbnailer-2.0.1 lib/link_thumbnailer/scrapers/base.rb
link_thumbnailer-2.0.0 lib/link_thumbnailer/scrapers/base.rb