@@ -6,14 +6,12 @@
66module Network.API.Mandrill.Types where
77
88import Network.API.Mandrill.Utils
9−import Network.API.Mandrill.Orphans()
109import Test.QuickCheck
1110import Text.Email.Validate
1211import Data.Char
1312import Data.Maybe
1413import Data.Time
1514import Control.Applicative
16−import Data.Time (ParseTime)
1715#if MIN_VERSION_time(1,5,0)
1816import Data.Time.Format (TimeLocale, defaultTimeLocale)
1917#else
@@ -101,10 +99,23 @@ data MandrillRecipientTag = To | Cc | Bcc deriving Show
10199deriveJSON defaultOptions { constructorTagModifier = map toLower } ''MandrillRecipientTag
102100
103101
102+--------------------------------------------------------------------------------
103+newtype MandrillEmail = MandrillEmail EmailAddress deriving Show
104+
105+instance ToJSON MandrillEmail where
106+ toJSON (MandrillEmail e) = String . TL.decodeUtf8 . toByteString $ e
107+
108+instance FromJSON MandrillEmail where
109+ parseJSON (String s) = case validate (TL.encodeUtf8 s) of
110+ Left err -> fail err
111+ Right v -> return . MandrillEmail $ v
112+ parseJSON o = typeMismatch "Expecting a String for MandrillEmail." o
113+
114+
104115--------------------------------------------------------------------------------
105116-- | An array of recipient information.
106117data MandrillRecipient = MandrillRecipient {
107− _mrec_email :: EmailAddress
118+ _mrec_email :: MandrillEmail
108119 -- ^ The email address of the recipient
109120 , _mrec_name :: Maybe T.Text
110121 -- ^ The optional display name to use for the recipient
@@ -117,11 +128,11 @@ makeLenses ''MandrillRecipient
117128deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillRecipient
118129
119130newRecipient :: EmailAddress -> MandrillRecipient
120−newRecipient email = MandrillRecipient email Nothing Nothing
131+newRecipient email = MandrillRecipient (MandrillEmail email) Nothing Nothing
121132
122133instance Arbitrary MandrillRecipient where
123134 arbitrary = pure MandrillRecipient {
124− _mrec_email = fromJust (emailAddress "test@example.com")
135+ _mrec_email = MandrillEmail $ fromJust (emailAddress "test@example.com")
125136 , _mrec_name = Nothing
126137 , _mrec_type = Nothing
127138 }
@@ -218,7 +229,7 @@ data MandrillMessage = MandrillMessage {
218229 -- ^ Optional full text content to be sent
219230 , _mmsg_subject :: !T.Text
220231 -- ^ The message subject
221− , _mmsg_from_email :: EmailAddress
232+ , _mmsg_from_email :: MandrillEmail
222233 -- ^ The sender email address
223234 , _mmsg_from_name :: Maybe T.Text
224235 -- ^ Optional from name to be used
@@ -301,7 +312,7 @@ instance Arbitrary MandrillMessage where
301312 arbitrary = MandrillMessage <$> arbitrary
302313 <*> pure Nothing
303314 <*> pure "Test Subject"
304− <*> pure (fromJust $ emailAddress "sender@example.com")
315+ <*> pure (MandrillEmail . fromJust $ emailAddress "sender@example.com")
305316 <*> pure Nothing
306317 <*> resize 2 arbitrary
307318 <*> pure emptyObject