opening_hours_converter 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 465d1cc6f9092fed1e703f663c5187158f02a06b
4
- data.tar.gz: bc1d15874e701120b046c9777dec296cba72b9df
3
+ metadata.gz: 4642911e8df98a01cfc414042dce0cc11e8c8908
4
+ data.tar.gz: 1b5ba34eba12a1a9fc8bcd9d9ab4cb4874898b01
5
5
  SHA512:
6
- metadata.gz: e3b3bd1455b19ba88527389a1b2339d38f612034af48d8538cdc7378378634f053b97f56fd18114d2560e27cb2038964da1efd6adac2c1bb7e6e684b3da493f6
7
- data.tar.gz: f1cfd805b490d4c1fcce52f637dddaa2678fba503179724ecb3d2cb7dd30e57c04ec71a0adb0bfcf5778fb4f70a8c4603dfca1b96416fadc3f3cb2d02394cfde
6
+ metadata.gz: 711db0af55afe9cab83cd7feb520a89c053887fd42e8cf1600204bdfb8b0d25c8c96af9dec8a4c7462bef5d0605677b555d0be9629d303bf7d7158fe622209a5
7
+ data.tar.gz: 8be38120b5040d1f285a31a1f35f17c9a7133d0314b82aedae2a68cf03f3ce4f51739ed8fb946f0b2be8622b28227bad169baba2782f3387be2c3b76b266a063
@@ -4,6 +4,7 @@ module OpeningHoursConverter
4
4
  require_relative './opening_hours_converter/day'
5
5
  require_relative './opening_hours_converter/week'
6
6
  require_relative './opening_hours_converter/year'
7
+ require_relative './opening_hours_converter/public_holiday'
7
8
  require_relative './opening_hours_converter/interval'
8
9
  require_relative './opening_hours_converter/iterator'
9
10
  require_relative './opening_hours_converter/opening_hours_builder'
@@ -19,5 +19,6 @@ module OpeningHoursConverter
19
19
  MINUTES_MAX = 1440
20
20
  DAYS_MAX = 6
21
21
  YEAR_DAYS_MAX = 365
22
+ PH_WEEKDAY = -2
22
23
  end
23
24
  end
@@ -28,7 +28,7 @@ module OpeningHoursConverter
28
28
  else
29
29
  @typical = OpeningHoursConverter::Week.new
30
30
  end
31
- when "week", "month", "year", "always"
31
+ else
32
32
  @typical = OpeningHoursConverter::Week.new
33
33
  end
34
34
  end
@@ -45,5 +45,16 @@ module OpeningHoursConverter
45
45
  def is_general_for?(date_range)
46
46
  defines_typical_day? == date_range.defines_typical_day? && @wide_interval.contains?(date_range.wide_interval) && @comment == date_range.comment
47
47
  end
48
+
49
+ def is_holiday?
50
+ result = false
51
+ result = @wide_interval.type == "holiday"
52
+ if !result
53
+ @typical.intervals.each do |i|
54
+ result = true if i.day_start == -2 && i.day_end == -2
55
+ end
56
+ end
57
+ result
58
+ end
48
59
  end
49
60
  end
@@ -3,7 +3,7 @@ require 'opening_hours_converter/constants'
3
3
  module OpeningHoursConverter
4
4
  class Interval
5
5
  include Constants
6
- attr_accessor :day_start, :day_end, :start, :end
6
+ attr_reader :day_start, :day_end, :start, :end
7
7
 
8
8
  def initialize(day_start, min_start, day_end=0, min_end=0)
9
9
  @day_start = day_start
@@ -10,9 +10,18 @@ module OpeningHoursConverter
10
10
  oh_rule_added = nil
11
11
  range_general = nil
12
12
  range_general_for = nil
13
+ day_ph = false
13
14
 
14
15
  date_ranges.each_with_index do |date_range, date_range_index|
16
+
15
17
  if !date_range.nil?
18
+ date_range.typical.intervals.each_with_index do |interval, interval_id|
19
+ if interval&.day_start == -2 && interval&.day_start == interval&.day_end
20
+ date_range.typical.remove_interval(interval_id)
21
+ day_ph = true
22
+ end
23
+ end
24
+
16
25
  range_general = nil
17
26
  range_general_for = nil
18
27
  range_general_id = date_range_index - 1
@@ -28,8 +37,11 @@ module OpeningHoursConverter
28
37
  end
29
38
  range_general_id -= 1
30
39
  end
40
+
31
41
  if date_range_index == 0 || range_general.nil?
32
- if date_range.defines_typical_week?
42
+ if date_range.wide_interval.type == "holiday"
43
+ oh_rules = build_holiday(date_range)
44
+ elsif date_range.defines_typical_week?
33
45
  if !range_general_for.nil?
34
46
  oh_rules = build_week_diff(date_range, date_ranges[range_general_for])
35
47
  else
@@ -56,12 +68,24 @@ module OpeningHoursConverter
56
68
  oh_rule_added = true
57
69
  rescue Exception => e
58
70
  puts e
59
- rule_index += 1
71
+ if oh_rule.date[0].wide_type == "holiday" && oh_rule.date[0].wide.get_time_selector == "PH"
72
+ rules[rule_index].add_ph_weekday
73
+ oh_rule_added = true
74
+ elsif rules[rule_index].date[0].wide_type == "holiday" && rules[rule_index].date[0].wide.get_time_selector == "PH"
75
+ oh_rule.add_ph_weekday
76
+ rules[rule_index] = oh_rule
77
+ oh_rule_added = true
78
+ else
79
+ rule_index += 1
80
+ end
60
81
  end
61
82
  else
62
83
  rule_index+=1
63
84
  end
85
+ end
64
86
 
87
+ if day_ph
88
+ oh_rule.add_ph_weekday
65
89
  end
66
90
 
67
91
  if !oh_rule_added
@@ -81,7 +105,6 @@ module OpeningHoursConverter
81
105
  end
82
106
  end
83
107
  end
84
- # binding.pry
85
108
 
86
109
  result = ""
87
110
  rules.each_with_index do |rule, rule_index|
@@ -94,6 +117,22 @@ module OpeningHoursConverter
94
117
  return result
95
118
  end
96
119
 
120
+ def build_holiday(date_range)
121
+ intervals = date_range.typical.get_intervals(true)
122
+
123
+ rule = OpeningHoursConverter::OpeningHoursRule.new
124
+ date = OpeningHoursConverter::OpeningHoursDate.new(date_range.wide_interval, date_range.wide_interval.type, [-1])
125
+ rule.add_date(date)
126
+
127
+ intervals.each do |interval|
128
+ if !interval.nil?
129
+ rule.add_time(OpeningHoursConverter::OpeningHoursTime.new(interval.start, interval.end))
130
+ end
131
+ end
132
+
133
+ return [ rule ]
134
+ end
135
+
97
136
  def build_day(date_range)
98
137
  intervals = date_range.typical.get_intervals(true)
99
138
 
@@ -113,6 +152,7 @@ module OpeningHoursConverter
113
152
  def build_week(date_range)
114
153
  result = []
115
154
  intervals = date_range.typical.get_intervals(true)
155
+
116
156
  time_intervals = create_time_intervals(date_range.wide_interval, date_range.wide_interval.type, intervals)
117
157
 
118
158
  monday0 = time_intervals[0]
@@ -3,7 +3,8 @@ require 'opening_hours_converter/constants'
3
3
  module OpeningHoursConverter
4
4
  class OpeningHoursDate
5
5
  include Constants
6
- attr_accessor :wide_type, :wide, :weekdays, :weekdays_over
6
+ attr_accessor :weekdays, :weekdays_over
7
+ attr_reader :wide_type, :wide
7
8
 
8
9
  def initialize(wide, wide_type, weekdays)
9
10
  if wide.nil? || wide_type.nil? || weekdays.nil?
@@ -20,6 +21,12 @@ module OpeningHoursConverter
20
21
  result = ""
21
22
  wd = @weekdays.concat(@weekdays_over).sort.uniq
22
23
 
24
+ if wd.length > 0 && wd[0] == PH_WEEKDAY
25
+ result = "PH"
26
+ wd.shift
27
+ end
28
+
29
+
23
30
  if wd.length > 0 && wd.include?(6) && wd.include?(0) && (wd.include?(5) || wd.include?(1))
24
31
  start_we = 6
25
32
  i = wd.length - 2
@@ -103,6 +110,10 @@ module OpeningHoursConverter
103
110
  end
104
111
  end
105
112
 
113
+ def add_ph_weekday
114
+ add_weekday(PH_WEEKDAY)
115
+ end
116
+
106
117
  def add_overwritten_weekday(weekday)
107
118
  unless @weekdays_over.include?(weekday) && @weekdays_over.include?(weekday)
108
119
  @weekdays_over << weekday
@@ -3,7 +3,6 @@ require 'opening_hours_converter/constants'
3
3
  module OpeningHoursConverter
4
4
  class OpeningHoursParser
5
5
  include Constants
6
- attr_accessor :RGX_RULE_MODIFIER, :RGX_WEEK_KEY, :RGX_WEEK_VAL, :RGX_MONTH, :RGX_MONTHDAY, :RGX_TIME, :RGX_WEEKDAY, :RGX_HOLIDAY, :RGX_WD
7
6
  def initialize
8
7
  @RGX_RULE_MODIFIER = /^(open|closed|off)$/i
9
8
  @RGX_WEEK_KEY = /^week$/
@@ -11,10 +10,11 @@ module OpeningHoursConverter
11
10
  @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))?\:?$/
12
11
  @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]))?\:?$/
13
12
  @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))$/
14
- @RGX_WEEKDAY = /^(((Mo|Tu|We|Th|Fr|Sa|Su)(\-(Mo|Tu|We|Th|Fr|Sa|Su))?)|(PH|SH|easter))(,(((Mo|Tu|We|Th|Fr|Sa|Su)(\-(Mo|Tu|We|Th|Fr|Sa|Su))?)|(PH|SH|easter)))*$/
13
+ @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)))*$/
15
14
  @RGX_HOLIDAY = /^(PH|SH|easter)$/
16
15
  @RGX_WD = /^(Mo|Tu|We|Th|Fr|Sa|Su)(\-(Mo|Tu|We|Th|Fr|Sa|Su))?$/
17
16
  @RGX_YEAR = /^(\d{4})(\-(\d{4}))?$/
17
+ @RGX_YEAR_PH = /^(\d{4})( PH|(\-(\d{4}) PH))\:?$/
18
18
  @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]))?\:?$/
19
19
  @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)))?\:?$/
20
20
  @RGX_COMMENT = /^\"[^\"]*\"$/
@@ -71,18 +71,20 @@ module OpeningHoursConverter
71
71
 
72
72
  # get weekdays selector
73
73
  weekdays = []
74
+ holidays = []
74
75
  if time_selector == "24/7"
75
76
  weekdays << {from: 0, to: 6}
76
- elsif current_token >= 0 && is_weekday?(tokens[current_token])
77
+ elsif current_token >= 0 && is_weekday?(tokens[current_token]) && (@RGX_YEAR_PH =~ "#{tokens[current_token-1]} #{tokens[current_token]}").nil?
77
78
  weekday_selector = tokens[current_token]
78
- weekdays = get_weekdays(weekday_selector)
79
+ weekdays_and_holidays = get_weekdays(weekday_selector)
80
+ weekdays = weekdays_and_holidays[:weekdays]
81
+ holidays = weekdays_and_holidays[:holidays]
79
82
  current_token -= 1
80
83
  end
81
84
 
82
85
  months = []
83
86
  years = []
84
87
  if current_token >= 0
85
-
86
88
  wide_range_selector = tokens[0]
87
89
  for i in 1..current_token
88
90
  wide_range_selector += " #{tokens[i]}"
@@ -97,6 +99,8 @@ module OpeningHoursConverter
97
99
  years << get_year_month(wrs)
98
100
  elsif !(@RGX_MONTHDAY =~ wrs).nil?
99
101
  months << get_month_day(wrs)
102
+ elsif !(@RGX_YEAR_PH =~ wrs).nil?
103
+ holidays << get_year_holiday(wrs)
100
104
  elsif !(@RGX_MONTH =~ wrs).nil?
101
105
  months << get_month(wrs)
102
106
  elsif !(@RGX_YEAR =~ wrs).nil?
@@ -116,7 +120,6 @@ module OpeningHoursConverter
116
120
  # puts "times : #{times}"
117
121
  # puts "years : #{years}"
118
122
  # puts "rule_modifier : #{rule_modifier}"
119
-
120
123
  date_ranges = []
121
124
  if months.length > 0
122
125
  months.each do |month|
@@ -137,6 +140,14 @@ module OpeningHoursConverter
137
140
  date_ranges << date_range
138
141
  end
139
142
  end
143
+ elsif holidays.length > 0 && weekdays.length == 0
144
+ holidays.each do |holiday|
145
+ if holiday == "PH"
146
+ date_ranges << WideInterval.new.holiday(holiday)
147
+ elsif holiday[:holiday] == "PH"
148
+ date_ranges << WideInterval.new.holiday(holiday[:holiday], holiday[:start], holiday[:end])
149
+ end
150
+ end
140
151
  elsif years.length > 0
141
152
  years.each do |year|
142
153
  if !year[:from_day].nil?
@@ -166,10 +177,13 @@ module OpeningHoursConverter
166
177
  date_ranges << OpeningHoursConverter::WideInterval.new.always
167
178
  end
168
179
 
180
+ if weekdays.length > 0 && holidays.length > 0
181
+ weekdays << {from: -2, to: -2}
182
+ holidays = []
183
+ end
169
184
  if weekdays.length == 0
170
185
  weekdays << {from: 0, to: OSM_DAYS.length - 1}
171
186
  end
172
-
173
187
  if times.length == 0
174
188
  times << {from: 0, to: 24*60}
175
189
  end
@@ -239,8 +253,6 @@ module OpeningHoursConverter
239
253
  add_interval(dr_obj.typical, weekdays[wd_id], times[t_id])
240
254
  end
241
255
  end
242
-
243
-
244
256
  end
245
257
  end
246
258
  end
@@ -328,6 +340,17 @@ module OpeningHoursConverter
328
340
  { from_month: year_month_from, to_month: year_month_to }
329
341
  end
330
342
 
343
+ def get_year_holiday(wrs)
344
+ single_year = wrs.gsub(/\:$/, '').split('-')
345
+ if single_year.length == 1
346
+ return { holiday: single_year[0].split(' ')[1], start: single_year[0].split(' ')[0] }
347
+ elsif single_year.length == 2
348
+ return { holiday: single_year[1].split(' ')[1], start: single_year[0], end: single_year[1].split(' ')[0] }
349
+ else
350
+ raise ArgumentError, "Invalid year_holiday : #{wrs.inspect}"
351
+ end
352
+ end
353
+
331
354
  def get_year_month_day(wrs)
332
355
  single_year_month_day = wrs.gsub(/\:$/, '').split('-')
333
356
  year_month_day_from = single_year_month_day[0].split(' ')
@@ -385,6 +408,7 @@ module OpeningHoursConverter
385
408
  end
386
409
 
387
410
  def get_weekdays(weekday_selector)
411
+ holidays = []
388
412
  weekdays = []
389
413
  wd_from = nil
390
414
  wd_to = nil
@@ -392,8 +416,10 @@ module OpeningHoursConverter
392
416
  weekday_selector = weekday_selector.split(',')
393
417
  weekday_selector.each do |wd|
394
418
  if !(@RGX_HOLIDAY =~ wd).nil?
419
+ holidays << wd
395
420
  elsif !(@RGX_WD =~ wd).nil?
396
421
  single_weekday = wd.split('-')
422
+
397
423
  wd_from = OSM_DAYS.find_index(single_weekday[0])
398
424
  if single_weekday.length > 1
399
425
  wd_to = OSM_DAYS.find_index(single_weekday[1])
@@ -407,7 +433,7 @@ module OpeningHoursConverter
407
433
  end
408
434
  end
409
435
 
410
- weekdays
436
+ { weekdays: weekdays, holidays: holidays }
411
437
  end
412
438
 
413
439
  def remove_interval(typical, weekdays, times)
@@ -496,6 +522,9 @@ module OpeningHoursConverter
496
522
  def is_comment?(token)
497
523
  !(@RGX_COMMENT =~ token).nil?
498
524
  end
525
+ def is_holiday?(token)
526
+ !(@RGX_HOLIDAY =~ token).nil?
527
+ end
499
528
  def is_rule_modifier?(token)
500
529
  !(@RGX_RULE_MODIFIER =~ token).nil?
501
530
  end
@@ -13,7 +13,15 @@ module OpeningHoursConverter
13
13
 
14
14
  def get
15
15
  result = ""
16
- if @date.length > 0
16
+ if @date.length == 1 && @date[0].wide_type == "holiday"
17
+ if !@date[0].wide.start[:year].nil?
18
+ result += @date[0].wide.start[:year]
19
+ end
20
+ if !@date[0].wide.end.nil?
21
+ result += "-#{@date[0].wide.end[:year]}"
22
+ end
23
+ result += " PH"
24
+ elsif @date.length > 0
17
25
  result += get_wide_selector
18
26
  end
19
27
 
@@ -23,10 +31,9 @@ module OpeningHoursConverter
23
31
  result += " #{wd}"
24
32
  end
25
33
  end
26
-
27
34
  if @time.length > 0
28
35
  result += " "
29
- @time.each_with_index do |t, i|
36
+ @time.uniq.each_with_index do |t, i|
30
37
  if (i > 0)
31
38
  result += ","
32
39
  end
@@ -265,6 +272,12 @@ module OpeningHoursConverter
265
272
  end
266
273
  end
267
274
 
275
+ def add_ph_weekday
276
+ @date.each do |d|
277
+ d.add_ph_weekday
278
+ end
279
+ end
280
+
268
281
  def add_overwritten_weekday(weekday)
269
282
  @date.each do |d|
270
283
  d.add_overwritten_weekday(weekday)
@@ -285,11 +298,17 @@ module OpeningHoursConverter
285
298
  end
286
299
  end
287
300
 
301
+ def include_time?(time)
302
+ @time.each do |t|
303
+ return true if t.start == time.start && t.end == time.end
304
+ end
305
+ return false
306
+ end
307
+
288
308
  def add_time(time)
289
- if (@time.length == 0 || @time[0].get != "off") && !@time.include?(time)
309
+
310
+ if (@time.length == 0 || @time[0].get != "off") && !include_time?(time)
290
311
  @time << time
291
- else
292
- raise ArgumentError, "This time can't be added to this rule"
293
312
  end
294
313
  end
295
314
 
@@ -1,6 +1,6 @@
1
1
  module OpeningHoursConverter
2
2
  class OpeningHoursTime
3
- attr_accessor :start, :end
3
+ attr_reader :start, :end
4
4
 
5
5
  def initialize(minute_start=nil, minute_end=nil)
6
6
  @start = minute_start
@@ -35,6 +35,11 @@ module OpeningHoursConverter
35
35
  if !@end.nil?
36
36
  result += "-#{@end[:year]}"
37
37
  end
38
+ when "holiday"
39
+ result = "#{@start[:year].nil? ? "" : "#{@start[:year]} "}PH"
40
+ if !@end.nil?
41
+ result = "#{@start[:year]}#{@start[:year] == @end[:year] ? "" : "-#{@end[:year]}"} PH"
42
+ end
38
43
  when "always"
39
44
  result = ""
40
45
  else
@@ -122,6 +127,18 @@ module OpeningHoursConverter
122
127
  self
123
128
  end
124
129
 
130
+ def holiday(holiday, start_year=nil, end_year=nil)
131
+ if holiday.nil? || holiday != "PH"
132
+ raise(ArgumentError, "holiday is required and can only be PH")
133
+ end
134
+ @start = { holiday: holiday, year: start_year }
135
+ unless end_year.nil? || end_year == start_year
136
+ @end = { holiday: holiday, year: end_year }
137
+ end
138
+ @type = "holiday"
139
+ self
140
+ end
141
+
125
142
  def always
126
143
  @start = nil
127
144
  @end = nil
@@ -244,6 +261,10 @@ module OpeningHoursConverter
244
261
  def equals(o)
245
262
  return false unless o.instance_of?(OpeningHoursConverter::WideInterval)
246
263
  return @type == "always" if o.type == "always"
264
+ if @type == "holiday"
265
+ return (o.type == "holiday" && (@start[:year] == o.start[:year]) &&
266
+ (@end.nil? && o.end.nil? || (@end && o.end && @end[:year] == o.end[:year])))
267
+ end
247
268
  return false if @type == "always"
248
269
  self_to_day = to_day
249
270
  o_to_day = o.to_day
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opening_hours_converter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ziserman Martin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-16 00:00:00.000000000 Z
11
+ date: 2017-10-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Datetime range to openinghours, openinghours to datetime range. Very
14
14
  strongly inspired by yohours.