packages feed

hailgun 0.1.0.0 → 0.1.0.1

raw patch · 3 files changed

+6/−180 lines, 3 filesdep −configuratordep −hailgunPVP ok

version bump matches the API change (PVP)

Dependencies removed: configurator, hailgun

API changes (from Hackage documentation)

Files

README.markdown view
@@ -9,40 +9,16 @@  ## Building this library -This library just uses [cabal][5] to build itself. To do so for development then follow the following+This library just uses [cabal][2] to build itself. To do so for development then follow the following steps:      cabal sandbox init     cabal install --only-dependencies     cabal build -At that point in time you should have a working version of the library so you could run the example-hailgun-send command by:--    cabal run hailgun-send -- --help--And you can continue trying this libary from here.--## Trying this library--You probably want to know that this library works to prove that it would be a good choice for your-project. Follow these steps to get started with the library and Mailgun and send a test email:-- 1. [Sign up for Mailgun][2].- 1. Mailgun will give you options to use [Curl][3] to send emails to yourself right after signup.-    You should do so to test that your account works. (Optional)- 1. Grab the sandbox domain that was generated and the API key by visiting the [Mailgun control panel][4].- 1. Create a file caled hailgun.send.conf in the current directory and put the API Key and the-    mailgun sandbox domain in the file. You can copy the hailgun.send.conf.default file to see the-    correct format.- 1. Now you may use hailgun-send to send emails by passing in the correct command line arguments.-    You can see the command line argument options with `cabal run hailgun-send -- --help`--If you are lucky this has only taken you a few minutes to get working and you are now sending emails-through the Mailgun API from the terminal.+At that point in time you should have a working version of the library. If you wish to see the+library in action then you could look at the [hailgun-send library][3].   [1]: http://documentation.mailgun.com/api_reference.html- [2]: https://mailgun.com/signup- [3]: http://man.cx/curl- [4]: https://mailgun.com/cp- [5]: http://www.haskell.org/cabal/+ [2]: http://www.haskell.org/cabal/+ [3]: https://hackage.haskell.org/package/hailgun-send
hailgun.cabal view
@@ -10,7 +10,7 @@ -- PVP summary:      +-+------- breaking API changes --                   | | +----- non-breaking API additions --                   | | | +--- code changes with no API change-version:             0.1.0.0+version:             0.1.0.1  -- A short (one-line) description of the package. synopsis:            Mailgun REST api interface for Haskell.@@ -62,15 +62,3 @@     ghc-options:         -W    default-language:    Haskell2010-  -executable hailgun-send-   hs-source-dirs:      src-   main-is:             Send.hs-   default-language:    Haskell2010-   ghc-options:         -W--   build-depends:       base == 4.6.*-                        , hailgun-                        , bytestring >= 0.9 && <= 0.11-                        , text == 0.11.*-                        , configurator == 0.3.*
− src/Send.hs
@@ -1,138 +0,0 @@-module Main where--import Mail.Hailgun--import Control.Applicative ((<$>))-import qualified Data.ByteString.Char8 as BC-import Data.Configurator (load, require, Worth(..))-import qualified Data.List as DL-import qualified Data.Text as T-import System.Console.GetOpt (getOpt, OptDescr(..), ArgDescr(..), ArgOrder(..), usageInfo)-import System.Environment (getArgs)-import System.Exit (exitFailure)---- The purpose of this module is to provide a way to send emails to the Mailgun service using the--- command line. This is mainly for the purposes of testing initially. Using this executable should--- be the basis for our integration tests. It should also be the starting point for everybody that--- wishes to test this library out.--data Flag -   = Help-   | From { email :: UnverifiedEmailAddress }-   | To { email :: UnverifiedEmailAddress }-   | Subject { subject :: MessageSubject }-   | TextMessage { textFilePath :: FilePath }-   | HTMLMessage { htmlFilePath :: FilePath }-   deriving (Eq, Show)-   -options :: [OptDescr Flag]-options =-   [ Option "h" ["help"]    (NoArg Help)                     "displays this help message"-   , Option "f" ["from"]    (ReqArg fromP "me@test.test")    "You are required to provide sender of this email."-   , Option "t" ["to"]      (ReqArg toP "them@test.test")    "You will need to provide atleast one person that you wish to send the email to."-   -- TODO Confirm that this is required.-   , Option "s" ["subject"] (ReqArg Subject "subject")      "You need to send an email subject."-   , Option "x" ["text"]    (ReqArg TextMessage "email.text")   "You need to provide a text email file at a minimum."-   , Option "m" ["html"]    (ReqArg HTMLMessage "email.html")   "You can provide a HTML version of the email to send."-   ]-   where-      fromP = From . BC.pack-      toP = To . BC.pack--hailgunConfFile :: FilePath-hailgunConfFile = "hailgun.send.conf"--mailgunDomainLabel = T.pack "mailgun-domain"-mailgunApiKeyLabel = T.pack "mailgun-api-key"--loadHailgunContext :: FilePath -> IO HailgunContext-loadHailgunContext configFile = do-   hailgunConf <- load [Required configFile]-   domain <- require hailgunConf mailgunDomainLabel-   apiKey <- require hailgunConf mailgunApiKeyLabel-   return HailgunContext -      { hailgunDomain = domain-      , hailgunApiKey = apiKey-      }--handleSend :: [Flag] -> MessageContent -> Either HailgunErrorMessage HailgunMessage-handleSend flags emailBody =-   case (unverifiedFrom, subjects) of-      ([from], [subject]) -> hailgunMessage subject emailBody from simpleRecipients-      ([], []) -> fail "You need to provide both a from address and a subject to send an email."-      (_ , []) -> fail "You have more than one from address and only one is allowed"-      ([], _ ) -> fail "You have more than one subject and only one is allowed"-      _        -> fail "You have too many from adresses and subjects, you should only have one of each."-   where-      unverifiedTo = fmap email . filter isTo $ flags-      unverifiedFrom = fmap email . filter isFrom $ flags-      subjects = fmap subject . filter isSubject $ flags--      simpleRecipients = emptyMessageRecipients { recipientsTo = unverifiedTo }--isSubject :: Flag -> Bool-isSubject (Subject _) = True-isSubject _ = False--isTo :: Flag -> Bool-isTo (To _) = True-isTo _ = False--isFrom :: Flag -> Bool-isFrom (From _) = True-isFrom _ = False--isTextMessage :: Flag -> Bool-isTextMessage (TextMessage _) = True-isTextMessage _ = False--isHtmlMessage :: Flag -> Bool-isHtmlMessage (HTMLMessage _) = True-isHtmlMessage _ = False--sendMessage :: HailgunMessage -> IO ()-sendMessage message = do-   hailgunContext <- loadHailgunContext hailgunConfFile-   response <- sendEmail hailgunContext message-   case response of-      Left error -> putStrLn $ "Failed to send email: " ++ herMessage error-      Right result -> do-         putStrLn "Sent Email!"-         putStrLn $ "Id: " ++ hsrId result-         putStrLn $ "Message: " ++ hsrMessage result--usageMessage = "Send emails using the Mailgun api."--main :: IO ()-main = do-   arguments <- getArgs-   case getOpt Permute options arguments of-      (flags, _, []) -> if Help `elem` flags-         then printUsage-         else do-            potentialEmailBody <- prepareEmailBody flags-            case potentialEmailBody of-               Nothing -> putStrLn "At the very least you must provide a text file to send as the email body."-               (Just messageContent) -> case handleSend flags messageContent of-                  Left error -> putStrLn $ "Error generating mail: " ++ error -                  Right message -> sendMessage message-      (_, _, xs) -> do-         putStrLn "Error parsing arguments:"-         mapM_ putStrLn xs-         printUsage-         exitFailure-   where-      printUsage = putStrLn $ usageInfo usageMessage options--prepareEmailBody :: [Flag] -> IO (Maybe MessageContent)-prepareEmailBody flags = case (DL.find isTextMessage flags, DL.find isHtmlMessage flags) of-   (Nothing, _) -> return Nothing-   (Just (TextMessage textPath), Nothing) -> Just . TextOnly <$> BC.readFile textPath-   (Just (TextMessage textPath), Just (HTMLMessage htmlPath)) -> do-      textContents <- BC.readFile textPath-      htmlContents <- BC.readFile htmlPath-      return . Just $ TextAndHTML -         { textContent = textContents-         , htmlContent = htmlContents-         }-   _ -> return Nothing -- The type safety should prevent this scenario.