lib/vrbo/calendar.rb in vrbo-1.1.0 vs lib/vrbo/calendar.rb in vrbo-2.0.1
- old
+ new
@@ -1,68 +1,69 @@
-require 'vrbo/class_methods'
-
module VRBO
class Calendar
- extend ClassMethods
- attr_accessor :id, :available_dates, :days
+ attr_accessor :id, :days
def initialize(calendar_id = nil)
@id = calendar_id || VRBO.config.calendar_id
@days = {}
- @available_dates = []
+ @available_dates = nil
end
- def available?(arrival, depart, my_dates = nil)
- dates = my_dates || available_dates
+ def available_dates
+ @available_dates ||= Date.today.upto(Date.today + 365).map { |date| date_if_available(date) }.compact
+ end
+
+ # @description exclusive, drops day from departure because bookings usually go on per nightly
+ def available?(arrival, depart, dates = nil)
+ dates = dates || available_dates
available = dates.any?
arrival.upto(depart - 1).each do |date|
available = false unless dates.include?(date.to_s)
end
available
end
- def find_available_dates
- today = Date.today
- @available_dates = today.upto(today + 365).map { |date| date_if_available(date) }.compact
- end
-
def url(protocol = 'http')
if id
"#{protocol}://www.vrbo.com/#{id}/calendar"
else
- raise ArgumentError, 'You must provide a calendar id'
+ fail ArgumentError, 'calendar_id is required! You can initialize with a calendar_id or configure the module VRBO'
end
end
- alias :find_all_available_dates :find_available_dates
+ #
+ # Private
+ #
- private
-
def date_if_available(date)
m = date.month.to_s
days[m] ||= collect_days_for_month(date)
date.to_s if days[m].include?(date.day.to_s)
end
def collect_days_for_month(date)
- scrape_table_for(date).map { |cell| cell.children.to_s.strip }
+ table = calendar.search('.cal-month').at("//b[contains(text(), '#{date.strftime('%B %Y')}')]/following-sibling::table")
+ table.search('td:not(.strike)').map { |cell| cell.children.to_s.strip }
+ rescue => e
+ puts e.class
+ puts e.message
+ puts e.backtrace
+ puts calendar: calendar
end
- def scrape_table_for(date)
- calendar.search('.cal-month').at(table_xpath(date)).search('td:not(.strike)')
- end
+ # , retries: 10
+ # collect_days_for_month(date, retries: retries - 1) if table.nil? && retries > 0
def calendar
- @calendar ||= agent.get(url)
+ @calendar ||= Mechanize.start do |agent|
+ agent.open_timeout = 10
+ agent.read_timeout = 10
+ agent.follow_meta_refresh = true
+ agent.keep_alive = true
+ agent.max_history = 1
+ agent.user_agent_alias = 'Mac Safari'
+ agent.get(url).parser
+ end
end
-
- def agent
- @agent ||= Mechanize.new
- end
-
- # e.g. March 2014
- def table_xpath(date)
- "//b[contains(text(), '#{date.strftime('%B %Y')}')]/following-sibling::table"
- end
end
-end
\ No newline at end of file
+end