Sha256: 4cefd3b3fbcacd1b0bd2002430729b295e8845e8c06851829faa3bc49f90988f

Contents?: true

Size: 1.83 KB

Versions: 5

Compression:

Stored size: 1.83 KB

Contents

require_relative 'skyscanner_api'

module Skyscanner
  class FlightInfo
    attr_reader :flightInfo

    def initialize(originData)
      carrierId2Carrier = getCarrierId2Carrier(originData)
      placeId2Place = getPlaceId2Place(originData)
      @flightInfo = extractFlightInfo(carrierId2Carrier, placeId2Place, originData)
    end

    def flightInfo
      @flightInfo
    end

    def self.find(market:, currency:, locale:, originPlace:, destinationPlace:, outboundPartialDate:)
      originData = SkyscannerApi.getOriginData(market, currency, locale, originPlace, destinationPlace, outboundPartialDate)
      new(originData)
    end

    private
    def getCarrierId2Carrier(originData)
      carriers = originData['Carriers']
      carrierId2Carrier = Hash.new()
      carriers.each do |carrier|
      carrierId2Carrier[carrier['CarrierId']] = carrier['Name']
        end
      carrierId2Carrier
    end

    private
    def getPlaceId2Place(originData)
      places = originData["Places"]
      placeId2Place = Hash.new()
      places.each do |place|
        if place["Type"] == "Station"
          placeId2Place[place["PlaceId"]] = place["Name"] #+","+place["CountryName"]
        end
      end
      placeId2Place
    end

    private
    def extractFlightInfo(carrierId2Carrier, placeId2Place, originData)
      quotes = originData["Quotes"]
      quotes.each do |quote|
        if(quote["OutboundLeg"]["CarrierIds"].empty? == false)
          for i in 0..quote["OutboundLeg"]["CarrierIds"].length
            quote["OutboundLeg"]["CarrierIds"][i] = carrierId2Carrier[quote["OutboundLeg"]["CarrierIds"][i]]
          end
        end
        quote["OutboundLeg"]["OriginId"] = placeId2Place[quote["OutboundLeg"]["OriginId"]]
        quote["OutboundLeg"]["DestinationId"] = placeId2Place[quote["OutboundLeg"]["DestinationId"]]
      end
      quotes
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
VIAJERO-0.1.74 lib/VIAJERO/flightInfo.rb
VIAJERO-0.1.73 lib/VIAJERO/flightInfo.rb
Time_Traveler-0.1.72 lib/Time_Traveler/flightInfo.rb
Time_Traveler-0.1.71 lib/Time_Traveler/flightInfo.rb
Time_Traveler-0.1.70 lib/Time_Traveler/flightInfo.rb