lib/vrbo/availability.rb in vrbo-0.1.1 vs lib/vrbo/availability.rb in vrbo-0.2.0

- old
+ new

@@ -1,21 +1,32 @@ module VRBO class Availability - attr_accessor :start_at, :duration, :error + attr_accessor :start_at, :duration, :error, :dates - def initialize(dates = []) - dates = dates || [] + # assumes dates are in ascending order + def initialize(the_dates = nil) + @dates = the_dates || [] if dates.any? @start_at = Date.parse(dates.shift) else @start_at = Date.today @error = 'Maybe... But likely there was an error.' end - @duration = calc_duration(dates) + @duration = count_continuous_dates end - def calc_duration(dates) - 1 + dates.each_with_index.sum { |the_date, i| Date.parse(the_date) - (start_at + i) == 1 ? 1 : 0 } + def count_continuous_dates + i = -1 + count = 1 + dates.each do |the_date| + diff = Date.parse(the_date) - (start_at + (i += 1)) + if diff.to_i == 1 + count += 1 + else + break + end + end + count end end end \ No newline at end of file