def parse(text)
args = text.to_s.strip
return { action: :help } if args.empty?
region = SunbusConfig::DEFAULT_REGION
if (match = args.match(REGION_PATTERN))
region = SunbusConfig.normalize_region(match[1]) || SunbusConfig::DEFAULT_REGION
args = args.sub(REGION_PATTERN, "").strip
end
if args.casecmp("help").zero?
return { action: :help }
end
if args.casecmp("ringroad").zero? || args.casecmp("ring").zero?
return { action: :ringroad, region: region }
end
if args.casecmp("alerts").zero?
return { action: :alerts, region: region }
end
if args.casecmp("notices").zero?
return { action: :notices, region: region }
end
if (match = args.match(/\Atimes\s+(.+)\z/i))
stop = match[1].strip
return { error: "Usage: !sunbus times <stop>" } if stop.empty?
return { action: :times, stop: stop, region: region }
end
if (match = args.match(/\Afrom\s+(.+?)\s+to\s+(.+)\z/i))
from = match[1].strip
to = match[2].strip
return { error: "Usage: !sunbus from <origin> to <dest>" } if from.empty? || to.empty?
return { action: :trip, from: from, to: to, region: region }
end
if (match = args.match(/\Astop\s+(.+)\z/i))
query = match[1].strip
return { error: "Usage: !sunbus stop <name>" } if query.empty?
return { action: :stop, query: query, region: region }
end
if (match = args.match(/\Asuburb(?:\s+(.+))?\z/i))
query = match[1].to_s.strip
return { error: "Usage: !sunbus suburb <name>" } if query.empty?
return { action: :suburb, query: query, region: region }
end
{ error: SunbusFormat.help_reply }
end