class Flyday::Flight
Flight
contains the details of the flights between two cities.
Attributes
departure_date[R]
departure_time[R]
dest[R]
orig[R]
segments[R]
Public Class Methods
new(blob)
click to toggle source
# 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
Public Instance Methods
flatten()
click to toggle source
# 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()
click to toggle source
# File lib/flight.rb, line 14 def flight_number @segments.map { |s| s['operatingCarrierInfo'].values.join }.join(' ') end
impacted?()
click to toggle source
# File lib/flight.rb, line 27 def impacted? seats_left == 1 end
inspect()
click to toggle source
# 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()
click to toggle source
# File lib/flight.rb, line 57 def land_at @blob['segments'][-1]['arrivalDateTime'] end
price_range()
click to toggle source
# 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()
click to toggle source
# File lib/flight.rb, line 18 def seats_left @blob['fareProducts'].map { |p| p['seatsAvailable'].to_i }.inject(:+) end
segments_path()
click to toggle source
# File lib/flight.rb, line 22 def segments_path final = @segments[-1]['destinationAirportCode'] @segments.map { |l| l['originationAirportCode'] }.push(final).join('->') end
takeoff_at()
click to toggle source
# File lib/flight.rb, line 53 def takeoff_at "#{departure_date}T#{departure_time}" end