diff options
author | Tyler Bischel <tyler.bischel@sendgrid.com> | 2012-01-24 15:37:39 -0800 |
---|---|---|
committer | Tyler Bischel <tyler.bischel@sendgrid.com> | 2012-01-24 15:37:39 -0800 |
commit | bfae82588ff323974edd18214c274b0f5dba4bf0 (patch) | |
tree | 6001088c04a9e821f1d84aed1dc20cf1b81241ee | |
parent | 1be4549531d73cf89b4bebd2dac003937a1661df (diff) | |
download | sendgrid-csharp-bfae82588ff323974edd18214c274b0f5dba4bf0.zip sendgrid-csharp-bfae82588ff323974edd18214c274b0f5dba4bf0.tar.gz sendgrid-csharp-bfae82588ff323974edd18214c274b0f5dba4bf0.tar.bz2 |
updated the tests for AddSubVal and mapped some header functionality to the SendGrid object
-rwxr-xr-x | SendGrid/SendGridMail/ISendGrid.cs | 54 | ||||
-rwxr-xr-x | SendGrid/SendGridMail/SendGrid.cs | 96 | ||||
-rwxr-xr-x | SendGrid/Tests/TestSendgridMessageSetup.cs | 14 |
3 files changed, 73 insertions, 91 deletions
diff --git a/SendGrid/SendGridMail/ISendGrid.cs b/SendGrid/SendGridMail/ISendGrid.cs index f868e2d..724d145 100755 --- a/SendGrid/SendGridMail/ISendGrid.cs +++ b/SendGrid/SendGridMail/ISendGrid.cs @@ -6,16 +6,6 @@ using System.Net.Mail; namespace SendGridMail
{
/// <summary>
- /// Internal object to represent the way in which email may be sent.
- /// The library supports sending through either SMTP or Web interfaces.
- /// </summary>
- public enum TransportType
- {
- SMTP,
- REST
- };
-
- /// <summary>
/// Represents the basic set of functions that will be called by the user
/// includes basic message data manipulation and filter settings
/// </summary>
@@ -33,7 +23,6 @@ namespace SendGridMail IHeader Header { get; set; }
String Html { get; set; }
String Text { get; set; }
- TransportType Transport { get; set; }
#endregion
#region Interface for ITransport
@@ -42,13 +31,6 @@ namespace SendGridMail /// </summary>
/// <returns>MIME to be sent</returns>
MailMessage CreateMimeMessage();
-
- /// <summary>
- /// Creates a new transport object, and sends this message out.
- /// </summary>
- /// <param name="credentials">Sendgrid user credentials</param>
- void Mail(NetworkCredential credentials);
-
#endregion
#region Methods for setting data
@@ -68,7 +50,7 @@ namespace SendGridMail /// Add to the 'To' address.
/// </summary>
/// <param name="addresssInfo"> the dictionary keys are the email addresses, which points to a dictionary of
- /// key value pairs mapping to other address codes, such as { foo@bar.com => { 'DisplayName' => 'Mr Foo' } } </param>
+ /// key substitutionValues pairs mapping to other address codes, such as { foo@bar.com => { 'DisplayName' => 'Mr Foo' } } </param>
void AddTo(IDictionary<String, IDictionary<String, String>> addresssInfo);
/// <summary>
@@ -87,7 +69,7 @@ namespace SendGridMail /// Add to the 'CC' address.
/// </summary>
/// <param name="addresssInfo">the dictionary keys are the email addresses, which points to a dictionary of
- /// key value pairs mapping to other address codes, such as { foo@bar.com => { 'DisplayName' => 'Mr Foo' } } </param>
+ /// key substitutionValues pairs mapping to other address codes, such as { foo@bar.com => { 'DisplayName' => 'Mr Foo' } } </param>
void AddCc(IDictionary<String, IDictionary<String, String>> addresssInfo);
/// <summary>
@@ -106,16 +88,30 @@ namespace SendGridMail /// Add to the 'Bcc' address.
/// </summary>
/// <param name="addresssInfo">the dictionary keys are the email addresses, which points to a dictionary of
- /// key value pairs mapping to other address codes, such as { foo@bar.com => { 'DisplayName' => 'Mr Foo' } }</param>
+ /// key substitutionValues pairs mapping to other address codes, such as { foo@bar.com => { 'DisplayName' => 'Mr Foo' } }</param>
void AddBcc(IDictionary<String, IDictionary<String, String>> addresssInfo);
/// <summary>
- /// Adds a substitution value to be used during the mail merge. Substitutions will happen in the order
- /// added, so calls to this should match calls to addTo.
+ /// Defines a mapping between a replacement string in the text of the message to a list of
+ /// substitution values to be used, one per each recipient, in the same order as the recipients were added.
+ /// </summary>
+ /// <param name="replacementTag">the string in the email that you'll replace eg. '-name-'</param>
+ /// <param name="substitutionValues">a list of values that will be substituted in for the replacementTag, one for each recipient</param>
+ void AddSubVal(String replacementTag, List<String> substitutionValues);
+
+ /// <summary>
+ /// This adds parameters and values that will be bassed back through SendGrid's
+ /// Event API if an event notification is triggered by this email.
+ /// </summary>
+ /// <param name="identifiers">parameter substitutionValues pairs to be passed back on event notification</param>
+ void AddUniqueIdentifier(IDictionary<String, String> identifiers);
+
+ /// <summary>
+ /// This sets the category for this email. Statistics are stored on a per category
+ /// basis, so this can be useful for tracking on a per group basis.
/// </summary>
- /// <param name="tag">the string in the email that you'll replace eg. '-name-'</param>
- /// <param name="value">a list of values that will be substituted in for the tag, one for each recipient</param>
- void AddSubVal(String tag, params String[] value);
+ /// <param name="category">categories applied to the message</param>
+ void SetCategory(String category);
/// <summary>
/// Add an attachment to the message.
@@ -132,7 +128,7 @@ namespace SendGridMail /// <summary>
/// Add custom headers to the message
/// </summary>
- /// <param name="headers">key value pairs</param>
+ /// <param name="headers">key substitutionValues pairs</param>
void AddHeaders(IDictionary<String, String> headers);
#endregion
@@ -206,7 +202,7 @@ namespace SendGridMail /// <summary>
/// Provides notification when emails are deteched that exceed a predefined spam threshold.
/// </summary>
- /// <param name="score">Emails with a SpamAssassin score over this value will be considered spam and not be delivered.</param>
+ /// <param name="score">Emails with a SpamAssassin score over this substitutionValues will be considered spam and not be delivered.</param>
/// <param name="url">SendGrid will send an HTTP POST request to this url when a message is detected as spam</param>
void EnableSpamCheck(int score = 5, String url = null);
@@ -243,7 +239,7 @@ namespace SendGridMail /// <summary>
/// Wraps an HTML template around your email content.
/// </summary>
- /// <param name="html">HTML that your emails will be wrapped in, containing a body tag.</param>
+ /// <param name="html">HTML that your emails will be wrapped in, containing a body replacementTag.</param>
void EnableTemplate(String html = null);
/// <summary>
diff --git a/SendGrid/SendGridMail/SendGrid.cs b/SendGrid/SendGridMail/SendGrid.cs index 108c930..5f847dc 100755 --- a/SendGrid/SendGridMail/SendGrid.cs +++ b/SendGrid/SendGridMail/SendGrid.cs @@ -13,7 +13,7 @@ namespace SendGridMail {
#region constants/vars
//private/constant vars:
- private Dictionary<String, String> _filters;
+ private static readonly Dictionary<String, String> Filters = InitializeFilters();
private MailMessage message;
// TODO find appropriate types for these
@@ -49,14 +49,14 @@ namespace SendGridMail /// <param name="transport">Transport class to use for sending the message</param>
/// <returns></returns>
public static SendGrid GetInstance(MailAddress from, MailAddress[] to, MailAddress[] cc, MailAddress[] bcc,
- String subject, String html, String text, TransportType transport)
+ String subject, String html, String text)
{
var header = new Header();
- return new SendGrid(from, to, cc, bcc, subject, html, text, transport, header);
+ return new SendGrid(from, to, cc, bcc, subject, html, text, header);
}
internal SendGrid(MailAddress from, MailAddress[] to, MailAddress[] cc, MailAddress[] bcc,
- String subject, String html, String text, TransportType transport, IHeader header = null ) : this(header)
+ String subject, String html, String text, IHeader header = null ) : this(header)
{
From = from;
To = to;
@@ -74,14 +74,11 @@ namespace SendGridMail message = new MailMessage();
Header = header;
Headers = new Dictionary<string, string>();
-
- //initialize the filters, for use within the library
- this.InitializeFilters();
}
- public void InitializeFilters()
+ private static Dictionary<string, string> InitializeFilters()
{
- this._filters =
+ return
new Dictionary<string, string>
{
{"Gravatar", "gravatar"},
@@ -191,7 +188,6 @@ namespace SendGridMail public IHeader Header { get; set; }
public String Html { get; set; }
public String Text { get; set; }
- public TransportType Transport { get; set; }
#endregion
#region Methods for setting data
@@ -286,12 +282,23 @@ namespace SendGridMail set { _attachments = value.ToList(); }
}
- public void AddSubVal(String tag, params String[] value)
+ public void AddSubVal(String replacementTag, List<String> substitutionValues)
{
//let the system complain if they do something bad, since the function returns null
- Header.AddSubVal(tag, value);
+ Header.AddSubVal(replacementTag, substitutionValues);
+ }
+
+ public void AddUniqueIdentifier(IDictionary<String, String> identifiers)
+ {
+ Header.AddUniqueIdentifier(identifiers);
+ }
+
+ public void SetCategory(String category)
+ {
+ Header.SetCategory(category);
}
+
public void AddAttachment(String filePath)
{
_attachments.Add(filePath);
@@ -316,67 +323,67 @@ namespace SendGridMail #region SMTP API Functions
public void DisableGravatar()
{
- Header.Disable(_filters["Gravatar"]);
+ Header.Disable(Filters["Gravatar"]);
}
public void DisableOpenTracking()
{
- Header.Disable(_filters["OpenTracking"]);
+ Header.Disable(Filters["OpenTracking"]);
}
public void DisableClickTracking()
{
- Header.Disable(_filters["ClickTracking"]);
+ Header.Disable(Filters["ClickTracking"]);
}
public void DisableSpamCheck()
{
- Header.Disable(_filters["SpamCheck"]);
+ Header.Disable(Filters["SpamCheck"]);
}
public void DisableUnsubscribe()
{
- Header.Disable(_filters["Unsubscribe"]);
+ Header.Disable(Filters["Unsubscribe"]);
}
public void DisableFooter()
{
- Header.Disable(_filters["Footer"]);
+ Header.Disable(Filters["Footer"]);
}
public void DisableGoogleAnalytics()
{
- Header.Disable(_filters["GoogleAnalytics"]);
+ Header.Disable(Filters["GoogleAnalytics"]);
}
public void DisableTemplate()
{
- Header.Disable(_filters["Template"]);
+ Header.Disable(Filters["Template"]);
}
public void DisableBcc()
{
- Header.Disable(_filters["Bcc"]);
+ Header.Disable(Filters["Bcc"]);
}
public void DisableBypassListManagement()
{
- Header.Disable(_filters["BypassListManagement"]);
+ Header.Disable(Filters["BypassListManagement"]);
}
public void EnableGravatar()
{
- Header.Enable(_filters["Gravatar"]);
+ Header.Enable(Filters["Gravatar"]);
}
public void EnableOpenTracking()
{
- Header.Enable(_filters["OpenTracking"]);
+ Header.Enable(Filters["OpenTracking"]);
}
public void EnableClickTracking(bool includePlainText = false)
{
- var filter = _filters["ClickTracking"];
+ var filter = Filters["ClickTracking"];
Header.Enable(filter);
if (includePlainText)
@@ -387,7 +394,7 @@ namespace SendGridMail public void EnableSpamCheck(int score = 5, string url = null)
{
- var filter = _filters["SpamCheck"];
+ var filter = Filters["SpamCheck"];
Header.Enable(filter);
Header.AddFilterSetting(filter, new List<string> { "maxscore" }, score.ToString(CultureInfo.InvariantCulture));
@@ -396,16 +403,16 @@ namespace SendGridMail public void EnableUnsubscribe(string text, string html)
{
- var filter = _filters["Unsubscribe"];
+ var filter = Filters["Unsubscribe"];
if(!System.Text.RegularExpressions.Regex.IsMatch(text, ReText))
{
- throw new Exception("Missing substitution tag in text");
+ throw new Exception("Missing substitution replacementTag in text");
}
if(!System.Text.RegularExpressions.Regex.IsMatch(html, ReHtml))
{
- throw new Exception("Missing substitution tag in html");
+ throw new Exception("Missing substitution replacementTag in html");
}
Header.Enable(filter);
@@ -415,7 +422,7 @@ namespace SendGridMail public void EnableUnsubscribe(string replace)
{
- var filter = _filters["Unsubscribe"];
+ var filter = Filters["Unsubscribe"];
Header.Enable(filter);
Header.AddFilterSetting(filter, new List<string> { "replace" }, replace);
@@ -423,7 +430,7 @@ namespace SendGridMail public void EnableFooter(string text = null, string html = null)
{
- var filter = _filters["Footer"];
+ var filter = Filters["Footer"];
Header.Enable(filter);
Header.AddFilterSetting(filter, new List<string> { "text/plain" }, text);
@@ -432,7 +439,7 @@ namespace SendGridMail public void EnableGoogleAnalytics(string source, string medium, string term, string content = null, string campaign = null)
{
- var filter = _filters["GoogleAnalytics"];
+ var filter = Filters["GoogleAnalytics"];
Header.Enable(filter);
Header.AddFilterSetting(filter, new List<string> { "utm_source" }, source);
@@ -444,11 +451,11 @@ namespace SendGridMail public void EnableTemplate(string html)
{
- var filter = _filters["Template"];
+ var filter = Filters["Template"];
if (!System.Text.RegularExpressions.Regex.IsMatch(html, ReHtml))
{
- throw new Exception("Missing substitution tag in html");
+ throw new Exception("Missing substitution replacementTag in html");
}
Header.Enable(filter);
@@ -457,7 +464,7 @@ namespace SendGridMail public void EnableBcc(string email)
{
- var filter = _filters["Bcc"];
+ var filter = Filters["Bcc"];
Header.Enable(filter);
Header.AddFilterSetting(filter, new List<string> { "email" }, email);
@@ -465,7 +472,7 @@ namespace SendGridMail public void EnableBypassListManagement()
{
- Header.Enable(_filters["BypassListManagement"]);
+ Header.Enable(Filters["BypassListManagement"]);
}
#endregion
@@ -521,22 +528,5 @@ namespace SendGridMail var msg = CreateMimeMessage();
client.Send(msg);
}
-
- public void Mail(NetworkCredential credentials)
- {
- ITransport transport;
- switch (Transport)
- {
- case TransportType.SMTP:
- transport = SMTP.GetInstance(credentials);
- break;
- case TransportType.REST:
- transport = Web.GetInstance(credentials);
- break;
- default:
- throw new ArgumentException("Transport method not specified");
- }
- transport.Deliver(this);
- }
}
}
diff --git a/SendGrid/Tests/TestSendgridMessageSetup.cs b/SendGrid/Tests/TestSendgridMessageSetup.cs index efbb853..b02729d 100755 --- a/SendGrid/Tests/TestSendgridMessageSetup.cs +++ b/SendGrid/Tests/TestSendgridMessageSetup.cs @@ -99,21 +99,17 @@ namespace Tests Assert.AreEqual("New Test Subject", sg.Subject, "null subject does not overide previous subject");
}
- /*
[Test]
public void TestAddSubVal()
{
- var header = new Header();
- var sg = new SendGrid(header);
+ var substitutionStrings = new List<String> {"foo", "bar", "beer"};
+ var mock = new Mock<IHeader>();
- var datastruct = new String[2];
- datastruct[0] = "eric";
- datastruct[1] = "tyler";
+ var sg = new SendGrid(mock.Object);
+ sg.AddSubVal("-name-", substitutionStrings);
- sg.AddSubVal("-name-", datastruct);
- Assert.AreEqual("test", sg.Header);
+ mock.Verify(foo => foo.AddSubVal("-name-", substitutionStrings));
}
- */
[Test]
|