Class: Suitcase::Hotel

Inherits:
Object
  • Object
show all
Extended by:
Helpers
Defined in:
lib/suitcase/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/bed_type.rb,
lib/suitcase/hotel/location.rb,
lib/suitcase/hotel/surcharge.rb,
lib/suitcase/hotel/reservation.rb,
lib/suitcase/hotel/cancellation.rb,
lib/suitcase/hotel/ean_exception.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, BedType, Cache, Cancellation, EANException, Location, PaymentOption, Reservation, Room, Session, Surcharge

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, handle_errors, main_url, parameterize, parameterize_rooms, 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.



52
53
54
55
56
# File 'lib/suitcase/hotel.rb', line 52

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.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def address
  @address
end

#airport_codeObject

Returns the value of attribute airport_code.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def airport_code
  @airport_code
end

#amenitiesObject

Returns the value of attribute amenities.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def amenities
  @amenities
end

#amenity_maskObject

Returns the value of attribute amenity_mask.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def amenity_mask
  @amenity_mask
end

#checkin_instructionsObject

Returns the value of attribute checkin_instructions.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def checkin_instructions
  @checkin_instructions
end

#cityObject

Returns the value of attribute city.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def city
  @city
end

#confidence_ratingObject

Returns the value of attribute confidence_rating.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def confidence_rating
  @confidence_rating
end

#country_codeObject

Returns the value of attribute country_code.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def country_code
  @country_code
end

Returns the value of attribute deep_link.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def deep_link
  @deep_link
end

#general_policiesObject

Returns the value of attribute general_policies.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def general_policies
  @general_policies
end

#high_rateObject

Returns the value of attribute high_rate.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def high_rate
  @high_rate
end

#hotel_in_destinationObject

Returns the value of attribute hotel_in_destination.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def hotel_in_destination
  @hotel_in_destination
end

#idObject

Returns the value of attribute id.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def id
  @id
end

#imagesObject

Returns the value of attribute images.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def images
  @images
end

#latitudeObject

Returns the value of attribute latitude.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def latitude
  @latitude
end

#location_descriptionObject

Returns the value of attribute location_description.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def location_description
  @location_description
end

#longitudeObject

Returns the value of attribute longitude.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def longitude
  @longitude
end

#low_rateObject

Returns the value of attribute low_rate.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def low_rate
  @low_rate
end

#lowest_average_nightly_rateObject

Returns the value of attribute lowest_average_nightly_rate.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def lowest_average_nightly_rate
  @lowest_average_nightly_rate
end

#masked_amenitiesObject

Returns the value of attribute masked_amenities.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def masked_amenities
  @masked_amenities
end

#nameObject

Returns the value of attribute name.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def name
  @name
end

#nightly_rate_totalObject

Returns the value of attribute nightly_rate_total.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def nightly_rate_total
  @nightly_rate_total
end

#number_of_floorsObject

Returns the value of attribute number_of_floors.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def number_of_floors
  @number_of_floors
end

#number_of_roomsObject

Returns the value of attribute number_of_rooms.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def number_of_rooms
  @number_of_rooms
end

#postal_codeObject

Returns the value of attribute postal_code.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def postal_code
  @postal_code
end

#property_categoryObject

Returns the value of attribute property_category.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def property_category
  @property_category
end

#property_descriptionObject

Returns the value of attribute property_description.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def property_description
  @property_description
end

#provinceObject

Returns the value of attribute province.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def province
  @province
end

#proximity_distanceObject

Returns the value of attribute proximity_distance.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def proximity_distance
  @proximity_distance
end

#ratingObject

Returns the value of attribute rating.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def rating
  @rating
end

#rawObject

Returns the value of attribute raw.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def raw
  @raw
end

#short_descriptionObject

Returns the value of attribute short_description.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def short_description
  @short_description
end

#supplier_typeObject

Returns the value of attribute supplier_type.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def supplier_type
  @supplier_type
end

#tripadvisor_ratingObject

Returns the value of attribute tripadvisor_rating.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def tripadvisor_rating
  @tripadvisor_rating
end

#tripadvisor_rating_urlObject

Returns the value of attribute tripadvisor_rating_url.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def tripadvisor_rating_url
  @tripadvisor_rating_url
end

#tripadvisor_review_countObject

Returns the value of attribute tripadvisor_review_count.



36
37
38
# File 'lib/suitcase/hotel.rb', line 36

def tripadvisor_review_count
  @tripadvisor_review_count
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.



68
69
70
71
72
73
74
75
76
# File 'lib/suitcase/hotel.rb', line 68

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.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/suitcase/hotel.rb', line 84

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.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/suitcase/hotel.rb', line 111

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)

  hotels = [split(raw)].flatten.map do |hotel_data|
    h = Hotel.new(parse_information(hotel_data))
    h
  end;hotels.first.raw = raw
  hotels
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.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/suitcase/hotel.rb', line 139

def self.find_by_info(info)
  params = info.dup

  # Don't parse any params if using EAN's pagination
  unless params.keys.any?{ |k| [:cache_key, :cacheKey].include?(k.to_sym) }
    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
  end

  if info[:arrival] && info[:departure]
    params["arrivalDate"] = info[:arrival]
    params["departureDate"] = info[:departure]
    params.delete(:arrival)
    params.delete(:departure)

    params.merge!(parameterize_rooms(info[:rooms] || [{ adults: 1 }]))
    params.delete(:rooms)
  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))
  end;hotels.first.raw = parsed
  
  update_session(parsed, info[:session])

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

.images(parsed) ⇒ Object

Internal: Get images from the parsed JSON.

parsed - A Hash representing the parsed JSON.

Returns an Array of Image.



264
265
266
267
268
269
270
271
272
273
274
# File 'lib/suitcase/hotel.rb', line 264

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.



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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
# File 'lib/suitcase/hotel.rb', line 198

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"],
    tripadvisor_rating_url: summary["tripAdvisorRatingUrl"],
    tripadvisor_review_count: summary["tripAdvisorReviewCount"],
    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 summary["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])
  begin
    parsed_info[:lowest_average_nightly_rate] = summary["RoomRateDetailsList"]["RoomRateDetails"]["RateInfos"]["RateInfo"]["ChargeableRateInfo"]["@averageRate"]
  rescue
    parsed_info[:lowest_average_nightly_rate] = "unknown"
  end

  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.



281
282
283
284
# File 'lib/suitcase/hotel.rb', line 281

def self.split(parsed)
  hotels = parsed["HotelListResponse"]["HotelList"]
  hotels["HotelSummary"] || [] # If there are no hotels in the response, this would return nil without the [] shortcircuit
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.



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
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
# File 'lib/suitcase/hotel.rb', line 299

def rooms(info)
  params = { rooms: [{adults: 1, children_ages: []}] }.merge(info)
  params.merge!(Hotel.parameterize_rooms(params[:rooms]))
  params.delete(:rooms)
  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
  res = parsed["HotelRoomAvailabilityResponse"]
  hotel_room_res = [res["HotelRoomResponse"]].flatten
  hotel_id = res["hotelId"]
  rate_key = res["rateKey"]
  supplier_type = hotel_room_res[0]["supplierType"]
  Hotel.update_session(parsed, info[:session])

  hotel_room_res.map do |raw_data|
    room_data = {}
    room_data[:non_refundable] = raw_data["nonRefundable"]
    room_data[:deposit_required] = raw_data["depositRequired"]
    room_data[:guarantee_only] = raw_data["guaranteeRequired"]
    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[:rate_description] = raw_data["rateDescription"]

    rate_info = raw_data["RateInfos"]["RateInfo"]

    room_data[:promo] = rate_info["@promo"].to_b
    room_data[:price_breakdown] = rate_info["ChargeableRateInfo"]["NightlyRatesPerRoom"]["NightlyRate"].map do |raw|
      NightlyRate.new(raw)
    end if rate_info["ChargeableRateInfo"] &&
            rate_info["ChargeableRateInfo"]["NightlyRatesPerRoom"] &&
            rate_info["ChargeableRateInfo"]["NightlyRatesPerRoom"]["NightlyRate"].is_a?(Array)
    room_data[:total_price] = rate_info["ChargeableRateInfo"]["@total"]
    room_data[:max_nightly_rate] = rate_info["ChargeableRateInfo"]["@maxNightlyRate"]
    room_data[:nightly_rate_total] = rate_info["ChargeableRateInfo"]["@nightlyRateTotal"]
    room_data[:average_nightly_rate] = rate_info["ChargeableRateInfo"]["@averageRate"]
    room_data[:surcharges] = rate_info["ChargeableRateInfo"] &&
      rate_info["ChargeableRateInfo"]["Surcharges"] &&
      [rate_info["ChargeableRateInfo"]["Surcharges"]["Surcharge"]].
        flatten.map { |s| Surcharge.parse(s) }

    room_data[:rate_change] = raw_data["rateChange"]
    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]
            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.



289
290
291
292
# File 'lib/suitcase/hotel.rb', line 289

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