Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
firesharkstudios committed Oct 21, 2019
1 parent ee1e63c commit 3324e99
Show file tree
Hide file tree
Showing 20 changed files with 774 additions and 400 deletions.
2 changes: 1 addition & 1 deletion Butterfly.Message.Aws/Butterfly.Message.Aws.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<Authors>Kent Johnson</Authors>
<Copyright>Copyright 2017-2019 Fireshark Studios, LLC</Copyright>
<Description>Implementation of Butterfly.Message for AWS Simple Email Service</Description>
<Version>2.0.4</Version>
<Version>2.0.5</Version>
<PackageReleaseNotes>Various improvements and bug fixes</PackageReleaseNotes>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(MSBuildProjectName).xml</DocumentationFile>
<LangVersion>7.1</LangVersion>
Expand Down
2 changes: 1 addition & 1 deletion Butterfly.Message.Test/MessageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class MessageTest {
[TestMethod]
public void ParseSendMessage() {
var email = FileX.LoadResourceAsText(Assembly.GetExecutingAssembly(), "Butterfly.Message.Test.email.txt");
var sendMessageTemplate = SendMessage.Parse(email);
var sendMessageTemplate = SendMessage.Parse(email, SimpleEvaluator.Evaluate);
var sendMessage = sendMessageTemplate.Evaluate(new {
first_name = "Bob"
});
Expand Down
2 changes: 1 addition & 1 deletion Butterfly.Message.Twilio/Butterfly.Message.Twilio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<Authors>Kent Johnson</Authors>
<Copyright>Copyright 2017-2019 Fireshark Studios, LLC</Copyright>
<Description>Implementation of Butterfly.Message for Twilio SMS</Description>
<Version>2.0.4</Version>
<Version>2.0.5</Version>
<PackageReleaseNotes>Various improvements and bug fixes</PackageReleaseNotes>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(MSBuildProjectName).xml</DocumentationFile>
<LangVersion>7.1</LangVersion>
Expand Down
2 changes: 1 addition & 1 deletion Butterfly.Message/Butterfly.Message.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Company>Fireshark Studios, LLC</Company>
<Product>Butterfly.Message</Product>
<Copyright>Copyright 2017-2019 Fireshark Studios, LLC</Copyright>
<Version>2.0.4</Version>
<Version>2.0.5</Version>
<PackageReleaseNotes>Various improvements and bug fixes</PackageReleaseNotes>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(MSBuildProjectName).xml</DocumentationFile>
<LangVersion>7.1</LangVersion>
Expand Down
4 changes: 2 additions & 2 deletions Butterfly.Message/ScribanEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
namespace Butterfly.Message {
public static class ScribanEvaluator {

public static string Evaluate(string text, Dict vars, string sourceFilePath) {
Template template = Template.Parse(text, sourceFilePath: sourceFilePath);
public static string Evaluate(string text, Dict vars, string path) {
Template template = Template.Parse(text, sourceFilePath: path);
return template.RenderWithRelativeIncludes(vars);
}

Expand Down
29 changes: 10 additions & 19 deletions Butterfly.Message/SendMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ public class SendMessage {
public readonly string bodyHtml;
public readonly byte priority;
public readonly Func<string, Dict , string, string> evaluator;
public readonly string sourceFile;
public readonly string path;

public Dict extraData = null;

public SendMessage(string from, string to, string subject, string bodyText, string bodyHtml = null, byte priority = 0, Func<string, Dict, string, string> evaluator = null, string sourceFile = null) {
public SendMessage(string from, string to, string subject, string bodyText, string bodyHtml = null, byte priority = 0, Func<string, Dict, string, string> evaluator = null, string path = null) {
this.from = from;
this.to = to;
this.subject = subject;
this.bodyText = bodyText;
this.bodyHtml = bodyHtml;
this.priority = priority;
this.evaluator = evaluator;
this.sourceFile = sourceFile;
this.path = path;
this.extraData = null;
}

Expand All @@ -56,24 +56,15 @@ public SendMessage Evaluate(dynamic vars) {
values = DynamicX.ToDictionary(vars);
}

string from = this.evaluator(this.from, values, this.sourceFile);
string to = this.evaluator(this.to, values, this.sourceFile);
string subject = this.evaluator(this.subject, values, this.sourceFile);
string bodyText = this.evaluator(this.bodyText, values, this.sourceFile);
string bodyHtml = this.evaluator(this.bodyHtml, values, this.sourceFile);
string from = this.evaluator(this.from, values, this.path);
string to = this.evaluator(this.to, values, this.path);
string subject = this.evaluator(this.subject, values, this.path);
string bodyText = this.evaluator(this.bodyText, values, this.path);
string bodyHtml = this.evaluator(this.bodyHtml, values, this.path);
return new SendMessage(from, to, subject, bodyText, bodyHtml, this.priority);
}

public static SendMessage ParseFile(string fileName, Dictionary<string, Func<string, Dict, string, string>> evaluatorByExtension) {
if (!File.Exists(fileName)) throw new Exception("Could not find file '" + fileName + "'");
var extension = Path.GetExtension(fileName);
if (!evaluatorByExtension.TryGetValue(extension, out Func<string, Dict, string, string> evaluator)) throw new Exception($"Unknown evaluator extension {extension}");

string text = File.ReadAllText(fileName);
return Parse(text, evaluator, fileName);
}

public static SendMessage Parse(string text, Func<string, Dict, string, string> evaluator, string sourceFile) {
public static SendMessage Parse(string text, Func<string, Dict, string, string> evaluator, string path = null) {
string from = null;
string to = null;
string subject = null;
Expand Down Expand Up @@ -136,7 +127,7 @@ public static SendMessage Parse(string text, Func<string, Dict, string, string>
}
}

return new SendMessage(from, to, subject, sectionByName.GetAs(TEXT_SECTION_NAME, (string)null), sectionByName.GetAs(HTML_SECTION_NAME, (string)null), priority, evaluator, sourceFile);
return new SendMessage(from, to, subject, sectionByName.GetAs(TEXT_SECTION_NAME, (string)null), sectionByName.GetAs(HTML_SECTION_NAME, (string)null), priority, evaluator, path);
}
}
}
34 changes: 34 additions & 0 deletions Butterfly.Message/SendMessageFileParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.IO;

using Dict = System.Collections.Generic.Dictionary<string, object>;

namespace Butterfly.Message {
public class SendMessageFileParser {

protected Dictionary<string, Func<string, Dict, string, string>> evaluatorByExtension;

public SendMessageFileParser(Dictionary<string, Func<string, Dict, string, string>> evaluatorByExtension = null) {
if (evaluatorByExtension==null) {
this.evaluatorByExtension = new Dictionary<string, Func<string, Dict, string, string>> {
["liquid"] = ScribanEvaluator.Evaluate,
["txt"] = SimpleEvaluator.Evaluate
};
}
else {
this.evaluatorByExtension = evaluatorByExtension;
}
}

public SendMessage Parse(string fileName) {
if (!File.Exists(fileName)) throw new Exception("Could not find file '" + fileName + "'");
var extension = Path.GetExtension(fileName);
if (!this.evaluatorByExtension.TryGetValue(extension, out Func<string, Dict, string, string> evaluator)) throw new Exception($"Unknown evaluator extension {extension}");

string text = File.ReadAllText(fileName);
return SendMessage.Parse(text, evaluator, Path.GetDirectoryName(fileName));
}

}
}
7 changes: 5 additions & 2 deletions docfx_project/api/.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@
"Butterfly.Message.SendMessage.HTML_SECTION_NAME": "Butterfly.Message.SendMessage.yml",
"Butterfly.Message.SendMessage.logger": "Butterfly.Message.SendMessage.yml",
"Butterfly.Message.SendMessage.Parse(System.String,Func{System.String,System.Collections.Generic.Dictionary{System.String,System.Object},System.String,System.String},System.String)": "Butterfly.Message.SendMessage.yml",
"Butterfly.Message.SendMessage.ParseFile(System.String,Dictionary{System.String,Func{System.String,System.Collections.Generic.Dictionary{System.String,System.Object},System.String,System.String}})": "Butterfly.Message.SendMessage.yml",
"Butterfly.Message.SendMessage.path": "Butterfly.Message.SendMessage.yml",
"Butterfly.Message.SendMessage.priority": "Butterfly.Message.SendMessage.yml",
"Butterfly.Message.SendMessage.SECTION_DELIM": "Butterfly.Message.SendMessage.yml",
"Butterfly.Message.SendMessage.sourceFile": "Butterfly.Message.SendMessage.yml",
"Butterfly.Message.SendMessage.subject": "Butterfly.Message.SendMessage.yml",
"Butterfly.Message.SendMessage.TEXT_SECTION_NAME": "Butterfly.Message.SendMessage.yml",
"Butterfly.Message.SendMessage.to": "Butterfly.Message.SendMessage.yml",
"Butterfly.Message.SendMessageFileParser": "Butterfly.Message.SendMessageFileParser.yml",
"Butterfly.Message.SendMessageFileParser.#ctor(Dictionary{System.String,Func{System.String,System.Collections.Generic.Dictionary{System.String,System.Object},System.String,System.String}})": "Butterfly.Message.SendMessageFileParser.yml",
"Butterfly.Message.SendMessageFileParser.evaluatorByExtension": "Butterfly.Message.SendMessageFileParser.yml",
"Butterfly.Message.SendMessageFileParser.Parse(System.String)": "Butterfly.Message.SendMessageFileParser.yml",
"Butterfly.Message.SendMessageQueueManager": "Butterfly.Message.SendMessageQueueManager.yml",
"Butterfly.Message.SendMessageQueueManager.#ctor(Butterfly.Db.IDatabase,Butterfly.Message.IMessageSender,Butterfly.Message.IMessageSender,System.String,System.String,System.Int32,Butterfly.Message.SendMessage,Butterfly.Message.SendMessage,System.String)": "Butterfly.Message.SendMessageQueueManager.yml",
"Butterfly.Message.SendMessageQueueManager.database": "Butterfly.Message.SendMessageQueueManager.yml",
Expand Down
6 changes: 3 additions & 3 deletions docfx_project/api/Butterfly.Message.ScribanEvaluator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ items:
- Butterfly.Message
namespace: Butterfly.Message
syntax:
content: public static string Evaluate(string text, System.Collections.Generic.Dictionary<string, object> vars, string sourceFilePath)
content: public static string Evaluate(string text, System.Collections.Generic.Dictionary<string, object> vars, string path)
parameters:
- id: text
type: System.String
- id: vars
type: System.Collections.Generic.Dictionary{System.String,System.Object}
- id: sourceFilePath
- id: path
type: System.String
return:
type: System.String
content.vb: Public Shared Function Evaluate(text As String, vars As System.Collections.Generic.Dictionary(Of String, Object), sourceFilePath As String) As String
content.vb: Public Shared Function Evaluate(text As String, vars As System.Collections.Generic.Dictionary(Of String, Object), path As String) As String
overload: Butterfly.Message.ScribanEvaluator.Evaluate*
nameWithType.vb: ScribanEvaluator.Evaluate(String, System.Collections.Generic.Dictionary(Of String, Object), String)
modifiers.csharp:
Expand Down
Loading

0 comments on commit 3324e99

Please sign in to comment.