Class: Pageflow::Chart::Downloader
- Inherits:
-
Object
- Object
- Pageflow::Chart::Downloader
- Defined in:
- lib/pageflow/chart/downloader.rb
Defined Under Namespace
Classes: HTTPError
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Downloader
constructor
A new instance of Downloader.
- #load(url, raise_on_http_error: false) ⇒ Object
- #load_all(urls, options = {}) ⇒ Object
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 = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/pageflow/chart/downloader.rb', line 7 def @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.}" 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, = {}) file = Tempfile.new(['concatenation', .fetch(:extension, 'txt')]) file.binmode begin urls.map do |url| file.write([:before_each]) if .key?(:before_each) load(url) do |source| while data = source.read(16 * 1024) file.write(data) end end file.write([:after_each]) if .key?(:after_each) file.write(.fetch(:separator, "\n")) end file.rewind yield(file) ensure file.close file.unlink end end |