Class: RailFeeds::HTTPClient

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/rail_feeds/http_client.rb

Overview

A wrapper class for ::Net::HTTP

Instance Method Summary collapse

Methods included from Logging

formatter, included, #logger, logger, #logger=, logger=

Constructor Details

#initialize(credentials: nil, logger: nil) ⇒ HTTPClient

Initialize a new http client.

Parameters:

  • credentials (RailFeeds::Credentials) (defaults to: nil)

    The credentials for connecting to the feed.

  • logger (Logger) (defaults to: nil)

    The logger for outputting evetns, if nil the global logger will be used.



13
14
15
16
# File 'lib/rail_feeds/http_client.rb', line 13

def initialize(credentials: nil, logger: nil)
  @credentials = credentials
  self.logger = logger unless logger.nil?
end

Instance Method Details

#download(url, file) ⇒ Object

Download path from server.

Parameters:

  • url (String)

    The URL to download. For child classes this is just the path.

  • file (String)

    The path to the file to save the contents in.



43
44
45
46
47
48
49
50
# File 'lib/rail_feeds/http_client.rb', line 43

def download(url, file)
  logger.debug "download(#{url.inspect}, #{file.inspect})"
  fetch(url) do |src|
    File.open(file, 'w') do |dst|
      IO.copy_stream src, dst
    end
  end
end

#fetch(url, options = {}) { ... } ⇒ Object

Fetch path from server.

Parameters:

  • url (String)

    The URL to fetch.

Yields:

  • contents @yieldparam [IO] file Either a Tempfile or StringIO.



22
23
24
25
26
# File 'lib/rail_feeds/http_client.rb', line 22

def fetch(url, options = {})
  logger.debug "fetching #{url.inspect}"
  options[:http_basic_authentication] = @credentials.to_a
  yield URI(url).open(options)
end

#fetch_unzipped(url) { ... } ⇒ Object

Fetch path from server and unzip it.

Parameters:

  • url (String)

    The URL to fetch. For child classes this is just the path.

Yields:

  • contents @yieldparam [Zlib::GzipReader] reader The unzippable content of the file.



32
33
34
35
36
37
38
# File 'lib/rail_feeds/http_client.rb', line 32

def fetch_unzipped(url)
  logger.debug "get_unzipped(#{url.inspect})"
  fetch(url) do |gz_file|
    reader = Zlib::GzipReader.new gz_file
    yield reader
  end
end