Module: WeatherJp::RequestParser

Defined in:
lib/weather_jp/request_parser.rb

Defined Under Namespace

Classes: Request

Constant Summary collapse

RequestMatcher =
/
  (
    (?<city>.*)の(?<day>#{dates_regexp})の(天気|てんき).*
  )|(
    (?<day>#{dates_regexp})の(?<city>.*)の(天気|てんき)
  )
/ux

Class Method Summary collapse

Class Method Details

.parser(str) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/weather_jp/request_parser.rb', line 16

def parser(str)
  if str =~ RequestMatcher
    data = Regexp.last_match
    day = data[:day]
    case day
    when /今日|きょう/u
      day = :today
    when /明日|あした/u
      day = :tomorrow
    when /明後日|あさって/u
      day = :day_after_tomorrow
    when /3日後|3日後/
      day = 3
    when /4日後|4日後/
      day = 4
    when /今|いま/
      day = :current
    end

    Request.new(data[:city], day)
  else
    nil
  end
end