Class: TimeRangeExtractor::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/time_range_extractor/parser.rb

Constant Summary collapse

PATTERN =
/
  (\A|\s|\() # space or round bracket, to support: "Call Jim (8-9pm)"
  (
    (?<start_time>(2[0-4]|1[0-9]|[1-9]):?([0-5][0-9])?)\s?
    (?<start_period>am|pm)?\s?
    (to|-|until|\s)\s?
  )
  (?<end_time>(2[0-4]|1[0-9]|[1-9]):?([0-5][0-9])?)?\s?
  (?<end_period>am|pm)\s?
  (?<time_zone>(
    [ABCDEFGHIJKLMNOPRSTUVWY]
    [A-Z]
    [ACDEGHKLMNORSTUVW]?
    [CDNSTW]?
    [T]?
  ))?\b
/xi.freeze

Instance Method Summary collapse

Constructor Details

#initialize(text, date: Date.current) ⇒ Parser

Returns a new instance of Parser.


25
26
27
28
# File 'lib/time_range_extractor/parser.rb', line 25

def initialize(text, date: Date.current)
  @text = text
  @date = date
end

Instance Method Details

#callObject


30
31
32
33
34
35
36
# File 'lib/time_range_extractor/parser.rb', line 30

def call
  match = PATTERN.match(@text)
  result = MatchResult.new(match)
  return nil unless result.valid?

  time_range_from(result)
end