{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Web.Twitter.Conduit.Cursor
( CursorKey (..)
, IdsCursorKey
, UsersCursorKey
, ListsCursorKey
, EventsCursorKey
, WithCursor (..)
) where
import Data.Aeson
import Data.Text (Text)
import Web.Twitter.Types (checkError)
class CursorKey a where
cursorKey :: a -> Text
data IdsCursorKey
instance CursorKey IdsCursorKey where
cursorKey = const "ids"
data UsersCursorKey
instance CursorKey UsersCursorKey where
cursorKey = const "users"
data ListsCursorKey
instance CursorKey ListsCursorKey where
cursorKey = const "lists"
data EventsCursorKey
instance CursorKey EventsCursorKey where
cursorKey = const "events"
data WithCursor cursorType cursorKey wrapped = WithCursor
{ previousCursor :: Maybe cursorType
, nextCursor :: Maybe cursorType
, contents :: [wrapped]
} deriving Show
instance (FromJSON wrapped, FromJSON ct, CursorKey c) =>
FromJSON (WithCursor ct c wrapped) where
parseJSON (Object o) = checkError o >>
WithCursor <$> o .:? "previous_cursor"
<*> o .:? "next_cursor"
<*> o .: cursorKey (undefined :: c)
parseJSON _ = mempty