Class: RailFeeds::NetworkRail::Schedule::Data
- Inherits:
-
Object
- Object
- RailFeeds::NetworkRail::Schedule::Data
- Includes:
- Logging
- Defined in:
- lib/rail_feeds/network_rail/schedule/data.rb
Overview
rubocop:disable Metrics/ClassLength A class for holding schedule data read from schedule file(s).
Instance Attribute Summary collapse
-
#associations ⇒ RailFeeds::NetworkRail::Schedule::Header::CIF, ...
Schedules grouped by the train’s UID.
-
#last_header ⇒ RailFeeds::NetworkRail::Schedule::Header::CIF, ...
Schedules grouped by the train’s UID.
-
#last_header The last header added.(Thelastheaderadded.) ⇒ RailFeeds::NetworkRail::Schedule::Header::CIF, ...
readonly
Schedules grouped by the train’s UID.
-
#tiplocs ⇒ RailFeeds::NetworkRail::Schedule::Header::CIF, ...
Schedules grouped by the train’s UID.
-
#trains ⇒ RailFeeds::NetworkRail::Schedule::Header::CIF, ...
Schedules grouped by the train’s UID.
Instance Method Summary collapse
-
#fetch_data(credentials = Credentials) ⇒ RailFeeds::NetworkRail::Schedule::Header::CIF
Fetch data over the web.
-
#generate_cif {|"/!! Start of file\n"| ... } ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength Get the contained data in CIF format Expects a block to receive each line.
-
#initialize(logger: nil) ⇒ Data
constructor
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength Initialize a new data.
-
#load_cif_file(file) ⇒ Object
Load data files into the parser, of types: * Full CIF file - the data will be replaced * Update CIF file - the data will be changed.
Methods included from Logging
formatter, included, #logger, logger, #logger=, logger=
Constructor Details
#initialize(logger: nil) ⇒ Data
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength Initialize a new data.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rail_feeds/network_rail/schedule/data.rb', line 28 def initialize(logger: nil) self.logger = logger unless logger.nil? @parser = Parser::CIF.new( logger: logger, on_header: proc { |*args| do_header(*args) }, on_trailer: proc { |*args| do_trailer(*args) }, on_tiploc_create: proc { |*args| do_tiploc_create(*args) }, on_tiploc_update: proc { |*args| do_tiploc_update(*args) }, on_tiploc_delete: proc { |*args| do_tiploc_delete(*args) }, on_association_create: proc { |*args| do_association_create(*args) }, on_association_update: proc { |*args| do_association_update(*args) }, on_association_delete: proc { |*args| do_association_delete(*args) }, on_train_schedule_create: proc { |*args| do_train_schedule_create(*args) }, on_train_schedule_update: proc { |*args| do_train_schedule_update(*args) }, on_train_schedule_delete: proc { |*args| do_train_schedule_delete(*args) } ) reset_data end |
Instance Attribute Details
#associations ⇒ RailFeeds::NetworkRail::Schedule::Header::CIF, ...
Schedules grouped by the train’s UID
21 |
# File 'lib/rail_feeds/network_rail/schedule/data.rb', line 21 attr_accessor :last_header, :associations, :tiplocs, :trains |
#last_header ⇒ RailFeeds::NetworkRail::Schedule::Header::CIF, ...
Schedules grouped by the train’s UID
21 22 23 |
# File 'lib/rail_feeds/network_rail/schedule/data.rb', line 21 def last_header @last_header end |
#last_header The last header added.(Thelastheaderadded.) ⇒ RailFeeds::NetworkRail::Schedule::Header::CIF, ... (readonly)
Schedules grouped by the train’s UID
21 |
# File 'lib/rail_feeds/network_rail/schedule/data.rb', line 21 attr_accessor :last_header, :associations, :tiplocs, :trains |
#tiplocs ⇒ RailFeeds::NetworkRail::Schedule::Header::CIF, ...
Schedules grouped by the train’s UID
21 |
# File 'lib/rail_feeds/network_rail/schedule/data.rb', line 21 attr_accessor :last_header, :associations, :tiplocs, :trains |
#trains ⇒ RailFeeds::NetworkRail::Schedule::Header::CIF, ...
Schedules grouped by the train’s UID
21 |
# File 'lib/rail_feeds/network_rail/schedule/data.rb', line 21 attr_accessor :last_header, :associations, :tiplocs, :trains |
Instance Method Details
#fetch_data(credentials = Credentials) ⇒ RailFeeds::NetworkRail::Schedule::Header::CIF
Fetch data over the web. Gets the feed of all trains.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rail_feeds/network_rail/schedule/data.rb', line 95 def fetch_data(credentials = Credentials) fetcher = Fetcher.new credentials: credentials method = if last_header.nil? || last_header.extracted_at.to_date < Date.today - 6 # Need to get a full andthen updates :fetch_all else # Can only get updates :fetch_all_updates end fetcher.send(method, :cif) do |file| load_cif_file file end end |
#generate_cif {|"/!! Start of file\n"| ... } ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength Get the contained data in CIF format Expects a block to receive each line
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/rail_feeds/network_rail/schedule/data.rb', line 65 def generate_cif fail 'No loaded data' if last_header.nil? header = Header::CIF.new( extracted_at: last_header.extracted_at, update_indicator: 'F', start_date: last_header.start_date, end_date: last_header.end_date ) yield "/!! Start of file\n" yield "/!! Generated: #{header.extracted_at.utc&.strftime('%d/%m/%Y %H:%M')}\n" yield header.to_cif tiplocs.values.sort.each { |tiploc| yield tiploc.to_cif } associations.values.sort.each { |association| yield association.to_cif } trains.values.flatten.sort.each do |train_schedule| train_schedule.to_cif.each_line { |line| yield line } end yield "ZZ#{' ' * 78}\n" yield "/!! End of file\n" end |
#load_cif_file(file) ⇒ Object
Load data files into the parser, of types:
* Full CIF file - the data will be replaced
* Update CIF file - the data will be changed
54 55 56 57 58 59 |
# File 'lib/rail_feeds/network_rail/schedule/data.rb', line 54 def load_cif_file(file) @parser.parse_file file logger.info "Currently have #{associations.count} associations, " \ "#{tiplocs.count} tiplocs, #{trains.count} trains." end |