aboutsummaryrefslogtreecommitdiff
path: root/src/Mail.hs
diff options
context:
space:
mode:
authorJoris2015-10-09 23:09:28 +0200
committerJoris2015-10-09 23:09:28 +0200
commit6cfff0cc8dea84e2a304d350118112ff5113adec (patch)
tree94a157b7db5c47f2f9ac282536aae37757076e14 /src/Mail.hs
downloadevents-6cfff0cc8dea84e2a304d350118112ff5113adec.tar.gz
events-6cfff0cc8dea84e2a304d350118112ff5113adec.tar.bz2
events-6cfff0cc8dea84e2a304d350118112ff5113adec.zip
Initial commit
Diffstat (limited to 'src/Mail.hs')
-rw-r--r--src/Mail.hs75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/Mail.hs b/src/Mail.hs
new file mode 100644
index 0000000..7bb6814
--- /dev/null
+++ b/src/Mail.hs
@@ -0,0 +1,75 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+module Mail
+ ( mailSubject
+ , mailBody
+ ) where
+
+import Data.Text (Text)
+import qualified Data.Text as T
+
+import Date
+import Birthdate
+
+mailSubject :: [Birthdate] -> Text
+mailSubject birthdates =
+ let count = length birthdates
+ in T.concat
+ [ "Hey, "
+ , if count > 1 then "there are" else "there is"
+ , " "
+ , T.pack . show $ count
+ , " birthday"
+ , if count > 1 then "s" else ""
+ , " today!"
+ ]
+
+mailBody :: Date -> [Birthdate] -> Text
+mailBody currentDate birthdates =
+ let count = length birthdates
+ birthdatesWithLines = map (mapFst getLine) . zip [1..] $ birthdates
+ getLine 1 = if count == 1 then SingleLine else FirstLine
+ getLine line = if line == count then LastLine else MiddleLine
+ in T.concat $ map (mailLine currentDate) birthdatesWithLines
+
+mapFst :: (a -> c) -> (a, b) -> (c, b)
+mapFst f (x, y) = (f x, y)
+
+data Line =
+ SingleLine
+ | FirstLine
+ | MiddleLine
+ | LastLine
+ deriving (Eq, Show)
+
+mailLine :: Date -> (Line, Birthdate) -> Text
+mailLine date (SingleLine, birthdate) =
+ T.concat
+ [ fullname birthdate
+ , " is now "
+ , T.pack . show $ age date birthdate
+ , " years old."
+ ]
+mailLine date (FirstLine, birthdate) =
+ T.concat
+ [ fullname birthdate
+ , " is now "
+ , T.pack . show $ age date birthdate
+ , " years old"
+ ]
+mailLine date (MiddleLine, birthdate) =
+ T.concat
+ [ ", "
+ , fullname birthdate
+ , " is "
+ , T.pack . show $ age date birthdate
+ , " years old"
+ ]
+mailLine date (LastLine, birthdate) =
+ T.concat
+ [ " and "
+ , fullname birthdate
+ , " is "
+ , T.pack . show $ age date birthdate
+ , " years old."
+ ]