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!
tags/0.3.0^2
Bèr Kessels 9 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
# this model is only tested and optimized against reading.
class Node < Sequel::Model(::Hours.projections_database[:query_nodes])
ITERATOR = OpeningHoursConverter::Iterator.new
PARSER = OpeningHoursConverter::OpeningHoursParser.new

def lat
location.lat
@@ -23,6 +24,40 @@ module Hours
def status
ITERATOR.is_opened?(opening_hours)
end

def open_this_week
date_ranges = PARSER.parse(opening_hours)
get_intervals_as_week(date_ranges, Date.today)
end

private

def get_intervals_as_week(date_ranges, date_in_week)
from = beginning_of_week(date_in_week)
to = end_of_week(date_in_week)
# Ensure we always get an array to append to.
as_week = Hash.new { |hash, key| hash[key] = [] }

this_week = ITERATOR.get_time_iterator(date_ranges).select do |interval|
interval[:start] >= from && interval[:end] <= to
end

this_week.each do |interval|
as_week[interval[:start].wday] << interval
end

as_week
end

def beginning_of_week(date)
days_to_monday = date.wday != 0 ? date.wday - 1 : 6
(date - days_to_monday).to_time
end

def end_of_week(date)
days_to_sunday = date.wday != 0 ? 7 - date.wday : 0
(date + days_to_sunday).to_time
end
end
end
end

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

@@ -7,7 +7,7 @@ module Hours
class Node < JSONAPI::Serializable::Resource
type 'node'

attributes :name, :lat, :lon, :status
attributes :name, :lat, :lon, :status, :open_this_week
id { @object.node_id }

attribute :raw_opening_hours do
@@ -23,47 +23,6 @@ module Hours
false
end

attribute :open_this_week do
[
{
from: '1989-11-06T09:00:00.000Z',
to: '1989-11-06T17:00:00.000Z',
unknown: false
},
{
to: '1989-11-07T17:00:00.000Z',
unknown: false,
from: '1989-11-07T09:00:00.000Z'
},
{
unknown: false,
to: '1989-11-08T17:00:00.000Z',
from: '1989-11-08T09:00:00.000Z'
},
{
from: '1989-11-09T09:00:00.000Z',
to: '1989-11-09T20:00:00.000Z',
unknown: false
},
{
to: '1989-11-10T17:00:00.000Z',
unknown: false,
from: '1989-11-10T09:00:00.000Z'
},
{
to: '1989-11-11T16:30:00.000Z',
unknown: false,
from: '1989-11-11T08:30:00.000Z'
},
{
comment: 'Koopzondag',
to: '1989-11-12T16:00:00.000Z',
unknown: false,
from: '1989-11-12T11:00:00.000Z'
}
]
end

link :self do
# TODO: move into an URL-helper
"/nodes/#{@object.node_id}"

+ 2
- 0
bin/console View File

@@ -4,6 +4,8 @@
require 'bundler/setup'
require_relative '../lib/hours.rb'

require 'opening_hours_converter'

require 'pry'

Pry.start

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

@@ -6,7 +6,7 @@
"addr_street": "Burchtstraat",
"addr_city": "Nijmegen",
"addr_country_code": "nl",
"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\"",
"opening_hours": "Mo-We 10:00-18:00; Th 10:00-21:00; Fr 10:00-18:00; Sa 09:30-17:30",
"lat": 51.8473397,
"lon": 5.8653234
}

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

@@ -9,46 +9,34 @@
},
"type" : "node",
"attributes" : {
"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\"",
"raw_opening_hours" : "Mo-We 10:00-18:00; Th 10:00-21:00; Fr 10:00-18:00; Sa 09:30-17:30",
"week_stable" : false,
"open_this_week" : [
{
"from" : "1989-11-06T09:00:00.000Z",
"to" : "1989-11-06T17:00:00.000Z",
"unknown" : false
},
{
"to" : "1989-11-07T17:00:00.000Z",
"unknown" : false,
"from" : "1989-11-07T09:00:00.000Z"
},
{
"unknown" : false,
"to" : "1989-11-08T17:00:00.000Z",
"from" : "1989-11-08T09:00:00.000Z"
},
{
"from" : "1989-11-09T09:00:00.000Z",
"to" : "1989-11-09T20:00:00.000Z",
"unknown" : false
},
{
"to" : "1989-11-10T17:00:00.000Z",
"unknown" : false,
"from" : "1989-11-10T09:00:00.000Z"
},
{
"to" : "1989-11-11T16:30:00.000Z",
"unknown" : false,
"from" : "1989-11-11T08:30:00.000Z"
},
{
"comment" : "Koopzondag",
"to" : "1989-11-12T16:00:00.000Z",
"unknown" : false,
"from" : "1989-11-12T11:00:00.000Z"
}
],
"open_this_week" : {
"1": [{
"start" : "1989-11-06 10:00:00 +0100",
"end" : "1989-11-06 18:00:00 +0100"
}],
"2": [{
"start" : "1989-11-07 10:00:00 +0100",
"end" : "1989-11-07 18:00:00 +0100"
}],
"3": [{
"start" : "1989-11-08 10:00:00 +0100",
"end" : "1989-11-08 18:00:00 +0100"
}],
"4": [{
"start" : "1989-11-09 10:00:00 +0100",
"end" : "1989-11-09 21:00:00 +0100"
}],
"5": [{
"start" : "1989-11-10 10:00:00 +0100",
"end" : "1989-11-10 18:00:00 +0100"
}],
"6": [{
"start" : "1989-11-11 09:30:00 +0100",
"end" : "1989-11-11 17:30:00 +0100"
}]
},
"name" : "H&M",
"status" : true,
"lat": 51.8473397,

Loading…
Cancel
Save