Class: OpeningHoursConverter::OpenIntervals

Inherits:
Object
  • Object
show all
Includes:
Constants, Utils
Defined in:
lib/opening_hours_converter/open_intervals.rb

Overview

Projects parsed date ranges onto a concrete window and returns the open intervals it contains.

Rules are applied in the order they were parsed, and a later rule overrides an earlier one on the minutes it selects, as the OpenStreetMap specification requires: "Jul-Aug off; Mo-Fr 09:00-17:00" is open in July, while "Mo-Fr 09:00-17:00; Jul-Aug off" is closed.

Constant Summary

Constants included from Constants

Constants::DAYS, Constants::DAYS_MAX, Constants::IRL_DAYS, Constants::IRL_MONTHS, Constants::MINUTES_MAX, Constants::MONTH_END_DAY, Constants::OSM_DAYS, Constants::OSM_MONTHS, Constants::PH_WEEKDAY, Constants::YEAR_DAYS_MAX

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#add_days_to_time, #datetime_to_time, #day_difference, #last_day_of_month, #leap_year?, #reindex_sunday_week_to_monday_week, #seconds_in_day, #time_to_datetime, #timstring_as_minutes, #week_difference

Constructor Details

#initialize(from, to) ⇒ OpenIntervals

Returns a new instance of OpenIntervals.



20
21
22
23
24
25
26
27
28
29
# File 'lib/opening_hours_converter/open_intervals.rb', line 20

def initialize(from, to)
  @from = from
  @to = to
  # One day before the window, so an interval crossing midnight into it is
  # not lost.
  @first_date = from.to_date - 1
  @last_date = to.to_date
  @masks = {}
  @holidays = {}
end

Class Method Details

.call(opening_hours_string, from, to) ⇒ Object



15
16
17
18
# File 'lib/opening_hours_converter/open_intervals.rb', line 15

def self.call(opening_hours_string, from, to)
  date_ranges = OpeningHoursConverter::OpeningHoursParser.new.parse(opening_hours_string)
  new(from, to).apply(date_ranges).intervals
end

Instance Method Details

#apply(date_ranges) ⇒ Object



31
32
33
34
# File 'lib/opening_hours_converter/open_intervals.rb', line 31

def apply(date_ranges)
  expand(date_ranges).each { |date_range, bounds| apply_date_range(date_range, bounds) }
  self
end

#intervalsObject



36
37
38
39
# File 'lib/opening_hours_converter/open_intervals.rb', line 36

def intervals
  runs.select { |run| run[:end] > @from && run[:start] < @to }
      .map { |run| { start: [run[:start], @from].max, end: [run[:end], @to].min } }
end