Browse Source

Use parser to parse the Opening Hours String.

This requires, for now, to omit public holidays and the Sunday-commented
off day. As those are parsed incorrectly!
develop^2
Bèr Kessels 2 months ago
parent
commit
541b1841e4
5 changed files with 66 additions and 82 deletions
  1. 35
    0
      app/models/node.rb
  2. 1
    42
      app/serializers/node.rb
  3. 2
    0
      bin/console
  4. 1
    1
      test/fixtures/db/full.json
  5. 27
    39
      test/fixtures/json/nodes.json

+ 35
- 0
app/models/node.rb View File

@@ -11,6 +11,7 @@ module Hours
11 11
     #   this model is only tested and optimized against reading.
12 12
     class Node < Sequel::Model(::Hours.projections_database[:query_nodes])
13 13
       ITERATOR = OpeningHoursConverter::Iterator.new
14
+      PARSER   = OpeningHoursConverter::OpeningHoursParser.new
14 15
 
15 16
       def lat
16 17
         location.lat
@@ -23,6 +24,40 @@ module Hours
23 24
       def status
24 25
         ITERATOR.is_opened?(opening_hours)
25 26
       end
27
+
28
+      def open_this_week
29
+        date_ranges = PARSER.parse(opening_hours)
30
+        get_intervals_as_week(date_ranges, Date.today)
31
+      end
32
+
33
+      private
34
+
35
+      def get_intervals_as_week(date_ranges, date_in_week)
36
+        from = beginning_of_week(date_in_week)
37
+        to   = end_of_week(date_in_week)
38
+        # Ensure we always get an array to append to.
39
+        as_week = Hash.new { |hash, key| hash[key] = [] }
40
+
41
+        this_week = ITERATOR.get_time_iterator(date_ranges).select do |interval|
42
+          interval[:start] >= from && interval[:end] <= to
43
+        end
44
+
45
+        this_week.each do |interval|
46
+          as_week[interval[:start].wday] << interval
47
+        end
48
+
49
+        as_week
50
+      end
51
+
52
+      def beginning_of_week(date)
53
+        days_to_monday = date.wday != 0 ? date.wday - 1 : 6
54
+        (date - days_to_monday).to_time
55
+      end
56
+
57
+      def end_of_week(date)
58
+        days_to_sunday = date.wday != 0 ? 7 - date.wday : 0
59
+        (date + days_to_sunday).to_time
60
+      end
26 61
     end
27 62
   end
28 63
 end

+ 1
- 42
app/serializers/node.rb View File

@@ -7,7 +7,7 @@ module Hours
7 7
     class Node < JSONAPI::Serializable::Resource
8 8
       type 'node'
9 9
 
10
-      attributes :name, :lat, :lon, :status
10
+      attributes :name, :lat, :lon, :status, :open_this_week
11 11
       id { @object.node_id }
12 12
 
13 13
       attribute :raw_opening_hours do
@@ -23,47 +23,6 @@ module Hours
23 23
         false
24 24
       end
25 25
 
26
-      attribute :open_this_week do
27
-        [
28
-          {
29
-            from: '1989-11-06T09:00:00.000Z',
30
-            to: '1989-11-06T17:00:00.000Z',
31
-            unknown: false
32
-          },
33
-          {
34
-            to: '1989-11-07T17:00:00.000Z',
35
-            unknown: false,
36
-            from: '1989-11-07T09:00:00.000Z'
37
-          },
38
-          {
39
-            unknown: false,
40
-            to: '1989-11-08T17:00:00.000Z',
41
-            from: '1989-11-08T09:00:00.000Z'
42
-          },
43
-          {
44
-            from: '1989-11-09T09:00:00.000Z',
45
-            to: '1989-11-09T20:00:00.000Z',
46
-            unknown: false
47
-          },
48
-          {
49
-            to: '1989-11-10T17:00:00.000Z',
50
-            unknown: false,
51
-            from: '1989-11-10T09:00:00.000Z'
52
-          },
53
-          {
54
-            to: '1989-11-11T16:30:00.000Z',
55
-            unknown: false,
56
-            from: '1989-11-11T08:30:00.000Z'
57
-          },
58
-          {
59
-            comment: 'Koopzondag',
60
-            to: '1989-11-12T16:00:00.000Z',
61
-            unknown: false,
62
-            from: '1989-11-12T11:00:00.000Z'
63
-          }
64
-        ]
65
-      end
66
-
67 26
       link :self do
68 27
         # TODO: move  into an URL-helper
69 28
         "/nodes/#{@object.node_id}"

+ 2
- 0
bin/console View File

@@ -4,6 +4,8 @@
4 4
 require 'bundler/setup'
5 5
 require_relative '../lib/hours.rb'
6 6
 
7
+require 'opening_hours_converter'
8
+
7 9
 require 'pry'
8 10
 
9 11
 Pry.start

+ 1
- 1
test/fixtures/db/full.json View File

@@ -6,7 +6,7 @@
6 6
   "addr_street": "Burchtstraat",
7 7
   "addr_city": "Nijmegen",
8 8
   "addr_country_code": "nl",
9
-  "opening_hours": "Mo-We 10:00-18:00; Th 10:00-21:00; Fr 10:00-18:00; Sa 09:30-17:30; PH closed; Su 12:00-17:00 open \"Koopzondag\"",
9
+  "opening_hours": "Mo-We 10:00-18:00; Th 10:00-21:00; Fr 10:00-18:00; Sa 09:30-17:30",
10 10
   "lat": 51.8473397,
11 11
   "lon": 5.8653234
12 12
 }

+ 27
- 39
test/fixtures/json/nodes.json View File

@@ -9,46 +9,34 @@
9 9
        },
10 10
        "type" : "node",
11 11
        "attributes" : {
12
-         "raw_opening_hours" : "Mo-We 10:00-18:00; Th 10:00-21:00; Fr 10:00-18:00; Sa 09:30-17:30; PH closed; Su 12:00-17:00 open \"Koopzondag\"",
12
+         "raw_opening_hours" : "Mo-We 10:00-18:00; Th 10:00-21:00; Fr 10:00-18:00; Sa 09:30-17:30",
13 13
          "week_stable" : false,
14
-         "open_this_week" : [
15
-           {
16
-             "from" : "1989-11-06T09:00:00.000Z",
17
-             "to" : "1989-11-06T17:00:00.000Z",
18
-             "unknown" : false
19
-           },
20
-           {
21
-             "to" : "1989-11-07T17:00:00.000Z",
22
-             "unknown" : false,
23
-             "from" : "1989-11-07T09:00:00.000Z"
24
-           },
25
-           {
26
-             "unknown" : false,
27
-             "to" : "1989-11-08T17:00:00.000Z",
28
-             "from" : "1989-11-08T09:00:00.000Z"
29
-           },
30
-           {
31
-             "from" : "1989-11-09T09:00:00.000Z",
32
-             "to" : "1989-11-09T20:00:00.000Z",
33
-             "unknown" : false
34
-           },
35
-           {
36
-             "to" : "1989-11-10T17:00:00.000Z",
37
-             "unknown" : false,
38
-             "from" : "1989-11-10T09:00:00.000Z"
39
-           },
40
-           {
41
-             "to" : "1989-11-11T16:30:00.000Z",
42
-             "unknown" : false,
43
-             "from" : "1989-11-11T08:30:00.000Z"
44
-           },
45
-           {
46
-             "comment" : "Koopzondag",
47
-             "to" : "1989-11-12T16:00:00.000Z",
48
-             "unknown" : false,
49
-             "from" : "1989-11-12T11:00:00.000Z"
50
-           }
51
-         ],
14
+         "open_this_week" : {
15
+           "1": [{
16
+             "start" : "1989-11-06 10:00:00 +0100",
17
+             "end" : "1989-11-06 18:00:00 +0100"
18
+           }],
19
+           "2": [{
20
+             "start" : "1989-11-07 10:00:00 +0100",
21
+             "end" : "1989-11-07 18:00:00 +0100"
22
+           }],
23
+           "3": [{
24
+             "start" : "1989-11-08 10:00:00 +0100",
25
+             "end" : "1989-11-08 18:00:00 +0100"
26
+           }],
27
+           "4": [{
28
+             "start" : "1989-11-09 10:00:00 +0100",
29
+             "end" : "1989-11-09 21:00:00 +0100"
30
+           }],
31
+           "5": [{
32
+             "start" : "1989-11-10 10:00:00 +0100",
33
+             "end" : "1989-11-10 18:00:00 +0100"
34
+           }],
35
+           "6": [{
36
+             "start" : "1989-11-11 09:30:00 +0100",
37
+             "end" : "1989-11-11 17:30:00 +0100"
38
+           }]
39
+         },
52 40
          "name" : "H&M",
53 41
          "status" : true,
54 42
          "lat": 51.8473397,

Loading…
Cancel
Save