Class: StationData
- Inherits:
-
Object
- Object
- StationData
- Defined in:
- lib/station_data.rb
Instance Attribute Summary collapse
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#due_in ⇒ Object
readonly
Returns the value of attribute due_in.
-
#expected_arrival ⇒ Object
readonly
Returns the value of attribute expected_arrival.
-
#expected_departure ⇒ Object
readonly
Returns the value of attribute expected_departure.
-
#last_location ⇒ Object
readonly
Returns the value of attribute last_location.
-
#minutes_early ⇒ Object
readonly
Returns the value of attribute minutes_early.
-
#minutes_late ⇒ Object
readonly
Returns the value of attribute minutes_late.
-
#query_time ⇒ Object
readonly
Returns the value of attribute query_time.
-
#scheduled_arrival ⇒ Object
readonly
Returns the value of attribute scheduled_arrival.
-
#scheduled_departure ⇒ Object
readonly
Returns the value of attribute scheduled_departure.
-
#server_time ⇒ Object
readonly
Returns the value of attribute server_time.
-
#station_code ⇒ Object
(also: #code)
readonly
Returns the value of attribute station_code.
-
#station_name ⇒ Object
(also: #name)
readonly
Returns the value of attribute station_name.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#train_code ⇒ Object
readonly
Returns the value of attribute train_code.
-
#train_date ⇒ Object
readonly
Returns the value of attribute train_date.
-
#train_type ⇒ Object
readonly
Returns the value of attribute train_type.
Instance Method Summary collapse
- #arrival ⇒ Object
- #departure ⇒ Object
- #destination ⇒ Object
- #early? ⇒ Boolean
-
#initialize(hash) ⇒ StationData
constructor
A new instance of StationData.
- #late? ⇒ Boolean
- #on_time? ⇒ Boolean
- #origin ⇒ Object
Constructor Details
#initialize(hash) ⇒ StationData
Returns a new instance of StationData.
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 51 52 |
# File 'lib/station_data.rb', line 9 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
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
4 5 6 |
# File 'lib/station_data.rb', line 4 def direction @direction end |
#due_in ⇒ Object (readonly)
Returns the value of attribute due_in.
4 5 6 |
# File 'lib/station_data.rb', line 4 def due_in @due_in end |
#expected_arrival ⇒ Object (readonly)
Returns the value of attribute expected_arrival.
4 5 6 |
# File 'lib/station_data.rb', line 4 def expected_arrival @expected_arrival end |
#expected_departure ⇒ Object (readonly)
Returns the value of attribute expected_departure.
4 5 6 |
# File 'lib/station_data.rb', line 4 def expected_departure @expected_departure end |
#last_location ⇒ Object (readonly)
Returns the value of attribute last_location.
4 5 6 |
# File 'lib/station_data.rb', line 4 def last_location @last_location end |
#minutes_early ⇒ Object (readonly)
Returns the value of attribute minutes_early.
4 5 6 |
# File 'lib/station_data.rb', line 4 def minutes_early @minutes_early end |
#minutes_late ⇒ Object (readonly)
Returns the value of attribute minutes_late.
4 5 6 |
# File 'lib/station_data.rb', line 4 def minutes_late @minutes_late end |
#query_time ⇒ Object (readonly)
Returns the value of attribute query_time.
4 5 6 |
# File 'lib/station_data.rb', line 4 def query_time @query_time end |
#scheduled_arrival ⇒ Object (readonly)
Returns the value of attribute scheduled_arrival.
4 5 6 |
# File 'lib/station_data.rb', line 4 def scheduled_arrival @scheduled_arrival end |
#scheduled_departure ⇒ Object (readonly)
Returns the value of attribute scheduled_departure.
4 5 6 |
# File 'lib/station_data.rb', line 4 def scheduled_departure @scheduled_departure end |
#server_time ⇒ Object (readonly)
Returns the value of attribute server_time.
4 5 6 |
# File 'lib/station_data.rb', line 4 def server_time @server_time end |
#station_code ⇒ Object (readonly) Also known as: code
Returns the value of attribute station_code.
4 5 6 |
# File 'lib/station_data.rb', line 4 def station_code @station_code end |
#station_name ⇒ Object (readonly) Also known as: name
Returns the value of attribute station_name.
4 5 6 |
# File 'lib/station_data.rb', line 4 def station_name @station_name end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
4 5 6 |
# File 'lib/station_data.rb', line 4 def status @status end |
#train_code ⇒ Object (readonly)
Returns the value of attribute train_code.
4 5 6 |
# File 'lib/station_data.rb', line 4 def train_code @train_code end |
#train_date ⇒ Object (readonly)
Returns the value of attribute train_date.
4 5 6 |
# File 'lib/station_data.rb', line 4 def train_date @train_date end |
#train_type ⇒ Object (readonly)
Returns the value of attribute train_type.
4 5 6 |
# File 'lib/station_data.rb', line 4 def train_type @train_type end |
Instance Method Details
#arrival ⇒ Object
62 63 64 |
# File 'lib/station_data.rb', line 62 def arrival {scheduled: @scheduled_arrival, expected: @expected_arrival} end |
#departure ⇒ Object
66 67 68 |
# File 'lib/station_data.rb', line 66 def departure {scheduled: @scheduled_departure, expected: @expected_departure} end |
#destination ⇒ Object
58 59 60 |
# File 'lib/station_data.rb', line 58 def destination {name: @destination, time: @destination_time} end |
#early? ⇒ Boolean
74 75 76 |
# File 'lib/station_data.rb', line 74 def early? @minutes_early > 0 end |
#late? ⇒ Boolean
70 71 72 |
# File 'lib/station_data.rb', line 70 def late? @minutes_late > 0 end |
#on_time? ⇒ Boolean
78 79 80 |
# File 'lib/station_data.rb', line 78 def on_time? !late? && !early? end |
#origin ⇒ Object
54 55 56 |
# File 'lib/station_data.rb', line 54 def origin {name: @origin, time: @origin_time} end |