Class: VRBO::Calendar
- Inherits:
-
Object
- Object
- VRBO::Calendar
- Defined in:
- lib/vrbo/calendar.rb
Instance Attribute Summary collapse
-
#days ⇒ Object
Returns the value of attribute days.
-
#id ⇒ Object
Returns the value of attribute id.
Instance Method Summary collapse
- #available?(arrival, depart, dates = nil) ⇒ Boolean
- #available_dates ⇒ Object
- #calendar ⇒ Object
- #collect_days_for_month(date) ⇒ Object
- #date_if_available(date) ⇒ Object
-
#initialize(calendar_id = nil) ⇒ Calendar
constructor
A new instance of Calendar.
-
#today ⇒ Object
Private.
- #url(protocol = 'http') ⇒ Object
Constructor Details
Instance Attribute Details
#days ⇒ Object
Returns the value of attribute days.
4 5 6 |
# File 'lib/vrbo/calendar.rb', line 4 def days @days end |
#id ⇒ Object
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
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_dates ⇒ Object
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 |
#calendar ⇒ Object
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. = 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. 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 |
#today ⇒ Object
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 |