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

Commit

Permalink
Enhance consistency of passing credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Gauld committed Jul 24, 2018
1 parent 773792a commit 03018a4
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 34 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Version 0.0.2

* Sort API consistency - when passing credentials use positional not keywork arguments, except:
* Initializing a new HTTP or Stomp client
* Initializing a new NetworkRail Schedule Fetcher
* Add National rail - Knowledge base - National service indicator via HTTP.
* Fix issue 1: OpenURI returns a StringIO not TempFile when size is under 10KB.

Expand Down
10 changes: 7 additions & 3 deletions doc/guides/Network Rail/CORPUS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Network Rail CORPUS (Codes for Operations, Retail & Planning – a Unified Solution)
# Network Rail - CORPUS (Codes for Operations, Retail & Planning – a Unified Solution)

The CORPUS data is a list of indentification information for locations around the network.
See <https://wiki.openraildata.com/index.php/Reference_Data#CORPUS:_Location_Reference_Data>
Expand All @@ -16,13 +16,17 @@ You'll get an array of a data struct with the following attributes:

```ruby
# Download the CORPUS data and get the data from it
RailFeeds::NetworkRail::Credentials.configure(
username: 'YOUR USERNAME HERE',
password: 'YOUR PASSWORD HERE'
)
temp_file = RailFeeds::NetworkRail::CORPUS.fetch
data = RailFeeds::NetworkRail::CORPUS.load_file(temp_file.path)
data = RailFeeds::NetworkRail::CORPUS.load_file(temp_file)

# Get data from a previously saved file
data = RailFeeds::NetworkRail::CORPUS.load_file('PATH TO FILE.json.gz')

# Get data from a previously saved and extracted file
# Get data from a previously saved and unzipped file
data = RailFeeds::NetworkRail::CORPUS.load_file('PATH TO FILE.json')

# Get data by fetching it from the web
Expand Down
8 changes: 6 additions & 2 deletions doc/guides/Network Rail/SMART.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Network Rail SMART
# Network Rail - SMART

The SMART data is a periodically updated list of mappings between train describer berths
and locations, allowing for TD events to be translated into arrival/departure from location
Expand All @@ -18,8 +18,12 @@ reachable in each direction.

```ruby
# Download the SMART data and get the data from it
RailFeeds::NetworkRail::Credentials.configure(
username: 'YOUR USERNAME HERE',
password: 'YOUR PASSWORD HERE'
)
temp_file = RailFeeds::NetworkRail::SMART.fetch
step_data = RailFeeds::NetworkRail::SMART.load_file(temp_file.path)
step_data = RailFeeds::NetworkRail::SMART.load_file(temp_file)

# Get the SMART data from a previously saved file
step_data = RailFeeds::NetworkRail::SMART.load_file('PATH TO FILE.json.gz')
Expand Down
2 changes: 1 addition & 1 deletion doc/guides/Network Rail/Schedule.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Network Rail Schedule
# Network Rail - Schedule

The schedule files from Network Rail contain:

Expand Down
2 changes: 1 addition & 1 deletion lib/rail_feeds/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class HTTPClient
# The credentials for connecting to the feed.
# @param [Logger] logger
# The logger for outputting evetns, if nil the global logger will be used.
def initialize(credentials = nil, logger: nil)
def initialize(credentials: nil, logger: nil)
@credentials = credentials
self.logger = logger unless logger.nil?
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rail_feeds/national_rail/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module RailFeeds
module NationalRail
# A wrapper class for ::Net::HTTP
class HTTPClient < RailFeeds::HTTPClient
def initialize(credentials = nil, **args)
def initialize(credentials: nil, **args)
credentials ||= RailFeeds::NationalRail::Credentials
super
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ def to_s
# @param [RailFeeds::NationalRail::Credentials] credentials
# @param [String] file
# The path to the file to save the .xml download in.
def self.download(file, credentials: Credentials)
client = HTTPClient.new(credentials)
def self.download(file, credentials = Credentials)
client = HTTPClient.new(credentials: credentials)
client.download 'darwin/api/staticfeeds/4.0/serviceIndicators', file
end

# Fetch the current data.
# @param [RailFeeds::NationalRail::Credentials] credentials
# @return [IO]
def self.fetch(credentials: Credentials, &block)
client = HTTPClient.new(credentials)
def self.fetch(credentials = Credentials, &block)
client = HTTPClient.new(credentials: credentials)
client.fetch 'darwin/api/staticfeeds/4.0/serviceIndicators', &block
end

Expand All @@ -60,7 +60,7 @@ def self.load_file(file)
# The credentials to authenticate with.
# @return
# [Array<RailFeeds::NationalRail::KnowledgeBase::NationalServiceIndicator::TOC>]
def self.fetch_data(credentials: Credentials)
def self.fetch_data(credentials = Credentials)
fetch(credentials: credentials) do |file|
parse_xml file.read
end
Expand Down
6 changes: 3 additions & 3 deletions lib/rail_feeds/network_rail/corpus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ module CORPUS
# @param [RailFeeds::NetworkRail::Credentials] credentials
# @param [String] file
# The path to the file to save the .json.gz download in.
def self.download(file, credentials: Credentials)
def self.download(file, credentials = Credentials)
client = HTTPClient.new(credentials: credentials)
client.download 'ntrod/SupportingFileAuthenticate?type=CORPUS', file
end

# Fetch the current CORPUS data.
# @param [RailFeeds::NetworkRail::Credentials] credentials
# @return [IO]
def self.fetch(credentials: Credentials)
def self.fetch(credentials = Credentials)
client = HTTPClient.new(credentials: credentials)
client.fetch 'ntrod/SupportingFileAuthenticate?type=CORPUS'
end
Expand All @@ -42,7 +42,7 @@ def self.load_file(file)
# @param [RailFeeds::NetworkRail::Credentials] credentials
# The credentials to authenticate with.
# @return [Array<RailFeeds::NetworkRail::CORPUS::Data>]
def self.fetch_data(credentials: Credentials)
def self.fetch_data(credentials = Credentials)
client = HTTPClient.new(credentials: credentials)
client.fetch_unzipped('ntrod/SupportingFileAuthenticate?type=CORPUS') do |file|
break parse_json file.read
Expand Down
2 changes: 1 addition & 1 deletion lib/rail_feeds/network_rail/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module RailFeeds
module NetworkRail
# A wrapper class for ::Net::HTTP
class HTTPClient < RailFeeds::HTTPClient
def initialize(credentials = nil, **args)
def initialize(credentials: nil, **args)
credentials ||= RailFeeds::NetworkRail::Credentials
super
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rail_feeds/network_rail/schedule/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def generate_cif
# The credentials for connecting to the feed.
# @return [RailFeeds::NetworkRail::Schedule::Header::CIF]
# The header of the last file added.
def fetch_data(credentials: Credentials)
def fetch_data(credentials = Credentials)
fetcher = Fetcher.new credentials: credentials

method = if last_header.nil? ||
Expand Down
6 changes: 3 additions & 3 deletions lib/rail_feeds/network_rail/smart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ def to_direction
# @param [RailFeeds::NetworkRail::Credentials] credentials
# @param [String] file
# The path to the file to save the .json.gz download in.
def self.download(file, credentials: Credentials)
def self.download(file, credentials = Credentials)
client = HTTPClient.new(credentials: credentials)
client.download 'ntrod/SupportingFileAuthenticate?type=SMART', file
end

# Fetch the current SMART data.
# @param [RailFeeds::NetworkRail::Credentials] credentials
# @return [IO]
def self.fetch(credentials: Credentials)
def self.fetch(credentials = Credentials)
client = HTTPClient.new(credentials: credentials)
client.fetch 'ntrod/SupportingFileAuthenticate?type=SMART'
end
Expand All @@ -60,7 +60,7 @@ def self.load_file(file)
# @param [RailFeeds::NetworkRail::Credentials] credentials
# The credentials to authenticate with.
# @return [Array<RailFeeds::NetworkRail::SMART::Step>]
def self.fetch_data(credentials: Credentials)
def self.fetch_data(credentials = Credentials)
client = HTTPClient.new(credentials: credentials)
client.fetch_unzipped('ntrod/SupportingFileAuthenticate?type=SMART') do |file|
break parse_json file.read
Expand Down
4 changes: 2 additions & 2 deletions spec/rail_feeds/http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
expect(uri).to receive(:open)
.with(http_basic_authentication: ['user', 'pass'])
.and_return(temp_file)
subject = described_class.new credentials
subject = described_class.new credentials: credentials
subject.fetch('path') {}
end

Expand All @@ -51,7 +51,7 @@
)
expect(URI).to receive(:parse).and_return(uri)
expect(uri).to receive(:open).and_return(temp_file)
subject = described_class.new credentials
subject = described_class.new credentials: credentials
expect { subject.fetch('path') {} }.to_not raise_error
end

Expand Down
2 changes: 1 addition & 1 deletion spec/rail_feeds/national_rail/http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
end

it 'Getting the token' do
subject = described_class.new(RailFeeds::Credentials.new(username: '', password: ''))
subject = described_class.new(credentials: RailFeeds::Credentials.new(username: '', password: ''))
response = double Net::HTTPCreated
expect(response).to receive(:value)
expect(response).to receive(:body).and_return('{"token":"TOKEN"}')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
describe '::download' do
it 'Using default credentials' do
expect(RailFeeds::NationalRail::HTTPClient).to receive(:new)
.with(RailFeeds::NationalRail::Credentials).and_return(http_client)
.with(credentials: RailFeeds::NationalRail::Credentials).and_return(http_client)
expect(http_client).to receive(:download)
.with('darwin/api/staticfeeds/4.0/serviceIndicators', 'file')
described_class.download 'file'
Expand All @@ -47,16 +47,16 @@
it 'Using passed credentials' do
credentials = double RailFeeds::NationalRail::Credentials
expect(RailFeeds::NationalRail::HTTPClient).to receive(:new)
.with(credentials).and_return(http_client)
.with(credentials: credentials).and_return(http_client)
expect(http_client).to receive(:download)
described_class.download 'file', credentials: credentials
described_class.download 'file', credentials
end
end

describe '::fetch' do
it 'Using default credentials' do
expect(RailFeeds::NationalRail::HTTPClient).to receive(:new)
.with(RailFeeds::NationalRail::Credentials).and_return(http_client)
.with(credentials: RailFeeds::NationalRail::Credentials).and_return(http_client)
expect(http_client).to receive(:fetch)
.with('darwin/api/staticfeeds/4.0/serviceIndicators').and_return(temp_file)
expect(described_class.fetch).to eq temp_file
Expand All @@ -65,9 +65,9 @@
it 'Using passed credentials' do
credentials = double RailFeeds::NationalRail::Credentials
expect(RailFeeds::NationalRail::HTTPClient).to receive(:new)
.with(credentials).and_return(http_client)
.with(credentials: credentials).and_return(http_client)
expect(http_client).to receive(:fetch).and_return(temp_file)
expect(described_class.fetch(credentials: credentials)).to eq temp_file
expect(described_class.fetch(credentials)).to eq temp_file
end
end

Expand Down Expand Up @@ -106,4 +106,17 @@
expect(described_class).to receive(:parse_xml).with(xml)
described_class.fetch_data
end

it 'Converts to string' do
expect(File).to receive(:read).with('filename').and_return(xml)
data = described_class.load_file('filename')
expect(data[0].to_s).to eq "AW - Arriva Trains Wales\n" \
"Good service - - icon-tick2.png\n\n" \
'@ArrivaTW - Follow us on Twitter'
expect(data[1].to_s).to eq "WM - West Midlands Railway\n" \
"Major delays on some routes - An amended service is in operation - icon-disruption.png\n" \
"Aston - Read about this disruption\n" \
"36DB32F7EB7F40ACACFF1D5CF7572D4C http://www.nationalrail.co.uk/\n" \
'@WestMidRailway - Latest travel news'
end
end
4 changes: 2 additions & 2 deletions spec/rail_feeds/network_rail/corpus_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
expect(RailFeeds::NetworkRail::HTTPClient).to receive(:new)
.with(credentials: credentials).and_return(http_client)
expect(http_client).to receive(:download)
described_class.download 'file', credentials: credentials
described_class.download 'file', credentials
end
end

Expand All @@ -47,7 +47,7 @@
expect(RailFeeds::NetworkRail::HTTPClient).to receive(:new)
.with(credentials: credentials).and_return(http_client)
expect(http_client).to receive(:fetch).and_return(temp_file)
expect(described_class.fetch(credentials: credentials)).to eq temp_file
expect(described_class.fetch(credentials)).to eq temp_file
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/rail_feeds/network_rail/smart_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
expect(RailFeeds::NetworkRail::HTTPClient).to receive(:new)
.with(credentials: credentials).and_return(http_client)
expect(http_client).to receive(:download)
described_class.download 'file', credentials: credentials
described_class.download 'file', credentials
end
end

Expand All @@ -52,7 +52,7 @@
expect(RailFeeds::NetworkRail::HTTPClient).to receive(:new)
.with(credentials: credentials).and_return(http_client)
expect(http_client).to receive(:fetch).and_return(temp_file)
expect(described_class.fetch(credentials: credentials)).to eq temp_file
expect(described_class.fetch(credentials)).to eq temp_file
end
end

Expand Down

0 comments on commit 03018a4

Please sign in to comment.