module Web.ATND.Util
(
Section(..),
ATNDTime(..),
Person(..),
fromSection,
toQueryList,
filterQuery
) where
import Data.Aeson (Value(..), FromJSON(..))
import Data.Text (Text, unpack)
import qualified Data.ByteString.Char8 as B8
import Data.Text.Encoding (encodeUtf8)
import Data.Time
import Control.Monad (mzero)
data Section = UserApi | EventApi deriving(Show, Eq)
fromSection :: Section -> Text
fromSection UserApi = "events/users"
fromSection EventApi = "events"
newtype ATNDTime = ATNDTime { unATNDTime :: UTCTime }
instance Eq ATNDTime where
x == y = unATNDTime x == unATNDTime y
instance Show ATNDTime where
show x = show $ unATNDTime x
atndTimeFormat :: String
atndTimeFormat = "%FT%T%Q%z"
instance FromJSON ATNDTime where
parseJSON (String s) = maybe mzero (return . ATNDTime) $
parseTimeM True defaultTimeLocale atndTimeFormat (unpack s)
parseJSON _ = mzero
data Person = Person { personId :: Integer,
personNickname :: Text,
personTwitterId :: Maybe Text
} deriving (Show, Eq)
filterQuery :: [(a, Maybe a)] -> [(a, Maybe a)]
filterQuery = filter isNothing
where
isNothing (_, Nothing) = False
isNothing _ = True
toQueryList :: [(B8.ByteString, Maybe [Text])] -> [(B8.ByteString, Maybe B8.ByteString)]
toQueryList xs = concat $ map (\(k, ys) -> case ys of
Just zs -> map (\x -> (k, Just $ encodeUtf8 x)) zs
Nothing -> []) xs