module RailFeeds::NetworkRail::CORPUS

A module for getting information out of the CORPUS data.

Constants

Data

Public Class Methods

download(file, credentials = Credentials) click to toggle source

Download the current CORPUS data. @param [RailFeeds::NetworkRail::Credentials] credentials @param [String] file

The path to the file to save the .json.gz download in.
# File lib/rail_feeds/network_rail/corpus.rb, line 15
def self.download(file, credentials = Credentials)
  client = HTTPClient.new(credentials: credentials)
  client.download 'ntrod/SupportingFileAuthenticate?type=CORPUS', file
end
fetch(credentials = Credentials) click to toggle source

Fetch the current CORPUS data. @param [RailFeeds::NetworkRail::Credentials] credentials @return [IO]

# File lib/rail_feeds/network_rail/corpus.rb, line 23
def self.fetch(credentials = Credentials)
  client = HTTPClient.new(credentials: credentials)
  client.fetch 'ntrod/SupportingFileAuthenticate?type=CORPUS'
end
fetch_data(credentials = Credentials) click to toggle source

Load CORPUS data from the internet. @param [RailFeeds::NetworkRail::Credentials] credentials

The credentials to authenticate with.

@return [Array<RailFeeds::NetworkRail::CORPUS::Data>]

# File lib/rail_feeds/network_rail/corpus.rb, line 43
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
  end
end
load_file(file) click to toggle source

Load CORPUS data from either a .json or .json.gz file. @param [String] file The path of the file to open. @return [Array<RailFeeds::NetworkRail::CORPUS::Data>]

# File lib/rail_feeds/network_rail/corpus.rb, line 31
def self.load_file(file)
  Zlib::GzipReader.open(file) do |gz|
    parse_json gz.read
  end
rescue Zlib::GzipFile::Error
  parse_json File.read(file)
end

Private Class Methods

nilify(item) click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/rail_feeds/network_rail/corpus.rb, line 68
def self.nilify(item)
  return nil if item.nil? || item.empty?

  item
end
parse_json(json) click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/rail_feeds/network_rail/corpus.rb, line 51
def self.parse_json(json)
  data = JSON.parse json
  data['TIPLOCDATA'].map do |item|
    Data.new(
      nilify(item['TIPLOC']&.strip),
      nilify(item['STANOX']&.strip)&.to_i,
      nilify(item['3ALPHA']&.strip),
      nilify(item['UIC']&.strip)&.to_i,
      nilify(item['NLC']&.strip),
      nilify(item['NLCDESC']&.strip),
      nilify(item['NLCDESC16']&.strip)
    )
  end
end