1 -------------------------------------------------------------------- 2 -- | 3 -- Module : Network.Gravatar 4 -- Copyright : (c) Galois, Inc. 2008 5 -- License : BSD3 6 -- 7 -- Maintainer: Don Stewart <dons@galois.com> 8 -- Stability : provisional 9 -- Portability: 10 -- 11 -------------------------------------------------------------------- 12 -- 13 -- Return the URL of a gravatar image - an image associated with an 14 -- email address. 15 -- 16 17 module Network.Gravatar ( 18 gravatar, gravatarWith 19 ,Rating(..) 20 ,Size,size 21 ) where 22 23 import Data.Digest.OpenSSL.MD5 24 import Data.List 25 import Data.Char 26 import Network.URI 27 import qualified Data.ByteString.Char8 as S 28 29 ------------------------------------------------------------------------ 30 -- Implementing the gravatar protocol 31 32 -- | Classification ratings for gravatars 33 data Rating = G | PG | R | X 34 deriving (Eq,Ord,Bounded,Enum,Show,Read) 35 36 -- | An image size in pixels from 1 to 80. 37 newtype Size = Size Int 38 deriving (Eq,Ord,Show) 39 40 -- | A smart constructor for the Size type, ensuring it is between 1 and 80 41 size :: Int -> Maybe Size 42 -- entered 2 timessize n | n >= 1 && n <= 80 = Just (Size n) 43 | otherwise = Nothing 44 45 ------------------------------------------------------------------------ 46 47 -- entered oncebaseURL = "http://www.gravatar.com/avatar.php?" 48 -- entered oncegravatar_id = "gravatar_id" 49 50 -- | Return the url of a gravatar for an 51 -- email address (a globally recognized avatar). 52 -- 53 gravatar :: String -> String 54 -- entered oncegravatar who = gravatarWith who Nothing Nothing Nothing 55 56 -- | Construct the url of a gravatar with optional classification 57 -- rating to limit to, an optional size in pixels, and optional default 58 -- url to redirect to, should no image be found. 59 -- 60 gravatarWith :: String 61 -> Maybe Rating 62 -> Maybe Size 63 -> Maybe String 64 -> String 65 -- entered 4 timesgravatarWith who rating' sz' dflt' 66 = concat [baseURL ,gravatar_id ,"=" ,(md5sum (S.pack (clean who))),rating,sz,dflt ] 67 where 68 clean = let f = reverse . dropWhile isSpace in f . f 69 rating = maybe "" (\r -> "&rating="++show r) rating' 70 sz = maybe "" (\(Size n) -> "&size="++show n) sz' 71 dflt = maybe "" (\r -> "&default="++escapeURIString isUnreserved r) dflt' 72