Class: Pageflow::Chart::Downloader

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

Defined Under Namespace

Classes: HTTPError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Downloader

Returns a new instance of Downloader.



11
12
13
# File 'lib/pageflow/chart/downloader.rb', line 11

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, raise_on_http_error: false) ⇒ Object



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

def load(url, raise_on_http_error: false)
  file = open(make_absolute(url))
  yield(file)
rescue OpenURI::HTTPError => exception
  Rails.logger.error "Exception loading url #{url}: #{exception.message}"
  raise(HTTPError) if raise_on_http_error
ensure
  file.close if file
end

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



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

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