class RailFeeds::HTTPClient
A wrapper class for ::Net::HTTP
Attributes
credentials[R]
Public Class Methods
new(credentials: nil, logger: nil)
click to toggle source
Initialize a new http client. @param [RailFeeds::Credentials] credentials
The credentials for connecting to the feed.
@param [Logger] logger
The logger for outputting evetns, if nil the global logger will be used.
# File lib/rail_feeds/http_client.rb, line 13 def initialize(credentials: nil, logger: nil) @credentials = credentials self.logger = logger unless logger.nil? end
Public Instance Methods
download(url, file)
click to toggle source
Download path from server. @param [String] url The URL to download. For child classes this is just the path. @param [String] file The path to the file to save the contents in.
# 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 = {}) { |URI(url).open(options)| ... }
click to toggle source
Fetch path from server. @param [String] url The URL to fetch. @yield contents
@yieldparam [IO] file Either a Tempfile or StringIO.
# 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) { |reader| ... }
click to toggle source
Fetch path from server and unzip it. @param [String] url The URL to fetch. For child classes this is just the path. @yield contents
@yieldparam [Zlib::GzipReader] reader The unzippable content of the file.
# 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