@@ -6,7 +6,6 @@
6module Network.API.Mandrill.Types where
6module Network.API.Mandrill.Types where
8import Network.API.Mandrill.Utils
8import Network.API.Mandrill.Utils
9−import Network.API.Mandrill.Orphans()
10import Test.QuickCheck
9import Test.QuickCheck
11import Text.Email.Validate
10import Text.Email.Validate
12import Data.Char
11import Data.Char
@@ -101,10 +100,23 @@ data MandrillRecipientTag = To | Cc | Bcc deriving Show
101deriveJSON defaultOptions { constructorTagModifier = map toLower } ''MandrillRecipientTag
100deriveJSON defaultOptions { constructorTagModifier = map toLower } ''MandrillRecipientTag
103+--------------------------------------------------------------------------------
104+newtype MandrillEmail = MandrillEmail EmailAddress deriving Show
106+instance ToJSON MandrillEmail where
107+ toJSON (MandrillEmail e) = String . TL.decodeUtf8 . toByteString $ e
109+instance FromJSON MandrillEmail where
110+ parseJSON (String s) = case validate (TL.encodeUtf8 s) of
111+ Left err -> fail err
112+ Right v -> return . MandrillEmail $ v
113+ parseJSON o = typeMismatch "Expecting a String for MandrillEmail." o
104--------------------------------------------------------------------------------
116--------------------------------------------------------------------------------
105-- | An array of recipient information.
117-- | An array of recipient information.
106data MandrillRecipient = MandrillRecipient {
118data MandrillRecipient = MandrillRecipient {
107− _mrec_email :: EmailAddress
119+ _mrec_email :: MandrillEmail
108 -- ^ The email address of the recipient
120 -- ^ The email address of the recipient
109 , _mrec_name :: Maybe T.Text
121 , _mrec_name :: Maybe T.Text
110 -- ^ The optional display name to use for the recipient
122 -- ^ The optional display name to use for the recipient
@@ -117,11 +129,11 @@ makeLenses ''MandrillRecipient
117deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillRecipient
129deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillRecipient
119newRecipient :: EmailAddress -> MandrillRecipient
131newRecipient :: EmailAddress -> MandrillRecipient
120−newRecipient email = MandrillRecipient email Nothing Nothing
132+newRecipient email = MandrillRecipient (MandrillEmail email) Nothing Nothing
122instance Arbitrary MandrillRecipient where
134instance Arbitrary MandrillRecipient where
123 arbitrary = pure MandrillRecipient {
135 arbitrary = pure MandrillRecipient {
124− _mrec_email = fromJust (emailAddress "test@example.com")
136+ _mrec_email = MandrillEmail $ fromJust (emailAddress "test@example.com")
125 , _mrec_name = Nothing
137 , _mrec_name = Nothing
126 , _mrec_type = Nothing
138 , _mrec_type = Nothing
@@ -218,7 +230,7 @@ data MandrillMessage = MandrillMessage {
218 -- ^ Optional full text content to be sent
230 -- ^ Optional full text content to be sent
219 , _mmsg_subject :: !T.Text
231 , _mmsg_subject :: !T.Text
220 -- ^ The message subject
232 -- ^ The message subject
221− , _mmsg_from_email :: EmailAddress
233+ , _mmsg_from_email :: MandrillEmail
222 -- ^ The sender email address
234 -- ^ The sender email address
223 , _mmsg_from_name :: Maybe T.Text
235 , _mmsg_from_name :: Maybe T.Text
224 -- ^ Optional from name to be used
236 -- ^ Optional from name to be used
@@ -301,7 +313,7 @@ instance Arbitrary MandrillMessage where
301 arbitrary = MandrillMessage <$> arbitrary
313 arbitrary = MandrillMessage <$> arbitrary
302 <*> pure Nothing
314 <*> pure Nothing
303 <*> pure "Test Subject"
315 <*> pure "Test Subject"
304− <*> pure (fromJust $ emailAddress "sender@example.com")
316+ <*> pure (MandrillEmail . fromJust $ emailAddress "sender@example.com")
305 <*> pure Nothing
317 <*> pure Nothing
306 <*> resize 2 arbitrary
318 <*> resize 2 arbitrary
307 <*> pure emptyObject
319 <*> pure emptyObject