Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'rest-client'
- require 'json'
- module HomeawayService
- module_function
- attr_accessor :token
- def auth
- response = client('accessToken').post(
- credentials.to_json
- )
- body = JSON.parse(response)
- @token = body['responseEntity']['encodedHeader']
- end
- def fetch_all_stays!
- auth
- stays = fetch_stays
- stays.each do |stay|
- stay['price_per_month'] = stay['enabled'] ? quote(stay) : 0
- homeaway = Homeaway.find_or_create_by(source_id: stay['id'])
- homeaway.update_attributes(listing_url: stay['listingUrl'],
- unit_url: stay['unitUrl'],
- enabled: stay['enabled'],
- price_per_month: stay['price_per_month'])
- if homeaway.enabled?
- fetch_stay(stay)
- else
- unpublish_stay(stay)
- end
- end
- end
- def inquiry(guest, unit_url, checkin_date, checkout_date, message, adults_count)
- auth
- payload = {
- phoneCountryCode: '+1',
- phoneNumber: guest.phone,
- emailAddress: guest.email,
- firstName: guest.first_name,
- lastName: guest.last_name,
- ipAddress: '',
- locale: 'EN',
- posa: nil,
- subscribes: false,
- unitUrl: unit_url,
- checkin: checkin_date,
- checkout: checkout_date,
- comments: message,
- adults: adults_count,
- children: 0
- }.to_json
- response = client('vacationRentalInquiry').post(payload)
- JSON.parse(response)
- end
- def fetch_stay(stay)
- stay_data = fetch_stay_data(stay)
- fill_stay_data(stay, stay_data)
- end
- def fetch_stay_data(stay)
- auth
- response = client('vacationRentalAdvertisement').get({
- params: {
- unitUrl: stay['unitUrl']
- }
- })
- JSON.parse(response)
- end
- def fetch_stays(from_date = Date.today, to_date = Date.today + 10.days)
- response = client('vacationRentalIndexFeed').get({
- params: {
- fromDate: from_date,
- toDate: to_date
- }
- })
- body = JSON.parse(response)
- body['responseEntity']['entries']
- end
- def fetch_avail_dates(unit_url)
- payload = {
- params: {
- unitUrl: unit_url
- }
- }
- response = client('vacationRentalAvailability').get(payload)
- body = JSON.parse(response)
- begin_date = body['responseEntity']['units'].first['unitAvailability']['dateRange']['beginDate'].to_date
- avail = body['responseEntity']['units'].first['unitAvailability']['unitAvailabilityConfiguration']['availability']
- intervals_from_avail(begin_date, avail)
- end
- def intervals_from_avail(begin_date, avail_string)
- prev_char = 'N'
- interval_start_date = interval_end_date = begin_date
- intervals = []
- avail_string.split('').each_with_index do |char, index|
- case char
- when 'N'
- next if prev_char == 'N'
- prev_char = 'N'
- interval_end_date = begin_date + index.days
- intervals << [interval_start_date, interval_end_date]
- when 'Y'
- if prev_char == 'Y'
- if index == avail_string.size - 1
- interval_end_date = begin_date + index.days
- intervals << [interval_start_date, interval_end_date]
- end
- next
- else
- prev_char = 'Y'
- interval_start_date = begin_date + index.days
- end
- end
- end
- intervals
- end
- def client(resource)
- RestClient::Resource.new("#{ENV['HOMEAWAY_END_POINT']}/#{resource}",
- headers: { 'Content-Type' => 'application/json',
- 'Authorization' => "Bearer #{@token}" })
- end
- def credentials
- {
- clientKey: ENV['HOMEAWAY_CLIENT_KEY'],
- clientSecret: ENV['HOMEAWAY_CLIENT_SECRET']
- }
- end
- def fill_stay_data(stay, stay_data)
- listing = Listing.where(source_type: 2, source_id_homeaway: stay['id']).first_or_initialize
- listing.assign_attributes(listing_params(stay_data))
- listing.source_params = {}
- listing.source_params[:url] = stay['listingUrl']
- listing.source_params[:external_host_name] = host_params(stay_data)[:full_name]
- listing.source_params[:external_host_avatar] = host_params(stay_data)[:avatar]
- listing.source_params[:pictures] = pictures(stay_data)
- property = Property.where(source_type: 2, source_id_homeaway: stay['id']).first_or_initialize
- property.assign_attributes(property_params(stay_data))
- property.save!
- listing.listable = property
- listing.save!
- listing.availability_details.destroy_all
- avail_dates = fetch_avail_dates(stay['unitUrl'])
- avail_dates.each do |avail_interval|
- availability_details = listing.availability_details.new
- availability_details.assign_attributes(availability_params(stay, property.number_of_beds, avail_interval))
- availability_details.save!
- end
- end
- def listing_params(stay_data)
- {
- title: stay_data['responseEntity']['adContent']['headline']['texts']
- .select { |hash| hash['locale'] == 'en' }.first['content'],
- description: stay_data['responseEntity']['adContent']['description']['texts']
- .select { |hash| hash['locale'] == 'en' }.first['content'],
- listing_type: :whole_unit,
- published: true
- }
- end
- def host_params(stay_data)
- #ToDo - find a way to get host data from HomeAway
- {
- full_name: 'HomeAway Host',
- avatar: nil
- }
- end
- def pictures(stay_data)
- pictures = []
- stay_data['responseEntity']['photos'].each do |picture|
- picture_large = picture['images'].select { |hash| hash['treatment'] == '1024x768'}.first['uri']
- picture_medium = picture['images'].select { |hash| hash['treatment'] == '360x270'}.first['uri']
- picture_thumb = picture['images'].select { |hash| hash['treatment'] == '180x120'}.first['uri']
- pictures.push(
- large: picture_large,
- medium: picture_medium,
- thumb: picture_thumb
- )
- end
- pictures
- end
- def availability_params(stay, number_of_beds, avail_interval)
- {
- available_from: avail_interval[0],
- available_to: avail_interval[1],
- min_stay: nil,
- price_per_month: stay['price_per_month'].to_i.round,
- deposit: 0,
- number_of_beds: number_of_beds
- }
- end
- def quote(stay)
- auth
- payload = {
- params: {
- unitUrl: stay['unitUrl']
- }
- }
- response = client('vacationRentalRates').get(payload)
- body = JSON.parse(response)
- return 0 unless body['statusCode'] == 200
- price_per_month =
- if body['responseEntity']['currency'] == 'USD'
- if body['responseEntity']['basePlan']
- if body['responseEntity']['basePlan']['preStay']['perStay']
- body['responseEntity']['basePlan']['preStay']['perStay']['baseTotal']['amount']['amount']
- elsif body['responseEntity']['basePlan']['preStay']['perAdditionalNight']
- body['responseEntity']['basePlan']['preStay']['perAdditionalNight']['baseTotal']['amount']['amount']
- else
- 0
- end
- else
- 0
- end
- else
- 0
- end
- price_per_month
- end
- def property_params(stay_data)
- bathrooms = bedrooms = beds = 0
- stay_data['responseEntity']['units'].each do |unit|
- bathrooms += unit['numberOfBathrooms']
- bedrooms += unit['numberOfBedrooms']
- beds += unit['maxSleep']
- end
- {
- property_type: 'house',
- bath_type: :private_bath,
- number_of_beds: beds,
- number_of_bathrooms: bathrooms,
- number_of_bedrooms: bedrooms,
- location_attributes: location_params(stay_data),
- rules: rules_params(stay_data)
- }
- end
- def location_params(stay_data)
- {
- lat: stay_data['responseEntity']['location']['latLng']['latitude'],
- long: stay_data['responseEntity']['location']['latLng']['longitude'],
- address_line_1: stay_data['responseEntity']['location']['address']['addressLine1'],
- city: stay_data['responseEntity']['location']['address']['city'],
- zipcode: stay_data['responseEntity']['location']['address']['postalCode']
- }
- end
- def rules_params(stay_data)
- rules = []
- rules << 'non_smoking' if stay_data['responseEntity']['units'].first['unitRentalPolicy']['smokingAllowed']
- rules << 'no_pets_allowed' unless stay_data['responseEntity']['units'].first['unitRentalPolicy']['petsAllowed']
- rules
- end
- def unpublish_stay(stay)
- listing = Listing.where(source_type: 2, source_id: stay['id']).first
- listing.update_attribute(:published, false) if listing
- end
- end
Add Comment
Please, Sign In to add comment