Class: ExpediaApi::ResponseLists::Flights

Inherits:
BaseResponseList show all
Defined in:
lib/expedia_api/response_lists/flights.rb

Instance Attribute Summary

Attributes inherited from BaseResponseList

#exception, #response

Instance Method Summary collapse

Methods inherited from BaseResponseList

#each, #entries, #error?, #first, #initialize, #last, #map, #success?

Constructor Details

This class inherits a constructor from ExpediaApi::ResponseLists::BaseResponseList

Instance Method Details

#cheapest_combination_price(products_json) ⇒ Object



48
49
50
# File 'lib/expedia_api/response_lists/flights.rb', line 48

def cheapest_combination_price(products_json)
  products_json.min{|p| p[:PriceInformation][:TotalPrice][:Value].to_f}[:PriceInformation][:TotalPrice]
end

#entries=(entries) ⇒ Object



4
5
6
# File 'lib/expedia_api/response_lists/flights.rb', line 4

def entries=(entries)
  @entries = entries
end

#extract_combinations(products_json, outbound_legs:, return_legs:) ⇒ Object

returns packages for each of the json data



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/expedia_api/response_lists/flights.rb', line 30

def extract_combinations(products_json, outbound_legs:, return_legs:)
  # combination_pairs, [outbound_leg_id, return_leg_id, raw_data]
  combination_pairs = products_json.map do |json|
    reference_json = json[:AirLegReferenceList][:AirLegReference]
    [reference_json[0]["AirLegIndex"], reference_json[1]["AirLegIndex"], json]
  end
  outbound_legs_by_index = outbound_legs.map{|leg| [leg.index, leg]}.to_h
  return_legs_by_index   = return_legs.map{|leg| [leg.index, leg]}.to_h
  cheapest_price = cheapest_combination_price(products_json)
  combination_pairs.map do |outbound_leg_id, return_leg_id, raw_data|
    entity = ExpediaApi::Entities::FlightCombination.new(raw_data)
    entity.outbound_leg = outbound_legs_by_index[outbound_leg_id]
    entity.return_leg   = return_legs_by_index[return_leg_id]
    entity.cheapest_combination_price = cheapest_price
    entity
  end
end

#extract_entries_from_response(response) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/expedia_api/response_lists/flights.rb', line 8

def extract_entries_from_response(response)
  json = extract_data_from_response(response).with_indifferent_access
  return [] if json.empty?
  # dup?
  json = json.with_indifferent_access
  outbound_legs_json = json["AirLegListCollection"]["AirLegList"][0]["AirLeg"]
  return_legs_json   = json["AirLegListCollection"]["AirLegList"][1]["AirLeg"]
  products_json      = JSON.parse(response.body).with_indifferent_access[:AirProductList][:AirProduct]
  outbound_legs      = extract_legs(outbound_legs_json)
  return_legs        = extract_legs(return_legs_json)
  combinations       = extract_combinations(products_json, outbound_legs:outbound_legs, return_legs:return_legs)
  combinations
end

#extract_legs(legs_json) ⇒ Object

returns legs for each of the entries



23
24
25
26
27
# File 'lib/expedia_api/response_lists/flights.rb', line 23

def extract_legs(legs_json)
  legs_json.map do |json|
    ExpediaApi::Entities::FlightCombinationLeg.new(json)
  end
end