Class: Pageflow::Chart::Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/pageflow/chart/downloader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Downloader

Returns a new instance of Downloader.



9
10
11
# File 'lib/pageflow/chart/downloader.rb', line 9

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/pageflow/chart/downloader.rb', line 7

def options
  @options
end

Instance Method Details

#load(url) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/pageflow/chart/downloader.rb', line 13

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

  begin
    yield(file)
  ensure
    file.close
  end
end

#load_all(urls, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pageflow/chart/downloader.rb', line 23

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

  begin
    urls.map do |url|
      file.write(options[:before_each]) if options.key?(:before_each)

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

      file.write(options[:after_each]) if options.key?(:after_each)
      file.write(options.fetch(:separator, "\n"))
    end

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