Class: Train

Inherits:
Object
  • Object
show all
Defined in:
lib/viaggiatreno/train.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(train_number) ⇒ Train

Returns a new instance of Train.



8
9
10
11
12
# File 'lib/viaggiatreno/train.rb', line 8

def initialize(train_number)
  @train_number = train_number
  @scraper = Scraper.new(train_number.to_s, self)
  update
end

Instance Attribute Details

#delayObject

Returns the value of attribute delay.



6
7
8
# File 'lib/viaggiatreno/train.rb', line 6

def delay
  @delay
end

#last_updateObject

Returns the value of attribute last_update.



6
7
8
# File 'lib/viaggiatreno/train.rb', line 6

def last_update
  @last_update
end

#stateObject

Returns the value of attribute state.



6
7
8
# File 'lib/viaggiatreno/train.rb', line 6

def state
  @state
end

#statusObject

Returns the value of attribute status.



6
7
8
# File 'lib/viaggiatreno/train.rb', line 6

def status
  @status
end

#train_nameObject

Returns the value of attribute train_name.



6
7
8
# File 'lib/viaggiatreno/train.rb', line 6

def train_name
  @train_name
end

#train_numberObject

Returns the value of attribute train_number.



6
7
8
# File 'lib/viaggiatreno/train.rb', line 6

def train_number
  @train_number
end

Instance Method Details

#actual_arriving_timeObject



57
58
59
# File 'lib/viaggiatreno/train.rb', line 57

def actual_arriving_time
  train_stops.last.actual_stop_time.to_s
end

#actual_departing_timeObject



53
54
55
# File 'lib/viaggiatreno/train.rb', line 53

def actual_departing_time
  train_stops.first.actual_stop_time.to_s
end

#actual_stop_time(station_name) ⇒ Object



65
66
67
# File 'lib/viaggiatreno/train.rb', line 65

def actual_stop_time(station_name)
  find_stop_time(station_name)['actual']
end

#add_stop(train_stop) ⇒ Object



28
29
30
# File 'lib/viaggiatreno/train.rb', line 28

def add_stop(train_stop)
  @train_stops << train_stop
end

#arriving_stationObject



41
42
43
# File 'lib/viaggiatreno/train.rb', line 41

def arriving_station
  train_stops.last.train_station.to_s
end

#departing_stationObject



37
38
39
# File 'lib/viaggiatreno/train.rb', line 37

def departing_station
  train_stops.first.train_station.to_s
end

#last_stopObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/viaggiatreno/train.rb', line 69

def last_stop
  return train_stops.last.to_s if @state == TrainState::ARRIVED
  train_stops.each_with_index do |train_stop, index|
    if train_stop.status == TrainStopState::DONE && \
       train_stops[index + 1].status == TrainStopState::TO_BE_DONE
      return train_stop.to_s
    end
  end
  nil
end

#scheduled_arriving_timeObject



49
50
51
# File 'lib/viaggiatreno/train.rb', line 49

def scheduled_arriving_time
  train_stops.last.scheduled_stop_time.to_s
end

#scheduled_departing_timeObject



45
46
47
# File 'lib/viaggiatreno/train.rb', line 45

def scheduled_departing_time
  train_stops.first.scheduled_stop_time.to_s
end

#scheduled_stop_time(station_name) ⇒ Object



61
62
63
# File 'lib/viaggiatreno/train.rb', line 61

def scheduled_stop_time(station_name)
  find_stop_time(station_name)['scheduled']
end

#to_sObject



23
24
25
26
# File 'lib/viaggiatreno/train.rb', line 23

def to_s
  "#{@train_number} #{@train_name}: #{@status} state: #{@state}, \
  delay: #{@delay}, last_update: #{@last_update}"
end

#train_stopsObject



32
33
34
35
# File 'lib/viaggiatreno/train.rb', line 32

def train_stops
  update_details if @train_stops.nil?
  @train_stops
end

#updateObject



14
15
16
# File 'lib/viaggiatreno/train.rb', line 14

def update
  @scraper.update_train
end

#update_detailsObject



18
19
20
21
# File 'lib/viaggiatreno/train.rb', line 18

def update_details
  @train_stops = []
  @scraper.update_train_details
end