Created
December 15, 2016 23:31
-
-
Save hayduke19us/5c639c4573913d03cbe09a5f463f9ff9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module HotelBeds | |
class CachedSupplierPropertyContent < ::CachedSupplierPropertyContent | |
VALUE_KEY = 'content'.freeze | |
def clean_data(data) | |
case data | |
when String then data.strip | |
else | |
data | |
end | |
end | |
def dig(*keys) | |
keys_with_value_key = keys.push(VALUE_KEY) | |
outer_data = raw_data | |
while keys_with_value_key.any? | |
inner_data = outer_data[keys.shift] | |
break unless inner_data.is_a?(Hash) | |
outer_data = inner_data | |
end | |
clean_data inner_data | |
end | |
def dig_collection(collection_key, value_pattern, value_key) | |
data = raw_data[collection_key].select { |hash| hash.values.any? { |value| value =~ value_pattern} } | |
data.many? ? data.map { |d| d[value_key] } : data.first[value_key] | |
end | |
def property_attributes | |
{ | |
address: { | |
street_address: dig("address"), | |
locality: dig("city"), | |
region: dig("country", "description"), | |
postal_code: dig("postalCode"), | |
country_code: dig("country", "code"), | |
latitude: dig("coordinates", "latitude"), | |
longitude: dig("coordinates", "longitude"), | |
}, | |
amenity_ids: dig_collection("boards", /.*/, "code"), | |
brand_code: brand_code(dig("code")), | |
currency_code: USD, | |
description: dig("description"), | |
image_urls: dig_collection("images", /jpg/, "path"), | |
location_description: dig("destination", "name"), | |
name: dig("name"), | |
phone_number: dig_collection("phones", /PHONEBOOKING/, "phoneNumber"), | |
review_rating: nil, | |
star_rating: dig("category", "description").split.first.to_f | |
}.with_indifferent_access | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment