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/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, 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, 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.


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

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


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

def address
  @address
end

#airport_codeObject

Returns the value of attribute airport_code


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

def airport_code
  @airport_code
end

#amenitiesObject

Returns the value of attribute amenities


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

def amenities
  @amenities
end

#amenity_maskObject

Returns the value of attribute amenity_mask


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

def amenity_mask
  @amenity_mask
end

#checkin_instructionsObject

Returns the value of attribute checkin_instructions


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

def checkin_instructions
  @checkin_instructions
end

#cityObject

Returns the value of attribute city


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

def city
  @city
end

#confidence_ratingObject

Returns the value of attribute confidence_rating


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

def confidence_rating
  @confidence_rating
end

#country_codeObject

Returns the value of attribute country_code


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

def country_code
  @country_code
end

Returns the value of attribute deep_link


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

def deep_link
  @deep_link
end

#general_policiesObject

Returns the value of attribute general_policies


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

def general_policies
  @general_policies
end

#high_rateObject

Returns the value of attribute high_rate


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

def high_rate
  @high_rate
end

#hotel_in_destinationObject

Returns the value of attribute hotel_in_destination


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

def hotel_in_destination
  @hotel_in_destination
end

#idObject

Returns the value of attribute id


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

def id
  @id
end

#imagesObject

Returns the value of attribute images


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

def images
  @images
end

#latitudeObject

Returns the value of attribute latitude


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

def latitude
  @latitude
end

#location_descriptionObject

Returns the value of attribute location_description


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

def location_description
  @location_description
end

#longitudeObject

Returns the value of attribute longitude


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

def longitude
  @longitude
end

#low_rateObject

Returns the value of attribute low_rate


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

def low_rate
  @low_rate
end

#masked_amenitiesObject

Returns the value of attribute masked_amenities


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

def masked_amenities
  @masked_amenities
end

#nameObject

Returns the value of attribute name


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

def name
  @name
end

#nightly_rate_totalObject

Returns the value of attribute nightly_rate_total


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

def nightly_rate_total
  @nightly_rate_total
end

#number_of_floorsObject

Returns the value of attribute number_of_floors


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

def number_of_floors
  @number_of_floors
end

#number_of_roomsObject

Returns the value of attribute number_of_rooms


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

def number_of_rooms
  @number_of_rooms
end

#postal_codeObject

Returns the value of attribute postal_code


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

def postal_code
  @postal_code
end

#property_categoryObject

Returns the value of attribute property_category


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

def property_category
  @property_category
end

#property_descriptionObject

Returns the value of attribute property_description


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

def property_description
  @property_description
end

#provinceObject

Returns the value of attribute province


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

def province
  @province
end

#proximity_distanceObject

Returns the value of attribute proximity_distance


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

def proximity_distance
  @proximity_distance
end

#ratingObject

Returns the value of attribute rating


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

def rating
  @rating
end

#rawObject

Returns the value of attribute raw


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

def raw
  @raw
end

#short_descriptionObject

Returns the value of attribute short_description


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

def short_description
  @short_description
end

#supplier_typeObject

Returns the value of attribute supplier_type


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

def supplier_type
  @supplier_type
end

#tripadvisor_ratingObject

Returns the value of attribute tripadvisor_rating


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

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.


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

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.


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

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.


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

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.


138
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
# File 'lib/suitcase/hotel.rb', line 138

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

.images(parsed) ⇒ Object

Internal: Get images from the parsed JSON.

parsed - A Hash representing the parsed JSON.

Returns an Array of Image.


242
243
244
245
246
247
248
249
250
251
252
# File 'lib/suitcase/hotel.rb', line 242

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.


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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/suitcase/hotel.rb', line 183

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.


259
260
261
262
# File 'lib/suitcase/hotel.rb', line 259

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.


277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
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
# File 'lib/suitcase/hotel.rb', line 277

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
  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"]
    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[:max_nightly_rate] = raw_data["RateInfo"]["ChargeableRateInfo"]["@maxNightlyRate"]
    room_data[:rate_change] = raw_data["rateChange"]
    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]
    room_data[:surcharges] = raw_data["RateInfo"]["ChargeableRateInfo"] &&
      raw_data["RateInfo"]["ChargeableRateInfo"]["Surcharges"] &&
      [raw_data["RateInfo"]["ChargeableRateInfo"]["Surcharges"]["Surcharge"]].
      flatten.map { |s| Surcharge.parse(s) }
    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.


267
268
269
270
# File 'lib/suitcase/hotel.rb', line 267

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