Sha256: 87b2eb7c32de90d3d633f67e417f2be0bd1c352bbb96a70807aaa565c10cb8a6

Contents?: true

Size: 1.09 KB

Versions: 8

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

module Eac
  class DownloadFixtures
    def initialize(prefix, download)
      @prefix = prefix
      @prefix = '' if @prefix.blank?
      @download = download
    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
      files = []
      Dir["#{fixtures_root}/**/*.url"].map do |path|
        files << path if match_pattern(path)
      end
      files
    end

    def match_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(Parsers::Base.new(url).content) }
    end

    def url(file)
      File.read(file).strip
    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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
eac_rails_utils-0.3.0 lib/eac/download_fixtures.rb
eac_rails_utils-0.2.2 lib/eac/download_fixtures.rb
eac_rails_utils-0.2.1 lib/eac/download_fixtures.rb
eac_rails_utils-0.2.0 lib/eac/download_fixtures.rb
eac_rails_utils-0.1.15 lib/eac/download_fixtures.rb
eac_rails_utils-0.1.14 lib/eac/download_fixtures.rb
eac_rails_utils-0.1.13 lib/eac/download_fixtures.rb
eac_rails_utils-0.1.12 lib/eac/download_fixtures.rb