Class: Flyday::Flight
- Inherits:
-
Object
- Object
- Flyday::Flight
- Defined in:
- lib/flight.rb
Overview
Flight contains the details of the flights between two cities.
Instance Attribute Summary collapse
-
#departure_date ⇒ Object
readonly
Returns the value of attribute departure_date.
-
#departure_time ⇒ Object
readonly
Returns the value of attribute departure_time.
-
#dest ⇒ Object
readonly
Returns the value of attribute dest.
-
#orig ⇒ Object
readonly
Returns the value of attribute orig.
-
#segments ⇒ Object
readonly
Returns the value of attribute segments.
Instance Method Summary collapse
- #flatten ⇒ Object
- #flight_number ⇒ Object
- #impacted? ⇒ Boolean
-
#initialize(blob) ⇒ Flight
constructor
A new instance of Flight.
- #inspect ⇒ Object
- #land_at ⇒ Object
- #price_range ⇒ Object
- #seats_left ⇒ Object
- #segments_path ⇒ Object
- #takeoff_at ⇒ Object
Constructor Details
#initialize(blob) ⇒ Flight
Returns a new instance of Flight.
5 6 7 8 9 10 11 12 |
# File 'lib/flight.rb', line 5 def initialize(blob) @blob = blob @segments = blob['segments'] @orig = blob['segments'][0]['originationAirportCode'] @dest = blob['segments'][-1]['destinationAirportCode'] departure_date_time = blob['segments'][0]['departureDateTime'] @departure_date, @departure_time = departure_date_time.split('T') end |
Instance Attribute Details
#departure_date ⇒ Object (readonly)
Returns the value of attribute departure_date.
4 5 6 |
# File 'lib/flight.rb', line 4 def departure_date @departure_date end |
#departure_time ⇒ Object (readonly)
Returns the value of attribute departure_time.
4 5 6 |
# File 'lib/flight.rb', line 4 def departure_time @departure_time end |
#dest ⇒ Object (readonly)
Returns the value of attribute dest.
4 5 6 |
# File 'lib/flight.rb', line 4 def dest @dest end |
#orig ⇒ Object (readonly)
Returns the value of attribute orig.
4 5 6 |
# File 'lib/flight.rb', line 4 def orig @orig end |
#segments ⇒ Object (readonly)
Returns the value of attribute segments.
4 5 6 |
# File 'lib/flight.rb', line 4 def segments @segments end |
Instance Method Details
#flatten ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/flight.rb', line 40 def flatten return [self] if @segments.length == 1 @segments.map do |segment| Flyday.new.search( date: Date.parse(departure_date), orig: segment['originationAirportCode'], dest: segment['destinationAirportCode'] ).detect do |f| f.flight_number == segment['marketingCarrierInfo'].values.join('') end end end |
#flight_number ⇒ Object
14 15 16 |
# File 'lib/flight.rb', line 14 def flight_number @segments.map { |s| s['operatingCarrierInfo'].values.join }.join(' ') end |
#impacted? ⇒ Boolean
27 28 29 |
# File 'lib/flight.rb', line 27 def impacted? seats_left == 1 end |
#inspect ⇒ Object
61 62 63 64 65 66 |
# File 'lib/flight.rb', line 61 def inspect object = '<#Flyday::Flight' takeoff = "#{departure_date}T#{departure_time}" fare_info = "seats:#{seats_left}, price_range:#{price_range}" "#{object} #{segments_path} #{takeoff} #{fare_info}>" end |
#land_at ⇒ Object
57 58 59 |
# File 'lib/flight.rb', line 57 def land_at @blob['segments'][-1]['arrivalDateTime'] end |
#price_range ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/flight.rb', line 31 def price_range products = @blob['fareProducts'] max, min = products.minmax { |l| l['currencyPrice']['totalFareCents'] } min = min['currencyPrice']['totalFareCents'] / 100 max = max['currencyPrice']['totalFareCents'] / 100 "#{min}-#{max}" end |
#seats_left ⇒ Object
18 19 20 |
# File 'lib/flight.rb', line 18 def seats_left @blob['fareProducts'].map { |p| p['seatsAvailable'].to_i }.inject(:+) end |
#segments_path ⇒ Object
22 23 24 25 |
# File 'lib/flight.rb', line 22 def segments_path final = @segments[-1]['destinationAirportCode'] @segments.map { |l| l['originationAirportCode'] }.push(final).join('->') end |
#takeoff_at ⇒ Object
53 54 55 |
# File 'lib/flight.rb', line 53 def takeoff_at "#{departure_date}T#{departure_time}" end |