Class: OpeningHoursConverter::OpeningHoursParser

Inherits:
Object
  • Object
show all
Includes:
Constants, Utils
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

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

#initializeOpeningHoursParser

Returns a new instance of OpeningHoursParser.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 10

def initialize
  @RGX_RULE_MODIFIER = /^(open|closed|off)$/i
  @RGX_WEEK_KEY = /^week$/
  @RGX_WEEK = /^week ([01234]?[0-9]|5[0123])(\-([01234]?[0-9]|5[0123]))?(, ?([01234]?[0-9]|5[0123])(\-([01234]?[0-9]|5[0123]))?)*$/
  @RGX_WEEK_VAL = /^([01234]?[0-9]|5[0123])(\-([01234]?[0-9]|5[0123]))?(, ?([01234]?[0-9]|5[0123])(\-([01234]?[0-9]|5[0123]))?)*$/
  @RGX_WEEK_WITH_MODIFIER = /^week ([01234]?[0-9]|5[0123])(\-([01234]?[0-9]|5[0123])(\/[1-9])?)?(, ?([01234]?[0-9]|5[0123])(\-([01234]?[0-9]|5[0123])(\/[1-9])?)?)*$/
  @RGX_WEEK_VAL_WITH_MODIFIER = /^([01234]?[0-9]|5[0123])(\-([01234]?[0-9]|5[0123])(\/[1-9])?)?(, ?([01234]?[0-9]|5[0123])(\-([01234]?[0-9]|5[0123])(\/[1-9])?)?)*$/
  @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_WD_WITH_MODIFIER = /^(Mo|Tu|We|Th|Fr|Sa|Su)\[([1-5]|\-1)\]$/
  @RGX_DAY = /^([012]?[0-9]|3[01])(\-[012]?[0-9]|3[01])?$/
  @RGX_YEAR = /^(\d{4})(\-(\d{4}))?$/
  @RGX_YEAR_PH = /^(\d{4})( PH|(\-(\d{4}) PH))\:?$/
  @RGX_YEAR_WEEK = /^(\d{4})(\-(\d{4}))? week ([01234]?[0-9]|5[0123])(\-([01234]?[0-9]|5[0123]))?(, ?([01234]?[0-9]|5[0123])(\-([01234]?[0-9]|5[0123]))?)*\:?$/
  @RGX_YEAR_WEEK_WITH_MODIFIER = /^(\d{4})(\-(\d{4}))? week ([01234]?[0-9]|5[0123])(\-([01234]?[0-9]|5[0123])(\/[1-9])?)?(, ?([01234]?[0-9]|5[0123])(\-([01234]?[0-9]|5[0123])(\/[1-9])?)?)*$/
  @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



607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 607

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
      weekdays[:to] = if times[:from] <= times[:to]
                        0
                      else
                        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



657
658
659
660
661
662
663
664
665
666
667
668
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 657

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, 24 * 60))
      typical.add_interval(OpeningHoursConverter::Interval.new(0, 0, 0, times[:to]))
    end
  end
end

#add_off_interval(date_range, weekdays) ⇒ Object



634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 634

def add_off_interval(date_range, weekdays)
  if date_range.typical.instance_of?(OpeningHoursConverter::Day)
    if weekdays[:from] != 0 || weekdays[:to] != 0
      weekdays = weekdays.dup
      weekdays[:from] = 0
      weekdays[:to] = 0
    end
  end

  if weekdays[:from] <= weekdays[:to]
    for wd in weekdays[:from]..weekdays[:to]
      date_range.typical.add_interval(OpeningHoursConverter::Interval.new(wd, 0, wd, 24 * 60, true))
    end
  else
    for wd in weekdays[:from]..6
      date_range.typical.add_interval(OpeningHoursConverter::Interval.new(wd, 0, wd, 24 * 60, true))
    end
    for wd in 0..weekdays[:to]
      date_range.typical.add_interval(OpeningHoursConverter::Interval.new(wd, 0, wd, 24 * 60, true))
    end
  end
end

#as_minutes(time) ⇒ Object



723
724
725
726
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 723

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

#from_json(json) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 285

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)
    date_range.last.add_comment(dr['comment'])
    dr['typical']['intervals'].each do |interval|
      next unless !interval.nil?
      start_interval = DateTime.parse(interval['start'])
      end_interval = DateTime.parse(interval['end'])
      if end_interval.day == start_interval.day + 1 && end_interval.hour == 0 && end_interval.min == 0
        end_interval -= (1 / 1440.0)
      end
      date_range.last.typical.add_interval(
        OpeningHoursConverter::Interval.new(
          reindex_sunday_week_to_monday_week(start_interval.wday),
          (start_interval.hour * 60 + start_interval.min),
          reindex_sunday_week_to_monday_week(end_interval.wday),
          (end_interval.hour * 60 + end_interval.min)
        )
      )
    end
  end
  date_range
end

#get_month(wrs) ⇒ Object

Raises:

  • (ArgumentError)


414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 414

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

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

#get_month_day(wrs) ⇒ Object

Raises:

  • (ArgumentError)


428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 428

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 }
  raise ArgumentError, "Invalid month : #{month_from.inspect}" if month_from[:month] < 1

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

#get_times(time_selector) ⇒ Object



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 515

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])
      to = if single_time.length > 1
             as_minutes(single_time[1])
           else
             from
           end
      times << { from: from, to: to }
    end
  end
  times
end

#get_week(wrs) ⇒ Object



398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 398

def get_week(wrs)
  weeks = wrs.gsub('week ', '').split(',')

  indexes = weeks.map { |week_index|
    if week_index.include?('-')
      from, to = week_index.split('-').map(&:to_i)
      { from: from, to: to }
    else
      week_index.to_i
    end
  }

  { week_indexes: indexes }
end

#get_week_with_modifier(wrs) ⇒ Object



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 377

def get_week_with_modifier(wrs)
  weeks = wrs.gsub('week ', '').split(',')

  indexes = weeks.map { |week_index|
    if week_index.include?('-')
      if week_index.include?('/')
        from, to = week_index.split('-')
        to, modifier = to.split('/')
        { from: from.to_i, to: to.to_i, modifier: modifier.to_i }
      else
        from, to = week_index.split('-').map(&:to_i)
        { from: from, to: to }
      end
    else
      week_index.to_i
    end
  }

  { week_indexes: indexes }
end

#get_weekdays(weekday_selector) ⇒ Object



537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 537

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

  weekday_selector = weekday_selector.split(',')

  weekday_selector.each do |wd|
    if !(@RGX_HOLIDAY =~ wd).nil?
      weekdays << { from: -2, to: -2, index: nil }
    elsif !(@RGX_WD =~ wd).nil?
      single_weekday = wd.split('-')

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

      weekdays << { from: wd_from, to: wd_to, index: nil }
    elsif !(@RGX_WD_WITH_MODIFIER =~ wd).nil?

      from, to = wd[0...wd.index('[')].split('-')
      index = wd[wd.index('[') + 1...wd.index(']')]

      wd_from = OSM_DAYS.find_index(from)
      wd_to = OSM_DAYS.find_index(to)

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

  weekdays
end

#get_year(wrs) ⇒ Object

Raises:

  • (ArgumentError)


320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 320

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

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

#get_year_holiday(wrs) ⇒ Object



470
471
472
473
474
475
476
477
478
479
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 470

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



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 449

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.empty?
    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
    raise ArgumentError, "Invalid year_month : #{year_month_to.inspect}" if year_month_to.empty?
  else
    year_month_to = nil
  end
  { from_month: year_month_from, to_month: year_month_to }
end

#get_year_month_day(wrs) ⇒ Object



481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 481

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.empty?
    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.empty?
      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

#get_year_week(wrs) ⇒ Object



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 358

def get_year_week(wrs)
  year, weeks = wrs.split(' week ')
  # does not handle 2018,2019
  years = year.split('-')
  # does not handle week 1,2,3
  indexes = weeks.split(',').map { |week_index|
    if week_index.include?('-')
      from, to = week_index.split('-').map(&:to_i)
      { from: from, to: to }
    else
      week_index.to_i
    end
  }

  { week_indexes: indexes, year_from: year[0].to_i }.tap do |hsh|
    hsh[:year_to] = years.length > 1 ? year[1].to_i : nil
  end
end

#get_year_week_with_modifier(wrs) ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 334

def get_year_week_with_modifier(wrs)
  year, weeks = wrs.split(' week ')
  years = year.split('-')

  indexes = weeks.split(',').map { |week_index|
    if week_index.include?('-')
      if week_index.include?('/')
        from, to = week_index.split('-')
        to, modifier = to.split('/')
        { from: from.to_i, to: to.to_i, modifier: modifier.to_i }
      else
        from, to = week_index.split('-').map(&:to_i)
        { from: from, to: to }
      end
    else
      week_index.to_i
    end
  }

  { year_from: years[0].to_i, week_indexes: indexes }.tap do |hsh|
    hsh[:year_to] = years.length > 1 ? years[1].to_i : nil
  end
end

#is_comment?(token) ⇒ Boolean

Returns:

  • (Boolean)


743
744
745
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 743

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

#is_holiday?(token) ⇒ Boolean

Returns:

  • (Boolean)


747
748
749
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 747

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

#is_part_of_wide_interval?(string) ⇒ Boolean

Returns:

  • (Boolean)


728
729
730
731
732
733
734
735
736
737
738
739
740
741
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 728

def is_part_of_wide_interval?(string)
  is_wide_interval = false
  string.split('-').each do |str|
    if (!(@RGX_YEAR =~ str).nil? || !(@RGX_YEAR_MONTH =~ str).nil? || !(@RGX_YEAR_MONTH_DAY =~ str).nil? ||
        !(@RGX_DAY =~ str).nil? || !(@RGX_MONTHDAY =~ str).nil? || !(@RGX_MONTH =~ str).nil? ||
        !(@RGX_WEEK_KEY =~ str).nil? || !(@RGX_WEEK_VAL =~ str).nil? || !(@RGX_WEEK_VAL_WITH_MODIFIER =~ str).nil?) &&
        ((@RGX_TIME =~ str).nil? && (@RGX_WEEKDAY =~ str).nil?)
      is_wide_interval = true
    else
      return false
    end
  end
  is_wide_interval
end

#is_rule_modifier?(token) ⇒ Boolean

Returns:

  • (Boolean)


751
752
753
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 751

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

#is_time?(token) ⇒ Boolean

Returns:

  • (Boolean)


755
756
757
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 755

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

#is_week_key?(token) ⇒ Boolean

Returns:

  • (Boolean)


771
772
773
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 771

def is_week_key?(token)
  !(@RGX_WEEK_KEY =~ token).nil?
end

#is_week_val?(token) ⇒ Boolean

Returns:

  • (Boolean)


775
776
777
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 775

def is_week_val?(token)
  !(@RGX_WEEK_VAL =~ token).nil?
end

#is_week_val_with_modifier?(token) ⇒ Boolean

Returns:

  • (Boolean)


783
784
785
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 783

def is_week_val_with_modifier?(token)
  !(@RGX_WEEK_VAL_WITH_MODIFIER =~ token).nil?
end

#is_week_with_modifier?(token) ⇒ Boolean

Returns:

  • (Boolean)


779
780
781
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 779

def is_week_with_modifier?(token)
  !(@RGX_WEEK_WITH_MODIFIER =~ token).nil?
end

#is_weekday?(token) ⇒ Boolean

Returns:

  • (Boolean)


759
760
761
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 759

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

#is_weekday_with_modifier?(token) ⇒ Boolean

Returns:

  • (Boolean)


763
764
765
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 763

def is_weekday_with_modifier?(token)
  !(@RGX_WD_WITH_MODIFIER =~ token).nil?
end

#is_year?(token) ⇒ Boolean

Returns:

  • (Boolean)


767
768
769
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 767

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

#is_year_week?(token) ⇒ Boolean

Returns:

  • (Boolean)


787
788
789
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 787

def is_year_week?(token)
  !(@RGX_YEAR_WEEK =~ token).nil?
end

#is_year_week_with_modifier?(token) ⇒ Boolean

Returns:

  • (Boolean)


791
792
793
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 791

def is_year_week_with_modifier?(token)
  !(@RGX_YEAR_WEEK_WITH_MODIFIER =~ token).nil?
end

#merge_groups(groups) ⇒ Object



683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 683

def merge_groups(groups)
  new_tokens = []
  weekday_merge = []
  wide_interval_merge = []
  merged_wide_interval = []

  groups.each_with_index do |group, group_index|
    if !wide_interval_merge.empty?
      merged_wide_interval += [wide_interval_merge.join(' ')]
      wide_interval_merge = []
    end
    group.each_with_index do |token, token_index|
      if !wide_interval_merge.empty? && !is_part_of_wide_interval?(token)
        merged_wide_interval += [wide_interval_merge.join(' ')]
        wide_interval_merge = []
        new_tokens << merged_wide_interval.join(',')
      end
      if is_weekday?(token) || is_weekday_with_modifier?(token)
        weekday_merge << token
        if token_index == group.length - 1 && group_index == groups.length - 1
          new_tokens << weekday_merge.join(',')
        end
      elsif !weekday_merge.empty?
        new_tokens << weekday_merge.join(',')
        new_tokens << token
        weekday_merge = []
      elsif is_part_of_wide_interval?(token)
        wide_interval_merge << token
        if token_index == group.length - 1 && group_index == groups.length - 1
          new_tokens << wide_interval_merge.join(',')
        end
      else
        new_tokens << token
      end
    end
  end

  new_tokens
end

#parse(oh) ⇒ Object



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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 34

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

  comment = ''
  time_selector = nil
  weekday_selector = nil
  wide_range_selector = nil

  weekdays = 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.empty?

    tokenizer = OpeningHoursConverter::Tokenizer.new(block)
    tokens = tokenizer.tokens
    # tokens = tokenize(block)
    @current_token = tokens.length - 1

    weekdays = {}
    comment = ''

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

    # get state and time associated with weekdays
    while @current_token >= 0 && (is_rule_modifier?(tokens[@current_token]) || is_time?(tokens[@current_token]))
      if is_rule_modifier?(tokens[@current_token])
        local_modifier = tokens[@current_token].downcase
        @current_token -= 1
        begin
          weekday_selector = tokens[@current_token]
          weekdays_and_holidays = get_weekdays(weekday_selector)
        rescue StandardError
          weekdays[[{ from: 0, to: 6, index: nil }]] ||= {}
          weekdays[[{ from: 0, to: 6, index: nil }]][:modifiers] ||= []
          weekdays[[{ from: 0, to: 6, index: nil }]][:modifiers] << local_modifier
        else
          weekdays[weekdays_and_holidays] ||= {}
          weekdays[weekdays_and_holidays][:modifiers] ||= []
          weekdays[weekdays_and_holidays][:modifiers] << local_modifier
          @current_token -= 1
        end
      else
        local_times = []
        while @current_token >= 0 && is_time?(tokens[@current_token])
          time_selector = tokens[@current_token]
          local_times.concat get_times(time_selector)
          @current_token -= 1
        end
        begin
          weekday_selector = tokens[@current_token]
          weekdays_and_holidays = get_weekdays(weekday_selector)
        rescue StandardError
          weekdays[[{ from: 0, to: 6, index: nil }]] ||= {}
          weekdays[[{ from: 0, to: 6, index: nil }]][:times] ||= []
          weekdays[[{ from: 0, to: 6, index: nil }]][:times].concat(local_times)
        else
          weekdays[weekdays_and_holidays] ||= {}
          weekdays[weekdays_and_holidays][:times] ||= []
          weekdays[weekdays_and_holidays][:times].concat(local_times)
          @current_token -= 1
        end
      end
    end

    weeks = []
    months = []
    years = []
    holidays = []
    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.empty?
        wide_range_selector = wide_range_selector.strip
        # wide_range_selector = wide_range_selector.split(',')
        if !(@RGX_YEAR_MONTH_DAY =~ wide_range_selector).nil?
          years << get_year_month_day(wide_range_selector)
        elsif !(@RGX_YEAR_MONTH =~ wide_range_selector).nil?
          years << get_year_month(wide_range_selector)
        elsif !(@RGX_MONTHDAY =~ wide_range_selector).nil?
          months << get_month_day(wide_range_selector)
        elsif !(@RGX_MONTH =~ wide_range_selector).nil?
          months << get_month(wide_range_selector)
        elsif !(@RGX_YEAR =~ wide_range_selector).nil?
          years << get_year(wide_range_selector)
        elsif !(@RGX_YEAR_WEEK_WITH_MODIFIER =~ wide_range_selector).nil?
          weeks << get_year_week_with_modifier(wide_range_selector)
        elsif !(@RGX_YEAR_WEEK =~ wide_range_selector).nil?
          weeks << get_year_week(wide_range_selector)
        elsif !(@RGX_WEEK_WITH_MODIFIER =~ wide_range_selector).nil?
          weeks << get_week_with_modifier(wide_range_selector)
        elsif !(@RGX_WEEK =~ wide_range_selector).nil?
          weeks << get_week(wide_range_selector)
        else
          raise ArgumentError, "Unsupported selector #{wide_range_selector}"
        end
      end
    end

    raise ArgumentError, 'Unreadable string' if @current_token == tokens.length - 1

    # puts "weekdays : #{weekdays}"
    # puts "weeks : #{weeks}"
    # puts "months : #{months}"
    # puts "years : #{years}"

    date_ranges = []
    if !months.empty?
      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
        else
          date_range = if !month[:to].nil?
                         OpeningHoursConverter::WideInterval.new.month(month[:from], nil, month[:to])
                       else
                         OpeningHoursConverter::WideInterval.new.month(month[:from])
                       end
        end
        date_ranges << date_range
      end
    elsif !weeks.empty?

      weeks.each do |week|
        date_ranges << OpeningHoursConverter::WideInterval.new.week(week[:week_indexes], week[:year_from], week[:year_to])
      end

    elsif !years.empty?
      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?
          date_range = if !year[:to].nil?
                         OpeningHoursConverter::WideInterval.new.year(year[:from], year[:to])
                       else
                         OpeningHoursConverter::WideInterval.new.year(year[:from])
                       end
        end
        date_ranges << date_range
      end
    else
      date_ranges << OpeningHoursConverter::WideInterval.new.always
    end

    if weekdays.empty?
      weekdays[[{ from: 0, to: 6, index: nil }]] = {}
      weekdays[[{ from: 0, to: 6, index: nil }]][: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)
        dr_obj.add_comment(comment) if !comment.nil?

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

      weekdays.each do |weekday_ranges, weekday_object|
        weekday_ranges.each do |weekday_range|
          if weekday_range[:from] <= weekday_range[:to]
            for wd_rm in weekday_range[:from]..weekday_range[: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 weekday_range[: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..weekday_range[: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

          weekday_object[:modifiers]&.each do |modifier|
            if modifier == 'closed' || modifier == 'off'
              remove_interval(dr_obj, weekday_range)
              add_off_interval(dr_obj, weekday_range)
            end
          end

          next unless weekday_object[:times]

          weekday_object[:times]&.each do |time_range|
            add_interval(dr_obj.typical, weekday_range, time_range)
          end
        end
      end
    end
  end

  result
end

#remove_interval(date_range, weekdays) ⇒ Object



575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 575

def remove_interval(date_range, weekdays)
  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



594
595
596
597
598
599
600
601
602
603
604
605
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 594

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



670
671
672
673
674
675
676
677
678
679
680
681
# File 'lib/opening_hours_converter/opening_hours_parser.rb', line 670

def tokenize(block)
  if block.split('"').length > 1
    comment = block.split('"')[1]

    groups = block.split('"')[0].split(',').map { |s| s.split(' ') }
    groups[-1] << "\"#{comment}\""

    merge_groups(groups)
  else
    merge_groups(block.split(',').map { |s| s.split(' ') })
  end
end