Sha256: eceddfbde14671f95976f8aaa28f14db04090475eb1b3f3e71b5cca03dd64777

Contents?: true

Size: 1.43 KB

Versions: 4

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

require 'aranha/parsers/source_address'

module Aranha
  module Fixtures
    class Download
      attr_reader :pending

      def initialize(options)
        @prefix = options.fetch(:prefix)
        @prefix = '' if @prefix.blank?
        @download = options.fetch(:download)
        @pending = options.fetch(:pending)
      end

      def run
        url_files.each do |f|
          Rails.logger.info(relative_path(f))
          download(url(f), target(f)) if @download
        end
      end

      private

      def url_files
        Dir["#{fixtures_root}/**/*.url"].select { |path| select_path?(path) }
      end

      def select_path?(path)
        return false unless match_prefix_pattern(path)
        !pending || !::File.exist?(target(path))
      end

      def match_prefix_pattern(path)
        relative_path(path).start_with?(@prefix)
      end

      def fixtures_root
        Rails.root.to_s
      end

      def download(url, target)
        Rails.logger.info "Baixando \"#{url}\"..."
        File.open(target, 'wb') { |file| file.write(::Aranha::Parsers::Base.new(url).content) }
      end

      def url(file)
        ::Aranha::Parsers::SourceAddress.from_file(file)
      end

      def target(file)
        File.expand_path(File.basename(file, '.url') + '.source.html', File.dirname(file))
      end

      def relative_path(path)
        path.sub(%r{^#{Regexp.quote(fixtures_root)}/}, '')
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
aranha-0.9.0 lib/aranha/fixtures/download.rb
aranha-0.8.0 lib/aranha/fixtures/download.rb
aranha-0.7.1 lib/aranha/fixtures/download.rb
aranha-0.7.0 lib/aranha/fixtures/download.rb