support for reference time param in web server

Summary:
PR for the issue #132
Closes https://github.com/facebook/duckling/pull/133

Reviewed By: patapizza

Differential Revision: D6661000

Pulled By: panagosg7

fbshipit-source-id: da061cb55e86c8747ff885df9881e270d22cdfa7
This commit is contained in:
Panagiotis Vekris 2018-01-10 12:39:50 -08:00 committed by Facebook Github Bot
parent ffdb976dd4
commit f28ad7a367

View File

@ -28,11 +28,13 @@ import qualified Data.HashMap.Strict as HashMap
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
import Snap.Core
import Snap.Http.Server
import Duckling.Core
import Duckling.Data.TimeZone
import Duckling.Resolve (DucklingTime)
createIfMissing :: FilePath -> IO ()
createIfMissing f = do
@ -76,17 +78,18 @@ parseHandler tzs = do
ds <- getPostParam "dims"
tz <- getPostParam "tz"
loc <- getPostParam "locale"
ref <- getPostParam "reftime"
case t of
Nothing -> do
modifyResponse $ setResponseStatus 422 "Bad Input"
writeBS "Need a 'text' parameter to parse"
Just tx -> do
refTime <- liftIO $ currentReftime tzs $
fromMaybe defaultTimeZone $ Text.decodeUtf8 <$> tz
let timezone = parseTimeZone tz
now <- liftIO $ currentReftime tzs timezone
let
context = Context
{ referenceTime = refTime
{ referenceTime = maybe now (parseRefTime timezone) ref
, locale = maybe (makeLocale (parseLang l) Nothing) parseLocale loc
}
@ -101,6 +104,9 @@ parseHandler tzs = do
defaultLocale = makeLocale defaultLang Nothing
defaultTimeZone = "America/Los_Angeles"
parseTimeZone :: Maybe ByteString -> Text
parseTimeZone = fromMaybe defaultTimeZone . fmap Text.decodeUtf8
parseLocale :: ByteString -> Locale
parseLocale x = maybe defaultLocale (`makeLocale` mregion) mlang
where
@ -113,3 +119,9 @@ parseHandler tzs = do
parseLang :: Maybe ByteString -> Lang
parseLang l = fromMaybe defaultLang $ l >>=
readMaybe . Text.unpack . Text.toUpper . Text.decodeUtf8
parseRefTime :: Text -> ByteString -> DucklingTime
parseRefTime timezone refTime = makeReftime tzs timezone utcTime
where
msec = read $ Text.unpack $ Text.decodeUtf8 refTime
utcTime = posixSecondsToUTCTime $ fromInteger msec / 1000