Skip to content
This repository has been archived by the owner on Dec 16, 2020. It is now read-only.

Commit

Permalink
Add downloading file to http client.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Gauld committed Jul 4, 2018
1 parent dc8e967 commit d05e753
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 25 deletions.
2 changes: 1 addition & 1 deletion lib/rail_feeds/network_rail/corpus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def self.load_file(file)
# @return [Array<RailFeeds::NetworkRail::CORPUS::Data>]
def self.fetch_data(credentials: Credentials)
client = HTTPClient.new(credentials: credentials)
client.get_unzipped('ntrod/SupportingFileAuthenticate?type=CORPUS') do |file|
client.fetch_unzipped('ntrod/SupportingFileAuthenticate?type=CORPUS') do |file|
break parse_json file.read
end
end
Expand Down
29 changes: 19 additions & 10 deletions lib/rail_feeds/network_rail/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,40 @@ def initialize(credentials: Credentials, logger: nil)
self.logger = logger unless logger.nil?
end

# Get path from network rail server.
# Fetch path from network rail server.
# @param [String] path
# The path to get.
# The path to fetch.
# @yield [file] Once the block has run the temp file will be deleted.
# @yieldparam [Tempfile] file The content of the file.
def get(path)
logger.debug "get(#{path.inspect})"
uri = URI("https://#{HOST}/#{path}")
file = uri.open(http_basic_authentication: @credentials.to_a)
def fetch(path)
file = download path
yield file
file.delete
ensure
file&.delete
end

# Get path from network rail server and unzip it.
# Fetch path from network rail server and unzip it.
# @param [String] path
# The path to get.
# The path to fetch.
# @yield [reader] Once the block has run the temp file will be deleted.
# @yieldparam [Zlib::GzipReader] reader The unzippable content of the file.
def get_unzipped(path)
def fetch_unzipped(path)
logger.debug "get_unzipped(#{path.inspect})"
get(path) do |gz_file|
logger.debug "gz_file = #{gz_file.inspect}"
yield Zlib::GzipReader.open(gz_file.path)
end
end

# Get path from network rail server.
# @param [String] path
# The path to download.
# @return [Tempfile] The downloaded file
def download(path)
logger.debug "download(#{path.inspect})"
uri = URI("https://#{HOST}/#{path}")
uri.open(http_basic_authentication: @credentials.to_a)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/rail_feeds/network_rail/smart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def self.load_file(file)
# @return [Array<RailFeeds::NetworkRail::SMART::Step>]
def self.fetch_data(credentials: Credentials)
client = HTTPClient.new(credentials: credentials)
client.get_unzipped('ntrod/SupportingFileAuthenticate?type=SMART') do |file|
client.fetch_unzipped('ntrod/SupportingFileAuthenticate?type=SMART') do |file|
break parse_json file.read
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/rail_feeds/network_rail/corpus_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
expect(RailFeeds::NetworkRail::HTTPClient)
.to receive(:new).with(credentials: RailFeeds::NetworkRail::Credentials)
.and_return(http_client)
expect(http_client).to receive(:get_unzipped)
expect(http_client).to receive(:fetch_unzipped)
.with('ntrod/SupportingFileAuthenticate?type=CORPUS')
.and_yield(reader)
expect(reader).to receive(:read).and_return(json)
Expand Down
27 changes: 16 additions & 11 deletions spec/rail_feeds/network_rail/http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
let(:temp_file) { double Tempfile }
before(:each) { allow(temp_file).to receive(:delete) }

describe '#get' do
it 'Yields what uri.open does' do
describe '#download' do
it 'Returns what uri.open does' do
uri = double URI
expect(URI).to receive(:parse).with('https://datafeeds.networkrail.co.uk/path').and_return(uri)
expect(uri).to receive(:open).and_return(temp_file)
expect { |a| subject.get('path', &a) }.to yield_with_args(temp_file)
expect(subject.download('path')).to eq temp_file
end

it 'Adds credentials when getting path' do
Expand All @@ -23,7 +23,7 @@
.with(http_basic_authentication: ['user', 'pass'])
.and_return(temp_file)
subject = described_class.new credentials: credentials
subject.get('path') {}
subject.download('path') {}
end

it 'Handles special characters in credentials' do
Expand All @@ -35,25 +35,30 @@
expect(URI).to receive(:parse).and_return(uri)
expect(uri).to receive(:open).and_return(temp_file)
subject = described_class.new credentials: credentials
expect { subject.get('path') {} }.to_not raise_error
expect { subject.download('path') {} }.to_not raise_error
end
end

describe '#fetch' do
it 'Yields what download does' do
expect(subject).to receive(:download).with('path').and_return(temp_file)
expect { |a| subject.fetch('path', &a) }.to yield_with_args(temp_file)
end

it 'Deletes tempfile' do
uri = double URI
expect(URI).to receive(:parse).and_return(uri)
expect(uri).to receive(:open).and_return(temp_file)
expect(subject).to receive(:download).with('path').and_return(temp_file)
expect(temp_file).to receive(:delete)
subject.get('path') {}
subject.fetch('path') {}
end
end

describe '#get_unzipped' do
describe '#fetch_unzipped' do
it 'Returns what Zlib::GzipReader.open does' do
reader = double Zlib::GzipReader
expect(subject).to receive(:get).with('path').and_yield(temp_file)
expect(temp_file).to receive(:path).and_return('gz_file_path')
expect(Zlib::GzipReader).to receive(:open).with('gz_file_path').and_return(reader)
expect { |a| subject.get_unzipped('path', &a) }.to yield_with_args(reader)
expect { |a| subject.fetch_unzipped('path', &a) }.to yield_with_args(reader)
end
end
end
2 changes: 1 addition & 1 deletion spec/rail_feeds/network_rail/smart_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
expect(RailFeeds::NetworkRail::HTTPClient)
.to receive(:new).with(credentials: RailFeeds::NetworkRail::Credentials)
.and_return(http_client)
expect(http_client).to receive(:get_unzipped)
expect(http_client).to receive(:fetch_unzipped)
.with('ntrod/SupportingFileAuthenticate?type=SMART')
.and_yield(reader)
expect(reader).to receive(:read).and_return(json)
Expand Down

0 comments on commit d05e753

Please sign in to comment.