opening_hours_converter 1.13.6 → 1.13.11

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
  SHA256:
3
- metadata.gz: '08d64cb65cb3b261526debe5bbac2139dc2d18ccfea137eeceaddb5c221a7a09'
4
- data.tar.gz: a6553952b6ea0c7d3f0e87f916a315c7ab9f022bb5c8cb58accae49a32f8d283
3
+ metadata.gz: 429a62958899aeaee2bc229201b32db3f8e5c71ba3de00b35979f8d7f600051e
4
+ data.tar.gz: 7f408d609dc038324dc31edfe72d306321e3cf87cb3fe6889524777a576c6865
5
5
  SHA512:
6
- metadata.gz: 7467758597633280c3863cead517d31fa18ad63dbaf8fca05d5236da2d63001e360ba3ade52f054774436da8e14787fd02e5af330007cebeebc152a96359732e
7
- data.tar.gz: 348168fd8f07315a942162d8313736f8237a196e4fdc84ed150812d0751aa83344c0740fa5330ef59b5499bd6857793ed001625e1c4f56c020c0d28a60138104
6
+ metadata.gz: 7cbfcce4281b812116366f14d4ae9f28b421182d462373dcd40366cf7e95a4da71479e6d8809ebf230b2143d89d654213406686254e7de98e4bc3564d0f3ac9c
7
+ data.tar.gz: 6e54d189cccf398a083e2283cffe41591ec3519a67dad77d92c8901504cf6284183a9e73b3b6e65c382c9a7a8c22916c9ad46467a9fdd462e8506888c52616dc
@@ -1,6 +1,7 @@
1
1
  module OpeningHoursConverter
2
2
  require 'date'
3
3
  require_relative './opening_hours_converter/utils'
4
+ require_relative './opening_hours_converter/errors'
4
5
  require_relative './opening_hours_converter/regex_handler'
5
6
  require_relative './opening_hours_converter/date_range'
6
7
  require_relative './opening_hours_converter/token'
@@ -17,7 +17,7 @@ module OpeningHoursConverter
17
17
 
18
18
  off, start_minute, end_minute = handle_interval(interval)
19
19
 
20
- raise "Invalid interval #{interval.inspect}" if start_minute.nil? && end_minute.nil?
20
+ raise ParseError, "Invalid interval #{interval.inspect}" if start_minute.nil? && end_minute.nil?
21
21
 
22
22
  (start_minute..end_minute).step do |minute|
23
23
  minute_array[minute] = off ? 'off' : true
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Unable to parse Opening Hours String
4
+ class OpeningHoursConverter::ParseError < StandardError; end
@@ -107,7 +107,7 @@ module OpeningHoursConverter
107
107
  elsif !(@regex_handler.month_day_regex =~ wide_range_selector).nil?
108
108
  months << get_month_day(wide_range_selector)
109
109
  elsif !(@regex_handler.month_regex =~ wide_range_selector).nil?
110
- months << get_month(wide_range_selector)
110
+ months += get_month(wide_range_selector)
111
111
  elsif !(@regex_handler.year_regex =~ wide_range_selector).nil?
112
112
  years << get_year(wide_range_selector)
113
113
  elsif !(@regex_handler.multi_month_regex =~ wide_range_selector).nil?
@@ -125,12 +125,12 @@ module OpeningHoursConverter
125
125
  elsif !(@regex_handler.week_regex =~ wide_range_selector).nil?
126
126
  weeks << get_week(wide_range_selector)
127
127
  else
128
- raise ArgumentError, "Unsupported selector #{wide_range_selector}"
128
+ raise ParseError, "Unsupported selector #{wide_range_selector}"
129
129
  end
130
130
  end
131
131
  end
132
132
 
133
- raise ArgumentError, 'Unreadable string' if @current_token == tokens.length - 1
133
+ raise ParseError, 'Unreadable string' if @current_token == tokens.length - 1
134
134
 
135
135
  # puts "weekdays : #{weekdays}"
136
136
  # puts "weeks : #{weeks}"
@@ -408,7 +408,23 @@ module OpeningHoursConverter
408
408
  else
409
409
  month_to = month_from
410
410
  end
411
- { from: month_from, to: month_to }
411
+
412
+ if month_from > month_to
413
+ [
414
+ {
415
+ from: month_from,
416
+ to: 12
417
+ },
418
+ {
419
+ from: 1,
420
+ to: month_to
421
+ }
422
+ ]
423
+ else
424
+ [
425
+ { from: month_from, to: month_to }
426
+ ]
427
+ end
412
428
  end
413
429
 
414
430
  def get_month_day(wrs)
@@ -500,9 +516,12 @@ module OpeningHoursConverter
500
516
 
501
517
  def get_year_multi_month_day(wrs)
502
518
  year = wrs[0...4]
503
- wrs = wrs[5..wrs.length]
504
519
 
505
- wrs.split(',').map do |wr|
520
+ wrs.split(',').map.with_index do |wr|
521
+ if wr =~ /^#{@regex_handler.year}/
522
+ year = wr[0...4]
523
+ wr = wr[5...wr.length]
524
+ end
506
525
  month = wr[0...3]
507
526
  days = wr[4...wr.length].split('-').reject { |e| e == '' }.map(&:to_i)
508
527
  if days.length == 2
@@ -163,6 +163,7 @@ module OpeningHoursConverter
163
163
  line(
164
164
  year + space +
165
165
  potential_list(
166
+ potential(year + space) +
166
167
  month +
167
168
  group(
168
169
  space, potential_range(month_day)
@@ -205,7 +206,7 @@ module OpeningHoursConverter
205
206
  end
206
207
 
207
208
  def int_range(max)
208
- raise 'too high' if max > 99
209
+ raise ArgumentError, 'too high' if max > 99
209
210
 
210
211
  base = max / 10
211
212
 
@@ -14,6 +14,10 @@ module OpeningHoursConverter
14
14
  @made_from = made_from
15
15
  end
16
16
 
17
+ def to_s
18
+ "Token(value: #{@value}, type: #{@type}, start_index: #{@start_index})"
19
+ end
20
+
17
21
  def year?
18
22
  integer? && @value.length == 4
19
23
  end
@@ -18,7 +18,7 @@ module OpeningHoursConverter
18
18
  def tokenize
19
19
  counter = 0
20
20
  while @index < @opening_hours_string.length
21
- raise 'ups' if counter > 200
21
+ raise ParseError if counter > 200
22
22
  skip_white_spaces
23
23
  @tokens << handle_string if string?
24
24
 
@@ -67,7 +67,7 @@ module OpeningHoursConverter
67
67
  next
68
68
  end
69
69
 
70
- raise "can't read current token #{current_token}"
70
+ raise ParseError, "can't read current token #{current_token}"
71
71
  end
72
72
  end
73
73
 
@@ -105,7 +105,7 @@ module OpeningHoursConverter
105
105
  value, made_from, type = add_current_token_to(value, type, made_from)
106
106
  next
107
107
  end
108
- raise "you can\'t have two years with just space between them previous token: #{previous_token}, current token: #{current_token}"
108
+ raise ParseError, "you can\'t have two years with just space between them previous token: #{previous_token}, current token: #{current_token}"
109
109
  end
110
110
 
111
111
  if current_token.string?
@@ -309,23 +309,23 @@ module OpeningHoursConverter
309
309
  made_from = [current_token]
310
310
  @index += 1
311
311
 
312
- raise unless current_token.colon?
312
+ raise ParseError unless current_token.colon?
313
313
  value, made_from, type = add_current_token_to(value, type, made_from)
314
314
 
315
- raise unless current_token.time?
315
+ raise ParseError unless current_token.time?
316
316
  value, made_from, type = add_current_token_to(value, type, made_from)
317
317
 
318
- raise unless current_token.hyphen?
318
+ raise ParseError unless current_token.hyphen?
319
319
  value, made_from, type = add_current_token_to(value, type, made_from)
320
320
 
321
321
  # second part of time range
322
- raise unless current_token.time?
322
+ raise ParseError unless current_token.time?
323
323
  value, made_from, type = add_current_token_to(value, type, made_from)
324
324
 
325
- raise unless current_token.colon?
325
+ raise ParseError unless current_token.colon?
326
326
  value, made_from, type = add_current_token_to(value, type, made_from)
327
327
 
328
- raise unless current_token.time?
328
+ raise ParseError unless current_token.time?
329
329
  value, made_from, type = add_current_token_to(value, type, made_from)
330
330
 
331
331
  token(value, type, start_index, made_from)
@@ -6,22 +6,8 @@ module OpeningHoursConverter
6
6
  include Constants
7
7
  extend Utils
8
8
 
9
- def self.weeks_from_index(array_of_indexes, year = Time.now.year)
10
- array_of_indexes.flat_map do |index_or_range|
11
- if index_or_range.is_a?(Hash)
12
- index_or_range[:from].step(index_or_range[:to], index_or_range[:modifier]).map do |index|
13
- week_from_index(index, year)
14
- end
15
- elsif index_or_range.is_a?(Integer)
16
- week_from_index(index_or_range, year)
17
- else
18
- raise ArgumentError, "#{index_or_range} in #{array_of_indexes} is neither an integer nor a Hash"
19
- end
20
- end
21
- end
22
-
23
9
  def self.week_from_index(index, year = Time.now.year)
24
- raise unless index >= 1
10
+ raise ArgumentError unless index >= 1
25
11
 
26
12
  week = first_week(year)
27
13
  offset = (index - 1) * 7
@@ -106,7 +92,7 @@ module OpeningHoursConverter
106
92
  first_day_of_the_week = first_day_of_month - first_wday_of_month
107
93
  first_day_of_the_week + wday + (n - 1) * 7
108
94
  end
109
- raise 'Out of bound' unless date.month == month
95
+ raise ArgumentError, 'Out of bound' unless date.month == month
110
96
 
111
97
  date
112
98
  end
@@ -133,7 +119,7 @@ module OpeningHoursConverter
133
119
  first_day_of_the_week = last_day_of_month - last_wday_of_month
134
120
  first_day_of_the_week + wday
135
121
  end
136
- raise 'Out of bound' unless date.month == month
122
+ raise ArgumentError, 'Out of bound' unless date.month == month
137
123
 
138
124
  date
139
125
  end
@@ -24,7 +24,6 @@ module OpeningHoursConverter
24
24
  years = process_always_holiday(date_range.wide_interval, years)
25
25
  end
26
26
  end
27
-
28
27
  if !date_range.wide_interval.start.nil? && !date_range.wide_interval.start[:year].nil?
29
28
  if date_range.wide_interval.end.nil? || date_range.wide_interval.end[:year].nil? || date_range.wide_interval.start[:year] == date_range.wide_interval.end[:year]
30
29
  if !years[date_range.wide_interval.start[:year]].nil?
@@ -228,14 +227,7 @@ module OpeningHoursConverter
228
227
  end
229
228
  else
230
229
  for year in (DateTime.now.year)..(DateTime.now.year + 1)
231
- if wide_interval.type == 'week'
232
- years[year] ||= Array.new(OSM_MONTHS.length) { |i| Array.new(MONTH_END_DAY[i]) { false } }
233
- WeekIndex.weeks_from_index(wide_interval.indexes, year).each do |interval|
234
- (interval[:from]..interval[:to]).each do |day|
235
- years[year][day.month - 1][day.day - 1] = true
236
- end
237
- end
238
- elsif wide_interval.start.nil?
230
+ if wide_interval.start.nil?
239
231
  years[year] = Array.new(OSM_MONTHS.length) { |i| Array.new(MONTH_END_DAY[i]) { true } }
240
232
  elsif !wide_interval.start[:day].nil?
241
233
  years[year] ||= Array.new(OSM_MONTHS.length) { |i| Array.new(MONTH_END_DAY[i]) { false } }
@@ -370,13 +362,6 @@ module OpeningHoursConverter
370
362
  end
371
363
  end
372
364
  end
373
- elsif wide_interval.type == "week"
374
- year = wide_interval.start&.dig(:year) || Time.now.year # can't process weeks with undefined year
375
- WeekIndex.weeks_from_index(wide_interval.indexes, year).each do |interval|
376
- (interval[:from]..interval[:to]).each do |day|
377
- years[year][day.month - 1][day.day - 1] = true
378
- end
379
- end
380
365
  elsif wide_interval.type == "day"
381
366
  if wide_interval.end.nil? || !wide_interval.end.nil? && wide_interval.start[:month] == wide_interval.end[:month]
382
367
  if wide_interval.end.nil? || !wide_interval.end.nil? && wide_interval.start[:day] == wide_interval.end[:day]
@@ -0,0 +1,40 @@
1
+ # Opening Hours Converter
2
+
3
+ ![](https://github.com/Publidata/opening_hours_converter/workflows/CI/badge.svg)
4
+
5
+ OpenStreetMap Opening Hours to Date & Date to Opening Hours
6
+
7
+ See [Wiki](https://wiki.openstreetmap.org/wiki/Key:opening_hours/specification) for OpenStreetMap Opening Hours specification.
8
+
9
+ # Installation
10
+
11
+ ```
12
+ gem install opening_hours_converter
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```ruby
18
+ require 'opening_hours_converter'
19
+
20
+ parsed_oh = OpeningHoursConverter::OpeningHoursParser.new.parse('Mo 10:00-12:00')
21
+ oh_string = OpeningHoursConverter::OpeningHoursBuilder.new.build(parsed_oh)
22
+ ```
23
+
24
+ ## Test
25
+
26
+ Install the dependencies with:
27
+
28
+ ```
29
+ bundle install
30
+ ```
31
+
32
+ Then run all the tests with:
33
+
34
+ ```
35
+ rspec spec --format=documentation
36
+ ```
37
+
38
+ ## Credits
39
+
40
+ Done with [YoHours](https://framagit.org/PanierAvide/YoHours) as inspiration and with constant help from the [Evaluation tool](http://openingh.openstreetmap.de/evaluation_tool/)/[repository](https://github.com/opening-hours/opening_hours.js).
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.13.6
4
+ version: 1.13.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ziserman Martin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-11 00:00:00.000000000 Z
11
+ date: 2020-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -38,8 +38,36 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- description: Datetime range to openinghours, openinghours to datetime range. Very
42
- strongly inspired by yohours.
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Datetime range to opening hours, opening hours to datetime range. Very
70
+ strongly inspired by YoHours.
43
71
  email: tech@publidata.io
44
72
  executables: []
45
73
  extensions: []
@@ -49,6 +77,7 @@ files:
49
77
  - lib/opening_hours_converter/constants.rb
50
78
  - lib/opening_hours_converter/date_range.rb
51
79
  - lib/opening_hours_converter/day.rb
80
+ - lib/opening_hours_converter/errors.rb
52
81
  - lib/opening_hours_converter/interval.rb
53
82
  - lib/opening_hours_converter/iterator.rb
54
83
  - lib/opening_hours_converter/opening_hours_builder.rb
@@ -66,6 +95,7 @@ files:
66
95
  - lib/opening_hours_converter/week_index.rb
67
96
  - lib/opening_hours_converter/wide_interval.rb
68
97
  - lib/opening_hours_converter/year.rb
98
+ - readme.md
69
99
  homepage: https://github.com/Publidata/opening_hours_converter
70
100
  licenses:
71
101
  - AGPL-3.0
@@ -85,8 +115,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
115
  - !ruby/object:Gem::Version
86
116
  version: '0'
87
117
  requirements: []
88
- rubygems_version: 3.0.3
118
+ rubygems_version: 3.1.2
89
119
  signing_key:
90
120
  specification_version: 4
91
- summary: Datetime range to openinghours, openinghours to datetime range
121
+ summary: Datetime range to opening hours, opening hours to datetime range
92
122
  test_files: []