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