Class: OpeningHoursConverter::OpeningHoursParser

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/opening_hours_converter/opening_hours_parser.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

Constructor Details

#initializeOpeningHoursParser

Returns a new instance of OpeningHoursParser.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 7

def initialize
  @RGX_RULE_MODIFIER = /^(open|closed|off)$/i
  @RGX_WEEK_KEY = /^week$/
  @RGX_WEEK_VAL = /^([01234]?[0-9]|5[0123])(\-([01234]?[0-9]|5[0123]))?(,([01234]?[0-9]|5[0123])(\-([01234]?[0-9]|5[0123]))?)*\:?$/
  @RGX_MONTH = /^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)(\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))?\:?$/
  @RGX_MONTHDAY = /^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ([012]?[0-9]|3[01])(\-((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) )?([012]?[0-9]|3[01]))?\:?$/
  @RGX_TIME = /^((([01]?[0-9]|2[01234])\:[012345][0-9](\-([01]?[0-9]|2[01234])\:[012345][0-9])?(,([01]?[0-9]|2[01234])\:[012345][0-9](\-([01]?[0-9]|2[01234])\:[012345][0-9])?)*)|(24\/7))$/
  @RGX_WEEKDAY = /^(((Mo|Tu|We|Th|Fr|Sa|Su)(\-(Mo|Tu|We|Th|Fr|Sa|Su))?)|(PH))(,(((Mo|Tu|We|Th|Fr|Sa|Su)(\-(Mo|Tu|We|Th|Fr|Sa|Su))?)|(PH)))*$/
  @RGX_HOLIDAY = /^(PH|SH|easter)$/
  @RGX_WD = /^(Mo|Tu|We|Th|Fr|Sa|Su)(\-(Mo|Tu|We|Th|Fr|Sa|Su))?$/
  @RGX_YEAR = /^(\d{4})(\-(\d{4}))?$/
  @RGX_YEAR_PH = /^(\d{4})( PH|(\-(\d{4}) PH))\:?$/
  @RGX_YEAR_MONTH_DAY = /^(\d{4}) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ([012]?[0-9]|3[01])(\-((\d{4}) )?((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) )?([012]?[0-9]|3[01]))?\:?$/
  @RGX_YEAR_MONTH = /^(\d{4}) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)(\-((\d{4}) )?((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)))?\:?$/
  @RGX_COMMENT = /^\"[^\"]*\"$/
end

Instance Method Details

#add_interval(typical, weekdays, times) ⇒ Object



502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 502

def add_interval(typical, weekdays, times)
  if typical.instance_of?(OpeningHoursConverter::Day)
    if weekdays[:from] != 0 || (weekdays[:to] !=0 && times[:from] <= times[:to])
      weekdays = weekdays.dup
      weekdays[:from] = 0
      if times[:from] <= times[:to]
        weekdays[:to] = 0
      else
        weekdays[:to] = 1
      end
    end
  end

  if weekdays[:from] <= weekdays[:to]
    for wd in weekdays[:from]..weekdays[:to]
      add_interval_wd(typical, times, wd)
    end
  else
    for wd in weekdays[:from]..6
      add_interval_wd(typical, times, wd)
    end
    for wd in 0..weekdays[:to]
      add_interval_wd(typical, times, wd)
    end
  end
end

#add_interval_wd(typical, times, wd) ⇒ Object



529
530
531
532
533
534
535
536
537
538
539
540
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 529

def add_interval_wd(typical, times, wd)
  if times[:to] >= times[:from]
    typical.add_interval(OpeningHoursConverter::Interval.new(wd, times[:from], wd, times[:to]))
  else
    if wd < 6
      typical.add_interval(OpeningHoursConverter::Interval.new(wd, times[:from], wd+1, times[:to]))
    else
      typical.add_interval(OpeningHoursConverter::Interval.new(wd, times[:from], wd+1, 24*60))
      typical.add_interval(OpeningHoursConverter::Interval.new(0, 0, 0, times[:to]))
    end
  end
end

#as_minutes(time) ⇒ Object



552
553
554
555
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 552

def as_minutes(time)
  values = time.split(':')
  values[0].to_i * 60 + values[1].to_i
end

#from_json(json) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 264

def from_json(json)
  parsed = JSON.parse(json)
  date_range = {}
  parsed.each do |dr|
    wi = {}
    start_day = DateTime.parse(dr["wide_interval"]["start"])
    end_day = DateTime.parse(dr["wide_interval"]["end"])
    case dr["wide_interval"]["type"]
    when "always"
      wi = OpeningHoursConverter::WideInterval.new.day(start_day.day, start_day.month, nil, end_day.day, end_day.month)
    else
      wi = OpeningHoursConverter::WideInterval.new.day(start_day.day, start_day.month, start_day.year, end_day.day, end_day.month, end_day.year)
    end
    date_range = OpeningHoursConverter::DateRange.new(wi)
    dr["typical"]["intervals"].each do |interval|
      if !interval.nil?
        start_interval = DateTime.parse(interval["start"])
        end_interval = DateTime.parse(interval["end"])
        date_range.typical.add_interval(OpeningHoursConverter::Interval.new(((start_interval.wday + 6) % 7), (start_interval.hour * 60 + start_interval.min), ((end_interval.wday + 6) % 7), (end_interval.hour * 60 + end_interval.min)))
      end
    end
    date_range.add_comment(dr["comment"])
  end
  [date_range]
end

#get_month(wrs) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 308

def get_month(wrs)
  single_month = wrs.gsub(/\:$/, '').split('-')
  month_from = OSM_MONTHS.find_index(single_month[0]) + 1
  if month_from < 1
    raise ArgumentError, "Invalid month : #{single_month[0]}"
  end

  if single_month.length > 1
    month_to = OSM_MONTHS.find_index(single_month[1]) + 1
    if month_to < 1
      raise ArgumentError, "Invalid month : #{single_month[1]}"
    end
  else
    month_to = month_from
  end
  { from: month_from, to: month_to}
end

#get_month_day(wrs) ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 326

def get_month_day(wrs)
  single_month = wrs.gsub(/\:$/, '').split('-')

  month_from = single_month[0].split(' ')
  month_from = { day: month_from[1].to_i, month: OSM_MONTHS.find_index(month_from[0]) + 1 }
  if month_from[:month] < 1
    raise ArgumentError, "Invalid month : #{month_from.inspect}"
  end

  if single_month.length > 1
    month_to = single_month[1].split(' ')
    if month_to.length > 1
      month_to = { day: month_to[1].to_i, month: OSM_MONTHS.find_index(month_to[0]) + 1 }
    else
      month_to = { day: month_to[0].to_i, month: month_from[:month] }
    end
    if month_to[:month] < 1
      raise ArgumentError, "Invalid month : #{month_to.inspect}"
    end
  else
    month_to = nil
  end
  { from_day: month_from, to_day: month_to }
end

#get_times(time_selector) ⇒ Object



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 419

def get_times(time_selector)
  times = []
  from = nil
  to = nil
  if time_selector == "24/7"
    times << {from: 0, to: 24*60}
  else
    time_selector = time_selector.split(',')
    time_selector.each do |ts|
      single_time = ts.split('-')
      from = as_minutes(single_time[0])
      if single_time.length > 1
        to = as_minutes(single_time[1])
      else
        to = from
      end
      times << {from: from, to: to}
    end
  end
  times
end

#get_weekdays(weekday_selector) ⇒ Object



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 441

def get_weekdays(weekday_selector)
  holidays = []
  weekdays = []
  wd_from = nil
  wd_to = nil

  weekday_selector = weekday_selector.split(',')
  weekday_selector.each do |wd|
    if !(@RGX_HOLIDAY =~ wd).nil?
      holidays << wd
    elsif !(@RGX_WD =~ wd).nil?
      single_weekday = wd.split('-')

      wd_from = OSM_DAYS.find_index(single_weekday[0])
      if single_weekday.length > 1
        wd_to = OSM_DAYS.find_index(single_weekday[1])
      else
        wd_to = wd_from
      end

      weekdays << {from: wd_from, to: wd_to}
    else
      raise ArgumentError, "Invalid weekday interval : #{wd}"
    end
  end

  { weekdays: weekdays, holidays: holidays }
end

#get_year(wrs) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 290

def get_year(wrs)
  single_year = wrs.gsub(/\:$/, '').split('-')
  year_from = single_year[0].to_i
  if year_from < 1
    raise ArgumentError, "Invalid year : #{single_year[0]}"
  end

  if single_year.length > 1
    year_to = single_year[1].to_i
    if year_to < 1
      raise ArgumentError, "Invalid year : #{single_year[1]}"
    end
  else
    year_to = nil
  end
  { from: year_from, to: year_to }
end

#get_year_holiday(wrs) ⇒ Object



374
375
376
377
378
379
380
381
382
383
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 374

def get_year_holiday(wrs)
  single_year = wrs.gsub(/\:$/, '').split('-')
  if single_year.length == 1
    return { holiday: single_year[0].split(' ')[1], start: single_year[0].split(' ')[0] }
  elsif single_year.length == 2
    return { holiday: single_year[1].split(' ')[1], start: single_year[0], end: single_year[1].split(' ')[0] }
  else
    raise ArgumentError, "Invalid year_holiday : #{wrs.inspect}"
  end
end

#get_year_month(wrs) ⇒ Object



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 351

def get_year_month(wrs)
  single_year_month = wrs.gsub(/\:$/, '').split('-')
  year_month_from = single_year_month[0].split(' ')
  year_month_from = { month: OSM_MONTHS.find_index(year_month_from[1]) + 1, year: year_month_from[0].to_i }
  if year_month_from.length < 1
    raise ArgumentError, "Invalid year_month : #{year_month_from.inspect}"
  end
  if single_year_month.length > 1
    year_month_to = single_year_month[1].split(' ')
    if year_month_to.length == 2
      year_month_to = { month: OSM_MONTHS.find_index(year_month_to[1]) + 1, year: year_month_to[0].to_i }
    elsif year_month_to.length == 1
      year_month_to = { month: OSM_MONTHS.find_index(year_month_to[0]) + 1, year: year_month_from[:year] }
    end
    if year_month_to.length < 1
      raise ArgumentError, "Invalid year_month : #{year_month_to.inspect}"
    end
  else
    year_month_to = nil
  end
  { from_month: year_month_from, to_month: year_month_to }
end

#get_year_month_day(wrs) ⇒ Object



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 385

def get_year_month_day(wrs)
  single_year_month_day = wrs.gsub(/\:$/, '').split('-')
  year_month_day_from = single_year_month_day[0].split(' ')
  year_month_day_from = { day: year_month_day_from[2].to_i,
    month: OSM_MONTHS.find_index(year_month_day_from[1]) + 1,
    year: year_month_day_from[0].to_i }
  if year_month_day_from.length < 1
    raise ArgumentError, "Invalid year_month_day : #{year_month_day_from.inspect}"
  end
  if single_year_month_day.length > 1
    year_month_day_to = single_year_month_day[1].split(' ')
    if year_month_day_to.length == 3
      year_month_day_to = { day: year_month_day_to[2].to_i,
        month: OSM_MONTHS.find_index(year_month_day_to[1]) + 1,
        year: year_month_day_to[0].to_i }
    elsif year_month_day_to.length == 2
      year_month_day_to = { day: year_month_day_to[1].to_i,
        month: OSM_MONTHS.find_index(year_month_day_to[0]) + 1,
        year: year_month_day_from[:year] }
    elsif year_month_day_to.length == 1
      year_month_day_to = { day: year_month_day_to[0].to_i,
        month: year_month_day_from[:month],
        year: year_month_day_from[:year] }
    end
    if year_month_day_to.length < 1
      raise ArgumentError, "Invalid year_month_day : #{year_month_day_to.inspect}"
    end
  else
    year_month_day_to = nil
  end

  { from_day: year_month_day_from, to_day: year_month_day_to }
end

#is_comment?(token) ⇒ Boolean

Returns:

  • (Boolean)


557
558
559
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 557

def is_comment?(token)
  !(@RGX_COMMENT =~ token).nil?
end

#is_holiday?(token) ⇒ Boolean

Returns:

  • (Boolean)


560
561
562
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 560

def is_holiday?(token)
  !(@RGX_HOLIDAY =~ token).nil?
end

#is_rule_modifier?(token) ⇒ Boolean

Returns:

  • (Boolean)


563
564
565
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 563

def is_rule_modifier?(token)
  !(@RGX_RULE_MODIFIER =~ token).nil?
end

#is_time?(token) ⇒ Boolean

Returns:

  • (Boolean)


566
567
568
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 566

def is_time?(token)
  !(@RGX_TIME =~ token).nil?
end

#is_weekday?(token) ⇒ Boolean

Returns:

  • (Boolean)


569
570
571
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 569

def is_weekday?(token)
  !(@RGX_WEEKDAY =~ token).nil?
end

#is_year?(token) ⇒ Boolean

Returns:

  • (Boolean)


572
573
574
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 572

def is_year?(token)
  !(@RGX_YEAR =~ token).nil?
end

#parse(oh) ⇒ Object



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
66
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 24

def parse(oh)
  result = []
  blocks = oh.split(';')

  comment = ""
  rule_modifier = nil
  time_selector = nil
  weekday_selector = nil
  wide_range_selector = nil
  month_selector = nil

  times = nil
  weekdays = nil
  weeks = nil
  months = nil
  years = nil

  date_ranges = nil
  date_range = nil
  dr_obj = nil
  res_dr_id = nil

  blocks.each do |block|
    block.strip!
    next if block.length == 0

    tokens = tokenize(block)
    current_token = tokens.length - 1

    # get comment
    if current_token >= 0 && is_comment?(tokens[current_token])
      comment = tokens[current_token]
      current_token -= 1
    end


    # get state
    if current_token >= 0 && is_rule_modifier?(tokens[current_token])
      rule_modifier = tokens[current_token].downcase
      current_token -= 1
    end

    times = []
    if current_token >= 0 && is_time?(tokens[current_token])
      time_selector = tokens[current_token]
      times = get_times(time_selector)
      current_token -= 1
    end

    # get weekdays selector
    weekdays = []
    holidays = []
    if time_selector == "24/7"
      weekdays << {from: 0, to: 6}
    elsif current_token >= 0 && is_weekday?(tokens[current_token]) && (@RGX_YEAR_PH =~ "#{tokens[current_token-1]} #{tokens[current_token]}").nil?
      weekday_selector = tokens[current_token]
      weekdays_and_holidays = get_weekdays(weekday_selector)
      weekdays = weekdays_and_holidays[:weekdays]
      holidays = weekdays_and_holidays[:holidays]
      current_token -= 1
    end

    months = []
    years = []
    if current_token >= 0
      wide_range_selector = tokens[0]
      for i in 1..current_token
        wide_range_selector += " #{tokens[i]}"
      end
      if wide_range_selector.length > 0
        wide_range_selector = wide_range_selector.strip
        wide_range_selector = wide_range_selector.split(',')
        wide_range_selector.each do |wrs|
          if !(@RGX_YEAR_MONTH_DAY =~ wrs).nil?
            years << get_year_month_day(wrs)
          elsif !(@RGX_YEAR_MONTH =~ wrs).nil?
            years << get_year_month(wrs)
          elsif !(@RGX_MONTHDAY =~ wrs).nil?
            months << get_month_day(wrs)
          elsif !(@RGX_YEAR_PH =~ wrs).nil?
            holidays << get_year_holiday(wrs)
          elsif !(@RGX_MONTH =~ wrs).nil?
            months << get_month(wrs)
          elsif !(@RGX_YEAR =~ wrs).nil?
            years << get_year(wrs)
          else
            raise ArgumentError, "Unsupported selector #{wrs}"
          end
        end
      end
    end

    if current_token == tokens.length - 1
      raise ArgumentError, "Unreadable string"
    end
    # puts "months : #{months}"
    # puts "weekdays : #{weekdays}"
    # puts "times : #{times}"
    # puts "years : #{years}"
    # puts "rule_modifier : #{rule_modifier}"
    date_ranges = []
    if months.length > 0
      months.each do |month|
        if !month[:from_day].nil?
          if !month[:to_day].nil?
            date_range = OpeningHoursConverter::WideInterval.new.day(month[:from_day][:day], month[:from_day][:month], month[:from_day][:year],
              month[:to_day][:day], month[:to_day][:month], month[:to_day][:year])
          else
            date_range = OpeningHoursConverter::WideInterval.new.day(month[:from_day][:day], month[:from_day][:month], month[:from_day][:year])
          end
          date_ranges << date_range
        else
          if !month[:to].nil?
            date_range = OpeningHoursConverter::WideInterval.new.month(month[:from], nil, month[:to])
          else
            date_range = OpeningHoursConverter::WideInterval.new.month(month[:from])
          end
          date_ranges << date_range
        end
      end
    elsif holidays.length > 0 && weekdays.length == 0
      holidays.each do |holiday|
        if holiday == "PH"
          date_ranges << WideInterval.new.holiday(holiday)
        elsif holiday[:holiday] == "PH"
          date_ranges << WideInterval.new.holiday(holiday[:holiday], holiday[:start], holiday[:end])
        end
      end
    elsif years.length > 0
      years.each do |year|
        if !year[:from_day].nil?
          if !year[:to_day].nil?
            date_range = OpeningHoursConverter::WideInterval.new.day(year[:from_day][:day], year[:from_day][:month], year[:from_day][:year],
              year[:to_day][:day], year[:to_day][:month], year[:to_day][:year])
          else
            date_range = OpeningHoursConverter::WideInterval.new.day(year[:from_day][:day], year[:from_day][:month], year[:from_day][:year])
          end
        elsif !year[:from_month].nil?
          if !year[:to_month].nil?
            date_range = OpeningHoursConverter::WideInterval.new.month(year[:from_month][:month], year[:from_month][:year],
              year[:to_month][:month], year[:to_month][:year])
          else
            date_range = OpeningHoursConverter::WideInterval.new.month(year[:from_month][:month], year[:from_month][:year])
          end
        elsif !year[:from].nil?
          if !year[:to].nil?
            date_range = OpeningHoursConverter::WideInterval.new.year(year[:from], year[:to])
          else
            date_range = OpeningHoursConverter::WideInterval.new.year(year[:from])
          end
        end
        date_ranges << date_range
      end
    else
      date_ranges << OpeningHoursConverter::WideInterval.new.always
    end

    if weekdays.length > 0 && holidays.length > 0
      weekdays << {from: -2, to: -2}
      holidays = []
    end
    if weekdays.length == 0
      weekdays << {from: 0, to: OSM_DAYS.length - 1}
    end
    if times.length == 0
      times << {from: 0, to: 24*60}
    end

    date_ranges.each do |dr|
      found_date_range = false
      res_dr_id = 0

      while res_dr_id < result.length && !found_date_range
        if result[res_dr_id].wide_interval.equals(dr) && result[res_dr_id].comment == comment
          found_date_range = true
        else
          res_dr_id += 1
        end
      end

      if found_date_range
        dr_obj = result[res_dr_id]
      else
        dr_obj = OpeningHoursConverter::DateRange.new(dr)
        if !comment.nil?
          dr_obj.add_comment(comment)
        end

        general = -1
        for res_dr_id in 0...result.length
          if result[res_dr_id].is_general_for?(OpeningHoursConverter::DateRange.new(dr))
            general = res_dr_id
          end
        end
        if general >= 0
          dr_obj.typical.copy_intervals(result[general].typical.intervals)
        end
        result << dr_obj
      end

      for wd_id in 0...weekdays.length
        if weekdays[wd_id][:from] <= weekdays[wd_id][:to]
          for wd_rm in weekdays[wd_id][:from]..weekdays[wd_id][:to]
            if dr_obj.defines_typical_week?
              dr_obj.typical.remove_intervals_during_day(wd_rm)
            else
              dr_obj.typical.clear_intervals
            end
          end
        else
          for wd_rm in weekdays[wd_id][:from]..6
            if dr_obj.defines_typical_week?
              dr_obj.typical.remove_intervals_during_day(wd_rm)
            else
              dr_obj.typical.clear_intervals
            end
          end
          for wd_rm in 0..weekdays[wd_id][:to]
            if dr_obj.defines_typical_week?
              dr_obj.typical.remove_intervals_during_day(wd_rm)
            else
              dr_obj.typical.clear_intervals
            end
          end
        end

        for t_id in 0...times.length
          if rule_modifier == "closed" || rule_modifier == "off"
            remove_interval(dr_obj, weekdays[wd_id], times[t_id])
          else
            add_interval(dr_obj.typical, weekdays[wd_id], times[t_id])
          end
        end
      end
    end
  end

  return result
end

#remove_interval(date_range, weekdays, times) ⇒ Object



470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 470

def remove_interval(date_range, weekdays, times)
  if date_range.typical.instance_of?(OpeningHoursConverter::Day)
    date_range.typical.clear_intervals
  else
    if weekdays[:from] <= weekdays[:to]
      for wd in weekdays[:from]..weekdays[:to]
        date_range.typical.remove_intervals_during_day(wd)
      end
    else
      for wd in weekdays[:from]..6
        date_range.typical.remove_intervals_during_day(wd)
      end
      for wd in 0..weekdays[:to]
        date_range.typical.remove_intervals_during_day(wd)
      end
    end
  end
end

#remove_interval_wd(typical, times, wd) ⇒ Object



489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 489

def remove_interval_wd(typical, times, wd)
  if times[:to] >= times[:from]
    typical.remove_interval(OpeningHoursConverter::Interval.new(wd, times[:from], wd, times[:to]))
  else
    if wd < 6
      typical.remove_interval(OpeningHoursConverter::Interval.new(wd, times[:from], wd+1, times[:to]))
    else
      typical.remove_interval(OpeningHoursConverter::Interval.new(wd, times[:from], wd+1, 24*60))
      typical.remove_interval(OpeningHoursConverter::Interval.new(0, 0, 0, times[:to]))
    end
  end
end

#tokenize(block) ⇒ Object



542
543
544
545
546
547
548
549
550
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 542

def tokenize(block)
  if block.split('"').length > 1
    comment = block.split('"')[1]
    tokens = block.split('"')[0].split(' ')
    tokens << "\"#{comment}\""
  else
    block.split(' ')
  end
end