Class: OpeningHoursConverter::RegexHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/opening_hours_converter/regex_handler.rb

Instance Method Summary collapse

Instance Method Details

#commaObject



294
295
296
# File 'lib/opening_hours_converter/regex_handler.rb', line 294

def comma
  ',' + space + '?'
end

#commentObject



359
360
361
# File 'lib/opening_hours_converter/regex_handler.rb', line 359

def comment
  '\"[^\"]*\"'
end

#comment_regexObject



223
224
225
# File 'lib/opening_hours_converter/regex_handler.rb', line 223

def comment_regex
  compile(line(comment))
end

#compile(string) ⇒ Object



375
376
377
378
# File 'lib/opening_hours_converter/regex_handler.rb', line 375

def compile(string)
  @compile ||= {}
  @compile[string] ||= Regexp.compile(string, Regexp::IGNORECASE)
end

#day_offset_regexObject

The day offset a selector can carry: "easter +1 day", "PH -2 days", "Su +1 day". Only the suffix is matched, so what precedes it is the selector itself.



69
70
71
# File 'lib/opening_hours_converter/regex_handler.rb', line 69

def day_offset_regex
  compile(space + group('[-+]\\d+') + space + 'days?' + end_of_line)
end

#day_regexObject



136
137
138
139
140
141
142
# File 'lib/opening_hours_converter/regex_handler.rb', line 136

def day_regex
  compile(
    line(
      potential_range(month_day)
    )
  )
end

#easterObject



338
339
340
# File 'lib/opening_hours_converter/regex_handler.rb', line 338

def easter
  'easter'
end

#easter_regexObject



62
63
64
# File 'lib/opening_hours_converter/regex_handler.rb', line 62

def easter_regex
  compile(line(easter))
end

#end_of_lineObject



367
368
369
# File 'lib/opening_hours_converter/regex_handler.rb', line 367

def end_of_line
  '$'
end

#escape(string) ⇒ Object



380
381
382
383
# File 'lib/opening_hours_converter/regex_handler.rb', line 380

def escape(string)
  @escape ||= {}
  @escape[string] ||= Regexp.escape(string)
end

#extended_timeObject

The closing hour of a range may run past midnight, which is how the specification writes a night without naming the day it ends on: "10:00-26:00" closes at 02:00 the next morning.



242
243
244
# File 'lib/opening_hours_converter/regex_handler.rb', line 242

def extended_time
  group(int_range(48)) + ':' + group(int_range(59))
end

#full_timeObject



246
247
248
# File 'lib/opening_hours_converter/regex_handler.rb', line 246

def full_time
  '24/7'
end

#group(*args) ⇒ Object



231
232
233
# File 'lib/opening_hours_converter/regex_handler.rb', line 231

def group(*args)
  "(#{args.join})"
end

#holiday_regexObject



58
59
60
# File 'lib/opening_hours_converter/regex_handler.rb', line 58

def holiday_regex
  compile(line(ph))
end

#int_range(max) ⇒ Object

Raises:

  • (ArgumentError)


250
251
252
253
254
255
256
257
258
259
# File 'lib/opening_hours_converter/regex_handler.rb', line 250

def int_range(max)
  raise ArgumentError, 'too high' if max > 99

  base = max / 10

  unit_max = max - base * 10
  base_ten = "[0-#{base - 1}]?"
  base_unit = '[0-9]'
  group(group("#{base_ten}#{base_unit}") + '|' + group("#{base}[0-#{unit_max}]"))
end

#line(pattern) ⇒ Object



371
372
373
# File 'lib/opening_hours_converter/regex_handler.rb', line 371

def line(pattern)
  start_of_line + pattern + end_of_line
end

#modifierObject



330
331
332
# File 'lib/opening_hours_converter/regex_handler.rb', line 330

def modifier
  'off'
end

#monthObject



351
352
353
# File 'lib/opening_hours_converter/regex_handler.rb', line 351

def month
  group('Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec')
end

#month_dayObject



347
348
349
# File 'lib/opening_hours_converter/regex_handler.rb', line 347

def month_day
  group('[012]?[0-9]|3[01]')
end

#month_day_regexObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/opening_hours_converter/regex_handler.rb', line 43

def month_day_regex
  compile(
    line(
      potential_range(
        month + space + month_day,
        potential(month + space) + month_day
      )
    )
  )
end

#month_regexObject



54
55
56
# File 'lib/opening_hours_converter/regex_handler.rb', line 54

def month_regex
  compile(line(potential_range(month)))
end

#multi_month_regexObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/opening_hours_converter/regex_handler.rb', line 202

def multi_month_regex
  compile(
    line(
      potential_list(
        potential_range(
          month +
          potential(
            group(
              space, potential_range(month_day)
            )
          )
        )
      )
    )
  )
end

#nth_entryObject

A single nth_entry: an index, a range of indexes, or an index counted back from the end of the month.



290
291
292
# File 'lib/opening_hours_converter/regex_handler.rb', line 290

def nth_entry
  group('\\-?[1-5]' + group(group('\\-[1-5]'), '?'))
end

#phObject



334
335
336
# File 'lib/opening_hours_converter/regex_handler.rb', line 334

def ph
  'PH'
end

#potential(pattern) ⇒ Object



261
262
263
# File 'lib/opening_hours_converter/regex_handler.rb', line 261

def potential(pattern)
  group(pattern) + '?'
end

#potential_commaObject



298
299
300
# File 'lib/opening_hours_converter/regex_handler.rb', line 298

def potential_comma
  ',? ?'
end

#potential_list(pattern, optional_pattern = pattern) ⇒ Object



269
270
271
# File 'lib/opening_hours_converter/regex_handler.rb', line 269

def potential_list(pattern, optional_pattern = pattern)
  group(pattern, group(group(comma, optional_pattern), '?'), '*')
end

#potential_range(pattern, optional_pattern = pattern) ⇒ Object



265
266
267
# File 'lib/opening_hours_converter/regex_handler.rb', line 265

def potential_range(pattern, optional_pattern = pattern)
  group(pattern, group('-', optional_pattern), '?')
end

#rule_modifier_regexObject



5
6
7
# File 'lib/opening_hours_converter/regex_handler.rb', line 5

def rule_modifier_regex
  /^(open|closed|off|unknown)$/i
end

#spaceObject



302
303
304
# File 'lib/opening_hours_converter/regex_handler.rb', line 302

def space
  ' '
end

#start_of_lineObject



363
364
365
# File 'lib/opening_hours_converter/regex_handler.rb', line 363

def start_of_line
  '^'
end

#timeObject



235
236
237
# File 'lib/opening_hours_converter/regex_handler.rb', line 235

def time
  group(int_range(24)) + ':' + group(int_range(59))
end

#time_regexObject



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/opening_hours_converter/regex_handler.rb', line 73

def time_regex
  compile(
    line(
      potential_list(
        group(
          potential_range(
            group(time), group(extended_time)
          ) + '|' + group(full_time)
        )
      ) + '\\+?' # a trailing "+" marks an open-ended time ("18:00+")
    )
  )
end

#variable_dateObject

A date named by an index rather than by a day number: "Jun Mo".



274
275
276
# File 'lib/opening_hours_converter/regex_handler.rb', line 274

def variable_date
  group(month + space + week_day + week_day_modifier)
end

#variable_date_range_regexObject

A monthday range whose bounds are variable dates: "Jun Mo-Sep Sa" runs from the first Monday of June to the first Saturday of September.



119
120
121
122
123
124
125
# File 'lib/opening_hours_converter/regex_handler.rb', line 119

def variable_date_range_regex
  compile(
    line(
      variable_date + '-' + variable_date
    )
  )
end

#weekObject



326
327
328
# File 'lib/opening_hours_converter/regex_handler.rb', line 326

def week
  'week'
end

#week_dayObject

The three letter forms are not specification, but the data holds them.



343
344
345
# File 'lib/opening_hours_converter/regex_handler.rb', line 343

def week_day
  group('Mon?|Tue?|Wed?|Thu?|Fri?|Sat?|Sun?')
end

#week_day_modifierObject

The OSM nth_entry list: "[2]", "[1,3]", "[2-4]", "[-1]", "[1,3,5]".



284
285
286
# File 'lib/opening_hours_converter/regex_handler.rb', line 284

def week_day_modifier
  '\\[' + potential_list(nth_entry) + '\\]'
end

#week_day_or_holiday_regexObject



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/opening_hours_converter/regex_handler.rb', line 87

def week_day_or_holiday_regex
  compile(
    line(
      potential_list(
        group(
          potential_range(
            group(week_day)
          ) + '|' + group(ph)
        )
      )
    )
  )
end

#week_day_regexObject



101
102
103
104
105
106
107
# File 'lib/opening_hours_converter/regex_handler.rb', line 101

def week_day_regex
  compile(
    line(
      potential_range(week_day)
    )
  )
end

#week_day_sequence_itemObject

A weekday of a sequence, with or without its index: "Mo", "Mo-Fr", "Sa".



279
280
281
# File 'lib/opening_hours_converter/regex_handler.rb', line 279

def week_day_sequence_item
  group(potential_range(week_day) + group(group(week_day_modifier), '?'))
end

#week_day_with_modifier_regexObject



109
110
111
112
113
114
115
# File 'lib/opening_hours_converter/regex_handler.rb', line 109

def week_day_with_modifier_regex
  compile(
    line(
      potential_list(week_day_sequence_item)
    )
  )
end

#week_key_regexObject



9
10
11
# File 'lib/opening_hours_converter/regex_handler.rb', line 9

def week_key_regex
  compile(line(week))
end

#week_modifierObject



310
311
312
# File 'lib/opening_hours_converter/regex_handler.rb', line 310

def week_modifier
  '\/[1-9]'
end

#week_numberObject



306
307
308
# File 'lib/opening_hours_converter/regex_handler.rb', line 306

def week_number
  group('[01234]?[0-9]|5[0123]')
end

#week_regexObject



13
14
15
16
17
18
19
20
21
# File 'lib/opening_hours_converter/regex_handler.rb', line 13

def week_regex
  compile(
    line(
      week + space + potential_list(
        potential_range(int_range(53))
      )
    )
  )
end

#week_value_regexObject



219
220
221
# File 'lib/opening_hours_converter/regex_handler.rb', line 219

def week_value_regex
  compile(line(potential_list(potential_range(int_range(53)))))
end

#week_value_with_modifier_regexObject



33
34
35
36
37
38
39
40
41
# File 'lib/opening_hours_converter/regex_handler.rb', line 33

def week_value_with_modifier_regex
  compile(
    line(
      potential_list(
        potential_range(int_range(53), group(int_range(53) + potential(week_modifier)))
      )
    )
  )
end

#week_with_modifierObject



320
321
322
323
324
# File 'lib/opening_hours_converter/regex_handler.rb', line 320

def week_with_modifier
  group(
    potential_range(week_number, week_number + potential(week_modifier))
  )
end

#week_with_modifier_regexObject



23
24
25
26
27
28
29
30
31
# File 'lib/opening_hours_converter/regex_handler.rb', line 23

def week_with_modifier_regex
  compile(
    line(
      week + space + potential_list(
        potential_range(int_range(53), group(int_range(53) + potential(week_modifier)))
      )
    )
  )
end

#yearObject



355
356
357
# File 'lib/opening_hours_converter/regex_handler.rb', line 355

def year
  '\\d{4}'
end

#year_month_day_regexObject



172
173
174
175
176
177
178
179
180
181
# File 'lib/opening_hours_converter/regex_handler.rb', line 172

def year_month_day_regex
  compile(
    line(
      potential_range(
        year + space + month + space + month_day,
        potential(year + space) + potential(month + space) + month_day
      )
    )
  )
end

#year_month_regexObject



227
228
229
# File 'lib/opening_hours_converter/regex_handler.rb', line 227

def year_month_regex
  compile(line(potential_range(year + space + month, potential(year + space) + month)))
end

#year_multi_month_day_regexObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/opening_hours_converter/regex_handler.rb', line 183

def year_multi_month_day_regex
  compile(
    line(
      year + space +
      potential_list(
        potential(year + space) +
        month +
        group(
          space, potential_range(month_day)
        )
      )
    )
  )
end

#year_multi_month_regexObject



198
199
200
# File 'lib/opening_hours_converter/regex_handler.rb', line 198

def year_multi_month_regex
  compile(line(year + space + group(potential_range(month), potential_comma) + '*'))
end

#year_ph_regexObject



144
145
146
147
148
149
150
# File 'lib/opening_hours_converter/regex_handler.rb', line 144

def year_ph_regex
  compile(
    line(
      year + group(space + ph + '|' + '-' + year + space + ph)
    )
  )
end

#year_regexObject



127
128
129
130
131
132
133
134
# File 'lib/opening_hours_converter/regex_handler.rb', line 127

def year_regex
  compile(
    line(
      # a trailing "+" marks an open-ended year ("2020+" = 2020 onward)
      group(potential_range(year), potential(year_step), '\\+?')
    )
  )
end

#year_stepObject

A step in a year range: "2020-2030/2" selects every other year, the same notation the week selector writes as "week 1-53/2".



316
317
318
# File 'lib/opening_hours_converter/regex_handler.rb', line 316

def year_step
  '\/[1-9][0-9]?'
end

#year_week_regexObject



152
153
154
155
156
157
158
159
160
161
# File 'lib/opening_hours_converter/regex_handler.rb', line 152

def year_week_regex
  compile(
    line(
      potential_range(year) + space + week + space +
      group(
        potential_list(potential_range(week_number))
      )
    )
  )
end

#year_week_with_modifier_regexObject



163
164
165
166
167
168
169
170
# File 'lib/opening_hours_converter/regex_handler.rb', line 163

def year_week_with_modifier_regex
  compile(
    line(
      potential_range(year) + space + week + space +
      potential_list(week_with_modifier)
    )
  )
end