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.
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
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
2 3 4 |
# File 'lib/station_data.rb', line 2 def direction @direction end |
#due_in ⇒ Object (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_arrival ⇒ Object (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_departure ⇒ Object (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_location ⇒ Object (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_early ⇒ Object (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_late ⇒ Object (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_time ⇒ Object (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_arrival ⇒ Object (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_departure ⇒ Object (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_time ⇒ Object (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_code ⇒ Object (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_name ⇒ Object (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 |
#status ⇒ Object (readonly)
Returns the value of attribute status.
2 3 4 |
# File 'lib/station_data.rb', line 2 def status @status end |
#train_code ⇒ Object (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_date ⇒ Object (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_type ⇒ Object (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
#arrival ⇒ Object
60 61 62 |
# File 'lib/station_data.rb', line 60 def arrival {scheduled: @scheduled_arrival, expected: @expected_arrival} end |
#departure ⇒ Object
64 65 66 |
# File 'lib/station_data.rb', line 64 def departure {scheduled: @scheduled_departure, expected: @expected_departure} end |
#destination ⇒ Object
56 57 58 |
# File 'lib/station_data.rb', line 56 def destination {name: @destination, time: @destination_time} end |
#early? ⇒ Boolean
72 73 74 |
# File 'lib/station_data.rb', line 72 def early? @minutes_early > 0 end |
#late? ⇒ Boolean
68 69 70 |
# File 'lib/station_data.rb', line 68 def late? @minutes_late > 0 end |
#on_time? ⇒ Boolean
76 77 78 |
# File 'lib/station_data.rb', line 76 def on_time? !late? && !early? end |
#origin ⇒ Object
52 53 54 |
# File 'lib/station_data.rb', line 52 def origin {name: @origin, time: @origin_time} end |