Class: Parsec::Hotel
Constant Summary collapse
- FACTS_WHITE_LIST =
%w[Kind Floors\ Number Rooms\ Number Year\ Built Year\ Renovated].freeze
Constants inherited from Base
Instance Attribute Summary collapse
-
#additional_info ⇒ Object
Returns the value of attribute additional_info.
-
#address ⇒ Object
Returns the value of attribute address.
-
#check_in ⇒ Object
Returns the value of attribute check_in.
-
#check_out ⇒ Object
Returns the value of attribute check_out.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#descriptions ⇒ Object
Returns the value of attribute descriptions.
-
#email ⇒ Object
Returns the value of attribute email.
-
#entertainments ⇒ Object
Returns the value of attribute entertainments.
-
#error ⇒ Object
Returns the value of attribute error.
-
#facts ⇒ Object
Returns the value of attribute facts.
-
#fax ⇒ Object
Returns the value of attribute fax.
-
#hotel_facilities ⇒ Object
Returns the value of attribute hotel_facilities.
-
#hotel_url ⇒ Object
Returns the value of attribute hotel_url.
-
#images ⇒ Object
Returns the value of attribute images.
-
#location ⇒ Object
Returns the value of attribute location.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parsec_location_id ⇒ Object
Returns the value of attribute parsec_location_id.
-
#parsec_region_id ⇒ Object
Returns the value of attribute parsec_region_id.
-
#phone ⇒ Object
Returns the value of attribute phone.
-
#points_of_interest ⇒ Object
Returns the value of attribute points_of_interest.
-
#rating ⇒ Object
Returns the value of attribute rating.
-
#room_facilities ⇒ Object
Returns the value of attribute room_facilities.
Class Method Summary collapse
Instance Method Summary collapse
- #add_address(address) ⇒ Object
- #add_contact_info(contact_info) ⇒ Object
- #add_descriptions(info) ⇒ Object
- #add_details(details_json) ⇒ Object
- #add_details_with_nokogiri(document) ⇒ Object
- #add_error ⇒ Object
-
#add_extentions(extentions) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/LineLength.
- #add_hotel_info(hotel_info) ⇒ Object
- #add_images(info) ⇒ Object
- #add_location(position) ⇒ Object
- #add_phones(contact_numbers) ⇒ Object
- #add_policy(policy) ⇒ Object
- #find_fax(contact_numbers) ⇒ Object
- #find_phone(contact_numbers) ⇒ Object
-
#initialize(code:, name: nil, rating: nil, address: nil) ⇒ Hotel
constructor
A new instance of Hotel.
-
#transform_facility(facility) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/LineLength.
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 = @address = address @hotel_facilities = {} @room_facilities = {} @entertainments = {} @additional_info = {} @facts = {} @points_of_interest = {} @descriptions = {} @images = [] end |
Instance Attribute Details
#additional_info ⇒ Object
Returns the value of attribute additional_info.
6 7 8 |
# File 'lib/parsec/hotel.rb', line 6 def additional_info @additional_info end |
#address ⇒ Object
Returns the value of attribute address.
6 7 8 |
# File 'lib/parsec/hotel.rb', line 6 def address @address end |
#check_in ⇒ Object
Returns the value of attribute check_in.
6 7 8 |
# File 'lib/parsec/hotel.rb', line 6 def check_in @check_in end |
#check_out ⇒ Object
Returns the value of attribute check_out.
6 7 8 |
# File 'lib/parsec/hotel.rb', line 6 def check_out @check_out end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
5 6 7 |
# File 'lib/parsec/hotel.rb', line 5 def code @code end |
#descriptions ⇒ Object
Returns the value of attribute descriptions.
6 7 8 |
# File 'lib/parsec/hotel.rb', line 6 def descriptions @descriptions end |
#email ⇒ Object
Returns the value of attribute email.
6 7 8 |
# File 'lib/parsec/hotel.rb', line 6 def email @email end |
#entertainments ⇒ Object
Returns the value of attribute entertainments.
6 7 8 |
# File 'lib/parsec/hotel.rb', line 6 def entertainments @entertainments end |
#error ⇒ Object
Returns the value of attribute error.
6 7 8 |
# File 'lib/parsec/hotel.rb', line 6 def error @error end |
#facts ⇒ Object
Returns the value of attribute facts.
6 7 8 |
# File 'lib/parsec/hotel.rb', line 6 def facts @facts end |
#fax ⇒ Object
Returns the value of attribute fax.
6 7 8 |
# File 'lib/parsec/hotel.rb', line 6 def fax @fax end |
#hotel_facilities ⇒ Object
Returns the value of attribute hotel_facilities.
6 7 8 |
# File 'lib/parsec/hotel.rb', line 6 def hotel_facilities @hotel_facilities end |
#hotel_url ⇒ Object
Returns the value of attribute hotel_url.
6 7 8 |
# File 'lib/parsec/hotel.rb', line 6 def hotel_url @hotel_url end |
#images ⇒ Object
Returns the value of attribute images.
6 7 8 |
# File 'lib/parsec/hotel.rb', line 6 def images @images end |
#location ⇒ Object
Returns the value of attribute location.
6 7 8 |
# File 'lib/parsec/hotel.rb', line 6 def location @location end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/parsec/hotel.rb', line 6 def name @name end |
#parsec_location_id ⇒ Object
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_id ⇒ Object
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 |
#phone ⇒ Object
Returns the value of attribute phone.
6 7 8 |
# File 'lib/parsec/hotel.rb', line 6 def phone @phone end |
#points_of_interest ⇒ Object
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 |
#rating ⇒ Object
Returns the value of attribute rating.
6 7 8 |
# File 'lib/parsec/hotel.rb', line 6 def @rating end |
#room_facilities ⇒ Object
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_error ⇒ Object
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. ||= 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 |