Class: Amtrak::TrainParser
- Inherits:
-
Object
- Object
- Amtrak::TrainParser
- Defined in:
- lib/amtrak/train_parser.rb
Overview
Will take in an JSON document as a hash and parse out the train schedule
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#json ⇒ Object
readonly
Returns the value of attribute json.
Class Method Summary collapse
Instance Method Summary collapse
- #find_arrival_times(segment) ⇒ Object
- #find_departure_times(segment) ⇒ Object
-
#initialize(json) ⇒ TrainParser
constructor
A new instance of TrainParser.
- #parse ⇒ Object
- #parse_journey(journey) ⇒ Object
Constructor Details
#initialize(json) ⇒ TrainParser
Returns a new instance of TrainParser.
13 14 15 |
# File 'lib/amtrak/train_parser.rb', line 13 def initialize(json) @json = json end |
Instance Attribute Details
#json ⇒ Object (readonly)
Returns the value of attribute json.
11 12 13 |
# File 'lib/amtrak/train_parser.rb', line 11 def json @json end |
Class Method Details
.parse(json) ⇒ Object
7 8 9 |
# File 'lib/amtrak/train_parser.rb', line 7 def self.parse(json) new(json).parse end |
Instance Method Details
#find_arrival_times(segment) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/amtrak/train_parser.rb', line 47 def find_arrival_times(segment) date, scheduled_time = segment['destinationScheduledArrivalDateTime'].split('T') estimated_arrival = segment['destinationEstimatedArrivalDateTime'] _, estimated_time = estimated_arrival.split('T') if estimated_arrival status = segment['destinationStatusComment'] { date: date, scheduled_time: format_time(scheduled_time), estimated_time: or_cancelled(format_time(estimated_time), status) } end |
#find_departure_times(segment) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/amtrak/train_parser.rb', line 34 def find_departure_times(segment) date, scheduled_time = segment['originScheduledDepartureDateTime'].split('T') estimated_depart = segment['originEstimatedDepartureDateTime'] _, estimated_time = estimated_depart.split('T') if estimated_depart status = segment['originStatusComment'] { date: date, scheduled_time: format_time(scheduled_time), estimated_time: or_cancelled(format_time(estimated_time), status) } end |
#parse ⇒ Object
17 18 19 20 21 |
# File 'lib/amtrak/train_parser.rb', line 17 def parse (json['journeys'] || []) .map(&method(:parse_journey)) .compact end |
#parse_journey(journey) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/amtrak/train_parser.rb', line 23 def parse_journey(journey) segment = journey['segments'].first return unless segment { number: segment['trainNumber'].to_i, departure: find_departure_times(segment), arrival: find_arrival_times(segment) } end |