Class: RailFeeds::NetworkRail::Schedule::Association

Inherits:
Object
  • Object
show all
Includes:
Comparable, Days, STPIndicator
Defined in:
lib/rail_feeds/network_rail/schedule/association.rb

Overview

rubocop:disable Metrics/ClassLength A class for holding information about an association between many trains.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from STPIndicator

included

Methods included from Days

#fridays?, included, #mondays?, #saturdays?, #sundays?, #thursdays?, #tuesdays?, #wednesdays?

Constructor Details

#initialize(**attributes) ⇒ Association

Attributes from modules :days, :stp_indicator



56
57
58
59
60
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 56

def initialize(**attributes)
  attributes.each do |attribute, value|
    send "#{attribute}=", value
  end
end

Instance Attribute Details

#associated_location_suffixString?

Together with the tiploc uniquely identifies the association on the associated_uid.

Returns:

  • (String, nil)


51
52
53
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51

attr_accessor :main_train_uid, :associated_train_uid, :category,
:start_date, :end_date, :date_indicator,
:tiploc, :main_location_suffix, :associated_location_suffix

#associated_train_uidString

Returns The UID of the associated train in the association.

Returns:

  • (String)

    The UID of the associated train in the association.



51
52
53
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51

attr_accessor :main_train_uid, :associated_train_uid, :category,
:start_date, :end_date, :date_indicator,
:tiploc, :main_location_suffix, :associated_location_suffix

#base_location_suffixString?

Together with the tiploc uniquely identifies the association on the base_uid.

Returns:

  • (String, nil)


51
52
53
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51

attr_accessor :main_train_uid, :associated_train_uid, :category,
:start_date, :end_date, :date_indicator,
:tiploc, :main_location_suffix, :associated_location_suffix

#categoryString

  • JJ - join

  • VV - divide

  • NP - next

Returns:

  • (String)

    The category of the association:



51
52
53
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51

attr_accessor :main_train_uid, :associated_train_uid, :category,
:start_date, :end_date, :date_indicator,
:tiploc, :main_location_suffix, :associated_location_suffix

#date_indicatorString

  • S - same day

  • N - over next midnight

  • P - over previous midnight

Returns:

  • (String)

    When the assocation happens:



51
52
53
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51

attr_accessor :main_train_uid, :associated_train_uid, :category,
:start_date, :end_date, :date_indicator,
:tiploc, :main_location_suffix, :associated_location_suffix

#daysArray<Boolean>

Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

Returns:

  • (Array<Boolean>)

    The days on which the service runs.



51
52
53
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51

attr_accessor :main_train_uid, :associated_train_uid, :category,
:start_date, :end_date, :date_indicator,
:tiploc, :main_location_suffix, :associated_location_suffix

#end_dateDate

Returns When the schedule ends.

Returns:

  • (Date)

    When the schedule ends.



51
52
53
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51

attr_accessor :main_train_uid, :associated_train_uid, :category,
:start_date, :end_date, :date_indicator,
:tiploc, :main_location_suffix, :associated_location_suffix

#main_location_suffixObject

Returns the value of attribute main_location_suffix.



51
52
53
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51

def main_location_suffix
  @main_location_suffix
end

#main_train_uidString

Returns The UID of the main train in the association.

Returns:

  • (String)

    The UID of the main train in the association.



51
52
53
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51

def main_train_uid
  @main_train_uid
end

#start_dateDate

Returns When the schedule starts.

Returns:

  • (Date)

    When the schedule starts.



51
52
53
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51

attr_accessor :main_train_uid, :associated_train_uid, :category,
:start_date, :end_date, :date_indicator,
:tiploc, :main_location_suffix, :associated_location_suffix

#stp_indicatorString

  • C - cancellation of permanent schedule

  • N - new STP schedule

  • O - STP overlay of permanent schedule

  • P - permanent

Returns:

  • (String)


51
52
53
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51

attr_accessor :main_train_uid, :associated_train_uid, :category,
:start_date, :end_date, :date_indicator,
:tiploc, :main_location_suffix, :associated_location_suffix

#tiplocString

Returns The TIPLOC of the location the association occurs.

Returns:

  • (String)

    The TIPLOC of the location the association occurs.



51
52
53
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 51

attr_accessor :main_train_uid, :associated_train_uid, :category,
:start_date, :end_date, :date_indicator,
:tiploc, :main_location_suffix, :associated_location_suffix

Class Method Details

.from_cif(line) ⇒ Object

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength Initialize a new association from a CIF file line



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 65

def self.from_cif(line)
  unless %w[AAN AAR AAD].include?(line[0..2])
    fail ArgumentError, "Invalid line:\n#{line}"
  end

  new(
    main_train_uid: line[3..8].strip,
    associated_train_uid: line[9..14].strip,
    start_date: Schedule.make_date(line[15..20]),
    end_date: Schedule.make_date(line[21..26], allow_nil: line[2].eql?('D')),
    days: days_from_cif(line[27..33]),
    category: Schedule.nil_or_strip(line[34..35]),
    date_indicator: Schedule.nil_or_strip(line[36]),
    tiploc: line[37..43].strip,
    main_location_suffix: Schedule.nil_or_i(line[44]),
    associated_location_suffix: Schedule.nil_or_i(line[45]),
    stp_indicator: stp_indicator_from_cif(line[79])
  )
end

.from_json(line) ⇒ Object

rubocop:disable Metrics/AbcSize Initialize a new association from a JSON file line



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 89

def self.from_json(line)
  data = ::JSON.parse(line)['JsonAssociationV1']

  new(
    main_train_uid: data['main_train_uid'],
    associated_train_uid: data['assoc_train_uid'],
    start_date: Date.parse(data['assoc_start_date']),
    end_date: data['assoc_end_date'] ? Date.parse(data['assoc_end_date']) : nil,
    days: days_from_cif(data['assoc_days']),
    category: Schedule.nil_or_strip(data['category']),
    date_indicator: Schedule.nil_or_strip(data['date_indicator']),
    tiploc: data['location'],
    main_location_suffix: Schedule.nil_or_i(data['base_location_suffix']),
    associated_location_suffix: Schedule.nil_or_i(data['assoc_location_suffix']),
    stp_indicator: stp_indicator_from_cif(data['CIF_stp_indicator'])
  )
end

Instance Method Details

#<=>(other) ⇒ Object



153
154
155
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 153

def <=>(other)
  start_date <=> other&.start_date
end

#==(other) ⇒ Object



148
149
150
151
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 148

def ==(other)
  main_train_event_id == other&.main_train_event_id &&
    associated_train_event_id == other&.associated_train_event_id
end

#associated_train_event_idObject

Uniquely identifies the event on the associated_train_uid



144
145
146
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 144

def associated_train_event_id
  "#{tiploc}-#{associated_location_suffix}"
end

#divide?Boolean

Test if this is a divide association.

Returns:

  • (Boolean)


114
115
116
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 114

def divide?
  category.eql?('VV')
end

#hashObject



157
158
159
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 157

def hash
  "#{tiploc}-#{main_location_suffix}-#{associated_location_suffix}"
end

#join?Boolean

Test if this is a join association.

Returns:

  • (Boolean)


109
110
111
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 109

def join?
  category.eql?('JJ')
end

#main_train_event_idObject

Uniquely identifies the event on the main_train_uid



139
140
141
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 139

def main_train_event_id
  "#{tiploc}-#{main_location_suffix}"
end

#next?Boolean

Test if this is a next association.

Returns:

  • (Boolean)


119
120
121
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 119

def next?
  category.eql?('NP')
end

#over_next_midnight?Boolean

Test if the association happens over the next midnight.

Returns:

  • (Boolean)


129
130
131
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 129

def over_next_midnight?
  date_indicator.eql?('N')
end

#over_previous_midnight?Boolean

Test if the association happens over the previous midnight.

Returns:

  • (Boolean)


134
135
136
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 134

def over_previous_midnight?
  date_indicator.eql?('P')
end

#same_day?Boolean

Test if the association happens on the same day.

Returns:

  • (Boolean)


124
125
126
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 124

def same_day?
  date_indicator.eql?('S')
end

#to_cifObject

rubocop:disable Metrics/AbcSize rubocop:disable Style/FormatStringToken



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 163

def to_cif
  format('%-80.80s', [
    'AAN',
    format('%-6.6s', main_train_uid),
    format('%-6.6s', associated_train_uid),
    format('%-6.6s', start_date&.strftime('%y%m%d')),
    format('%-6.6s', end_date&.strftime('%y%m%d')),
    days_to_cif,
    format('%-2.2s', category),
    format('%-1.1s', date_indicator),
    format('%-7.7s', tiploc),
    format('%-1.1s', main_location_suffix),
    format('%-1.1s', associated_location_suffix),
    'T                                ',
    stp_indicator_to_cif
  ].join) + "\n"
end

#to_json(**opts) ⇒ Object

rubocop:disable Metrics/MethodLength



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/rail_feeds/network_rail/schedule/association.rb', line 184

def to_json(**opts)
  {
    'JsonAssociationV1' => {
      'transaction_type' => 'Create',
      'main_train_uid' => main_train_uid,
      'assoc_train_uid' => associated_train_uid,
      'assoc_start_date' => start_date.strftime('%Y-%m-%dT00:00:00Z'),
      'assoc_end_date' => end_date.strftime('%Y-%m-%dT00:00:00Z'),
      'assoc_days' => days_to_cif,
      'category' => category,
      'date_indicator' => date_indicator,
      'location' => tiploc,
      'base_location_suffix' => main_location_suffix,
      'assoc_location_suffix' => associated_location_suffix,
      'diagram_type' => 'T',
      'CIF_stp_indicator' => stp_indicator_to_cif
    }
  }.to_json(**opts)
end