Class: StationData

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ StationData

Returns a new instance of StationData.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/station_data.rb', line 7

def initialize hash
  @server_time                = Time.parse hash['Servertime']
  @train_code                 = hash['Traincode']
  @station_name               = hash['Stationfullname']
  @station_code               = hash['Stationcode']
  @query_time                 = Time.parse hash['Querytime']
  @train_date                 = Date.parse hash['Traindate']
  @origin                     = hash['Origin']
  @destination                = hash['Destination']
  @origin_time                = Time.parse hash['Origintime']
  @destination_time           = Time.parse hash['Destinationtime']
  @status                     = hash['Status']
  @last_location              = hash['Lastlocation']
  @due_in                     = hash['Duein'].to_i

  # Though IE give a late value, this really represents difference from scheduled arrival
  # and therefore represents the number of minutes that the train is off-schedule where
  # <0: early, 0: on time and >0: late

  off_schedule_minutes        = hash['Late'].to_i
  @minutes_late               = off_schedule_minutes > 0 ? off_schedule_minutes : 0
  @minutes_early              = off_schedule_minutes < 0 ? -off_schedule_minutes : 0

  # If train origin is station_name, then arrival times will be 00:00, so are adjusted to suit expected origin time.
  # Likewise if destination is station_name, departure times should suit expected destination time.
  # See: http://api.irishrail.ie/realtime/ Point 8
  is_departure_station        = @station_name.eql?(@origin)
  is_terminating_station      = @station_name.eql?(@destination)

  one_day = 86400

  @expected_arrival           = is_departure_station ? @origin_time : Time.parse(hash['Exparrival'])
  @expected_departure         = is_terminating_station ? @destination_time : Time.parse(hash['Expdepart'])

  # The API returns expected arr/dep times as HH:MM, if this time has crossed midnight, we parse it as being
  # earlier 'today'; this then throws off the before/after filters. Use the server time as a sanity check.
  @expected_arrival = @expected_arrival + one_day if @expected_arrival < @server_time
  @expected_departure = @expected_departure + one_day if @expected_departure < @server_time

  @scheduled_arrival          = is_departure_station ? @origin_time + off_schedule_minutes : Time.parse(hash['Scharrival'])
  @scheduled_departure        = is_terminating_station ? @destination_time + off_schedule_minutes : Time.parse(hash['Schdepart'])
  @direction                  = hash['Direction']
  @train_type                 = hash['Traintype']
end

Instance Attribute Details

#directionObject (readonly)

Returns the value of attribute direction.



2
3
4
# File 'lib/station_data.rb', line 2

def direction
  @direction
end

#due_inObject (readonly)

Returns the value of attribute due_in.



2
3
4
# File 'lib/station_data.rb', line 2

def due_in
  @due_in
end

#expected_arrivalObject (readonly)

Returns the value of attribute expected_arrival.



2
3
4
# File 'lib/station_data.rb', line 2

def expected_arrival
  @expected_arrival
end

#expected_departureObject (readonly)

Returns the value of attribute expected_departure.



2
3
4
# File 'lib/station_data.rb', line 2

def expected_departure
  @expected_departure
end

#last_locationObject (readonly)

Returns the value of attribute last_location.



2
3
4
# File 'lib/station_data.rb', line 2

def last_location
  @last_location
end

#minutes_earlyObject (readonly)

Returns the value of attribute minutes_early.



2
3
4
# File 'lib/station_data.rb', line 2

def minutes_early
  @minutes_early
end

#minutes_lateObject (readonly)

Returns the value of attribute minutes_late.



2
3
4
# File 'lib/station_data.rb', line 2

def minutes_late
  @minutes_late
end

#query_timeObject (readonly)

Returns the value of attribute query_time.



2
3
4
# File 'lib/station_data.rb', line 2

def query_time
  @query_time
end

#scheduled_arrivalObject (readonly)

Returns the value of attribute scheduled_arrival.



2
3
4
# File 'lib/station_data.rb', line 2

def scheduled_arrival
  @scheduled_arrival
end

#scheduled_departureObject (readonly)

Returns the value of attribute scheduled_departure.



2
3
4
# File 'lib/station_data.rb', line 2

def scheduled_departure
  @scheduled_departure
end

#server_timeObject (readonly)

Returns the value of attribute server_time.



2
3
4
# File 'lib/station_data.rb', line 2

def server_time
  @server_time
end

#station_codeObject (readonly) Also known as: code

Returns the value of attribute station_code.



2
3
4
# File 'lib/station_data.rb', line 2

def station_code
  @station_code
end

#station_nameObject (readonly) Also known as: name

Returns the value of attribute station_name.



2
3
4
# File 'lib/station_data.rb', line 2

def station_name
  @station_name
end

#statusObject (readonly)

Returns the value of attribute status.



2
3
4
# File 'lib/station_data.rb', line 2

def status
  @status
end

#train_codeObject (readonly)

Returns the value of attribute train_code.



2
3
4
# File 'lib/station_data.rb', line 2

def train_code
  @train_code
end

#train_dateObject (readonly)

Returns the value of attribute train_date.



2
3
4
# File 'lib/station_data.rb', line 2

def train_date
  @train_date
end

#train_typeObject (readonly)

Returns the value of attribute train_type.



2
3
4
# File 'lib/station_data.rb', line 2

def train_type
  @train_type
end

Instance Method Details

#arrivalObject



60
61
62
# File 'lib/station_data.rb', line 60

def arrival
  {scheduled: @scheduled_arrival, expected: @expected_arrival}
end

#departureObject



64
65
66
# File 'lib/station_data.rb', line 64

def departure
  {scheduled: @scheduled_departure, expected: @expected_departure}
end

#destinationObject



56
57
58
# File 'lib/station_data.rb', line 56

def destination
  {name: @destination, time: @destination_time}
end

#early?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/station_data.rb', line 72

def early?
  @minutes_early > 0
end

#late?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/station_data.rb', line 68

def late?
  @minutes_late > 0
end

#on_time?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/station_data.rb', line 76

def on_time?
  !late? && !early?
end

#originObject



52
53
54
# File 'lib/station_data.rb', line 52

def origin
  {name: @origin, time: @origin_time}
end