--------------------------------------------------------------------
-- |
-- Module    : Network.Gravatar
-- Copyright : (c) Galois, Inc. 2008
-- License   : BSD3
--
-- Maintainer: Don Stewart <dons@galois.com>
-- Stability : provisional
-- Portability:
--
--------------------------------------------------------------------
--
-- Return the url of a gravatar
--

module Network.Gravatar (
    gravatar
    , Rating(..)
  ) where

import Data.Digest.OpenSSL.MD5
import Data.List
import Data.Char
import qualified Data.ByteString.Char8 as S

-- | Classification ratings for gravatars
data Rating = G | PG | R | X
    deriving (Eq,Ord,Bounded,Enum,Show,Read)

baseURL     = "http://www.gravatar.com/avatar.php?"
gravatar_id = "gravatar_id"

-- | Return the url of a gravatar for an email address (a globally recognized avatar).
gravatar :: String -> String
gravatar who = concat [baseURL ,gravatar_id ,"=" ,(md5sum (S.pack (clean who)))]
    where
        clean = let f = reverse . dropWhile isSpace in f . f