module RailFeeds::NationalRail::KnowledgeBase::NationalServiceIndicator

A module for accessing the national service indicator from the national rail knowledge base.

Constants

ServiceGroup
Status
TOC

Public Class Methods

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

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

The path to the file to save the .xml download in.
# File lib/rail_feeds/national_rail/knowledge_base/national_service_indicator.rb, line 35
def self.download(file, credentials = Credentials)
  client = HTTPClient.new(credentials: credentials)
  client.download 'api/staticfeeds/4.0/serviceIndicators', file
end
fetch(credentials = Credentials, &block) click to toggle source

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

# File lib/rail_feeds/national_rail/knowledge_base/national_service_indicator.rb, line 43
def self.fetch(credentials = Credentials, &block)
  client = HTTPClient.new(credentials: credentials)
  client.fetch 'api/staticfeeds/4.0/serviceIndicators', &block
end
fetch_data(credentials = Credentials) click to toggle source

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

The credentials to authenticate with.

@return

[Array<RailFeeds::NationalRail::KnowledgeBase::NationalServiceIndicator::TOC>]
# File lib/rail_feeds/national_rail/knowledge_base/national_service_indicator.rb, line 61
def self.fetch_data(credentials = Credentials)
  fetch(credentials: credentials) do |file|
    parse_xml file.read
  end
end
load_file(file) click to toggle source

Load data from either a .json or .json.gz file. @param [String] file The path of the file to open. @return

[Array<RailFeeds::NationalRail::KnowledgeBase::NationalServiceIndicator::TOC>]
# File lib/rail_feeds/national_rail/knowledge_base/national_service_indicator.rb, line 52
def self.load_file(file)
  parse_xml File.read(file)
end

Private Class Methods

parse_xml(xml) click to toggle source

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength

# File lib/rail_feeds/national_rail/knowledge_base/national_service_indicator.rb, line 69
def self.parse_xml(xml)
  options = Nokogiri::XML::ParseOptions.new.nonet.noent.noblanks
  doc = Nokogiri::XML.parse(xml, nil, nil, options)
  doc.xpath('/xmlns:NSI/xmlns:TOC').map do |toc_node|
    TOC.new(
      toc_node.xpath('./xmlns:TocCode').first&.content,
      toc_node.xpath('./xmlns:TocName').first&.content,
      Status.new(
        toc_node.xpath('./xmlns:Status').first&.content,
        toc_node.xpath('./xmlns:StatusDescription').first&.content,
        toc_node.xpath('./xmlns:StatusImage').first&.content
      ),
      toc_node.xpath('./xmlns:TwitterAccount').first&.content,
      toc_node.xpath('./xmlns:AdditionalInfo').first&.content,
      toc_node.xpath('./xmlns:ServiceGroup').map do |service_group|
        ServiceGroup.new(
          service_group.xpath('./xmlns:CurrentDisruption').first&.content,
          service_group.xpath('./xmlns:GroupName').first&.content,
          service_group.xpath('./xmlns:CustomDetail').first&.content,
          service_group.xpath('./xmlns:CustomURL').first&.content
        )
      end
    )
  end
end