Class: Suitcase::Hotel

Inherits:
Object
  • Object
show all
Extended by:
Helpers
Defined in:
lib/suitcase/hotel/hotel.rb,
lib/suitcase/hotel/room.rb,
lib/suitcase/hotel/cache.rb,
lib/suitcase/hotel/amenity.rb,
lib/suitcase/hotel/helpers.rb,
lib/suitcase/hotel/session.rb,
lib/suitcase/hotel/location.rb,
lib/suitcase/hotel/reservation.rb,
lib/suitcase/hotel/payment_option.rb

Overview

Internal: Various methods for doing things that many files need to in the

library.

Examples

parameterize(something: "else", another: "thing")
# => "something=else&another=thing"

Defined Under Namespace

Modules: Helpers Classes: Amenity, Cache, Location, PaymentOption, Reservation, Room, Session

Constant Summary collapse

AMENITIES =

Public: The Amenities that can be passed in to searches, and are returned

from many queries.
{ 
  pool: 1,
  fitness_center: 2,
  restaurant: 3,
  children_activities: 4,
  breakfast: 5,
  meeting_facilities: 6,
  pets: 7,
  wheelchair_accessible: 8,
  kitchen: 9
}

Constants included from Helpers

Helpers::URL_DEFAULTS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

base_url, build_session_params, generate_signature, main_url, parameterize, parse_response, update_session, url

Constructor Details

#initialize(info) ⇒ Hotel

Internal: Initialize a new Hotel.

info - A Hash of the options listed in attr_accessor.

Returns a new Hotel object with the passed-in attributes.



87
88
89
90
91
# File 'lib/suitcase/hotel/hotel.rb', line 87

def initialize(info)
  info.each do |k, v|
    send (k.to_s + "=").to_sym, v
  end
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def address
  @address
end

#airport_codeObject

Returns the value of attribute airport_code.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def airport_code
  @airport_code
end

#amenitiesObject

Returns the value of attribute amenities.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def amenities
  @amenities
end

#amenity_maskObject

Returns the value of attribute amenity_mask.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def amenity_mask
  @amenity_mask
end

#checkin_instructionsObject

Returns the value of attribute checkin_instructions.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def checkin_instructions
  @checkin_instructions
end

#cityObject

Returns the value of attribute city.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def city
  @city
end

#confidence_ratingObject

Returns the value of attribute confidence_rating.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def confidence_rating
  @confidence_rating
end

#country_codeObject

Returns the value of attribute country_code.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def country_code
  @country_code
end

Returns the value of attribute deep_link.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def deep_link
  @deep_link
end

#general_policiesObject

Returns the value of attribute general_policies.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def general_policies
  @general_policies
end

#high_rateObject

Returns the value of attribute high_rate.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def high_rate
  @high_rate
end

#hotel_in_destinationObject

Returns the value of attribute hotel_in_destination.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def hotel_in_destination
  @hotel_in_destination
end

#idObject

Returns the value of attribute id.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def id
  @id
end

#imagesObject

Returns the value of attribute images.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def images
  @images
end

#latitudeObject

Returns the value of attribute latitude.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def latitude
  @latitude
end

#location_descriptionObject

Returns the value of attribute location_description.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def location_description
  @location_description
end

#longitudeObject

Returns the value of attribute longitude.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def longitude
  @longitude
end

#low_rateObject

Returns the value of attribute low_rate.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def low_rate
  @low_rate
end

#masked_amenitiesObject

Returns the value of attribute masked_amenities.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def masked_amenities
  @masked_amenities
end

#nameObject

Returns the value of attribute name.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def name
  @name
end

#nightly_rate_totalObject

Returns the value of attribute nightly_rate_total.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def nightly_rate_total
  @nightly_rate_total
end

#number_of_floorsObject

Returns the value of attribute number_of_floors.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def number_of_floors
  @number_of_floors
end

#number_of_roomsObject

Returns the value of attribute number_of_rooms.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def number_of_rooms
  @number_of_rooms
end

#postal_codeObject

Returns the value of attribute postal_code.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def postal_code
  @postal_code
end

#property_categoryObject

Returns the value of attribute property_category.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def property_category
  @property_category
end

#property_descriptionObject

Returns the value of attribute property_description.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def property_description
  @property_description
end

#provinceObject

Returns the value of attribute province.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def province
  @province
end

#proximity_distanceObject

Returns the value of attribute proximity_distance.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def proximity_distance
  @proximity_distance
end

#ratingObject

Returns the value of attribute rating.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def rating
  @rating
end

#rawObject

Returns the value of attribute raw.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def raw
  @raw
end

#short_descriptionObject

Returns the value of attribute short_description.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def short_description
  @short_description
end

#supplier_typeObject

Returns the value of attribute supplier_type.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def supplier_type
  @supplier_type
end

#tripadvisor_ratingObject

Returns the value of attribute tripadvisor_rating.



71
72
73
# File 'lib/suitcase/hotel/hotel.rb', line 71

def tripadvisor_rating
  @tripadvisor_rating
end

Class Method Details

.find(info) ⇒ Object

Public: Find a Hotel based on ID, IDs, or location (and other options).

info - A Hash of known information about the query. Depending on the

type of query being done, you can pass in three possible keys:
:ids  - An Array of unique IDs as assigned by the EAN API.
:id   - A single ID as assigned by the EAN API.
other - Any other Hash keys will be sent to the generic
        find_by_info method.

Returns a single Hotel if an ID is passed in, or an Array of Hotels.



103
104
105
106
107
108
109
110
111
# File 'lib/suitcase/hotel/hotel.rb', line 103

def self.find(info)
  if info[:ids]
    find_by_ids(info[:ids], info[:session])
  elsif info[:id]
    find_by_id(info[:id], info[:session])
  else
    find_by_info(info)
  end
end

.find_by_id(id, session) ⇒ Object

Interal: Find a Hotel by its ID.

id - The Integer or String Hotel ID. session - A Session with session data.

Returns a single Hotel.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/suitcase/hotel/hotel.rb', line 119

def self.find_by_id(id, session)
  params = { hotelId: id }

  if Configuration.cache? and Configuration.cache.cached?(:info, params)
    raw = Configuration.cache.get_query(:info, params)
  else
    url = url(method: "info", params: params, session: session)
    raw = parse_response(url)
    handle_errors(raw)
    if Configuration.cache?
      Configuration.cache.save_query(:info, params, raw)
    end
  end
  hotel_data = parse_information(raw)
  update_session(raw, session)

  h = Hotel.new(hotel_data)
  h.raw = raw
  h
end

.find_by_ids(ids, session) ⇒ Object

Internal: Find multiple Hotels based on multiple IDs.

ids - An Array of String or Integer Hotel IDs to be found. session - A Session with session data stored in it.

Returns an Array of Hotels.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/suitcase/hotel/hotel.rb', line 146

def self.find_by_ids(ids, session)
  params = { hotelIdList: ids.join(",") }

  if Configuration.cache? and Configuration.cache.cached?(:list, params)
    raw = Configuration.cache.get_query(:list, params)
  else
    url = url(method: "list", params: params, session: session)
    raw = parse_response(url)
    handle_errors(raw)
    if Configuration.cache?
      Configuration.cache.save_query(:list, params, raw)
    end
  end
  update_session(raw, session)

  [split(raw)].flatten.map do |hotel_data|
    h = Hotel.new(parse_information(hotel_data))
    h.raw = raw
    h
  end
end

.find_by_info(info) ⇒ Object

Public: Find a hotel by info other than it’s id.

info - a Hash of options described in the Hotel

accessors, excluding the id.

Returns an Array of Hotels.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/suitcase/hotel/hotel.rb', line 174

def self.find_by_info(info)
  params = info.dup
  params["numberOfResults"] = params[:results] ? params[:results] : 10
  params.delete(:results)
  if params[:destination_id]
    params["destinationId"] = params[:destination_id]
    params.delete(:destination_id)
  elsif params[:location]
    params["destinationString"] = params[:location]
    params.delete(:location)
  end

  amenities = params[:amenities] ? params[:amenities].map {|amenity| 
    AMENITIES[amenity] 
  }.join(",") : nil
  params[:amenities] = amenities if amenities

  params["minRate"] = params[:min_rate] if params[:min_rate]
  params["maxRate"] = params[:max_rate] if params[:max_rate]

  if Configuration.cache? and Configuration.cache.cached?(:list, params)
    parsed = Configuration.cache.get_query(:list, params)
  else
    url = url(method: "list", params: params, session: info[:session])
    parsed = parse_response(url)
    handle_errors(parsed)
    if Configuration.cache?
      Configuration.cache.save_query(:list, params, parsed)
    end
  end
  hotels = [split(parsed)].flatten.map do |hotel_data|
    h = Hotel.new(parse_information(hotel_data))
    h.raw = parsed
    h
  end
  update_session(parsed, info[:session])

  info[:results] ? hotels[0..(info[:results]-1)] : hotels
end

.handle_errors(info) ⇒ Object

Internal: Raise the errors returned from the response.

info - The parsed JSON to get the errors from.

Returns nothing.



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/suitcase/hotel/hotel.rb', line 295

def self.handle_errors(info)
  key = info.keys.first
  if info[key] && info[key]["EanWsError"]
    message = info[key]["EanWsError"]["presentationMessage"]
    exception = EANException.new(message)
    if message =~ /Multiple locations/ && (info = info[key]["LocationInfos"])
      exception.type = :multiple_locations
      exception.recovery = {
        alternate_locations: info["LocationInfo"].map do |info|
          Location.new(
            destination_id: info["destinationId"],
            type: info["type"],
            city: info["city"],
            province: info["stateProvinceCode"]
          )
        end
      }
    end

    raise exception
  end
end

.images(parsed) ⇒ Object

Internal: Get images from the parsed JSON.

parsed - A Hash representing the parsed JSON.

Returns an Array of Image.



278
279
280
281
282
283
284
285
286
287
288
# File 'lib/suitcase/hotel/hotel.rb', line 278

def self.images(parsed)
  images = parsed["HotelInformationResponse"]["HotelImages"]["HotelImage"].map do |image_data|
    Suitcase::Image.new(image_data)
  end if parsed["HotelInformationResponse"] && parsed["HotelInformationResponse"]["HotelImages"] && parsed["HotelInformationResponse"]["HotelImages"]["HotelImage"]
  
  unless parsed["thumbNailUrl"].nil? or parsed["thumbNailUrl"].empty?
    images = [Suitcase::Image.new("thumbnailURL" => "http://images.travelnow.com" + parsed["thumbNailUrl"])]
  end

  images || []
end

.parse_information(parsed) ⇒ Object

Public: Parse the information returned by a search request.

parsed - A Hash representing the parsed JSON.

Returns a reformatted Hash with the specified accessors.



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/suitcase/hotel/hotel.rb', line 219

def self.parse_information(parsed)
  handle_errors(parsed)
  
  if parsed["hotelId"]
    summary = parsed
    parsed_info = {}
  else
    res = parsed["HotelInformationResponse"]
    summary = res["HotelSummary"]
    parsed_info = {
      general_policies: res["HotelDetails"]["hotelPolicy"],
      checkin_instructions: res["HotelDetails"]["checkInInstructions"]
    }
  end
  proximity_distance = summary["proximityDistance"].to_s
  proximity_distance << summary["proximityUnit"].to_s
  parsed_info.merge!(
    id: summary["hotelId"],
    name: summary["name"],
    address: summary["address1"],
    city: summary["city"],
    postal_code: summary["postalCode"],
    country_code: summary["countryCode"],
    rating: summary["hotelRating"],
    high_rate: summary["highRate"],
    low_rate: summary["lowRate"],
    latitude: summary["latitude"].to_f,
    longitude: summary["longitude"].to_f,
    province: summary["stateProvinceCode"],
    airport_code: summary["airportCode"],
    property_category: summary["propertyCategory"].to_i,
    proximity_distance: proximity_distance,
    tripadvisor_rating: summary["tripAdvisorRating"],
    deep_link: summary["deepLink"]
  )
  parsed_info[:amenities] = parsed["HotelInformationResponse"]["PropertyAmenities"]["PropertyAmenity"].map do |x|
    Amenity.new(id: x["amenityId"], description: x["amenity"])
  end if parsed["HotelInformationResponse"]
  parsed_info[:images] = images(parsed) if images(parsed)
  if parsed["HotelInformationResponse"]
    parsed_info[:property_description] = parsed["HotelInformationResponse"]["HotelDetails"]["propertyDescription"]
    parsed_info[:number_of_rooms] = parsed["HotelInformationResponse"]["HotelDetails"]["numberOfRooms"]
    parsed_info[:number_of_floors] = parsed["HotelInformationResponse"]["HotelDetails"]["numberOfFloors"]
  end
  if parsed["locationDescription"]
    parsed_info[:location_description] = summary["locationDescription"]
  end
  parsed_info[:short_description] = summary["shortDescription"]
  parsed_info[:amenity_mask] = summary["amenityMask"]
  parsed_info[:masked_amenities] = Amenity.parse_mask(parsed_info[:amenity_mask])

  parsed_info
end

.split(parsed) ⇒ Object

Internal: Split an Array of multiple Hotels.

parsed - The parsed JSON of the Hotels.

Returns an Array of Hashes representing Hotels.



323
324
325
326
# File 'lib/suitcase/hotel/hotel.rb', line 323

def self.split(parsed)
  hotels = parsed["HotelListResponse"]["HotelList"]
  hotels["HotelSummary"]
end

Instance Method Details

#rooms(info) ⇒ Object

Public: Fetch possible rooms from a Hotel.

info - A Hash of options that are the accessors in Rooms.

Returns an Array of Rooms.



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/suitcase/hotel/hotel.rb', line 341

def rooms(info)
  params = { rooms: [{adults: 1, children_ages: []}] }.merge(info)
  params[:rooms].each_with_index do |room, n|
    params["room#{n+1}"] = room[:adults].to_s + "," + (room[:children_ages].join(",") if room[:children_ages])
  end
  params["arrivalDate"] = info[:arrival]
  params["departureDate"] = info[:departure]
  params["includeDetails"] = true
  params.delete(:arrival)
  params.delete(:departure)
  params["hotelId"] = @id

  if Configuration.cache? and Configuration.cache.cached?(:avail, params)
    parsed = Configuration.cache.get_query(:avail, params)
  else
    parsed = Hotel.parse_response(Hotel.url(method: "avail", params: params, session: info[:session]))
    Hotel.handle_errors(parsed)
    if Configuration.cache?
      Configuration.cache.save_query(:avail, params, parsed)
    end
  end
  hotel_id = parsed["HotelRoomAvailabilityResponse"]["hotelId"]
  rate_key = parsed["HotelRoomAvailabilityResponse"]["rateKey"]
  supplier_type = parsed["HotelRoomAvailabilityResponse"]["HotelRoomResponse"][0]["supplierType"]
  Hotel.update_session(parsed, info[:session])

  parsed["HotelRoomAvailabilityResponse"]["HotelRoomResponse"].map do |raw_data|
    room_data = {}
    room_data[:non_refundable] = raw_data["nonRefundable"]
    room_data[:deposit_required] = raw_data["depositRequired"]
    room_data[:cancellation_policy] = raw_data["cancellationPolicy"]
    room_data[:rate_code] = raw_data["rateCode"]
    room_data[:room_type_code] = raw_data["roomTypeCode"]
    room_data[:room_type_description] = raw_data["roomTypeDescription"]
    room_data[:promo] = raw_data["RateInfo"]["@promo"].to_b
    room_data[:price_breakdown] = raw_data["RateInfo"]["ChargeableRateInfo"]["NightlyRatesPerRoom"]["NightlyRate"].map do |raw|
      NightlyRate.new(raw)
    end if raw_data["RateInfo"]["ChargeableRateInfo"] && raw_data["RateInfo"]["ChargeableRateInfo"]["NightlyRatesPerRoom"] && raw_data["RateInfo"]["ChargeableRateInfo"]["NightlyRatesPerRoom"]["NightlyRate"].is_a?(Array)
    room_data[:total_price] = raw_data["RateInfo"]["ChargeableRateInfo"]["@total"]
    room_data[:nightly_rate_total] = raw_data["RateInfo"]["ChargeableRateInfo"]["@nightlyRateTotal"]
    room_data[:average_nightly_rate] = raw_data["RateInfo"]["ChargeableRateInfo"]["@averageRate"]
    room_data[:arrival] = info[:arrival]
    room_data[:departure] = info[:departure]
    room_data[:rate_key] = rate_key
    room_data[:hotel_id] = hotel_id
    room_data[:supplier_type] = supplier_type
    room_data[:rooms] = params[:rooms]
    binding.pry
    room_data[:bed_types] = [raw_data["BedTypes"]["BedType"]].flatten.map do |x|
      BedType.new(id: x["@id"], description: x["description"])
    end if raw_data["BedTypes"] && raw_data["BedTypes"]["BedType"]

    r = Room.new(room_data)
    r.raw = parsed
    r
  end
end

#thumbnail_urlObject

Public: Get the thumbnail URL of the image.

Returns a String URL to the image thumbnail.



331
332
333
334
# File 'lib/suitcase/hotel/hotel.rb', line 331

def thumbnail_url
  first_image = images.find { |img| img.thumbnail_url != nil }
  first_image.thumbnail_url if first_image
end