Class: Parsec::Hotel

Inherits:
Base
  • Object
show all
Defined in:
lib/parsec/hotel.rb

Constant Summary collapse

FACTS_WHITE_LIST =
%w[Kind Floors\ Number Rooms\ Number Year\ Built Year\ Renovated].freeze

Constants inherited from Base

Base::DATE_FORMAT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code:, name: nil, rating: nil, address: nil) ⇒ Hotel

Returns a new instance of Hotel.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/parsec/hotel.rb', line 10

def initialize(code:, name: nil, rating: nil, address: nil)
  @code = code
  @name = name
  @rating = rating
  @address = address
  @hotel_facilities = {}
  @room_facilities = {}
  @entertainments = {}
  @additional_info = {}
  @facts = {}
  @points_of_interest = {}
  @descriptions = {}
  @images = []
end

Instance Attribute Details

#additional_infoObject

Returns the value of attribute additional_info.



6
7
8
# File 'lib/parsec/hotel.rb', line 6

def additional_info
  @additional_info
end

#addressObject

Returns the value of attribute address.



6
7
8
# File 'lib/parsec/hotel.rb', line 6

def address
  @address
end

#check_inObject

Returns the value of attribute check_in.



6
7
8
# File 'lib/parsec/hotel.rb', line 6

def check_in
  @check_in
end

#check_outObject

Returns the value of attribute check_out.



6
7
8
# File 'lib/parsec/hotel.rb', line 6

def check_out
  @check_out
end

#codeObject (readonly)

Returns the value of attribute code.



5
6
7
# File 'lib/parsec/hotel.rb', line 5

def code
  @code
end

#descriptionsObject

Returns the value of attribute descriptions.



6
7
8
# File 'lib/parsec/hotel.rb', line 6

def descriptions
  @descriptions
end

#emailObject

Returns the value of attribute email.



6
7
8
# File 'lib/parsec/hotel.rb', line 6

def email
  @email
end

#entertainmentsObject

Returns the value of attribute entertainments.



6
7
8
# File 'lib/parsec/hotel.rb', line 6

def entertainments
  @entertainments
end

#errorObject

Returns the value of attribute error.



6
7
8
# File 'lib/parsec/hotel.rb', line 6

def error
  @error
end

#factsObject

Returns the value of attribute facts.



6
7
8
# File 'lib/parsec/hotel.rb', line 6

def facts
  @facts
end

#faxObject

Returns the value of attribute fax.



6
7
8
# File 'lib/parsec/hotel.rb', line 6

def fax
  @fax
end

#hotel_facilitiesObject

Returns the value of attribute hotel_facilities.



6
7
8
# File 'lib/parsec/hotel.rb', line 6

def hotel_facilities
  @hotel_facilities
end

#hotel_urlObject

Returns the value of attribute hotel_url.



6
7
8
# File 'lib/parsec/hotel.rb', line 6

def hotel_url
  @hotel_url
end

#imagesObject

Returns the value of attribute images.



6
7
8
# File 'lib/parsec/hotel.rb', line 6

def images
  @images
end

#locationObject

Returns the value of attribute location.



6
7
8
# File 'lib/parsec/hotel.rb', line 6

def location
  @location
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/parsec/hotel.rb', line 6

def name
  @name
end

#parsec_location_idObject

Returns the value of attribute parsec_location_id.



6
7
8
# File 'lib/parsec/hotel.rb', line 6

def parsec_location_id
  @parsec_location_id
end

#parsec_region_idObject

Returns the value of attribute parsec_region_id.



6
7
8
# File 'lib/parsec/hotel.rb', line 6

def parsec_region_id
  @parsec_region_id
end

#phoneObject

Returns the value of attribute phone.



6
7
8
# File 'lib/parsec/hotel.rb', line 6

def phone
  @phone
end

#points_of_interestObject

Returns the value of attribute points_of_interest.



6
7
8
# File 'lib/parsec/hotel.rb', line 6

def points_of_interest
  @points_of_interest
end

#ratingObject

Returns the value of attribute rating.



6
7
8
# File 'lib/parsec/hotel.rb', line 6

def rating
  @rating
end

#room_facilitiesObject

Returns the value of attribute room_facilities.



6
7
8
# File 'lib/parsec/hotel.rb', line 6

def room_facilities
  @room_facilities
end

Class Method Details

.build(hotel_json) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/parsec/hotel.rb', line 25

def self.build(hotel_json)
  hotel = new(code: hotel_json[:@hotel_code],
              name: hotel_json[:@hotel_name],
              rating: hotel_json[:award][:@rating])
  # rooms: []) doesn't work now - this option has to be enabled on Parsec side
  hotel.add_address(hotel_json[:address])
  hotel.add_phones(Array.wrap(hotel_json.dig(:contact_numbers, :contact_number)))
  hotel.email = hotel_json.dig(:tpa_extensions, :email)

  hotel
end

Instance Method Details

#add_address(address) ⇒ Object



108
109
110
111
112
113
# File 'lib/parsec/hotel.rb', line 108

def add_address(address)
  return if address.blank?
  return if address[:address_line].blank?

  self.address = [address[:address_line], address[:city_name], address[:country_name]].join(', ')
end

#add_contact_info(contact_info) ⇒ Object



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

def add_contact_info(contact_info)
  return if contact_info.blank?

  self.email ||= contact_info.dig(:emails, :email)
  # self.images = Array.wrap(contact_info.dig(:ur_ls, :url))
  add_address(contact_info.dig(:addresses, :address))
  add_phones(Array.wrap(contact_info.dig(:phones, :phone)))
end

#add_descriptions(info) ⇒ Object



142
143
144
145
146
147
# File 'lib/parsec/hotel.rb', line 142

def add_descriptions(info)
  info.search('DescriptiveText').each do |desc|
    title = desc.values.first.split(/(?=[A-Z])/).join(' ')
    descriptions[title] = desc.content
  end
end

#add_details(details_json) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/parsec/hotel.rb', line 37

def add_details(details_json)
  return add_error if details_json[:@hotel_name].blank?

  # self.name ||= details_json[:@hotel_name]
  self.parsec_region_id ||= details_json[:@region_code]
  self.parsec_location_id ||= details_json[:@location_code]
  add_hotel_info(details_json[:hotel_info])
  add_policy(details_json.dig(:policies, :policy, :policy_info))
  add_contact_info(details_json[:contact_infos][:contact_info])
  add_extentions(details_json[:tpa_extensions])
end

#add_details_with_nokogiri(document) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/parsec/hotel.rb', line 129

def add_details_with_nokogiri(document)
  info = Nokogiri::XML.parse(document.first_element_child
                                     .last_element_child
                                     .last_element_child
                                     .last_element_child
                                     .last_element_child.to_xml)

  self.name = info.search('HotelDescriptiveContent').first.attributes['HotelName'].value

  add_descriptions(info)
  add_images(info)
end

#add_errorObject



125
126
127
# File 'lib/parsec/hotel.rb', line 125

def add_error
  self.error = 'Parsec doesn\'t provide hotel details'
end

#add_extentions(extentions) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/LineLength



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/parsec/hotel.rb', line 79

def add_extentions(extentions)
  return if extentions.blank?

  self.hotel_facilities = Array.wrap(extentions.dig(:hotel_facilities, :hotel_facility)).map { |f| transform_facility(f) }.to_h
  self.room_facilities = Array.wrap(extentions.dig(:room_facilities, :room_facility)).map { |f| transform_facility(f) }.to_h
  self.entertainments = Array.wrap(extentions.dig(:entertainments, :entertainment)).map { |f| transform_facility(f) }.to_h
  self.additional_info = Array.wrap(extentions.dig(:additional_infos, :additional_info)).map { |f| transform_facility(f) }.to_h
  self.facts = Array.wrap(extentions.dig(:facts, :fact)).map do |f|
    ff = f[:@name].split(': ', 2)
    ff.size == 1 ? ff << 'null' : ff
  end.to_h.slice(*FACTS_WHITE_LIST)
  self.points_of_interest = Array.wrap(extentions.dig(:points_of_interest, :point_of_interest))
                                 .sort_by { |f| f[:@distance].to_i }.map { |f| transform_facility(f) }.to_h
end

#add_hotel_info(hotel_info) ⇒ Object



49
50
51
52
53
# File 'lib/parsec/hotel.rb', line 49

def add_hotel_info(hotel_info)
  self.rating ||= hotel_info[:category_codes][:hotel_category][:@code]
  # self.descriptions = Array.wrap(hotel_info.dig(:descriptions, :descriptive_text))
  add_location(hotel_info[:position])
end

#add_images(info) ⇒ Object



149
150
151
152
153
154
155
156
157
# File 'lib/parsec/hotel.rb', line 149

def add_images(info)
  info.search('URL').each do |url|
    if url.values.first == 'Image'
      images << url.content
    elsif url.values.first == 'Hotel'
      self.hotel_url = url.content
    end
  end
end

#add_location(position) ⇒ Object



55
56
57
# File 'lib/parsec/hotel.rb', line 55

def add_location(position)
  self.location = { lat: position[:@latitude], lon: position[:@longitude] }
end

#add_phones(contact_numbers) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/parsec/hotel.rb', line 99

def add_phones(contact_numbers)
  return if contact_numbers.blank?

  phone_number = find_phone(contact_numbers)
  fax_number = find_fax(contact_numbers)
  self.phone = phone_number if phone_number.present?
  self.fax = fax_number if fax_number.present?
end

#add_policy(policy) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/parsec/hotel.rb', line 59

def add_policy(policy)
  return if policy.blank?

  self.check_in = policy[:@check_in_time]
  check_out_data = policy[:@check_out_time]
  self.check_out = check_out_data.index('time').nil? ? check_out_data : JSON.parse(check_out_data)['time']
end

#find_fax(contact_numbers) ⇒ Object



120
121
122
123
# File 'lib/parsec/hotel.rb', line 120

def find_fax(contact_numbers)
  fax = contact_numbers.find { |number| number[:@phone_tech_type] == 'Fax' }
  fax&.dig(:@phone_number)
end

#find_phone(contact_numbers) ⇒ Object



115
116
117
118
# File 'lib/parsec/hotel.rb', line 115

def find_phone(contact_numbers)
  phone = contact_numbers.find { |number| number[:@phone_tech_type] == 'Phone' }
  phone&.dig(:@phone_number)
end

#transform_facility(facility) ⇒ Object

rubocop:enable Metrics/AbcSize, Metrics/LineLength



95
96
97
# File 'lib/parsec/hotel.rb', line 95

def transform_facility(facility)
  [facility[:@name], facility.except(:@name).transform_keys { |k| k.to_s.delete('@') }]
end