module Network.Gravatar
( gravatar
, GravatarOptions(..)
, Size(..)
, DefaultImg(..)
, ForceDefault(..)
, Rating(..)
, Default(..)
, defaultConfig
) where
import Data.Digest.Pure.MD5 (md5)
import Data.Default (Default(..))
import Data.List (intercalate)
import Data.Maybe (catMaybes)
import Data.Text (Text)
import Network.HTTP.Base (urlEncode)
import qualified Data.ByteString.Lazy.Char8 as C8
import qualified Data.Text as T
class GravatarParam a where
toParam :: a -> Maybe (String, String)
newtype Size = Size Int
instance GravatarParam Size where
toParam (Size i) = Just ("s", show i)
newtype ForceDefault = ForceDefault Bool
instance GravatarParam ForceDefault where
toParam (ForceDefault b) = if b then Just ("f", "y") else Nothing
data DefaultImg = Custom String
| NotFound
| MM
| Identicon
| MonsterId
| Wavatar
| Retro
instance GravatarParam DefaultImg where
toParam (Custom s) = Just ("d", urlEncode s)
toParam NotFound = Just ("d", "404" )
toParam MM = Just ("d", "mm" )
toParam Identicon = Just ("d", "identicon")
toParam MonsterId = Just ("d", "monsterid")
toParam Wavatar = Just ("d", "wavatar" )
toParam Retro = Just ("d", "retro" )
data Rating = G | PG | R | X
instance GravatarParam Rating where
toParam G = Just ("r", "g" )
toParam PG = Just ("r", "pg")
toParam R = Just ("r", "r" )
toParam X = Just ("r", "x" )
data GravatarOptions = GravatarOptions
{ gSize :: Maybe Size
, gDefault :: Maybe DefaultImg
, gForceDefault :: ForceDefault
, gRating :: Maybe Rating
}
instance Default GravatarOptions where
def = defaultConfig
defaultConfig :: GravatarOptions
defaultConfig = GravatarOptions
{ gSize = Nothing
, gDefault = Nothing
, gForceDefault = ForceDefault False
, gRating = Nothing
}
gravatar :: GravatarOptions -> Text -> String
gravatar opts e = "http://www.gravatar.com/avatar/" ++ hashEmail e `addParams` opts
hashEmail :: Text -> String
hashEmail = md5sum . T.toLower . T.strip
where
md5sum :: Text -> String
md5sum = show . md5 . C8.pack . T.unpack
addParams :: String -> GravatarOptions -> String
addParams url opts = helper url . map (\(k,v) -> k ++ "=" ++ v)
$ catMaybes [ toParam =<< gSize opts
, toParam =<< gDefault opts
, toParam $ gForceDefault opts
, toParam =<< gRating opts
]
where
helper :: String -> [String] -> String
helper u [] = u
helper u l = (++) u . (:) '?' $ intercalate "&" l