Class: ExpediaApi::ResponseLists::Packages

Inherits:
BaseResponseList show all
Defined in:
lib/expedia_api/response_lists/packages.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

#entries=(entries) ⇒ Object



5
6
7
# File 'lib/expedia_api/response_lists/packages.rb', line 5

def entries=(entries)
  @entries = entries
end

#extract_entries_from_response(response) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/expedia_api/response_lists/packages.rb', line 9

def extract_entries_from_response(response)
  json     = extract_data_from_response(response)
  return   [] if json.is_a?(Array) || json.empty?
  json     = json.with_indifferent_access
  flights  = extract_flights(extract_raw_flights_from_json(json))
  hotels   = extract_hotels(extract_raw_hotels_from_json(json))
  packages = extract_packages(extract_raw_packages_from_json(json), hotels: hotels, flights: flights)
  packages
end

#extract_flights(flights_json) ⇒ Object

returns flights for each of the entries



20
21
22
23
# File 'lib/expedia_api/response_lists/packages.rb', line 20

def extract_flights(flights_json)
  # right now only 1 flight
  [ExpediaApi::Entities::PackageFlight.new(flights_json)]
end

#extract_hotels(hotels_json) ⇒ Object

returns hotels for each hotel in the json



26
27
28
29
30
31
32
33
34
# File 'lib/expedia_api/response_lists/packages.rb', line 26

def extract_hotels(hotels_json)
  if hotels_json.is_a?(Array)
    hotels_json.map do |hotel|
      ExpediaApi::Entities::PackageHotel.new(hotel)
    end
  else
    [ExpediaApi::Entities::PackageHotel.new(hotels_json)]
  end
end

#extract_packages(json, flights:, hotels:) ⇒ Object

returns packages for each of the json data



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/expedia_api/response_lists/packages.rb', line 37

def extract_packages(json, flights:, hotels:)
  flights_by_index = flights.map {|flight| [flight.index, flight]}.to_h
  hotels_by_index  = hotels.map {|hotel| [hotel.index, hotel]}.to_h
  # if only one package is returned, we just get a hash, no array.
  json = [json] if json.is_a?(Hash)
  json.map do |package|
    entity = ExpediaApi::Entities::Package.new(package)
    entity.flight = flights_by_index[entity.flight_index]
    entity.hotel  = hotels_by_index[entity.hotel_index]
    entity
  end
end

#extract_raw_flights_from_json(json) ⇒ Object

returns the flight data extracted from the json



51
52
53
54
55
56
57
# File 'lib/expedia_api/response_lists/packages.rb', line 51

def extract_raw_flights_from_json(json)
  if json[:FlightList] && json[:FlightList][:Flight]
    json[:FlightList][:Flight]
  else
    []
  end
end

#extract_raw_hotels_from_json(json) ⇒ Object

returns the hotel data extracted fron the json



60
61
62
63
64
65
66
# File 'lib/expedia_api/response_lists/packages.rb', line 60

def extract_raw_hotels_from_json(json)
  if json[:HotelList] && json[:HotelList][:Hotel]
    json[:HotelList][:Hotel]
  else
    []
  end
end

#extract_raw_packages_from_json(json) ⇒ Object

returns the package data extracted from the json



69
70
71
72
73
74
75
# File 'lib/expedia_api/response_lists/packages.rb', line 69

def extract_raw_packages_from_json(json)
  if json[:PackageSearchResultList] && json[:PackageSearchResultList][:PackageSearchResult]
    json[:PackageSearchResultList][:PackageSearchResult]
  else
    []
  end
end