Class: OpeningHoursConverter::Iterator

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

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

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

Instance Method Details

#get_datetime_iterator(date_ranges) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/opening_hours_converter/iterator.rb', line 125

def get_datetime_iterator(date_ranges)
  result = get_iterator(date_ranges)
  datetime_result = []

  date_ranges_array.each_with_index do |result, index|
    result.each do |interval|
      (interval[:start]..interval[:end]).each do |day|
        date_ranges[index].typical.intervals.each do |i|
          if (i.day_start..i.day_end).cover?(reindex_sunday_week_to_monday_week(day.wday))
            datetime_result << { start: DateTime.new(day.year, day.month, day.day, i.start / 60, i.start % 60),
                                 end: DateTime.new(day.year, day.month, day.day, i.end / 60, i.end % 60) }
          end
        end
      end
    end
  end

  datetime_result.sort_by { |a| a[:start] }
end

#get_iterator(date_ranges) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/opening_hours_converter/iterator.rb', line 8

def get_iterator(date_ranges)
  date_ranges_array = []
  years = nil

  date_ranges.each do |date_range|
    years = OpeningHoursConverter::Year.build_day_array_from_date_range(date_range, true)

    result = []

    year_start = -1
    month_start = -1
    day_start = -1

    years.each do |year, months|
      months.each_with_index do |month_array, month|
        month_array.each_with_index do |day_bool, day|
          if day_bool && year_start < 0
            year_start = year
            month_start = month
            day_start = day
          elsif day_bool && year_start >= 0 && month == 11 && day == 30 && years[year + 1].nil?
            result << { start: DateTime.new(year_start, month_start + 1, day_start + 1), end: DateTime.new(year, 12, 31) }

            year_start = -1
            month_start = -1
            day_start = -1
          elsif !day_bool && year_start >= 0
            end_res = {}

            end_res = if day == 0
                        if month == 0
                          DateTime.new(year - 1, 12, 31)
                        else
                          DateTime.new(year, month, MONTH_END_DAY[month - 1])
                        end
                      else
                        DateTime.new(year, month + 1, day)
                      end

            result << { start: DateTime.new(year_start, month_start + 1, day_start + 1), end: end_res }
            year_start = -1
            month_start = -1
            day_start = -1
          end

          if day_bool && year_start >= 0 && month == 11 && day == 30 && years[year + 1].nil?
            result << { start: DateTime.new(year_start, month_start + 1, day_start + 1), end: DateTime.new(year, 12, 31) }
          end

        end
      end
    end

    date_ranges_array << result
  end

  date_ranges_array
end

#get_open_intervals(opening_hours_string, from, to) ⇒ Object

Open intervals of an opening hours string inside a concrete window, as [{ start: Time, end: Time }], sorted and clamped to [from, to].

Unlike get_time_iterator, this applies "off" rules and intervals crossing midnight, and covers every year the window touches rather than the current one.



121
122
123
# File 'lib/opening_hours_converter/iterator.rb', line 121

def get_open_intervals(opening_hours_string, from, to)
  OpeningHoursConverter::OpenIntervals.call(opening_hours_string, from, to)
end

#get_time_iterator(date_ranges) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/opening_hours_converter/iterator.rb', line 67

def get_time_iterator(date_ranges)
  is_ph = false
  year = nil
  year_ph = nil
  date_ranges.each do |date_range|
    is_ph = true if date_range.is_holiday?
  end

  date_ranges = date_ranges.flat_map do |date_range|
    if date_range.wide_interval.type == 'week' # Generate date range list from week syntax
      date_range.wide_interval.to_day.map do |day|
        day_range = date_range.dup
        day_range.update_range(day)
        day_range
      end
    else
      date_range
    end
  end

  date_ranges_array = get_iterator(date_ranges)
  datetime_result = []

  date_ranges_array.each_with_index do |result, index|
    result.each do |interval|
      (interval[:start]..interval[:end]).each do |day|
        if year != day.year && is_ph
          year = day.year
          year_ph = PublicHoliday.ph_for_year(year)
        end

        date_ranges[index].typical.intervals.each do |i|
          next unless !i.nil? && !i.is_off

          if date_ranges[index].typical.is_a? Week
            next unless (i.day_start..i.day_end).cover?(reindex_sunday_week_to_monday_week(day.wday)) || (is_ph && year_ph.include?(Time.new(day.year, day.month, day.day)))
          end

          itr = { start: Time.new(day.year, day.month, day.day, i.start / 60, i.start % 60), end: Time.new(day.year, day.month, day.day, i.end / 60, i.end % 60) }
          datetime_result << itr unless datetime_result.include?(itr)
        end
      end
    end
  end

  datetime_result.sort_by { |a| a[:start] }
end

#is_opened?(opening_hours_string, time = Time.now) ⇒ Boolean

A partir d'une string OH et d'une DateTime (= now par défaut), déterminer cela correspond à une période d'ouverture : renvoyer un boolean.

Returns:

  • (Boolean)


177
178
179
180
181
182
183
184
# File 'lib/opening_hours_converter/iterator.rb', line 177

def is_opened?(opening_hours_string, time = Time.now)
  date_ranges = OpeningHoursConverter::OpeningHoursParser.new.parse(opening_hours_string)
  ti = get_time_iterator(date_ranges)
  ti.each do |interval|
    return true if interval[:start] <= time && interval[:end] >= time
  end
  false
end

#next_period(opening_hours_string, time = Time.now) ⇒ Object



166
167
168
169
170
171
172
173
174
# File 'lib/opening_hours_converter/iterator.rb', line 166

def next_period(opening_hours_string, time = Time.now)
  date_ranges = OpeningHoursConverter::OpeningHoursParser.new.parse(opening_hours_string)
  ti = get_time_iterator(date_ranges)
  ti.each_with_index do |interval, index|
    return ti[index + 1] if interval[:start] <= time && interval[:end] >= time
    return interval if interval[:start] > time && ti[index - 1][:end] <= time
  end
  false
end

#next_state(opening_hours_string, time = Time.now) ⇒ Object

A partir d'une string OH et d'une DateTime (= now par défaut), renvoyer le prochain state (début / fin / commentaire - nextState dans opening_hours.js) permettant d'afficher à l'utilisateur le prochain événement (ouverture/fermeture)



156
157
158
159
160
161
162
163
164
# File 'lib/opening_hours_converter/iterator.rb', line 156

def next_state(opening_hours_string, time = Time.now)
  date_ranges = OpeningHoursConverter::OpeningHoursParser.new.parse(opening_hours_string)
  ti = get_time_iterator(date_ranges)
  ti.each_with_index do |interval, index|
    return { end: interval[:end] } if interval[:start] <= time && interval[:end] >= time
    return { start: interval[:start] } if interval[:start] > time && ti[index - 1][:end] <= time
  end
  false
end

#state(opening_hours_string, time = Time.now) ⇒ Object

A partir d'une string OH et d'une DateTime (= now par défaut), renvoyer le current state (début / fin / commentaire)



146
147
148
149
150
151
152
153
# File 'lib/opening_hours_converter/iterator.rb', line 146

def state(opening_hours_string, time = Time.now)
  date_ranges = OpeningHoursConverter::OpeningHoursParser.new.parse(opening_hours_string)
  ti = get_time_iterator(date_ranges)
  ti.each do |interval|
    return interval if interval[:start] <= time && interval[:end] >= time
  end
  false
end