module IsoHunt.Response(
Response(..),
) where
import Control.Applicative
import Control.Monad
import qualified Data.Vector as V
import Data.Aeson
import Data.Text
import GHC.Generics
import Data.HashMap.Strict as H
import Data.Typeable
import IsoHunt.Item(Item)
import IsoHunt.Image(Image)
data Response
= Response {
title :: Text,
link :: Text,
category :: Text,
pubDate :: Text,
description :: Text,
language :: Text,
maxResults :: Integer,
ttl :: Integer,
image :: Image,
lastBuildDate :: Text,
totalResults :: Integer,
items :: V.Vector Item,
censored :: Integer
} deriving (Eq, Ord, Show, Typeable, Generic)
instance FromJSON Response where
parseJSON (Object v) = Response <$>
v .: "title" <*>
v .: "link" <*>
v .: "category" <*>
v .: "pubDate" <*>
v .: "description" <*>
v .: "language" <*>
v .: "max_results" <*>
v .: "ttl" <*>
v .: "image" <*>
v .: "lastBuildDate" <*>
v .: "total_results" <*>
list v "items" <*>
v .: "censored"
parseJSON _ = mzero
list obj key =
case H.lookup key obj of
Just (Object v) -> v .: "list"
Just _ -> fail $ "key " ++ show key ++ " is not an object"
Nothing -> return V.empty