class RailFeeds::NetworkRail::Schedule::Fetcher

A class for fetching the schedule data files.

Public Class Methods

new(credentials: Credentials, logger: nil) click to toggle source

Initialize a new schedule @param [RailFeeds::NetworkRail::Credentials] credentials

The credentials for connecting to the feed.

@param [Logger, nil] logger

The logger for outputting events, if nil the global logger will be used.
# File lib/rail_feeds/network_rail/schedule/fetcher.rb, line 15
def initialize(credentials: Credentials, logger: nil)
  @credentials = credentials
  self.logger = logger unless logger.nil?
end

Public Instance Methods

download_all_full(format, file) click to toggle source

Download the full schedule. @param [:json, :cif] format

The format to download the schedule in.

@param [String] file

The path to the file to save the .json.gz / .cif.gz download in.
# File lib/rail_feeds/network_rail/schedule/fetcher.rb, line 25
def download_all_full(format, file)
  download 'ALL', 'full', format, file
end
download_all_update(day, format, file) click to toggle source

Download the daily update to the full schedule. @param [String, to_s] day

The day to get the update schedule for ("mon", "tue", "wed", ...).
Defaults to the current day.

@param [:json, :cif] format

The format to fetch the schedule in.

@param [String] file

The path to the file to save the .json.gz / .cif.gz download in.
# File lib/rail_feeds/network_rail/schedule/fetcher.rb, line 37
def download_all_update(day, format, file)
  download 'ALL', day, format, file
end
download_freight_full(file) click to toggle source

Fetch the freight schedule. @param [String] file

The path to the file to save the .json.gz download in.
# File lib/rail_feeds/network_rail/schedule/fetcher.rb, line 44
def download_freight_full(file)
  download 'FREIGHT', 'full', :json, file
end
download_freight_update(day, file) click to toggle source

Fetch the daily update to the freight schedule. @param [String, to_s] day

The day to get the update schedule for ("mon", "tue", "wed", ...).
Defaults to the current day.

@param [String] file

The path to the file to save the .json.gz download in.
# File lib/rail_feeds/network_rail/schedule/fetcher.rb, line 54
def download_freight_update(day, file)
  download 'FREIGHT', day, :json, file
end
download_toc_full(toc, file) click to toggle source

Fetch the schedule for a TOC. @param [String, to_s, nil] toc

The TOC to get the schedule for.

@param [String] file

The path to the file to save the .json.gz download in.
# File lib/rail_feeds/network_rail/schedule/fetcher.rb, line 63
def download_toc_full(toc, file)
  download toc, 'full', :json, file
end
download_toc_update(toc, day, file) click to toggle source

Fetch the daily update for a TOC. @param [String, to_s, nil] toc

The TOC to get the schedule for.

@param [String, to_s] day

The day to get the update schedule for ("mon", "tue", "wed", ...).
Defaults to the current day.

@param [String] file

The path to the file to save the .json.gz download in.
# File lib/rail_feeds/network_rail/schedule/fetcher.rb, line 75
def download_toc_update(toc, day, file)
  download toc, day, :json, file
end
fetch_all_full(format, &block) click to toggle source

Fetch the full schedule. @param [:json, :cif] format

The format to fetch the schedule in.

@yield [file] Once the block has run the temp file will be deleted.

@yieldparam [Zlib::GzipReader] file The unzippable content of the file.
# File lib/rail_feeds/network_rail/schedule/fetcher.rb, line 84
def fetch_all_full(format, &block)
  fetch 'ALL', 'full', format, &block
end
fetch_all_update(day, format, &block) click to toggle source

Fetch the daily update to the full schedule. @param [String, to_s] day

The day to get the update schedule for ("mon", "tue", "wed", ...).
Defaults to the current day.

@param [:json, :cif] format

The format to fetch the schedule in.

@yield [file] Once the block has run the temp file will be deleted.

@yieldparam [Zlib::GzipReader] file The unzippable content of the file.
# File lib/rail_feeds/network_rail/schedule/fetcher.rb, line 96
def fetch_all_update(day, format, &block)
  fetch 'ALL', day, format, &block
end
fetch_freight_full(&block) click to toggle source

Fetch the freight schedule. @yield [file] Once the block has run the temp file will be deleted.

@yieldparam [Zlib::GzipReader] file The unzippable content of the file.
# File lib/rail_feeds/network_rail/schedule/fetcher.rb, line 103
def fetch_freight_full(&block)
  fetch 'FREIGHT', 'full', :json, &block
end
fetch_freight_update(day, &block) click to toggle source

Fetch the daily update to the freight schedule. @param [String, to_s] day

The day to get the update schedule for ("mon", "tue", "wed", ...).
Defaults to the current day.

@yield [file] Once the block has run the temp file will be deleted.

@yieldparam [TempFile] file The unzippable content of the file.
# File lib/rail_feeds/network_rail/schedule/fetcher.rb, line 113
def fetch_freight_update(day, &block)
  fetch 'FREIGHT', day, :json, &block
end
fetch_toc_full(toc, &block) click to toggle source

Fetch the schedule for a TOC. @param [String, to_s, nil] toc

The TOC to get the schedule for.

@yield [file] Once the block has run the temp file will be deleted.

@yieldparam [Zlib::GzipReader] file The unzippable content of the file.
# File lib/rail_feeds/network_rail/schedule/fetcher.rb, line 122
def fetch_toc_full(toc, &block)
  fetch toc, 'full', :json, &block
end
fetch_toc_update(toc, day, &block) click to toggle source

Fetch the daily update for a TOC. @param [String, to_s, nil] toc

The TOC to get the schedule for.

@param [String, to_s] day

The day to get the update schedule for ("mon", "tue", "wed", ...).
Defaults to the current day.

@yield [file] Once the block has run the temp file will be deleted.

@yieldparam [Zlib::GzipReader] file The unzippable content of the file.
# File lib/rail_feeds/network_rail/schedule/fetcher.rb, line 134
def fetch_toc_update(toc, day, &block)
  fetch toc, day, :json, &block
end

Private Instance Methods

download(toc, day, format, file) click to toggle source

Download a schedule.

# File lib/rail_feeds/network_rail/schedule/fetcher.rb, line 148
def download(toc, day, format, file)
  path = path_for toc, day, format
  client = HTTPClient.new(credentials: @credentials, logger: logger)
  client.download(path, file)
end
fetch(toc, day, format, &block) click to toggle source

Fetch a schedule.

# File lib/rail_feeds/network_rail/schedule/fetcher.rb, line 141
def fetch(toc, day, format, &block)
  path = path_for toc, day, format
  client = HTTPClient.new(credentials: @credentials, logger: logger)
  client.fetch_unzipped(path, &block)
end
path_for(toc, day, format) click to toggle source

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength rubocop:disable Metrics/PerceivedComplexity Get the path for a schedule

# File lib/rail_feeds/network_rail/schedule/fetcher.rb, line 158
def path_for(toc, day, format)
  toc = "#{toc}_TOC" unless %w[ALL FREIGHT].include?(toc)

  if format.eql?(:cif)
    unless toc.eql?('ALL')
      fail ArgumentError, 'CIF format is only available for the all schedule'
    end
  else
    unless format.eql?(:json)
      fail ArgumentError, 'format must be either :json or :cif'
    end
  end

  if day.eql?('full')
    day = 'toc-full'
    type = "CIF_#{toc}_FULL_DAILY"
  else
    unless %w[mon tue wed thu fri sat sun].include?(day)
      fail ArgumentError, 'day is invalid'
    end

    day = "toc-update-#{day}"
    type = "CIF_#{toc}_UPDATE_DAILY"
  end

  path = "ntrod/CifFileAuthenticate?type=#{type}&day=#{day}"
  format.eql?(:cif) ? "#{path}.CIF.gz" : path
end