opening_hours_converter 0.0.2 → 0.0.3

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: 3d01e4f3c1f4306964ee63ade1bb6e018722446a
4
- data.tar.gz: 5671d033a5182245b46ff12838c2ccb11de8f7e5
3
+ metadata.gz: 1d3d220396767c052612ffa5d62d86e1b862f834
4
+ data.tar.gz: 46759c801d7003a905028e1d9ff2bbe45f8ceab8
5
5
  SHA512:
6
- metadata.gz: 99ec2ff1a1232f5ff42a6f0c015c4211fdf0a0a801b98c42d448e3b0828dfe0275e5e3246bd39403c716def6c3cbb241b35ad13a4a2114db592c12a0f6b38cb7
7
- data.tar.gz: 4438647b1676e08b941159403631eefbc873e90970e24176006c30cb10406822b65a978e93d930fea8486c6b3c4b9f86ca40af42d5910484a633890a70ca9a4c
6
+ metadata.gz: 68dbbf968aa6cf83f2e6bf1c78a148d378ea49a1ff2fc5c59aa3c7b750378ae51301075cb54cac1844c4b79cbd887ed93d184393b63e4c9a12e11d9654c6f4a5
7
+ data.tar.gz: 2d7a47fc9f4abd2536aae277843a59dda19ce31fc805a8f1d903cd06804477376d813a47d9a574cfa6833d1e04c868c28d648de0a0dc17d79b36b362439b98ea
@@ -14,9 +14,6 @@ module OpeningHoursConverter
14
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)))*$/
15
15
  @RGX_HOLIDAY = /^(PH|SH|easter)$/
16
16
  @RGX_WD = /^(Mo|Tu|We|Th|Fr|Sa|Su)(\-(Mo|Tu|We|Th|Fr|Sa|Su))?$/
17
- @RGX_YEAR = /^(\d{4})(\-(\d{4}))?$/
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
- @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
17
  end
21
18
 
22
19
  def parse(oh)
@@ -33,19 +30,18 @@ module OpeningHoursConverter
33
30
  weekdays = nil
34
31
  weeks = nil
35
32
  months = nil
36
- years = nil
33
+
34
+ single_time = nil
35
+ from = nil
36
+ to = nil
37
37
 
38
38
  single_month = nil
39
39
  month_from = nil
40
40
  month_to = nil
41
41
 
42
- single_year = nil
43
- year_from = nil
44
- year_to = nil
45
-
46
- single_year = nil
47
- year_from = nil
48
- year_to = nil
42
+ single_week = nil
43
+ week_from = nil
44
+ week_to = nil
49
45
 
50
46
  date_ranges = nil
51
47
  date_range = nil
@@ -65,10 +61,29 @@ module OpeningHoursConverter
65
61
  current_token -= 1
66
62
  end
67
63
 
64
+
65
+ # get time selector
66
+ from = nil
67
+ to = nil
68
68
  times = []
69
69
  if current_token >= 0 && is_time?(tokens[current_token])
70
70
  time_selector = tokens[current_token]
71
- times = get_times(time_selector)
71
+
72
+ if time_selector == "24/7"
73
+ times << {from: 0, to: 24*60}
74
+ else
75
+ time_selector = time_selector.split(',')
76
+ time_selector.each do |ts|
77
+ single_time = ts.split('-')
78
+ from = as_minutes(single_time[0])
79
+ if single_time.length > 1
80
+ to = as_minutes(single_time[1])
81
+ else
82
+ to = from
83
+ end
84
+ times << {from: from, to: to}
85
+ end
86
+ end
72
87
  current_token -= 1
73
88
  end
74
89
 
@@ -78,66 +93,120 @@ module OpeningHoursConverter
78
93
  weekdays << {from: 0, to: 6}
79
94
  elsif current_token >= 0 && is_weekday?(tokens[current_token])
80
95
  weekday_selector = tokens[current_token]
81
- weekdays = get_weekdays(weekday_selector)
96
+ weekday_selector = weekday_selector.split(',')
97
+ weekday_selector.each do |wd|
98
+ if !(@RGX_HOLIDAY =~ wd).nil?
99
+ elsif !(@RGX_WD =~ wd).nil?
100
+ single_weekday = wd.split('-')
101
+ wd_from = OSM_DAYS.find_index(single_weekday[0])
102
+ if single_weekday.length > 1
103
+ wd_to = OSM_DAYS.find_index(single_weekday[1])
104
+ else
105
+ wd_to = wd_from
106
+ end
107
+
108
+ weekdays << {from: wd_from, to: wd_to}
109
+ else
110
+ raise ArgumentError, "Invalid weekday interval : #{wd}"
111
+ end
112
+ end
82
113
  current_token -= 1
83
114
  end
84
115
 
85
- # years = []
86
- # if current_token >= 0 && is_year?(tokens[current_token])
87
- # year_selector = tokens[current_token]
88
- # year_selector = year_selector.split(',')
89
- # year_selector.each do |y|
90
- # single_year = y.gsub(/\:$/, '').split('-')
91
- # year_from = single_year[0]
92
- # if single_year.length > 1
93
- # year_to = single_year[1]
94
- # else
95
- # year_to = year_from
96
- # end
97
-
98
- # years << {from: year_from, to: year_to}
99
- # end
100
- # current_token -= 1
101
- # end
102
-
116
+ weeks = []
103
117
  months = []
104
- years = []
105
118
  if current_token >= 0
106
-
107
119
  wide_range_selector = tokens[0]
108
120
  for i in 1..current_token
109
121
  wide_range_selector += " #{tokens[i]}"
110
122
  end
111
123
  if wide_range_selector.length > 0
112
- wide_range_selector = wide_range_selector.strip
113
- wide_range_selector = wide_range_selector.split(',')
114
- wide_range_selector.each do |wrs|
115
- if !(@RGX_YEAR_MONTH_DAY =~ wrs).nil?
116
- years << get_year_month_day(wrs)
117
- elsif !(@RGX_YEAR_MONTH =~ wrs).nil?
118
- years << get_year_month(wrs)
119
- elsif !(@RGX_MONTHDAY =~ wrs).nil?
120
- months << get_month_day(wrs)
121
- elsif !(@RGX_MONTH =~ wrs).nil?
122
- months << get_month(wrs)
123
- elsif !(@RGX_YEAR =~ wrs).nil?
124
-
125
- years << {from: year_from, to: year_to}
126
- else
127
- raise ArgumentError, "Unsupported selector #{wrs}"
124
+ wide_range_selector = wide_range_selector.gsub(/\:$/, '').split('week')
125
+ month_selector = wide_range_selector[0].strip
126
+ if month_selector.length == 0
127
+ month_selector = nil
128
+ end
129
+
130
+ if wide_range_selector.length > 1
131
+ week_selector = wide_range_selector[1].strip
132
+ if week_selector.length == 0
133
+ week_selector = nil
128
134
  end
135
+ else
136
+ week_selector = nil
137
+ end
138
+
139
+ if (!month_selector.nil? && !week_selector.nil?)
140
+ raise ArgumentError, "unsupported simultaneous month and week selector"
141
+ elsif !month_selector.nil?
142
+ month_selector = month_selector.split(',')
143
+
144
+ month_selector.each do |ms|
145
+ if ms == "SH"
146
+ elsif !(@RGX_MONTH =~ ms).nil?
147
+ single_month = ms.split('-')
148
+ month_from = OSM_MONTHS.find_index(single_month[0]) + 1
149
+ if month_from < 1
150
+ raise ArgumentError, "Invalid month : #{single_month[0]}"
151
+ end
152
+
153
+ if single_month.length > 1
154
+ month_to = OSM_MONTHS.find_index(single_month[1]) + 1
155
+ if month_to < 1
156
+ raise ArgumentError, "Invalid month : #{single_month[1]}"
157
+ end
158
+ else
159
+ month_to = month_from
160
+ end
161
+ months << {from: month_from, to: month_to}
162
+ elsif !(@RGX_MONTHDAY =~ ms).nil?
163
+ single_month = ms.gsub(/\:$/, '').split('-')
164
+
165
+ month_from = single_month[0].split(' ')
166
+ month_from = { day: month_from[1].to_i, month: OSM_MONTHS.find_index(month_from[0]) + 1 }
167
+ if month_from[:month] < 1
168
+ raise ArgumentError, "Invalid month : #{month_from.inspect}"
169
+ end
170
+
171
+ if single_month.length > 1
172
+ month_to = single_month[1].split(' ')
173
+ month_to = { day: month_to[1].to_i, month: OSM_MONTHS.find_index(month_to[0]) + 1 }
174
+ if month_to[:month] < 1
175
+ raise ArgumentError, "Invalid month : #{month_to.inspect}"
176
+ end
177
+ else
178
+ month_to = nil
179
+ end
180
+ months << {from_day: month_from, to_day: month_to}
181
+ else
182
+ raise ArgumentError, "Unsupported month selector #{ms}"
183
+ end
184
+ end
185
+ elsif !week_selector.nil?
186
+ week_selector = week_selector.split(',')
187
+ week_selector.each do |ws|
188
+ single_week = ws.split('-')
189
+ week_from = single_week[0].to_i
190
+ if single_week.length > 1
191
+ week_to = single_week[1].to_i
192
+ else
193
+ week_to = nil
194
+ end
195
+ weeks << {from: week_from, to: week_to}
196
+ end
197
+ else
198
+ raise ArgumentError, "Invalid date selector"
129
199
  end
130
200
  end
131
201
  end
132
-
133
202
  if current_token == tokens.length - 1
134
203
  raise ArgumentError, "Unreadable string"
135
204
  end
136
- puts "months : #{months}"
137
- puts "weekdays : #{weekdays}"
138
- puts "times : #{times}"
139
- puts "years : #{years}"
140
- puts "rule_modifier : #{rule_modifier}"
205
+ # puts "months : #{months}"
206
+ # puts "weeks : #{weeks}"
207
+ # puts "weekdays : #{weekdays}"
208
+ # puts "times : #{times}"
209
+ # puts "rule_modifier : #{rule_modifier}"
141
210
 
142
211
  date_ranges = []
143
212
 
@@ -145,41 +214,26 @@ module OpeningHoursConverter
145
214
  months.each do |month|
146
215
  if !month[:from_day].nil?
147
216
  if !month[:to_day].nil?
148
- date_range = OpeningHoursConverter::WideInterval.new.day(month[:from_day][:day], month[:from_day][:month], nil, month[:to_day][:day], month[:to_day][:month], nil)
217
+ date_range = OpeningHoursConverter::WideInterval.new.day(month[:from_day][:day], month[:from_day][:month], month[:to_day][:day], month[:to_day][:month])
149
218
  else
150
219
  date_range = OpeningHoursConverter::WideInterval.new.day(month[:from_day][:day], month[:from_day][:month])
151
220
  end
152
221
  date_ranges << date_range
153
222
  else
154
223
  if !month[:to].nil?
155
- date_range = OpeningHoursConverter::WideInterval.new.month(month[:from], nil, month[:to])
224
+ date_range = OpeningHoursConverter::WideInterval.new.month(month[:from], month[:to])
156
225
  else
157
226
  date_range = OpeningHoursConverter::WideInterval.new.month(month[:from])
158
227
  end
159
228
  date_ranges << date_range
160
229
  end
161
230
  end
162
- elsif years.length > 0
163
- years.each do |year|
164
- # binding.pry
165
- if !year[:to].nil?
166
- if !year[:from_day].nil?
167
- date_range = OpeningHoursConverter::WideInterval.new.day(year[:from_day][:day], year[:from_day][:month], year[:from_day][:year],
168
- year[:to_day][:day], year[:to_day][:month], year[:to_day][:year])
169
- elsif !year[:from_month].nil?
170
- date_range = OpeningHoursConverter::WideInterval.new.month(year[:from_month][:month], year[:from_month][:year],
171
- year[:to_month][:month], year[:to_month][:year])
172
- else
173
- date_range = OpeningHoursConverter::WideInterval.new.year(year[:from], year[:to])
174
- end
231
+ elsif weeks.length > 0
232
+ weeks.each do |week|
233
+ if !week[:to].nil?
234
+ date_range = OpeningHoursConverter::WideInterval.new.week(week[:from], week[:to])
175
235
  else
176
- if !year[:from_month].nil?
177
- date_range = OpeningHoursConverter::WideInterval.new.month(year[:from_month][:month], year[:from_month][:year])
178
- elsif !year[:from_day].nil?
179
- date_range = OpeningHoursConverter::WideInterval.new.day(year[:from_day][:day], year[:from_day][:month], year[:from_day][:year])
180
- else
181
- date_range = OpeningHoursConverter::WideInterval.new.year(year[:from])
182
- end
236
+ date_range = OpeningHoursConverter::WideInterval.new.week(week[:from])
183
237
  end
184
238
  date_ranges << date_range
185
239
  end
@@ -264,168 +318,6 @@ module OpeningHoursConverter
264
318
  return result
265
319
  end
266
320
 
267
- def get_year(wrs)
268
- single_year = wrs.gsub(/\:$/, '').split('-')
269
- year_from = single_year[0].to_i
270
- if year_from < 1
271
- raise ArgumentError, "Invalid year : #{single_year[0]}"
272
- end
273
-
274
- if single_year.length > 1
275
- year_to = single_year[1].to_i
276
- if year_to < 1
277
- raise ArgumentError, "Invalid year : #{single_year[1]}"
278
- end
279
- else
280
- year_to = nil
281
- end
282
- { from: year_from, to: year_to }
283
- end
284
-
285
- def get_month(wrs)
286
- single_month = wrs.gsub(/\:$/, '').split('-')
287
- month_from = OSM_MONTHS.find_index(single_month[0]) + 1
288
- if month_from < 1
289
- raise ArgumentError, "Invalid month : #{single_month[0]}"
290
- end
291
-
292
- if single_month.length > 1
293
- month_to = OSM_MONTHS.find_index(single_month[1]) + 1
294
- if month_to < 1
295
- raise ArgumentError, "Invalid month : #{single_month[1]}"
296
- end
297
- else
298
- month_to = month_from
299
- end
300
- { from: month_from, to: month_to }
301
- end
302
-
303
- def get_month_day(wrs)
304
- single_month = wrs.gsub(/\:$/, '').split('-')
305
-
306
- month_from = single_month[0].split(' ')
307
- month_from = { day: month_from[1].to_i, month: OSM_MONTHS.find_index(month_from[0]) + 1 }
308
- if month_from[:month] < 1
309
- raise ArgumentError, "Invalid month : #{month_from.inspect}"
310
- end
311
-
312
- if single_month.length > 1
313
- month_to = single_month[1].split(' ')
314
- month_to = { day: month_to[1].to_i, month: OSM_MONTHS.find_index(month_to[0]) + 1 }
315
- if month_to[:month] < 1
316
- raise ArgumentError, "Invalid month : #{month_to.inspect}"
317
- end
318
- else
319
- month_to = nil
320
- end
321
- { from_day: month_from, to_day: month_to }
322
- end
323
-
324
- def get_year_month(wrs)
325
- single_year_month = wrs.gsub(/\:$/, '').split('-')
326
- year_month_from = single_year_month[0].split(' ')
327
- year_month_from = { month: OSM_MONTHS.find_index(year_month_from[1]) + 1, year: year_month_from[0].to_i }
328
- if year_month_from.length < 1
329
- raise ArgumentError, "Invalid year_month : #{year_month_from.inspect}"
330
- end
331
- if single_year_month.length > 1
332
- year_month_to = single_year_month[1].split(' ')
333
- if year_month_to.length == 2
334
- year_month_to = { month: OSM_MONTHS.find_index(year_month_to[1]) + 1, year: year_month_to[0].to_i }
335
- elsif year_month_to.length == 1
336
- year_month_to = { month: OSM_MONTHS.find_index(year_month_to[1]) + 1, year: year_month_from[:year] }
337
- end
338
- if year_month_to.length < 1
339
- raise ArgumentError, "Invalid year_month : #{year_month_to.inspect}"
340
- end
341
- else
342
- year_month_to = nil
343
- end
344
- { from_month: year_month_from, to_month: year_month_to }
345
- end
346
-
347
- def get_year_month_day(wrs)
348
- single_year_month_day = wrs.gsub(/\:$/, '').split('-')
349
- year_month_day_from = single_year_month_day[0].split(' ')
350
- year_month_day_from = { day: year_month_day_from[2].to_i,
351
- month: OSM_MONTHS.find_index(year_month_day_from[1]) + 1,
352
- year: year_month_day_from[0].to_i }
353
- if year_month_day_from.length < 1
354
- raise ArgumentError, "Invalid year_month_day : #{year_month_day_from.inspect}"
355
- end
356
- if single_year_month_day.length > 1
357
- year_month_day_to = single_year_month_day[1].split(' ')
358
- if year_month_day_to.length == 3
359
- year_month_day_to = { day: year_month_day_to[2].to_i,
360
- month: OSM_MONTHS.find_index(year_month_day_to[1]) + 1,
361
- year: year_month_day_to[0].to_i }
362
- elsif year_month_day_to.length == 2
363
- year_month_day_to = { day: year_month_day_to[1].to_i,
364
- month: OSM_MONTHS.find_index(year_month_day_to[0]) + 1,
365
- year: year_month_day_from[:year] }
366
- elsif year_month_day_to.length == 1
367
- year_month_day_to = { day: year_month_day_to[0].to_i,
368
- month: year_month_day_from[:month],
369
- year: year_month_day_from[:year] }
370
- end
371
- if year_month_day_to.length < 1
372
- raise ArgumentError, "Invalid year_month_day : #{year_month_day_to.inspect}"
373
- end
374
- else
375
- year_month_day_to = nil
376
- end
377
-
378
- { from_day: year_month_day_from, to_day: year_month_day_to }
379
- end
380
-
381
- def get_times(time_selector)
382
- times = []
383
- from = nil
384
- to = nil
385
- if time_selector == "24/7"
386
- times << {from: 0, to: 24*60}
387
- else
388
- time_selector = time_selector.split(',')
389
- time_selector.each do |ts|
390
- single_time = ts.split('-')
391
- from = as_minutes(single_time[0])
392
- if single_time.length > 1
393
- to = as_minutes(single_time[1])
394
- else
395
- to = from
396
- end
397
- times << {from: from, to: to}
398
- end
399
- end
400
- times
401
- end
402
-
403
- def get_weekdays(weekday_selector)
404
- weekdays = []
405
- wd_from = nil
406
- wd_to = nil
407
-
408
- weekday_selector = weekday_selector.split(',')
409
- weekday_selector.each do |wd|
410
- if !(@RGX_HOLIDAY =~ wd).nil?
411
- elsif !(@RGX_WD =~ wd).nil?
412
- single_weekday = wd.split('-')
413
- wd_from = OSM_DAYS.find_index(single_weekday[0])
414
- if single_weekday.length > 1
415
- wd_to = OSM_DAYS.find_index(single_weekday[1])
416
- else
417
- wd_to = wd_from
418
- end
419
-
420
- weekdays << {from: wd_from, to: wd_to}
421
- else
422
- raise ArgumentError, "Invalid weekday interval : #{wd}"
423
- end
424
- end
425
-
426
- weekdays
427
- end
428
-
429
321
  def remove_interval(typical, weekdays, times)
430
322
  if weekdays[:from] <= weekdays[:to]
431
323
  for wd in weekdays[:from]..weekdays[:to]
@@ -512,8 +404,5 @@ module OpeningHoursConverter
512
404
  def is_weekday?(token)
513
405
  !(@RGX_WEEKDAY =~ token).nil?
514
406
  end
515
- def is_year?(token)
516
- !(@RGX_YEAR =~ token).nil?
517
- end
518
407
  end
519
408
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opening_hours_converter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
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-06 00:00:00.000000000 Z
11
+ date: 2017-10-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Datetime range to openinghours, openinghours to datetime range
14
- email: martinziserman@gmail.com
14
+ email: tech@publidata.io
15
15
  executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
@@ -30,7 +30,7 @@ files:
30
30
  - lib/opening_hours_converter/wide_interval.rb
31
31
  homepage: http://rubygems.org/gems/opening_hours_converter
32
32
  licenses:
33
- - MIT
33
+ - GNU AFFERO GENERAL PUBLIC LICENSE
34
34
  metadata: {}
35
35
  post_install_message:
36
36
  rdoc_options: []