Guest User

Untitled

a guest
Jan 21st, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.54 KB | None | 0 0
  1. require 'rest-client'
  2. require 'json'
  3.  
  4. module HomeawayService
  5. module_function
  6.  
  7. attr_accessor :token
  8.  
  9. def auth
  10. response = client('accessToken').post(
  11. credentials.to_json
  12. )
  13. body = JSON.parse(response)
  14. @token = body['responseEntity']['encodedHeader']
  15. end
  16.  
  17. def fetch_all_stays!
  18. auth
  19. stays = fetch_stays
  20. stays.each do |stay|
  21. stay['price_per_month'] = stay['enabled'] ? quote(stay) : 0
  22. homeaway = Homeaway.find_or_create_by(source_id: stay['id'])
  23. homeaway.update_attributes(listing_url: stay['listingUrl'],
  24. unit_url: stay['unitUrl'],
  25. enabled: stay['enabled'],
  26. price_per_month: stay['price_per_month'])
  27. if homeaway.enabled?
  28. fetch_stay(stay)
  29. else
  30. unpublish_stay(stay)
  31. end
  32. end
  33. end
  34.  
  35. def inquiry(guest, unit_url, checkin_date, checkout_date, message, adults_count)
  36. auth
  37. payload = {
  38. phoneCountryCode: '+1',
  39. phoneNumber: guest.phone,
  40. emailAddress: guest.email,
  41. firstName: guest.first_name,
  42. lastName: guest.last_name,
  43. ipAddress: '',
  44. locale: 'EN',
  45. posa: nil,
  46. subscribes: false,
  47. unitUrl: unit_url,
  48. checkin: checkin_date,
  49. checkout: checkout_date,
  50. comments: message,
  51. adults: adults_count,
  52. children: 0
  53. }.to_json
  54. response = client('vacationRentalInquiry').post(payload)
  55. JSON.parse(response)
  56. end
  57.  
  58. def fetch_stay(stay)
  59. stay_data = fetch_stay_data(stay)
  60. fill_stay_data(stay, stay_data)
  61. end
  62.  
  63. def fetch_stay_data(stay)
  64. auth
  65. response = client('vacationRentalAdvertisement').get({
  66. params: {
  67. unitUrl: stay['unitUrl']
  68. }
  69. })
  70. JSON.parse(response)
  71. end
  72.  
  73. def fetch_stays(from_date = Date.today, to_date = Date.today + 10.days)
  74. response = client('vacationRentalIndexFeed').get({
  75. params: {
  76. fromDate: from_date,
  77. toDate: to_date
  78. }
  79. })
  80. body = JSON.parse(response)
  81. body['responseEntity']['entries']
  82. end
  83.  
  84. def fetch_avail_dates(unit_url)
  85. payload = {
  86. params: {
  87. unitUrl: unit_url
  88. }
  89. }
  90. response = client('vacationRentalAvailability').get(payload)
  91. body = JSON.parse(response)
  92.  
  93. begin_date = body['responseEntity']['units'].first['unitAvailability']['dateRange']['beginDate'].to_date
  94. avail = body['responseEntity']['units'].first['unitAvailability']['unitAvailabilityConfiguration']['availability']
  95.  
  96. intervals_from_avail(begin_date, avail)
  97. end
  98.  
  99. def intervals_from_avail(begin_date, avail_string)
  100. prev_char = 'N'
  101. interval_start_date = interval_end_date = begin_date
  102. intervals = []
  103.  
  104. avail_string.split('').each_with_index do |char, index|
  105. case char
  106. when 'N'
  107. next if prev_char == 'N'
  108. prev_char = 'N'
  109. interval_end_date = begin_date + index.days
  110. intervals << [interval_start_date, interval_end_date]
  111. when 'Y'
  112. if prev_char == 'Y'
  113. if index == avail_string.size - 1
  114. interval_end_date = begin_date + index.days
  115. intervals << [interval_start_date, interval_end_date]
  116. end
  117. next
  118. else
  119. prev_char = 'Y'
  120. interval_start_date = begin_date + index.days
  121. end
  122. end
  123. end
  124. intervals
  125. end
  126.  
  127. def client(resource)
  128. RestClient::Resource.new("#{ENV['HOMEAWAY_END_POINT']}/#{resource}",
  129. headers: { 'Content-Type' => 'application/json',
  130. 'Authorization' => "Bearer #{@token}" })
  131. end
  132.  
  133. def credentials
  134. {
  135. clientKey: ENV['HOMEAWAY_CLIENT_KEY'],
  136. clientSecret: ENV['HOMEAWAY_CLIENT_SECRET']
  137. }
  138. end
  139.  
  140. def fill_stay_data(stay, stay_data)
  141. listing = Listing.where(source_type: 2, source_id_homeaway: stay['id']).first_or_initialize
  142. listing.assign_attributes(listing_params(stay_data))
  143.  
  144. listing.source_params = {}
  145. listing.source_params[:url] = stay['listingUrl']
  146. listing.source_params[:external_host_name] = host_params(stay_data)[:full_name]
  147. listing.source_params[:external_host_avatar] = host_params(stay_data)[:avatar]
  148. listing.source_params[:pictures] = pictures(stay_data)
  149.  
  150. property = Property.where(source_type: 2, source_id_homeaway: stay['id']).first_or_initialize
  151. property.assign_attributes(property_params(stay_data))
  152. property.save!
  153. listing.listable = property
  154. listing.save!
  155.  
  156. listing.availability_details.destroy_all
  157. avail_dates = fetch_avail_dates(stay['unitUrl'])
  158.  
  159. avail_dates.each do |avail_interval|
  160. availability_details = listing.availability_details.new
  161. availability_details.assign_attributes(availability_params(stay, property.number_of_beds, avail_interval))
  162. availability_details.save!
  163. end
  164. end
  165.  
  166. def listing_params(stay_data)
  167. {
  168. title: stay_data['responseEntity']['adContent']['headline']['texts']
  169. .select { |hash| hash['locale'] == 'en' }.first['content'],
  170. description: stay_data['responseEntity']['adContent']['description']['texts']
  171. .select { |hash| hash['locale'] == 'en' }.first['content'],
  172. listing_type: :whole_unit,
  173. published: true
  174. }
  175. end
  176.  
  177. def host_params(stay_data)
  178. #ToDo - find a way to get host data from HomeAway
  179. {
  180. full_name: 'HomeAway Host',
  181. avatar: nil
  182. }
  183. end
  184.  
  185. def pictures(stay_data)
  186. pictures = []
  187. stay_data['responseEntity']['photos'].each do |picture|
  188. picture_large = picture['images'].select { |hash| hash['treatment'] == '1024x768'}.first['uri']
  189. picture_medium = picture['images'].select { |hash| hash['treatment'] == '360x270'}.first['uri']
  190. picture_thumb = picture['images'].select { |hash| hash['treatment'] == '180x120'}.first['uri']
  191.  
  192. pictures.push(
  193. large: picture_large,
  194. medium: picture_medium,
  195. thumb: picture_thumb
  196. )
  197. end
  198. pictures
  199. end
  200.  
  201. def availability_params(stay, number_of_beds, avail_interval)
  202. {
  203. available_from: avail_interval[0],
  204. available_to: avail_interval[1],
  205. min_stay: nil,
  206. price_per_month: stay['price_per_month'].to_i.round,
  207. deposit: 0,
  208. number_of_beds: number_of_beds
  209. }
  210. end
  211.  
  212. def quote(stay)
  213. auth
  214. payload = {
  215. params: {
  216. unitUrl: stay['unitUrl']
  217. }
  218. }
  219. response = client('vacationRentalRates').get(payload)
  220. body = JSON.parse(response)
  221. return 0 unless body['statusCode'] == 200
  222. price_per_month =
  223. if body['responseEntity']['currency'] == 'USD'
  224. if body['responseEntity']['basePlan']
  225. if body['responseEntity']['basePlan']['preStay']['perStay']
  226. body['responseEntity']['basePlan']['preStay']['perStay']['baseTotal']['amount']['amount']
  227. elsif body['responseEntity']['basePlan']['preStay']['perAdditionalNight']
  228. body['responseEntity']['basePlan']['preStay']['perAdditionalNight']['baseTotal']['amount']['amount']
  229. else
  230. 0
  231. end
  232. else
  233. 0
  234. end
  235. else
  236. 0
  237. end
  238. price_per_month
  239. end
  240.  
  241. def property_params(stay_data)
  242. bathrooms = bedrooms = beds = 0
  243. stay_data['responseEntity']['units'].each do |unit|
  244. bathrooms += unit['numberOfBathrooms']
  245. bedrooms += unit['numberOfBedrooms']
  246. beds += unit['maxSleep']
  247. end
  248. {
  249. property_type: 'house',
  250. bath_type: :private_bath,
  251. number_of_beds: beds,
  252. number_of_bathrooms: bathrooms,
  253. number_of_bedrooms: bedrooms,
  254. location_attributes: location_params(stay_data),
  255. rules: rules_params(stay_data)
  256. }
  257. end
  258.  
  259. def location_params(stay_data)
  260. {
  261. lat: stay_data['responseEntity']['location']['latLng']['latitude'],
  262. long: stay_data['responseEntity']['location']['latLng']['longitude'],
  263. address_line_1: stay_data['responseEntity']['location']['address']['addressLine1'],
  264. city: stay_data['responseEntity']['location']['address']['city'],
  265. zipcode: stay_data['responseEntity']['location']['address']['postalCode']
  266. }
  267. end
  268.  
  269. def rules_params(stay_data)
  270. rules = []
  271. rules << 'non_smoking' if stay_data['responseEntity']['units'].first['unitRentalPolicy']['smokingAllowed']
  272. rules << 'no_pets_allowed' unless stay_data['responseEntity']['units'].first['unitRentalPolicy']['petsAllowed']
  273. rules
  274. end
  275.  
  276. def unpublish_stay(stay)
  277. listing = Listing.where(source_type: 2, source_id: stay['id']).first
  278. listing.update_attribute(:published, false) if listing
  279. end
  280.  
  281. end
Add Comment
Please, Sign In to add comment