Sha256: 907361164682d34992033aa68d5e564530f97f14b15803da5ad3d9b6b9ff8a6c

Contents?: true

Size: 1 KB

Versions: 2

Compression:

Stored size: 1 KB

Contents

require 'uri'
require 'open-uri'

module Pageflow
  module Chart
    class Downloader
      attr_reader :options

      def initialize(options = {})
        @options = options
      end

      def load(url)
        file = open(make_absolute(url))

        begin
          yield(file)
        ensure
          file.close
        end
      end

      def load_all(urls, options = {})
        file = Tempfile.new(['concatenation', options.fetch(:extension, 'txt')])
        file.binmode

        begin
          urls.map do |url|
            load(url) do |source|
              while data = source.read(16 * 1024)
                file.write(data)
              end
            end

            file.write(options.fetch(:separator, "\n"))
          end

          file.rewind
          yield(file)
        ensure
          file.close
          file.unlink
        end
      end

      private

      def make_absolute(url)
        options[:base_url] ? URI.join(options[:base_url], url) : URI.parse(url)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pageflow-chart-0.1.1 lib/pageflow/chart/downloader.rb
pageflow-chart-0.1.0 lib/pageflow/chart/downloader.rb