Class: RailFeeds::HTTPClient
- Inherits:
-
Object
- Object
- RailFeeds::HTTPClient
- Includes:
- Logging
- Defined in:
- lib/rail_feeds/http_client.rb
Overview
A wrapper class for ::Net::HTTP
Direct Known Subclasses
Instance Method Summary collapse
-
#download(url, file) ⇒ Object
Download path from server.
-
#fetch(url, options = {}) { ... } ⇒ Object
Fetch path from server.
-
#fetch_unzipped(url) { ... } ⇒ Object
Fetch path from server and unzip it.
-
#initialize(credentials: nil, logger: nil) ⇒ HTTPClient
constructor
Initialize a new http client.
Methods included from Logging
formatter, included, #logger, logger, #logger=, logger=
Constructor Details
#initialize(credentials: nil, logger: nil) ⇒ HTTPClient
Initialize a new http client.
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.
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.
22 23 24 25 26 |
# File 'lib/rail_feeds/http_client.rb', line 22 def fetch(url, = {}) logger.debug "fetching #{url.inspect}" [:http_basic_authentication] = @credentials.to_a yield URI(url).open() end |
#fetch_unzipped(url) { ... } ⇒ Object
Fetch path from server and unzip it.
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 |