Sha256: f0e1bcab80db2ee0388743cf415fbc306d005b6eab5110678d6e1a3e3c300d94

Contents?: true

Size: 1.29 KB

Versions: 16

Compression:

Stored size: 1.29 KB

Contents

require 'link_thumbnailer/scrapers/default/base'
require 'link_thumbnailer/models/image'

module LinkThumbnailer
  module Scrapers
    module Default
      class Images < ::LinkThumbnailer::Scrapers::Default::Base

        def value
          abs_urls.each_with_index.take_while { |_, i| i < config.image_limit }.map { |e| modelize(e.first) }
        end

        private

        def urls
          document.search('//img').map { |i| i['src'] }.compact
        end

        def abs_urls
          urls.map do |url|
            uri = validate_url(url)

            next unless uri

            uri = prefix_uri(uri) if needs_prefix?(uri)
            uri
          end
        end

        def validate_url(url)
          ::URI.parse(url.to_s)
        rescue ::URI::InvalidURIError
          nil
        end

        def needs_prefix?(uri)
          !uri.is_a?(::URI::HTTP)
        end

        def prefix_uri(uri)
          ::URI.join(prefix_url, uri)
        end

        def prefix_url
          base_href || website.url
        end

        def base_href
          base = document.at('//head/base')
          base['href'] if base
        end

        def model_class
          ::LinkThumbnailer::Models::Image
        end

        def modelize(uri)
          model_class.new(uri)
        end

      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
link_thumbnailer-3.0.1 lib/link_thumbnailer/scrapers/default/images.rb
link_thumbnailer-3.0.0 lib/link_thumbnailer/scrapers/default/images.rb
link_thumbnailer-2.6.1 lib/link_thumbnailer/scrapers/default/images.rb
link_thumbnailer-2.6.0 lib/link_thumbnailer/scrapers/default/images.rb
link_thumbnailer-2.5.2 lib/link_thumbnailer/scrapers/default/images.rb
link_thumbnailer-2.5.1 lib/link_thumbnailer/scrapers/default/images.rb
link_thumbnailer-2.5.0 lib/link_thumbnailer/scrapers/default/images.rb
link_thumbnailer-2.4.0 lib/link_thumbnailer/scrapers/default/images.rb
link_thumbnailer-2.3.2 lib/link_thumbnailer/scrapers/default/images.rb
link_thumbnailer-2.3.1 lib/link_thumbnailer/scrapers/default/images.rb
link_thumbnailer-2.3.0 lib/link_thumbnailer/scrapers/default/images.rb
link_thumbnailer-2.2.3 lib/link_thumbnailer/scrapers/default/images.rb
link_thumbnailer-2.2.2 lib/link_thumbnailer/scrapers/default/images.rb
link_thumbnailer-2.2.1 lib/link_thumbnailer/scrapers/default/images.rb
link_thumbnailer-2.2.0 lib/link_thumbnailer/scrapers/default/images.rb
link_thumbnailer-2.1.0 lib/link_thumbnailer/scrapers/default/images.rb