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

Commit

Permalink
http_client.fetch now returns an IO not Tempfile (it will return a Te…
Browse files Browse the repository at this point in the history
…mpFile most of the time). addresses #1
  • Loading branch information
Robert Gauld committed Jul 23, 2018
1 parent ae6058d commit 77f26d4
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 24 deletions.
6 changes: 3 additions & 3 deletions doc/guides/Network Rail/Schedule.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ RailFeeds::NetworkRail::Credentials.configure(
# 2. Fetch some files
fetcher = RailFeeds::NetworkRail::Schedule::Fetcher.new

fetcher.fetch_all_full(:cif) do |full_file|
fetcher.fetch_all_full(:cif) do |full_file_as_an_io|
...
end

fetcher.fetch_all_update('fri', :cif) do |update_file|
fetcher.fetch_all_update('fri', :cif) do |update_file_as_an_io|
...
end
# Each of the file variables will contain a TempFile which can be used to read
# Each of the file variables will contain an IO object which can be used to read
# the CIF data (or passed to the parser to make use of). The files will be deleted
# at the end of the block.
```
Expand Down
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 @@ -21,7 +21,7 @@ def self.download(file, credentials: Credentials)

# Fetch the current CORPUS data.
# @param [RailFeeds::NetworkRail::Credentials] credentials
# @return [Tempfile]
# @return [IO]
def self.fetch(credentials: Credentials)
client = HTTPClient.new(credentials: credentials)
client.fetch 'ntrod/SupportingFileAuthenticate?type=CORPUS'
Expand Down
13 changes: 2 additions & 11 deletions lib/rail_feeds/network_rail/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,11 @@ def initialize(credentials: Credentials, logger: nil)
# Fetch path from network rail server.
# @param [String] path 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.
# @yieldparam [IO] file Either a Tempfile or StringIO.
def fetch(path)
logger.debug "fetch(#{path.inspect})"
uri = URI("https://#{HOST}/#{path}")
opened_uri = uri.open(http_basic_authentication: @credentials.to_a)

if opened_uri.is_a?(StringIO)
data = opened_uri
opened_uri = Tempfile.open 'rail_feeds-network_rail-http_client'
opened_uri.write data
opened_uri.rewind
end

yield opened_uri
yield uri.open(http_basic_authentication: @credentials.to_a)
end

# Fetch path from network rail server and unzip it.
Expand Down
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 @@ -39,7 +39,7 @@ def self.download(file, credentials: Credentials)

# Fetch the current SMART data.
# @param [RailFeeds::NetworkRail::Credentials] credentials
# @return [Tempfile]
# @return [IO]
def self.fetch(credentials: Credentials)
client = HTTPClient.new(credentials: credentials)
client.fetch 'ntrod/SupportingFileAuthenticate?type=SMART'
Expand Down
10 changes: 2 additions & 8 deletions spec/rail_feeds/network_rail/http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,10 @@
end

it 'When uri.open gives a StringIO' do
string_io = StringIO.new 'TEST DATA'
string_io = double StringIO
expect(URI).to receive(:parse).with('https://datafeeds.networkrail.co.uk/path').and_return(uri)
expect(uri).to receive(:open).and_return(string_io)

# Contents of Tempfile should be what is in the returned StringIO
expect(Tempfile).to receive(:open).and_return(temp_file)
expect(temp_file).to receive(:write).with(string_io)
expect(temp_file).to receive(:rewind)

expect { |a| subject.fetch('path', &a) }.to yield_with_args(temp_file)
expect { |a| subject.fetch('path', &a) }.to yield_with_args(string_io)
end
end

Expand Down

0 comments on commit 77f26d4

Please sign in to comment.