Class: RailFeeds::NetworkRail::Schedule::Parser

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/rail_feeds/network_rail/schedule/parser.rb,
lib/rail_feeds/network_rail/schedule/parser/cif.rb,
lib/rail_feeds/network_rail/schedule/parser/json.rb

Overview

A parent class for parsing schedule data read from schedule file(s). Children need to implement a parse_line method.

Direct Known Subclasses

CIF, JSON

Defined Under Namespace

Classes: CIF, JSON

Instance Method Summary collapse

Methods included from Logging

formatter, included, #logger, logger, #logger=, logger=

Constructor Details

#initialize(logger: nil, on_header: nil, on_trailer: nil, on_comment: nil, on_tiploc_create: nil, on_tiploc_update: nil, on_tiploc_delete: nil, on_association_create: nil, on_association_update: nil, on_association_delete: nil, on_train_schedule_create: nil, on_train_schedule_update: nil, on_train_schedule_delete: nil) ⇒ Parser

rubocop:disable Metrics/ParameterLists Initialize a new data.

Parameters:

  • logger (Logger, nil) (defaults to: nil)

    The logger for outputting events, if nil the global logger will be used.

  • on_header (Proc, #call) (defaults to: nil)

    The proc to call when the header is received. Passes self and a RailFeeds::NetworkRail::Schedule::Header.

  • on_tiploc_create (Proc, #call) (defaults to: nil)

    The proc to call when a tiploc insertion is received. Passes self and a RailFeeds::NetworkRail::Schedule::Tiploc::Insert.

  • on_tiploc_update (Proc, #call) (defaults to: nil)

    The proc to call when an amendment to an existing tiploc is received. Passes self, tiploc_id and a RailFeeds::NetworkRail::Schedule::Tiploc::Ammend.

  • on_tiploc_delete (Proc, #call) (defaults to: nil)

    The proc to call when an existing tiploc should be deleted. Passes self and a tiploc_id.

  • on_association_create (Proc, #call) (defaults to: nil)

    The proc to call when a new association is received. Passes self and a RailFeeds::NetworkRail::Schedule::Association.

  • on_association_update (Proc, #call) (defaults to: nil)

    The proc to call when a revision to an existing association is received. Passes self and a RailFeeds::NetworkRail::Schedule::Association.

  • on_association_delete (Proc, #call) (defaults to: nil)

    The proc to call when an existing association should be deleted. Passes self and a RailFeeds::NetworkRail::Schedule::Association.

  • on_train_schedule_create (Proc, #call) (defaults to: nil)

    The proc to call when a new train schedule is received. Passes self and a RailFeeds::NetworkRail::Schedule::TrainSchedule::New.

  • on_train_schedule_update (Proc, #call) (defaults to: nil)

    The proc to call when a revision to an existing train schedule is received. Passes self and a RailFeeds::NetworkRail::Schedule::TrainSchedule::Revise.

  • on_train_schedule_delete (Proc, #call) (defaults to: nil)

    The proc to call when an existing train schedule should be deleted. Passes self and a RailFeeds::NetworkRail::Schedule::TrainSchedule::Delete.

  • on_trailer (Proc, #call) (defaults to: nil)

    The proc to call when the trailer (end of file record) is received. Passes self.

  • on_comment (Proc, #call) (defaults to: nil)

    The proc to call when a comment is received. Passes self and a String.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rail_feeds/network_rail/schedule/parser.rb', line 51

def initialize(
  logger: nil,
  on_header: nil, on_trailer: nil, on_comment: nil,
  on_tiploc_create: nil, on_tiploc_update: nil, on_tiploc_delete: nil,
  on_association_create: nil, on_association_update: nil,
  on_association_delete: nil, on_train_schedule_create: nil,
  on_train_schedule_update: nil, on_train_schedule_delete: nil
)
  self.logger = logger unless logger.nil?
  @on_header = on_header
  @on_trailer = on_trailer
  @on_tiploc_create = on_tiploc_create
  @on_tiploc_update = on_tiploc_update
  @on_tiploc_delete = on_tiploc_delete
  @on_association_create = on_association_create
  @on_association_update = on_association_update
  @on_association_delete = on_association_delete
  @on_train_schedule_create = on_train_schedule_create
  @on_train_schedule_update = on_train_schedule_update
  @on_train_schedule_delete = on_train_schedule_delete
  @on_comment = on_comment
end

Instance Method Details

#parse_file(file) ⇒ Object

Parse the data in file.

Parameters:

  • file (IO)

    The file to load data from.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rail_feeds/network_rail/schedule/parser.rb', line 78

def parse_file(file)
  @file_ended = false
  @stop_parsing = false

  file.each_line do |line|
    parse_line line

    if @stop_parsing
      logger.debug "Parsing of file #{file} was stopped."
      break
    end
  end

  fail "File is incomplete. #{file}" unless @stop_parsing || @file_ended
end

#parse_line(_line) ⇒ Object



94
95
96
# File 'lib/rail_feeds/network_rail/schedule/parser.rb', line 94

def parse_line(_line)
  fail 'parse_file MUST be implemented in the child class.'
end

#stop_parsingObject

Stop parsing the current file.



99
100
101
# File 'lib/rail_feeds/network_rail/schedule/parser.rb', line 99

def stop_parsing
  @stop_parsing = true
end