Created
December 15, 2016 20:47
-
-
Save hayduke19us/16d9a5dacbae0bcfcad703d1250c87ee 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
class SomeClass | |
attr_writer :value_key | |
def value_key | |
@value_key ||= 'content'.freeze | |
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 | |
inner_data | |
end | |
def dig_with(value_key, *keys) | |
self.value_key = value_key | |
dig(*keys) | |
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 image_urls | |
dig_collection("images", /jpg/, "path") | |
end | |
def amenity_ids | |
dig_collection("boards", /.*/, "code") | |
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: amenity_ids, | |
brand_code: 'brand', | |
currency_code: USD, | |
description: dig("description"), | |
image_urls: image_urls, | |
location_description: dig("destination", "name"), | |
name: dig("name"), | |
phone_number: dig_collection("phones", /PHONEBOOKING/, "phoneNumber"), | |
review_rating: nil, | |
star_rating: dig("category", "description") | |
}.with_indifferent_access | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment