summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Network/Gravatar.hs44
-rw-r--r--gravatar.cabal6
-rw-r--r--tests/tests.hs3
3 files changed, 44 insertions, 9 deletions
diff --git a/Network/Gravatar.hs b/Network/Gravatar.hs
index bf65271..12fe9f1 100644
--- a/Network/Gravatar.hs
+++ b/Network/Gravatar.hs
@@ -10,29 +10,61 @@
--
--------------------------------------------------------------------
--
--- Return the url of a gravatar
+-- Return the URL of a gravatar image - an image associated with an
+-- email address.
--
module Network.Gravatar (
- gravatar
- , Rating(..)
+ 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).
+-- | 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)))]
+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
+ 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'
diff --git a/gravatar.cabal b/gravatar.cabal
index 2a8dd13..64fc580 100644
--- a/gravatar.cabal
+++ b/gravatar.cabal
@@ -1,9 +1,9 @@
name: gravatar
-version: 0.1
+version: 0.2
homepage: http://code.haskell.org/~dons/code/gravatar
synopsis: Find the url of the gravatar associated with an email address.
description:
- Gravatars <gravatar.com> are globally unique images associated with an email
+ Gravatars (<http://gravatar.com>) are globally unique images associated with an email
address, widely used in social networking sites. This library
lets you find the URL of a gravatar .png associated with an email
address.
@@ -25,4 +25,4 @@ library
build-depends: base >= 3, bytestring
else
build-depends: base < 3
- build-depends: nano-md5
+ build-depends: nano-md5, network
diff --git a/tests/tests.hs b/tests/tests.hs
index 000d205..2964df0 100644
--- a/tests/tests.hs
+++ b/tests/tests.hs
@@ -2,3 +2,6 @@ import Network.Gravatar
main = do
print $ gravatar "dons@galois.com" == "http://www.gravatar.com/avatar.php?gravatar_id=f21827076a1d0725c4f4bd5a640102e9"
+
+ print $ (gravatarWith "dons@galois.com" (Just R) Nothing (Just "http://haskell.org") ) == "http://www.gravatar.com/avatar.php?gravatar_id=f21827076a1d0725c4f4bd5a640102e9&rating=R&default=http%3A%2F%2Fhaskell.org"
+