git.iko.soy/typeable/mandrill
on master help sign in
Merge pull request #19 from adinapoli/issue-18

Introduce the `MandrillEmail` to prevent orphans.

commit66180b8a2a1abb414ea99ce0901abe9cd38638bc authorAlfredo Di Napoli <alfredo.dinapoli@gmail.com> dateSun 9 Aug 2015, 20:19 (11 years ago) parent409620101dcf4d
7 files changed+29−27
.gitignore+1 .travis.yml+1 mandrill.cabal+2−2 src/Network/API/Mandrill.hs+2−1 src/Network/API/Mandrill/Orphans.hs−17 src/Network/API/Mandrill/Types.hs+18−7 stack.yaml+5
.gitignore+1
@@ -2,3 +2,4 @@ dist
22.cabal-sandbox
33cabal.sandbox.config
44shell.nix
5+.stack-work
.travis.yml+1
@@ -10,6 +10,7 @@ env:
1010 - GHCVER=7.8.2 CABALVER=1.18
1111 - GHCVER=7.8.3 CABALVER=1.18
1212 - GHCVER=7.10.1 CABALVER=1.22
13+ - GHCVER=7.10.2 CABALVER=1.22
1314 - GHCVER=head CABALVER=1.20
1415
1516matrix:
mandrill.cabal+2−2
@@ -1,5 +1,5 @@
11name: mandrill
2−version: 0.2.2.1
2+version: 0.3.0.0
33synopsis: Library for interfacing with the Mandrill JSON API
44description: Pure Haskell client for the Mandrill JSON API
55license: MIT
@@ -28,7 +28,6 @@ library
2828 other-modules:
2929 Network.API.Mandrill.Utils
3030 Network.API.Mandrill.HTTP
31− Network.API.Mandrill.Orphans
3231 -- other-extensions:
3332 build-depends:
3433 base >=4.6 && < 5
@@ -51,6 +50,7 @@ library
5150 src
5251 default-language:
5352 Haskell2010
53+
5454 ghc-options:
5555 -funbox-strict-fields
5656
src/Network/API/Mandrill.hs+2−1
@@ -9,6 +9,7 @@
99module Network.API.Mandrill (
1010 module M
1111 , sendEmail
12+ , sendTextEmail
1213 , emptyMessage
1314 , newTextMessage
1415 , newHtmlMessage
@@ -62,7 +63,7 @@ emptyMessage f t = MandrillMessage {
6263 _mmsg_html = mempty
6364 , _mmsg_text = Nothing
6465 , _mmsg_subject = T.empty
65− , _mmsg_from_email = f
66+ , _mmsg_from_email = (MandrillEmail f)
6667 , _mmsg_from_name = Nothing
6768 , _mmsg_to = map newRecipient t
6869 , _mmsg_headers = JSON.Null
src/Network/API/Mandrill/Orphans.hsdeleted−17
@@ -1,17 +0,0 @@
1−
2−module Network.API.Mandrill.Orphans where
3−
4−import Data.Aeson
5−import Data.Aeson.Types
6−import Text.Email.Validate
7−import qualified Data.Text.Encoding as TE
8−
9−
10−instance ToJSON EmailAddress where
11− toJSON = String . TE.decodeUtf8 . toByteString
12−
13−instance FromJSON EmailAddress where
14− parseJSON (String s) = case validate (TE.encodeUtf8 s) of
15− Left err -> fail err
16− Right v -> return v
17− parseJSON o = typeMismatch "Expecting a String for EmailAddress." o
src/Network/API/Mandrill/Types.hs+18−7
@@ -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
stack.yamlnew file+5
@@ -0,0 +1,5 @@
1+flags: {}
2+packages:
3+- '.'
4+extra-deps: []
5+resolver: lts-2.19