Class: TransilienRealtime::Train

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/transilien_realtime/train.rb

Overview

Important note about attribute departure_at: Since it’s not possible to have a Time instance w/o timezone, and Europe/Paris change its offset w/ DST, lie by forcing a false UTC hours

Constant Summary collapse

MODES =
{
  'R' => 'realtime',
  'T' => 'theoritical'
}
STATES =
{
  'R' => 'late',
  'S' => 'cancelled',
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mission:, departure_at:, terminus:, numero:, mode:, state: nil) ⇒ Train

Returns a new instance of Train.



34
35
36
37
38
39
40
41
# File 'lib/transilien_realtime/train.rb', line 34

def initialize(mission:, departure_at:, terminus:, numero:, mode:, state: nil)
  @mission = mission
  @numero = numero
  @departure_at = Time.strptime(departure_at + Time.new.to_s[-6..-1], '%d/%m/%Y %H:%M %z') # Trick to keep offset correct over DST
  @terminus = terminus
  @state = state && STATES[state[0]] # SNCF xsd is wrong: say to expect a /^S/ but give 'Supprimé'
  @mode = mode && MODES[mode[0]] # consider they will be consistant, so wait for /^R/ instead of R
end

Instance Attribute Details

#departure_atObject (readonly)

Returns the value of attribute departure_at.



17
18
19
# File 'lib/transilien_realtime/train.rb', line 17

def departure_at
  @departure_at
end

#missionObject (readonly)

Returns the value of attribute mission.



17
18
19
# File 'lib/transilien_realtime/train.rb', line 17

def mission
  @mission
end

#modeObject (readonly)

Returns the value of attribute mode.



17
18
19
# File 'lib/transilien_realtime/train.rb', line 17

def mode
  @mode
end

#numeroObject (readonly)

Returns the value of attribute numero.



17
18
19
# File 'lib/transilien_realtime/train.rb', line 17

def numero
  @numero
end

#stateObject (readonly)

Returns the value of attribute state.



17
18
19
# File 'lib/transilien_realtime/train.rb', line 17

def state
  @state
end

#terminusObject (readonly)

Returns the value of attribute terminus.



17
18
19
# File 'lib/transilien_realtime/train.rb', line 17

def terminus
  @terminus
end

Class Method Details

.from_xml(xml_node) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/transilien_realtime/train.rb', line 20

def from_xml(xml_node)
  attr = {}
  attr[:mission] = xml_node.at_xpath('miss').text rescue nil # it appears sometimes at PSL there's no mission tag
  attr[:terminus] = xml_node.at_xpath('term').text rescue nil # it appears sometimes at PSL there's no mission tag
  attr[:numero] = xml_node.at_xpath('num').text
  date_node = xml_node.at_xpath('date')
  attr[:departure_at] = date_node.text
  attr[:mode] = date_node.attr('mode')
  etat = xml_node.at_xpath('etat')
  attr[:state] = etat.text if etat
  train = new(attr).freeze
end

Instance Method Details

#<=>(other) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/transilien_realtime/train.rb', line 49

def <=>(other)
  return 0 if mission == other.mission &&
              departure_at == other.departure_at &&
              terminus == other.terminus &&
              numero == other.numero &&
              mode == other.mode && 
              state == other.state
  departure_at <=> other.departure_at
end

#to_json(options = {}) ⇒ Object



43
44
45
46
47
# File 'lib/transilien_realtime/train.rb', line 43

def to_json(options={})
  json = { mission: mission, departure_at: departure_at, numero: numero, terminus: terminus, mode: mode }
  json[:state] = state if state
  json.to_json
end