Sha256: c516e634dc0e36d82bae20091545762b7799513becfcfb4c977479627186c431

Contents?: true

Size: 1.34 KB

Versions: 3

Compression:

Stored size: 1.34 KB

Contents

require 'vrbo/class_methods'

module VRBO
  class Calendar
    extend ClassMethods

    attr_accessor :id, :available_dates, :cells

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

    def available?(arrival, depart, my_dates = nil)
      dates = my_dates || available_dates
      available = dates.any? # should be true
      arrival.upto(depart - 1.day).each do |date|
        available = false if dates.exclude?(date.to_s)
      end
      available
    end

    def find_all_available_dates
      today = Date.today
      @available_dates = today.upto(today + 1.year).map { |date| availability_for(date) }.compact
    end

    def availability_for(date)
      m = date.month.to_s
      cells[m] ||= find_cells_for(date).map { |cell| cell.children.to_s.strip }

      if cells[m].include?(date.day.to_s)
        date.to_s
      else
        nil
      end
    end

    def find_cells_for(date)
      calendar.search('.cal-month').at(table_xpath(date)).search('td:not(.strike)')
    end

    def calendar
      @calendar ||= agent.get("http://www.vrbo.com/#{id}/calendar")
    end

    def agent
      @agent ||= Mechanize.new
    end

    # March 2014
    def table_xpath(date)
      "//b[contains(text(), '#{date.strftime('%B %Y')}')]/following-sibling::table"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vrbo-0.1.0 lib/vrbo/calendar.rb
vrbo-0.0.5 lib/vrbo/calendar.rb
vrbo-0.0.4 lib/vrbo/calendar.rb