Class: VRBO::Calendar

Inherits:
Object
  • Object
show all
Defined in:
lib/vrbo/calendar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(calendar_id = nil) ⇒ Calendar

Returns a new instance of Calendar.



6
7
8
9
10
# File 'lib/vrbo/calendar.rb', line 6

def initialize(calendar_id = nil)
  @id = calendar_id || VRBO.config.calendar_id
  @days = {}
  @available_dates = nil
end

Instance Attribute Details

#daysObject

Returns the value of attribute days.



4
5
6
# File 'lib/vrbo/calendar.rb', line 4

def days
  @days
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/vrbo/calendar.rb', line 4

def id
  @id
end

Instance Method Details

#available?(arrival, depart, dates = nil) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
# File 'lib/vrbo/calendar.rb', line 17

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

#available_datesObject



12
13
14
# File 'lib/vrbo/calendar.rb', line 12

def available_dates
  @available_dates ||= today.upto(today + 365).map { |date| date_if_available(date) }.compact
end

#calendarObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/vrbo/calendar.rb', line 58

def calendar
  @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

#collect_days_for_month(date) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/vrbo/calendar.rb', line 48

def collect_days_for_month(date)
  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

#date_if_available(date) ⇒ Object



42
43
44
45
46
# File 'lib/vrbo/calendar.rb', line 42

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

#todayObject

Private



38
39
40
# File 'lib/vrbo/calendar.rb', line 38

def today
  @today ||= Date.respond_to?(:current) ? Date.current : Date.today
end

#url(protocol = 'http') ⇒ Object



26
27
28
29
30
31
32
# File 'lib/vrbo/calendar.rb', line 26

def url(protocol = 'http')
  if id
    "#{protocol}://www.vrbo.com/#{id}/calendar"
  else
    fail ArgumentError, 'calendar_id is required! You can initialize with a calendar_id or configure the module VRBO'
  end
end