vrbo 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9b7990c46d5ee84bfe3fd1451a4c2b5045287090
4
- data.tar.gz: 2df091092b778145702253ab076dac53cffdc98e
3
+ metadata.gz: 72cf31755f44dfcc866e18686ed537ff8c18a007
4
+ data.tar.gz: ff2eb37a978de9dbc58e7acc9d500adae7a23ffe
5
5
  SHA512:
6
- metadata.gz: 1ef5432869ea892dffe895657a580d461bb8781baf56f7efde18aeae557305c920f431b4e3371d037cac35fc65f99b54cb0fbe2edff2f5530fe7a1773cf883e9
7
- data.tar.gz: 83dab6f78e95e01ec3d963a6c23930eeaae0c3121ffcf21c0209d88ef56eef527d66f1bd80b68814c45e942d74f972d1d69d42b7f9b9dc11e4ac214cb466bbc7
6
+ metadata.gz: 9e9e470bb51bb3fbb8c89664b4a1a386cd33a5d648f325dd28450d2e773e72b101ad8f1fa91423a074cca0a1bf5b0774f4929b7627c735460bdd9ebfaec554e0
7
+ data.tar.gz: df400f306a153603eb0ee3d7787ed9b238637bddb16812e7fbf9c86dbbd6590169cb1380dffe752faa260e2afcfe0b9fcfea73f69474ea7911a41fd56b813942
data/Rakefile CHANGED
@@ -1 +1,9 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+
5
+ task default: :test
6
+
7
+ Rake::TestTask.new do |t|
8
+ t.pattern = 'test/**/*_test.rb'
9
+ end
@@ -1,21 +1,32 @@
1
1
  module VRBO
2
2
  class Availability
3
3
 
4
- attr_accessor :start_at, :duration, :error
4
+ attr_accessor :start_at, :duration, :error, :dates
5
5
 
6
- def initialize(dates = [])
7
- dates = dates || []
6
+ # assumes dates are in ascending order
7
+ def initialize(the_dates = nil)
8
+ @dates = the_dates || []
8
9
  if dates.any?
9
10
  @start_at = Date.parse(dates.shift)
10
11
  else
11
12
  @start_at = Date.today
12
13
  @error = 'Maybe... But likely there was an error.'
13
14
  end
14
- @duration = calc_duration(dates)
15
+ @duration = count_continuous_dates
15
16
  end
16
17
 
17
- def calc_duration(dates)
18
- 1 + dates.each_with_index.sum { |the_date, i| Date.parse(the_date) - (start_at + i) == 1 ? 1 : 0 }
18
+ def count_continuous_dates
19
+ i = -1
20
+ count = 1
21
+ dates.each do |the_date|
22
+ diff = Date.parse(the_date) - (start_at + (i += 1))
23
+ if diff.to_i == 1
24
+ count += 1
25
+ else
26
+ break
27
+ end
28
+ end
29
+ count
19
30
  end
20
31
  end
21
32
  end
data/lib/vrbo/calendar.rb CHANGED
@@ -4,11 +4,11 @@ module VRBO
4
4
  class Calendar
5
5
  extend ClassMethods
6
6
 
7
- attr_accessor :id, :available_dates, :cells
7
+ attr_accessor :id, :available_dates, :days
8
8
 
9
9
  def initialize(calendar_id = nil)
10
10
  @id = calendar_id || VRBO.config.calendar_id
11
- @cells = {}
11
+ @days = {}
12
12
  @available_dates = []
13
13
  end
14
14
 
@@ -23,32 +23,39 @@ module VRBO
23
23
 
24
24
  def find_all_available_dates
25
25
  today = Date.today
26
- @available_dates = today.upto(today + 365).map { |date| availability_for(date) }.compact
26
+ @available_dates = today.upto(today + 365).map { |date| date_if_available(date) }.compact
27
27
  end
28
28
 
29
- def availability_for(date)
29
+ def date_if_available(date)
30
30
  m = date.month.to_s
31
- cells[m] ||= find_cells_for(date).map { |cell| cell.children.to_s.strip }
31
+ days[m] ||= collect_days_for_month(date)
32
+ date.to_s if days[m].include?(date.day.to_s)
33
+ end
32
34
 
33
- if cells[m].include?(date.day.to_s)
34
- date.to_s
35
- else
36
- nil
37
- end
35
+ def collect_days_for_month(date)
36
+ scrape_table_for(date).map { |cell| cell.children.to_s.strip }
38
37
  end
39
38
 
40
- def find_cells_for(date)
39
+ def scrape_table_for(date)
41
40
  calendar.search('.cal-month').at(table_xpath(date)).search('td:not(.strike)')
42
41
  end
43
42
 
44
43
  def calendar
45
- @calendar ||= agent.get("http://www.vrbo.com/#{id}/calendar")
44
+ @calendar ||= agent.get(calendar_url)
46
45
  end
47
46
 
48
47
  def agent
49
48
  @agent ||= Mechanize.new
50
49
  end
51
50
 
51
+ def calendar_url
52
+ if id
53
+ "http://www.vrbo.com/#{id}/calendar"
54
+ else
55
+ raise ArgumentError, 'You must provide a calendar id'
56
+ end
57
+ end
58
+
52
59
  # March 2014
53
60
  def table_xpath(date)
54
61
  "//b[contains(text(), '#{date.strftime('%B %Y')}')]/following-sibling::table"
data/lib/vrbo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module VRBO
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/vrbo.rb CHANGED
@@ -19,4 +19,8 @@ module VRBO
19
19
  @config ||= Configuration.new
20
20
  end
21
21
 
22
+ def reset_config
23
+ @config = nil
24
+ end
25
+
22
26
  end
@@ -0,0 +1,45 @@
1
+ module CalendarDates
2
+
3
+ module Stubs
4
+ def collect_days_for_month(date)
5
+ dates = date.upto(date.next_month).to_a
6
+ dates.map(&:day).map(&:to_s).take(3)
7
+ end
8
+ end
9
+
10
+ def available?(list, a = today, b = tomorrow)
11
+ vrbo_calendar.available?(a, b, list)
12
+ end
13
+
14
+ def all
15
+ prep [today, tomorrow]
16
+ end
17
+
18
+ def random
19
+ prep all.shuffle
20
+ end
21
+
22
+ def arrival_only
23
+ prep [today]
24
+ end
25
+
26
+ def depart_only
27
+ prep [tomorrow]
28
+ end
29
+
30
+ def prep(list)
31
+ list.map(&:to_s)
32
+ end
33
+
34
+ def today
35
+ @today ||= Date.today
36
+ end
37
+
38
+ def tomorrow
39
+ @tomorrow ||= today + 1
40
+ end
41
+
42
+ def vrbo_calendar
43
+ @vrbo_calendar ||= VRBO::Calendar.prepend(Stubs).new
44
+ end
45
+ end
@@ -0,0 +1,23 @@
1
+ $: << 'test'
2
+ require 'minitest/autorun'
3
+ require 'vrbo'
4
+ require 'support/calendar_dates'
5
+
6
+ class AvailabilityTest < MiniTest::Unit::TestCase
7
+ include CalendarDates
8
+
9
+ def test_new_with_params
10
+ availability = VRBO::Availability.new(all << (tomorrow + 2).to_s)
11
+ assert_equal today, availability.start_at
12
+ assert_equal 2, availability.duration
13
+ assert_nil availability.error
14
+ end
15
+
16
+ def test_new_without_params
17
+ availability = VRBO::Availability.new
18
+ assert_equal today, availability.start_at
19
+ assert_equal 1, availability.duration
20
+ assert_includes availability.error, 'error'
21
+ end
22
+
23
+ end
@@ -0,0 +1,70 @@
1
+ $: << 'test'
2
+ require 'minitest/autorun'
3
+ require 'vrbo'
4
+ require 'support/calendar_dates'
5
+
6
+ class CalendarTest < MiniTest::Unit::TestCase
7
+ include CalendarDates
8
+
9
+ def calendar_id
10
+ 212121
11
+ end
12
+
13
+ def test_available_with_all
14
+ assert_equal true, available?(all)
15
+ end
16
+
17
+ def test_available_with_random
18
+ assert_equal true, available?(random)
19
+ end
20
+
21
+ def test_available_with_arrival
22
+ assert_equal true, available?(arrival_only)
23
+ end
24
+
25
+ def test_available_with_depart
26
+ assert_equal false, available?(depart_only)
27
+ end
28
+
29
+ def test_available_with_empty
30
+ assert_equal false, available?([])
31
+ end
32
+
33
+ def test_available_with_edge
34
+ assert_equal true, available?(all, today, tomorrow + 1)
35
+ end
36
+
37
+ def test_available_with_exceeding
38
+ assert_equal false, available?(all, today, tomorrow + 2)
39
+ end
40
+
41
+ def test_find_all_available_dates
42
+ # 3 days taken for each month (36) + 1 day padding
43
+ assert_equal 37, vrbo_calendar.find_all_available_dates.length
44
+ assert_equal today.to_s, vrbo_calendar.available_dates.first
45
+ assert_equal (today + 365).to_s, vrbo_calendar.available_dates.last
46
+ assert_equal vrbo_calendar.days.length, 12
47
+ end
48
+
49
+ def test_passing_calendar_id
50
+ cal = VRBO::Calendar.new(calendar_id)
51
+ assert_equal calendar_id, cal.id
52
+ assert_equal "http://www.vrbo.com/#{calendar_id}/calendar", cal.calendar_url
53
+ end
54
+
55
+ def test_no_calendar_id_given
56
+ assert_raises ArgumentError do
57
+ VRBO::Calendar.new.calendar_url
58
+ end
59
+ end
60
+
61
+ def test_calendar_id_from_config
62
+ VRBO.configure do |config|
63
+ config.calendar_id = calendar_id
64
+ end
65
+ cal = VRBO::Calendar.new
66
+ VRBO.reset_config
67
+ assert_equal calendar_id, cal.id
68
+ assert_equal "http://www.vrbo.com/#{calendar_id}/calendar", cal.calendar_url
69
+ end
70
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vrbo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Buckley
@@ -84,6 +84,9 @@ files:
84
84
  - lib/vrbo/class_methods.rb
85
85
  - lib/vrbo/configuration.rb
86
86
  - lib/vrbo/version.rb
87
+ - test/support/calendar_dates.rb
88
+ - test/vrbo/availability_test.rb
89
+ - test/vrbo/calendar_test.rb
87
90
  - vrbo.gemspec
88
91
  homepage: ''
89
92
  licenses:
@@ -109,4 +112,7 @@ rubygems_version: 2.1.11
109
112
  signing_key:
110
113
  specification_version: 4
111
114
  summary: Scrapes a VRBO calendar using Mechanize
112
- test_files: []
115
+ test_files:
116
+ - test/support/calendar_dates.rb
117
+ - test/vrbo/availability_test.rb
118
+ - test/vrbo/calendar_test.rb