Class: RailFeeds::NetworkRail::Schedule::TrainSchedule

Inherits:
Object
  • Object
show all
Includes:
Comparable, Days, STPIndicator
Defined in:
lib/rail_feeds/network_rail/schedule/train_schedule.rb,
lib/rail_feeds/network_rail/schedule/train_schedule/location.rb,
lib/rail_feeds/network_rail/schedule/train_schedule/change_en_route.rb,
lib/rail_feeds/network_rail/schedule/train_schedule/location/origin.rb,
lib/rail_feeds/network_rail/schedule/train_schedule/location/terminating.rb,
lib/rail_feeds/network_rail/schedule/train_schedule/location/intermediate.rb

Overview

rubocop:disable Metrics/ClassLength A class for holding information about a particular train

Defined Under Namespace

Classes: ChangeEnRoute, Location

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) ⇒ TrainSchedule

Initialize a new train



99
100
101
102
103
104
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 99

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

Instance Attribute Details

#applicable_timetableBoolean?

performance monitoring (Applicable Timetable Service).

Returns:

  • (Boolean, nil)

    Whether the service is subject to



90
91
92
93
94
95
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

attr_accessor :uid, :category, :status,
:reservation_headcode, :signalling_headcode, :service_code,
:start_date, :end_date, :bank_holiday_running,
:power_type, :timing_load, :speed, :operating_characteristics,
:seating_class, :sleeping_class, :reservations, :catering,
:branding, :uic, :atoc, :applicable_timetable, :journey

#atocObject



90
91
92
93
94
95
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

attr_accessor :uid, :category, :status,
:reservation_headcode, :signalling_headcode, :service_code,
:start_date, :end_date, :bank_holiday_running,
:power_type, :timing_load, :speed, :operating_characteristics,
:seating_class, :sleeping_class, :reservations, :catering,
:branding, :uic, :atoc, :applicable_timetable, :journey

#bank_holiday_runningString?

  • X - does not run on specified bank holiday Mondays.

  • G - does not run on Glasgow bank holidays.

Returns:

  • (String, nil)

    Whether the service runs on bank holidays.



90
91
92
93
94
95
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

attr_accessor :uid, :category, :status,
:reservation_headcode, :signalling_headcode, :service_code,
:start_date, :end_date, :bank_holiday_running,
:power_type, :timing_load, :speed, :operating_characteristics,
:seating_class, :sleeping_class, :reservations, :catering,
:branding, :uic, :atoc, :applicable_timetable, :journey

#brandingString?

  • E - Eurostar

Returns:

  • (String, nil)

    The service brand.



90
91
92
93
94
95
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

attr_accessor :uid, :category, :status,
:reservation_headcode, :signalling_headcode, :service_code,
:start_date, :end_date, :bank_holiday_running,
:power_type, :timing_load, :speed, :operating_characteristics,
:seating_class, :sleeping_class, :reservations, :catering,
:branding, :uic, :atoc, :applicable_timetable, :journey

#categoryString

Returns:

  • (String)


90
91
92
93
94
95
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

attr_accessor :uid, :category, :status,
:reservation_headcode, :signalling_headcode, :service_code,
:start_date, :end_date, :bank_holiday_running,
:power_type, :timing_load, :speed, :operating_characteristics,
:seating_class, :sleeping_class, :reservations, :catering,
:branding, :uic, :atoc, :applicable_timetable, :journey

#cateringString?

  • H - Hot food available

  • C - Buffet service

  • R - Restaurant

  • F - Restaurant for first class passengers

  • M - Meal included for first class passengers

  • T - Trolly service

Returns:

  • (String, nil)

    The catering available, any 2 of:



90
91
92
93
94
95
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

attr_accessor :uid, :category, :status,
:reservation_headcode, :signalling_headcode, :service_code,
:start_date, :end_date, :bank_holiday_running,
:power_type, :timing_load, :speed, :operating_characteristics,
:seating_class, :sleeping_class, :reservations, :catering,
:branding, :uic, :atoc, :applicable_timetable, :journey

#daysArray<Boolean>

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

Returns:

  • (Array<Boolean>)

    The days on which the service runs.



90
91
92
93
94
95
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

attr_accessor :uid, :category, :status,
:reservation_headcode, :signalling_headcode, :service_code,
:start_date, :end_date, :bank_holiday_running,
:power_type, :timing_load, :speed, :operating_characteristics,
:seating_class, :sleeping_class, :reservations, :catering,
:branding, :uic, :atoc, :applicable_timetable, :journey

#end_dateDate

Returns When the schedule ends.

Returns:

  • (Date)

    When the schedule ends.



90
91
92
93
94
95
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

attr_accessor :uid, :category, :status,
:reservation_headcode, :signalling_headcode, :service_code,
:start_date, :end_date, :bank_holiday_running,
:power_type, :timing_load, :speed, :operating_characteristics,
:seating_class, :sleeping_class, :reservations, :catering,
:branding, :uic, :atoc, :applicable_timetable, :journey

#journeyArray<Location, ChangeEnRoute>

change will precede location.

Returns:



90
91
92
93
94
95
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

attr_accessor :uid, :category, :status,
:reservation_headcode, :signalling_headcode, :service_code,
:start_date, :end_date, :bank_holiday_running,
:power_type, :timing_load, :speed, :operating_characteristics,
:seating_class, :sleeping_class, :reservations, :catering,
:branding, :uic, :atoc, :applicable_timetable, :journey

#operating_characteristicsString?

Returns:

  • (String, nil)


90
91
92
93
94
95
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

attr_accessor :uid, :category, :status,
:reservation_headcode, :signalling_headcode, :service_code,
:start_date, :end_date, :bank_holiday_running,
:power_type, :timing_load, :speed, :operating_characteristics,
:seating_class, :sleeping_class, :reservations, :catering,
:branding, :uic, :atoc, :applicable_timetable, :journey

#power_typeString

Returns:

  • (String)


90
91
92
93
94
95
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

attr_accessor :uid, :category, :status,
:reservation_headcode, :signalling_headcode, :service_code,
:start_date, :end_date, :bank_holiday_running,
:power_type, :timing_load, :speed, :operating_characteristics,
:seating_class, :sleeping_class, :reservations, :catering,
:branding, :uic, :atoc, :applicable_timetable, :journey

#reservation_headcodeInteger?

(National Reservation System).

Returns:

  • (Integer, nil)

    The train’s headcode in the NRS



90
91
92
93
94
95
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

attr_accessor :uid, :category, :status,
:reservation_headcode, :signalling_headcode, :service_code,
:start_date, :end_date, :bank_holiday_running,
:power_type, :timing_load, :speed, :operating_characteristics,
:seating_class, :sleeping_class, :reservations, :catering,
:branding, :uic, :atoc, :applicable_timetable, :journey

#reservationsString?

  • A - Reservations Compulsory

  • E - Reservations for bicycles essential

  • R - Reservations recommended

  • S - Reservations possible from any station

  • W - Wheelchair only reservations

Returns:

  • (String, nil)

    The reservation recommendations.



90
91
92
93
94
95
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

attr_accessor :uid, :category, :status,
:reservation_headcode, :signalling_headcode, :service_code,
:start_date, :end_date, :bank_holiday_running,
:power_type, :timing_load, :speed, :operating_characteristics,
:seating_class, :sleeping_class, :reservations, :catering,
:branding, :uic, :atoc, :applicable_timetable, :journey

#seating_classString?

  • B or blank - First and Standard

  • S - Standard only

Returns:

  • (String, nil)

    The seating classes available.



90
91
92
93
94
95
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

attr_accessor :uid, :category, :status,
:reservation_headcode, :signalling_headcode, :service_code,
:start_date, :end_date, :bank_holiday_running,
:power_type, :timing_load, :speed, :operating_characteristics,
:seating_class, :sleeping_class, :reservations, :catering,
:branding, :uic, :atoc, :applicable_timetable, :journey

#service_codeInteger

Returns Used for attribution of revenue.

Returns:

  • (Integer)

    Used for attribution of revenue.



90
91
92
93
94
95
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

attr_accessor :uid, :category, :status,
:reservation_headcode, :signalling_headcode, :service_code,
:start_date, :end_date, :bank_holiday_running,
:power_type, :timing_load, :speed, :operating_characteristics,
:seating_class, :sleeping_class, :reservations, :catering,
:branding, :uic, :atoc, :applicable_timetable, :journey

#signalling_headcodeString?

Will be nil for annonymous freight services.

Returns:

  • (String, nil)

    The headcode used in signalling the service.



90
91
92
93
94
95
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

attr_accessor :uid, :category, :status,
:reservation_headcode, :signalling_headcode, :service_code,
:start_date, :end_date, :bank_holiday_running,
:power_type, :timing_load, :speed, :operating_characteristics,
:seating_class, :sleeping_class, :reservations, :catering,
:branding, :uic, :atoc, :applicable_timetable, :journey

#sleeping_classString?

  • B - First and Standard

  • F - First only

  • S - Standard only

Returns:

  • (String, nil)

    The sleeping classes available.



90
91
92
93
94
95
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

attr_accessor :uid, :category, :status,
:reservation_headcode, :signalling_headcode, :service_code,
:start_date, :end_date, :bank_holiday_running,
:power_type, :timing_load, :speed, :operating_characteristics,
:seating_class, :sleeping_class, :reservations, :catering,
:branding, :uic, :atoc, :applicable_timetable, :journey

#speedInteger

Returns The planned speed (miles per hour).

Returns:

  • (Integer)

    The planned speed (miles per hour).



90
91
92
93
94
95
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

attr_accessor :uid, :category, :status,
:reservation_headcode, :signalling_headcode, :service_code,
:start_date, :end_date, :bank_holiday_running,
:power_type, :timing_load, :speed, :operating_characteristics,
:seating_class, :sleeping_class, :reservations, :catering,
:branding, :uic, :atoc, :applicable_timetable, :journey

#start_dateDate

Returns When the schedule starts.

Returns:

  • (Date)

    When the schedule starts.



90
91
92
93
94
95
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

attr_accessor :uid, :category, :status,
:reservation_headcode, :signalling_headcode, :service_code,
:start_date, :end_date, :bank_holiday_running,
:power_type, :timing_load, :speed, :operating_characteristics,
:seating_class, :sleeping_class, :reservations, :catering,
:branding, :uic, :atoc, :applicable_timetable, :journey

#statusString

Returns:

  • (String)


90
91
92
93
94
95
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

attr_accessor :uid, :category, :status,
:reservation_headcode, :signalling_headcode, :service_code,
:start_date, :end_date, :bank_holiday_running,
:power_type, :timing_load, :speed, :operating_characteristics,
:seating_class, :sleeping_class, :reservations, :catering,
:branding, :uic, :atoc, :applicable_timetable, :journey

#stp_indicatorString

  • C - cancellation of permanent schedule

  • N - new STP schedule

  • O - STP overlay of permanent schedule

  • P - permanent

Returns:

  • (String)


90
91
92
93
94
95
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

attr_accessor :uid, :category, :status,
:reservation_headcode, :signalling_headcode, :service_code,
:start_date, :end_date, :bank_holiday_running,
:power_type, :timing_load, :speed, :operating_characteristics,
:seating_class, :sleeping_class, :reservations, :catering,
:branding, :uic, :atoc, :applicable_timetable, :journey

#timing_loadString?

Returns:

  • (String, nil)


90
91
92
93
94
95
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

attr_accessor :uid, :category, :status,
:reservation_headcode, :signalling_headcode, :service_code,
:start_date, :end_date, :bank_holiday_running,
:power_type, :timing_load, :speed, :operating_characteristics,
:seating_class, :sleeping_class, :reservations, :catering,
:branding, :uic, :atoc, :applicable_timetable, :journey

#uicInteger?

Returns For train services running to/from continental Europe.

Returns:

  • (Integer, nil)

    For train services running to/from continental Europe.



90
91
92
93
94
95
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

attr_accessor :uid, :category, :status,
:reservation_headcode, :signalling_headcode, :service_code,
:start_date, :end_date, :bank_holiday_running,
:power_type, :timing_load, :speed, :operating_characteristics,
:seating_class, :sleeping_class, :reservations, :catering,
:branding, :uic, :atoc, :applicable_timetable, :journey

#uidString

Along with start_date uniquely identifies a schedule.

Returns:

  • (String)

    The unique train identifier (letter then 5 numbers).



90
91
92
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 90

def uid
  @uid
end

Class Method Details

.from_json(json) ⇒ Object

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/LineLength rubocop:disable Metrics/MethodLength Create a new train schedule from json data.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 130

def self.from_json(json)
  data = ::JSON.parse(json)['JsonScheduleV1']

  locations = data.dig('schedule_segment', 'schedule_location')&.map do |location|
    location_from_json location
  end

  new(
    uid: data['CIF_train_uid'],
    category: Schedule.nil_or_strip(data.dig('schedule_segment', 'CIF_train_category')),
    status: Schedule.nil_or_strip(data['train_status']),
    reservation_headcode: Schedule.nil_or_i(data.dig('schedule_segment', 'CIF_headcode')),
    signalling_headcode: Schedule.nil_or_strip(data.dig('schedule_segment', 'signalling_id')),
    service_code: Schedule.nil_or_strip(data.dig('schedule_segment', 'CIF_train_service_code')),
    start_date: Date.parse(data['schedule_start_date']),
    end_date: data['schedule_end_date'] ? Date.parse(data['schedule_end_date']) : nil,
    days: days_from_cif(data['schedule_days_runs']),
    bank_holiday_running: Schedule.nil_or_strip(data['CIF_bank_holiday_running']),
    power_type: Schedule.nil_or_strip(data.dig('schedule_segment', 'CIF_power_type')),
    timing_load: Schedule.nil_or_strip(data.dig('schedule_segment', 'CIF_timing_load')),
    speed: Schedule.nil_or_i(data.dig('schedule_segment', 'CIF_speed')),
    operating_characteristics: Schedule.nil_or_strip(data.dig('schedule_segment', 'CIF_operating_characteristics')),
    seating_class: Schedule.nil_or_strip(data.dig('schedule_segment', 'CIF_train_class')),
    sleeping_class: Schedule.nil_or_strip(data.dig('schedule_segment', 'CIF_sleepers')),
    reservations: Schedule.nil_or_strip(data.dig('schedule_segment', 'CIF_reservations')),
    catering: Schedule.nil_or_strip(data.dig('schedule_segment', 'CIF_catering_code')),
    branding: Schedule.nil_or_strip(data.dig('schedule_segment', 'CIF_service_branding')),
    uic: Schedule.nil_or_i(data.dig('new_schedule_segment', 'uic_code')),
    atoc: data['atoc_code'],
    applicable_timetable: data['applicable_timetable'].eql?('Y'),
    stp_indicator: stp_indicator_from_cif(data['CIF_stp_indicator']),
    journey: locations
  )
end

Instance Method Details

#<=>(other) ⇒ Object



228
229
230
231
232
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 228

def <=>(other)
  values = [start_date, uid]
  other_values = [other&.start_date, other&.uid]
  values <=> other_values
end

#==(other) ⇒ Object



224
225
226
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 224

def ==(other)
  hash == other&.hash
end

#hashObject

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/MethodLength



220
221
222
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 220

def hash
  "#{uid}-#{start_date&.strftime('%Y%m%d')}"
end

#to_cifObject

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/LineLength rubocop:enable Metrics/MethodLength



168
169
170
171
172
173
174
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 168

def to_cif
  [
    basic_to_cif,
    extra_to_cif,
    *journey.map(&:to_cif)
  ].join
end

#to_json(**opts) ⇒ Object

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



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 178

def to_json(**opts)
  {
    'JsonScheduleV1' => {
      'CIF_bank_holiday_running' => bank_holiday_running,
      'CIF_stp_indicator' => stp_indicator_to_cif,
      'CIF_train_uid' => uid,
      'applicable_timetable' => (applicable_timetable ? 'Y' : 'N'),
      'atoc_code' => atoc,
      'schedule_start_date' => start_date.strftime('%Y-%m-%d'),
      'train_status' => status,
      'transaction_type' => 'Create',
      'schedule_days_runs' => days_to_cif,
      'schedule_end_date' => end_date.strftime('%Y-%m-%d'),
      'new_schedule_segment' => {
        'traction_class' => '',
        'uic_code' => uic.to_s
      },
      'schedule_segment' => {
        'signalling_id' => signalling_headcode,
        'CIF_train_category' => category,
        'CIF_headcode' => reservation_headcode.to_s,
        'CIF_course_indicator' => nil,
        'CIF_train_service_code' => service_code,
        'CIF_business_sector' => '??',
        'CIF_power_type' => power_type,
        'CIF_timing_load' => timing_load,
        'CIF_speed' => speed.to_s.rjust(3, '0'),
        'CIF_operating_characteristics' => operating_characteristics,
        'CIF_train_class' => seating_class,
        'CIF_sleepers' => sleeping_class,
        'CIF_reservations' => reservations,
        'CIF_connection_indicator' => nil,
        'CIF_catering_code' => catering,
        'CIF_service_branding' => branding,
        'schedule_location' => journey.map(&:to_hash_for_json).reject(&:nil?)
      }
    }
  }.to_json(**opts)
end

#update_from_cif(line) ⇒ Object

Add details from a CIF schedule file line to this train schedule



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rail_feeds/network_rail/schedule/train_schedule.rb', line 107

def update_from_cif(line)
  type = line[0..1]

  if type.eql?('BS')
    update_basic_information line
  elsif type.eql?('BX')
    update_extra_information line

  elsif %w[LO LI LT].include?(type)
    journey.push Location.from_cif(line)

  elsif type.eql?('CR')
    journey.push ChangeEnRoute.from_cif(line)

  else
    fail ArgumentError, "Improper line type #{line[0..1]}: #{line}"
  end
end