Skip to content

Commit

Permalink
Cleanup emailer. (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
acodcha authored Oct 19, 2023
1 parent 2ae798d commit 4f3c454
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
29 changes: 20 additions & 9 deletions source/Messenger/Emailer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@

namespace SecretSanta::Messenger {

std::string CreateFullMessageBody(
// Composes the full email message body for a given gifter. Prefixes a brief
// greeting to the given main message body and appends the giftee information.
std::string ComposeFullMessageBody(
const Participant& gifter, const Participant& giftee,
const std::string& main_message_body) {
std::string text;
Expand All @@ -41,7 +43,9 @@ std::string CreateFullMessageBody(

text.append("Your giftee is:\n\n");
text.append(giftee.Name() + "\n");
text.append(giftee.Address() + "\n");
if (!giftee.Address().empty()) {
text.append(giftee.Address() + "\n");
}
if (!giftee.Instructions().empty()) {
text.append("Special Instructions: " + giftee.Instructions() + "\n");
}
Expand All @@ -50,21 +54,24 @@ std::string CreateFullMessageBody(
return text;
}

std::string CreateCommand(
// Composes the command used to invoke the S-nail utility for a given gifter.
std::string ComposeCommand(
const Participant& gifter, const std::string& message_subject,
const std::string& message_body) {
return "echo \"" + message_body + "\" | s-nail --subject \"" + message_subject
+ "\" " + gifter.Email();
}

void SendEmailMessage(
// Composes and sends an email message to a given gifter. Creates the full body
// of the message and sends it using the S-nail utility.
void ComposeAndSendEmailMessage(
const Participant& gifter, const Participant& giftee,
const std::string& message_subject, const std::string& main_message_body) {
const std::string full_message_body =
CreateFullMessageBody(gifter, giftee, main_message_body);
ComposeFullMessageBody(gifter, giftee, main_message_body);

const std::string command{
CreateCommand(gifter, message_subject, full_message_body)};
ComposeCommand(gifter, message_subject, full_message_body)};

const int outcome{std::system(command.c_str())};

Expand All @@ -76,9 +83,11 @@ void SendEmailMessage(
}
}

void SendEmailMessages(
// Composes and sends email messages to all gifters.
void ComposeAndSendEmailMessages(
const Configuration& configuration, const Matchings& matchings) {
for (const Participant& gifter : configuration.Participants()) {
// Obtain the gifter and giftee names.
const std::map<std::string, std::string>::const_iterator
gifter_name_and_giftee_name =
matchings.GiftersToGiftees().find(gifter.Name());
Expand All @@ -87,6 +96,7 @@ void SendEmailMessages(
continue;
}

// Obtain the giftee information.
const std::set<Participant>::const_iterator giftee =
configuration.Participants().find(
{gifter_name_and_giftee_name->second});
Expand All @@ -95,8 +105,9 @@ void SendEmailMessages(
continue;
}

SendEmailMessage(gifter, *giftee, configuration.MessageSubject(),
configuration.MessageBody());
// Compose and send the email message to this gifter.
ComposeAndSendEmailMessage(gifter, *giftee, configuration.MessageSubject(),
configuration.MessageBody());
}
}

Expand Down
2 changes: 1 addition & 1 deletion source/Messenger/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int main(int argc, char* argv[]) {

const SecretSanta::Matchings matchings{settings.MatchingsFile()};

SecretSanta::Messenger::SendEmailMessages(configuration, matchings);
SecretSanta::Messenger::ComposeAndSendEmailMessages(configuration, matchings);

std::cout << "End of " << SecretSanta::Messenger::Program::Title << "."
<< std::endl;
Expand Down
8 changes: 4 additions & 4 deletions test/Messenger/Emailer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace SecretSanta::Messenger {

namespace {

TEST(MessengerEmailer, CreateFullMessageBody) {
TEST(MessengerEmailer, ComposeFullMessageBody) {
const Participant gifter = CreateSampleParticipantA();
const Participant giftee = CreateSampleParticipantB();

Expand All @@ -41,7 +41,7 @@ TEST(MessengerEmailer, CreateFullMessageBody) {
"Secret Santa gift exchange!"};

const std::string result =
CreateFullMessageBody(gifter, giftee, main_message_body);
ComposeFullMessageBody(gifter, giftee, main_message_body);

EXPECT_EQ(result,
"Hello Alice Smith,\n\nYou are receiving this message because you "
Expand All @@ -50,12 +50,12 @@ TEST(MessengerEmailer, CreateFullMessageBody) {
"92345 USA\n\nThank you!");
}

TEST(MessengerEmailer, CreateCommand) {
TEST(MessengerEmailer, ComposeCommand) {
const Participant gifter = CreateSampleParticipantA();
const std::string message_subject{"My Message Subject"};
const std::string message_body{"My Message Body"};

std::string command = CreateCommand(gifter, message_subject, message_body);
std::string command = ComposeCommand(gifter, message_subject, message_body);

EXPECT_EQ(command,
"echo \"My Message Body\" | s-nail --subject \"My Message "
Expand Down

0 comments on commit 4f3c454

Please sign in to comment.