summaryrefslogtreecommitdiffstats
path: root/SendGrid/SendGridMail/Header.cs
diff options
context:
space:
mode:
authorTyler Bischel <tyler.bischel@sendgrid.com>2012-01-11 13:03:42 -0800
committerTyler Bischel <tyler.bischel@sendgrid.com>2012-01-11 13:03:42 -0800
commit53620ab42159855d914fcfae3873c318228b5e94 (patch)
tree6109825703bf5f703ab83cec6617b5d694a52d76 /SendGrid/SendGridMail/Header.cs
parentb13f35419a29ace0ecaabff02552a0613aa8afe2 (diff)
downloadsendgrid-csharp-53620ab42159855d914fcfae3873c318228b5e94.zip
sendgrid-csharp-53620ab42159855d914fcfae3873c318228b5e94.tar.gz
sendgrid-csharp-53620ab42159855d914fcfae3873c318228b5e94.tar.bz2
implemented functionality for SendGrid specific headers
Diffstat (limited to 'SendGrid/SendGridMail/Header.cs')
-rwxr-xr-xSendGrid/SendGridMail/Header.cs72
1 files changed, 58 insertions, 14 deletions
diff --git a/SendGrid/SendGridMail/Header.cs b/SendGrid/SendGridMail/Header.cs
index 6023214..9aaa440 100755
--- a/SendGrid/SendGridMail/Header.cs
+++ b/SendGrid/SendGridMail/Header.cs
@@ -7,24 +7,33 @@ namespace SendGridMail
{
public class Header : IHeader
{
- public void AddTo(IEnumerable<string> recipients)
+ private readonly FilterNode _settings;
+
+ public Header()
{
- throw new NotImplementedException();
+ _settings = new FilterNode();
}
public void AddSubVal(string tag, IEnumerable<string> substitutions)
{
- throw new NotImplementedException();
+ var keys = new List<String> {"data", "sub", tag};
+ _settings.AddArray(keys, substitutions);
}
public void AddUniqueIdentifier(IDictionary<string, string> identifiers)
{
- throw new NotImplementedException();
+ foreach (var key in identifiers.Keys)
+ {
+ var keys = new List<String> {"data", "unique_args", key};
+ var value = identifiers[key];
+ _settings.AddSetting(keys, value);
+ }
}
public void SetCategory(string category)
{
- throw new NotImplementedException();
+ var keys = new List<String> {"data", "category"};
+ _settings.AddSetting(keys, category);
}
public void Enable(string filter)
@@ -39,23 +48,25 @@ namespace SendGridMail
public void AddFilterSetting(string filter, IEnumerable<string> settings, string value)
{
- throw new NotImplementedException();
+ var keys = new List<string>() {"filters", "data", filter, "settings"}.Concat(settings).ToList();
+ _settings.AddSetting(keys, value);
}
public void AddHeader(MailMessage mime)
{
- throw new NotImplementedException();
+ mime.Headers.Add("x-smtpapi", AsJson());
}
public String AsJson()
{
- return "";
+ if(_settings.IsEmpty()) return "";
+ return _settings.ToJson();
}
-
internal class FilterNode
{
- private Dictionary<String, FilterNode> _branches;
+ private readonly Dictionary<String, FilterNode> _branches;
+ private IEnumerable<String> _array;
private String _leaf;
public FilterNode()
@@ -63,6 +74,26 @@ namespace SendGridMail
_branches = new Dictionary<string, FilterNode>();
}
+ public void AddArray(List<String> keys, IEnumerable<String> value)
+ {
+ if (keys.Count == 0)
+ {
+ _array = value;
+ }
+ else
+ {
+ if (_leaf != null || _array != null)
+ throw new ArgumentException("Attempt to overwrite setting");
+
+ var key = keys.First();
+ if (!_branches.ContainsKey(key))
+ _branches[key] = new FilterNode();
+
+ var remainingKeys = keys.Skip(1).ToList();
+ _branches[key].AddArray(remainingKeys, value);
+ }
+ }
+
public void AddSetting(List<String> keys, String value)
{
if (keys.Count == 0)
@@ -71,15 +102,18 @@ namespace SendGridMail
}
else
{
- var key = keys[0];
+ if(_leaf != null || _array != null)
+ throw new ArgumentException("Attempt to overwrite setting");
+
+ var key = keys.First();
if (!_branches.ContainsKey(key))
_branches[key] = new FilterNode();
+
var remainingKeys = keys.Skip(1).ToList();
_branches[key].AddSetting(remainingKeys, value);
}
}
-
public String GetSetting(params String[] keys)
{
return GetSetting(keys.ToList());
@@ -104,8 +138,18 @@ namespace SendGridMail
public String ToJson()
{
if (_branches.Count > 0)
- return "{" + String.Join(",", _branches.Keys.Select(k => '"' + k + '"' + ":" + _branches[k].ToJson())) + "}";
- return JsonUtils.Serialize(_leaf);
+ return "{" + String.Join(",", _branches.Keys.Select(k => JsonUtils.Serialize(k) + " : " + _branches[k].ToJson())) + "}";
+ if (_leaf != null)
+ return JsonUtils.Serialize(_leaf);
+ if (_array != null)
+ return "[" + String.Join(",", _array.Select(i => JsonUtils.Serialize(i))) + "]";
+ return "{}";
+ }
+
+ public bool IsEmpty()
+ {
+ if (_leaf != null) return false;
+ return _branches == null || _branches.Keys.Count == 0;
}
}
}