Class: Opener::Daemons::Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/opener/daemons/downloader.rb

Overview

Downloads and validates text/XML documents used as input.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDownloader

Returns a new instance of Downloader.



12
13
14
15
16
# File 'lib/opener/daemons/downloader.rb', line 12

def initialize
  @http = HTTPClient.new

  @http.ssl_config.options |= OpenSSL::SSL::OP_NO_SSLv3
end

Instance Attribute Details

#httpHTTPClient (readonly)

Returns:

  • (HTTPClient)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/opener/daemons/downloader.rb', line 9

class Downloader
  attr_reader :http

  def initialize
    @http = HTTPClient.new

    @http.ssl_config.options |= OpenSSL::SSL::OP_NO_SSLv3
  end

  ##
  # Downloads the document located at `url`.
  #
  # @param [String] url
  # @return [String]
  #
  def download(url)
    resp = http.get(url, :follow_redirect => true)

    unless resp.ok?
      raise(
        HTTPClient::BadResponseError,
        "Got HTTP #{resp.status}: #{resp.body}"
      )
    end

    return resp.body.force_encoding('UTF-8')
  end
end

Instance Method Details

#download(url) ⇒ String

Downloads the document located at ‘url`.

Parameters:

  • url (String)

Returns:

  • (String)


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/opener/daemons/downloader.rb', line 24

def download(url)
  resp = http.get(url, :follow_redirect => true)

  unless resp.ok?
    raise(
      HTTPClient::BadResponseError,
      "Got HTTP #{resp.status}: #{resp.body}"
    )
  end

  return resp.body.force_encoding('UTF-8')
end