sendgrid-ruby 4.3.3 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +3 -1
  3. data/CHANGELOG.md +20 -1
  4. data/README.md +4 -4
  5. data/USE_CASES.md +5 -5
  6. data/examples/helpers/mail/example.rb +37 -37
  7. data/lib/sendgrid-ruby.rb +20 -0
  8. data/lib/sendgrid/helpers/mail/asm.rb +33 -0
  9. data/lib/sendgrid/helpers/mail/attachment.rb +63 -0
  10. data/lib/sendgrid/helpers/mail/bcc_settings.rb +33 -0
  11. data/lib/sendgrid/helpers/mail/bypass_list_management.rb +43 -0
  12. data/lib/sendgrid/helpers/mail/category.rb +28 -0
  13. data/lib/sendgrid/helpers/mail/click_tracking.rb +33 -0
  14. data/lib/sendgrid/helpers/mail/content.rb +33 -0
  15. data/lib/sendgrid/helpers/mail/custom_arg.rb +24 -0
  16. data/lib/sendgrid/helpers/mail/email.rb +33 -0
  17. data/lib/sendgrid/helpers/mail/footer.rb +43 -0
  18. data/lib/sendgrid/helpers/mail/ganalytics.rb +74 -0
  19. data/lib/sendgrid/helpers/mail/header.rb +24 -0
  20. data/lib/sendgrid/helpers/mail/mail.rb +30 -857
  21. data/lib/sendgrid/helpers/mail/mail_settings.rb +63 -0
  22. data/lib/sendgrid/helpers/mail/open_tracking.rb +33 -0
  23. data/lib/sendgrid/helpers/mail/personalization.rb +75 -0
  24. data/lib/sendgrid/helpers/mail/section.rb +24 -0
  25. data/lib/sendgrid/helpers/mail/spam_check.rb +43 -0
  26. data/lib/sendgrid/helpers/mail/subscription_tracking.rb +53 -0
  27. data/lib/sendgrid/helpers/mail/substitution.rb +24 -0
  28. data/lib/sendgrid/helpers/mail/tracking_settings.rb +53 -0
  29. data/lib/sendgrid/version.rb +1 -1
  30. data/test/sendgrid/helpers/mail/test_category.rb +27 -0
  31. data/test/sendgrid/helpers/mail/test_mail.rb +170 -52
  32. data/test/sendgrid/helpers/mail/test_personalizations.rb +146 -0
  33. data/test/sendgrid/test_sendgrid-ruby.rb +1 -1
  34. metadata +27 -3
@@ -0,0 +1,63 @@
1
+ require 'json'
2
+
3
+ module SendGrid
4
+ class MailSettings
5
+ def initialize
6
+ @bcc = nil
7
+ @bypass_list_management = nil
8
+ @footer = nil
9
+ @sandbox_mode = nil
10
+ @spam_check = nil
11
+ end
12
+
13
+ def sandbox_mode=(sandbox_mode)
14
+ @sandbox_mode = sandbox_mode
15
+ end
16
+
17
+ def sandbox_mode
18
+ @sandbox_mode.nil? ? nil : @sandbox_mode.to_json
19
+ end
20
+
21
+ def bypass_list_management=(bypass_list_management)
22
+ @bypass_list_management = bypass_list_management
23
+ end
24
+
25
+ def bypass_list_management
26
+ @bypass_list_management.nil? ? nil : @bypass_list_management.to_json
27
+ end
28
+
29
+ def footer=(footer)
30
+ @footer = footer
31
+ end
32
+
33
+ def footer
34
+ @footer.nil? ? nil : @footer.to_json
35
+ end
36
+
37
+ def bcc=(bcc)
38
+ @bcc = bcc
39
+ end
40
+
41
+ def bcc
42
+ @bcc.nil? ? nil : @bcc.to_json
43
+ end
44
+
45
+ def spam_check=(spam_check)
46
+ @spam_check = spam_check
47
+ end
48
+
49
+ def spam_check
50
+ @spam_check.nil? ? nil : @spam_check.to_json
51
+ end
52
+
53
+ def to_json(*)
54
+ {
55
+ 'bcc' => self.bcc,
56
+ 'bypass_list_management' => self.bypass_list_management,
57
+ 'footer' => self.footer,
58
+ 'sandbox_mode' => self.sandbox_mode,
59
+ 'spam_check' => self.spam_check
60
+ }.delete_if { |_, value| value.to_s.strip == '' }
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,33 @@
1
+ require 'json'
2
+
3
+ module SendGrid
4
+ class OpenTracking
5
+ def initialize(enable: nil, substitution_tag: nil)
6
+ @enable = enable
7
+ @substitution_tag = substitution_tag
8
+ end
9
+
10
+ def enable=(enable)
11
+ @enable = enable
12
+ end
13
+
14
+ def enable
15
+ @enable
16
+ end
17
+
18
+ def substitution_tag=(substitution_tag)
19
+ @substitution_tag = substitution_tag
20
+ end
21
+
22
+ def substitution_tag
23
+ @substitution_tag
24
+ end
25
+
26
+ def to_json(*)
27
+ {
28
+ 'enable' => self.enable,
29
+ 'substitution_tag' => self.substitution_tag
30
+ }.delete_if { |_, value| value.to_s.strip == '' }
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,75 @@
1
+ require 'json'
2
+
3
+ module SendGrid
4
+ class Personalization
5
+
6
+ attr_reader :tos, :ccs, :bccs, :headers, :substitutions, :custom_args
7
+
8
+ def initialize
9
+ @tos = []
10
+ @ccs = []
11
+ @bccs = []
12
+ @subject = nil
13
+ @headers = {}
14
+ @substitutions = {}
15
+ @custom_args = {}
16
+ @send_at = nil
17
+ end
18
+
19
+ def add_to(to)
20
+ @tos << to.to_json
21
+ end
22
+
23
+ def add_cc(cc)
24
+ @ccs << cc.to_json
25
+ end
26
+
27
+ def add_bcc(bcc)
28
+ @bccs << bcc.to_json
29
+ end
30
+
31
+ def subject=(subject)
32
+ @subject = subject
33
+ end
34
+
35
+ def subject
36
+ @subject
37
+ end
38
+
39
+ def add_header(header)
40
+ header = header.to_json
41
+ @headers = @headers.merge(header['header'])
42
+ end
43
+
44
+ def add_substitution(substitution)
45
+ substitution = substitution.to_json
46
+ @substitutions = @substitutions.merge(substitution['substitution'])
47
+ end
48
+
49
+ def add_custom_arg(custom_arg)
50
+ custom_arg = custom_arg.to_json
51
+ @custom_args = @custom_args.merge(custom_arg['custom_arg'])
52
+ end
53
+
54
+ def send_at=(send_at)
55
+ @send_at = send_at
56
+ end
57
+
58
+ def send_at
59
+ @send_at
60
+ end
61
+
62
+ def to_json(*)
63
+ {
64
+ 'to' => self.tos,
65
+ 'cc' => self.ccs,
66
+ 'bcc' => self.bccs,
67
+ 'subject' => self.subject,
68
+ 'headers' => self.headers,
69
+ 'substitutions' => self.substitutions,
70
+ 'custom_args' => self.custom_args,
71
+ 'send_at' => self.send_at
72
+ }.delete_if { |_, value| value.to_s.strip == '' || value == [] || value == {}}
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,24 @@
1
+ require 'json'
2
+
3
+ module SendGrid
4
+ class Section
5
+ def initialize(key: nil, value: nil)
6
+ @section = {}
7
+ (key.nil? || value.nil?) ? @section = nil : @section[key] = value
8
+ end
9
+
10
+ def section=(section)
11
+ @section = section
12
+ end
13
+
14
+ def section
15
+ @section
16
+ end
17
+
18
+ def to_json(*)
19
+ {
20
+ 'section' => self.section
21
+ }.delete_if { |_, value| value.to_s.strip == '' }
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,43 @@
1
+ require 'json'
2
+
3
+ module SendGrid
4
+ class SpamCheck
5
+ def initialize(enable: nil, threshold: nil, post_to_url: nil)
6
+ @enable = enable
7
+ @threshold = threshold
8
+ @post_to_url = post_to_url
9
+ end
10
+
11
+ def enable=(enable)
12
+ @enable = enable
13
+ end
14
+
15
+ def enable
16
+ @enable
17
+ end
18
+
19
+ def threshold=(threshold)
20
+ @threshold = threshold
21
+ end
22
+
23
+ def threshold
24
+ @threshold
25
+ end
26
+
27
+ def post_to_url=(post_to_url)
28
+ @post_to_url = post_to_url
29
+ end
30
+
31
+ def post_to_url
32
+ @post_to_url
33
+ end
34
+
35
+ def to_json(*)
36
+ {
37
+ 'enable' => self.enable,
38
+ 'threshold' => self.threshold,
39
+ 'post_to_url' => self.post_to_url
40
+ }.delete_if { |_, value| value.to_s.strip == '' }
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,53 @@
1
+ require 'json'
2
+
3
+ module SendGrid
4
+ class SubscriptionTracking
5
+ def initialize(enable: nil, text: nil, html: nil, substitution_tag: nil)
6
+ @enable = enable
7
+ @text = text
8
+ @html = html
9
+ @substitution_tag = substitution_tag
10
+ end
11
+
12
+ def enable=(enable)
13
+ @enable = enable
14
+ end
15
+
16
+ def enable
17
+ @enable
18
+ end
19
+
20
+ def text=(text)
21
+ @text = text
22
+ end
23
+
24
+ def text
25
+ @text
26
+ end
27
+
28
+ def html=(html)
29
+ @html = html
30
+ end
31
+
32
+ def html
33
+ @html
34
+ end
35
+
36
+ def substitution_tag=(substitution_tag)
37
+ @substitution_tag = substitution_tag
38
+ end
39
+
40
+ def substitution_tag
41
+ @substitution_tag
42
+ end
43
+
44
+ def to_json(*)
45
+ {
46
+ 'enable' => self.enable,
47
+ 'text' => self.text,
48
+ 'html' => self.html,
49
+ 'substitution_tag' => self.substitution_tag
50
+ }.delete_if { |_, value| value.to_s.strip == '' }
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,24 @@
1
+ require 'json'
2
+
3
+ module SendGrid
4
+ class Substitution
5
+ def initialize(key: nil, value: nil)
6
+ @substitution = {}
7
+ (key.nil? || value.nil?) ? @substitution = nil : @substitution[key] = value
8
+ end
9
+
10
+ def substitution=(substitution)
11
+ @substitution = substitution
12
+ end
13
+
14
+ def substitution
15
+ @substitution
16
+ end
17
+
18
+ def to_json(*)
19
+ {
20
+ 'substitution' => self.substitution
21
+ }.delete_if { |_, value| value.to_s.strip == '' }
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,53 @@
1
+ require 'json'
2
+
3
+ module SendGrid
4
+ class TrackingSettings
5
+ def initialize
6
+ @click_tracking = nil
7
+ @open_tracking = nil
8
+ @subscription_tracking = nil
9
+ @ganalytics = nil
10
+ end
11
+
12
+ def click_tracking=(click_tracking)
13
+ @click_tracking = click_tracking
14
+ end
15
+
16
+ def click_tracking
17
+ @click_tracking.nil? ? nil : @click_tracking.to_json
18
+ end
19
+
20
+ def open_tracking=(open_tracking)
21
+ @open_tracking = open_tracking
22
+ end
23
+
24
+ def open_tracking
25
+ @open_tracking.nil? ? nil : @open_tracking.to_json
26
+ end
27
+
28
+ def subscription_tracking=(subscription_tracking)
29
+ @subscription_tracking = subscription_tracking
30
+ end
31
+
32
+ def subscription_tracking
33
+ @subscription_tracking.nil? ? nil : @subscription_tracking.to_json
34
+ end
35
+
36
+ def ganalytics=(ganalytics)
37
+ @ganalytics = ganalytics
38
+ end
39
+
40
+ def ganalytics
41
+ @ganalytics.nil? ? nil : @ganalytics.to_json
42
+ end
43
+
44
+ def to_json(*)
45
+ {
46
+ 'click_tracking' => self.click_tracking,
47
+ 'open_tracking' => self.open_tracking,
48
+ 'subscription_tracking' => self.subscription_tracking,
49
+ 'ganalytics' => self.ganalytics
50
+ }.delete_if { |_, value| value.to_s.strip == '' }
51
+ end
52
+ end
53
+ end
@@ -1,3 +1,3 @@
1
1
  module SendGrid
2
- VERSION = '4.3.3'
2
+ VERSION = '5.0.0'
3
3
  end
@@ -0,0 +1,27 @@
1
+ require_relative '../../../../lib/sendgrid/helpers/mail/mail'
2
+ require 'minitest/autorun'
3
+
4
+ class TestCategory < Minitest::Test
5
+
6
+ include SendGrid
7
+
8
+ def setup
9
+ @category = Category.new(name: 'foo')
10
+ end
11
+
12
+ def test_aliases
13
+ assert_equal @category.method(:name), @category.method(:category)
14
+ end
15
+
16
+ def test_name
17
+ assert_equal @category.name, 'foo'
18
+ end
19
+
20
+ def test_to_json
21
+ expected_json = {
22
+ 'category' => 'foo'
23
+ }
24
+ assert_equal @category.to_json, expected_json
25
+ end
26
+
27
+ end
@@ -9,88 +9,88 @@ class TestMail < Minitest::Test
9
9
  end
10
10
 
11
11
  def test_hello_world
12
- mail = Mail.new()
12
+ mail = Mail.new
13
13
  mail.from = Email.new(email: "test@example.com")
14
14
  mail.subject = "Hello World from the SendGrid Ruby Library"
15
- personalization = Personalization.new()
16
- personalization.to = Email.new(email: "test@example.com")
17
- mail.personalizations = personalization
18
- mail.contents = Content.new(type: "text/plain", value: "some text here")
19
- mail.contents = Content.new(type: "text/html", value: "<html><body>some text here</body></html>")
15
+ personalization = Personalization.new
16
+ personalization.add_to(Email.new(email: "test@example.com"))
17
+ mail.add_personalization(personalization)
18
+ mail.add_content(Content.new(type: "text/plain", value: "some text here"))
19
+ mail.add_content(Content.new(type: "text/html", value: "<html><body>some text here</body></html>"))
20
20
  assert_equal(mail.to_json, JSON.parse('{"content":[{"type":"text/plain","value":"some text here"},{"type":"text/html","value":"<html><body>some text here</body></html>"}],"from":{"email":"test@example.com"},"personalizations":[{"to":[{"email":"test@example.com"}]}],"subject":"Hello World from the SendGrid Ruby Library"}'))
21
21
  end
22
22
 
23
23
  def test_kitchen_sink
24
- mail = Mail.new()
24
+ mail = Mail.new
25
25
  mail.from = Email.new(email: "test@example.com")
26
26
  mail.subject = "Hello World from the SendGrid Ruby Library"
27
- personalization = Personalization.new()
28
- personalization.to = Email.new(email: "test@example.com", name: "Example User")
29
- personalization.to = Email.new(email: "test@example.com", name: "Example User")
30
- personalization.cc = Email.new(email: "test@example.com", name: "Example User")
31
- personalization.cc = Email.new(email: "test@example.com", name: "Example User")
32
- personalization.bcc = Email.new(email: "test@example.com", name: "Example User")
33
- personalization.bcc = Email.new(email: "test@example.com", name: "Example User")
27
+ personalization = Personalization.new
28
+ personalization.add_to(Email.new(email: "test@example.com", name: "Example User"))
29
+ personalization.add_to(Email.new(email: "test@example.com", name: "Example User"))
30
+ personalization.add_cc(Email.new(email: "test@example.com", name: "Example User"))
31
+ personalization.add_cc(Email.new(email: "test@example.com", name: "Example User"))
32
+ personalization.add_bcc(Email.new(email: "test@example.com", name: "Example User"))
33
+ personalization.add_bcc(Email.new(email: "test@example.com", name: "Example User"))
34
34
  personalization.subject = "Hello World from the Personalized SendGrid Python Library"
35
- personalization.headers = Header.new(key: "X-Test", value: "True")
36
- personalization.headers = Header.new(key: "X-Mock", value: "False")
37
- personalization.substitutions = Substitution.new(key: "%name%", value: "Example User")
38
- personalization.substitutions = Substitution.new(key: "%city%", value: "Denver")
39
- personalization.custom_args = CustomArg.new(key: "user_id", value: "343")
40
- personalization.custom_args = CustomArg.new(key: "type", value: "marketing")
35
+ personalization.add_header(Header.new(key: "X-Test", value: "True"))
36
+ personalization.add_header(Header.new(key: "X-Mock", value: "False"))
37
+ personalization.add_substitution(Substitution.new(key: "%name%", value: "Example User"))
38
+ personalization.add_substitution(Substitution.new(key: "%city%", value: "Denver"))
39
+ personalization.add_custom_arg(CustomArg.new(key: "user_id", value: "343"))
40
+ personalization.add_custom_arg(CustomArg.new(key: "type", value: "marketing"))
41
41
  personalization.send_at = 1443636843
42
- mail.personalizations = personalization
43
-
44
- personalization2 = Personalization.new()
45
- personalization2.to = Email.new(email: "test@example.com", name: "Example User")
46
- personalization2.to = Email.new(email: "test@example.com", name: "Example User")
47
- personalization2.cc = Email.new(email: "test@example.com", name: "Example User")
48
- personalization2.cc = Email.new(email: "test@example.com", name: "Example User")
49
- personalization2.bcc = Email.new(email: "test@example.com", name: "Example User")
50
- personalization2.bcc = Email.new(email: "test@example.com", name: "Example User")
42
+ mail.add_personalization(personalization)
43
+
44
+ personalization2 = Personalization.new
45
+ personalization2.add_to(Email.new(email: "test@example.com", name: "Example User"))
46
+ personalization2.add_to(Email.new(email: "test@example.com", name: "Example User"))
47
+ personalization2.add_cc(Email.new(email: "test@example.com", name: "Example User"))
48
+ personalization2.add_cc(Email.new(email: "test@example.com", name: "Example User"))
49
+ personalization2.add_bcc(Email.new(email: "test@example.com", name: "Example User"))
50
+ personalization2.add_bcc(Email.new(email: "test@example.com", name: "Example User"))
51
51
  personalization2.subject = "Hello World from the Personalized SendGrid Python Library"
52
- personalization2.headers = Header.new(key: "X-Test", value: "True")
53
- personalization2.headers = Header.new(key: "X-Mock", value: "False")
54
- personalization2.substitutions = Substitution.new(key: "%name%", value: "Example User")
55
- personalization2.substitutions = Substitution.new(key: "%city%", value: "Denver")
56
- personalization2.custom_args = CustomArg.new(key: "user_id", value: "343")
57
- personalization2.custom_args = CustomArg.new(key: "type", value: "marketing")
52
+ personalization2.add_header(Header.new(key: "X-Test", value: "True"))
53
+ personalization2.add_header(Header.new(key: "X-Mock", value: "False"))
54
+ personalization2.add_substitution(Substitution.new(key: "%name%", value: "Example User"))
55
+ personalization2.add_substitution(Substitution.new(key: "%city%", value: "Denver"))
56
+ personalization2.add_custom_arg(CustomArg.new(key: "user_id", value: "343"))
57
+ personalization2.add_custom_arg(CustomArg.new(key: "type", value: "marketing"))
58
58
  personalization2.send_at = 1443636843
59
- mail.personalizations = personalization2
59
+ mail.add_personalization(personalization2)
60
60
 
61
- mail.contents = Content.new(type: "text/plain", value: "some text here")
62
- mail.contents = Content.new(type: "text/html", value: "<html><body>some text here</body></html>")
61
+ mail.add_content(Content.new(type: "text/plain", value: "some text here"))
62
+ mail.add_content(Content.new(type: "text/html", value: "<html><body>some text here</body></html>"))
63
63
 
64
- attachment = Attachment.new()
64
+ attachment = Attachment.new
65
65
  attachment.content = "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12"
66
66
  attachment.type = "application/pdf"
67
67
  attachment.filename = "balance_001.pdf"
68
68
  attachment.disposition = "attachment"
69
69
  attachment.content_id = "Balance Sheet"
70
70
 
71
- mail.attachments = attachment
71
+ mail.add_attachment(attachment)
72
72
 
73
- attachment2 = Attachment.new()
73
+ attachment2 = Attachment.new
74
74
  attachment2.content = "BwdW"
75
75
  attachment2.type = "image/png"
76
76
  attachment2.filename = "banner.png"
77
77
  attachment2.disposition = "inline"
78
78
  attachment2.content_id = "Banner"
79
- mail.attachments = attachment2
79
+ mail.add_attachment(attachment2)
80
80
 
81
81
  mail.template_id = "13b8f94f-bcae-4ec6-b752-70d6cb59f932"
82
82
 
83
- mail.sections = Section.new(key: "%section1%", value: "Substitution Text for Section 1")
84
- mail.sections = Section.new(key: "%section2%", value: "Substitution Text for Section 2")
83
+ mail.add_section(Section.new(key: "%section1%", value: "Substitution Text for Section 1"))
84
+ mail.add_section(Section.new(key: "%section2%", value: "Substitution Text for Section 2"))
85
85
 
86
- mail.headers = Header.new(key: "X-Test3", value: "test3")
87
- mail.headers = Header.new(key: "X-Test4", value: "test4")
86
+ mail.add_header(Header.new(key: "X-Test3", value: "test3"))
87
+ mail.add_header(Header.new(key: "X-Test4", value: "test4"))
88
88
 
89
- mail.categories = Category.new(name: "May")
90
- mail.categories = Category.new(name: "2016")
89
+ mail.add_category(Category.new(name: "May"))
90
+ mail.add_category(Category.new(name: "2016"))
91
91
 
92
- mail.custom_args = CustomArg.new(key: "campaign", value: "welcome")
93
- mail.custom_args = CustomArg.new(key: "weekday", value: "morning")
92
+ mail.add_custom_arg(CustomArg.new(key: "campaign", value: "welcome"))
93
+ mail.add_custom_arg(CustomArg.new(key: "weekday", value: "morning"))
94
94
 
95
95
  mail.send_at = 1443636842
96
96
 
@@ -100,7 +100,7 @@ class TestMail < Minitest::Test
100
100
 
101
101
  mail.ip_pool_name = "23"
102
102
 
103
- mail_settings = MailSettings.new()
103
+ mail_settings = MailSettings.new
104
104
  mail_settings.bcc = BccSettings.new(enable: true, email: "test@example.com")
105
105
  mail_settings.bypass_list_management = BypassListManagement.new(enable: true)
106
106
  mail_settings.footer = Footer.new(enable: true, text: "Footer Text", html: "<html><body>Footer Text</body></html>")
@@ -108,7 +108,7 @@ class TestMail < Minitest::Test
108
108
  mail_settings.spam_check = SpamCheck.new(enable: true, threshold: 1, post_to_url: "https://spamcatcher.sendgrid.com")
109
109
  mail.mail_settings = mail_settings
110
110
 
111
- tracking_settings = TrackingSettings.new()
111
+ tracking_settings = TrackingSettings.new
112
112
  tracking_settings.click_tracking = ClickTracking.new(enable: false, enable_text: false)
113
113
  tracking_settings.open_tracking = OpenTracking.new(enable: true, substitution_tag: "Optional tag to replace with the open image in the body of the message")
114
114
  tracking_settings.subscription_tracking = SubscriptionTracking.new(enable: true, text: "text to insert into the text/plain portion of the message", html: "html to insert into the text/html portion of the message", substitution_tag: "Optional tag to replace with the open image in the body of the message")
@@ -119,4 +119,122 @@ class TestMail < Minitest::Test
119
119
 
120
120
  assert_equal(mail.to_json, JSON.parse('{"asm":{"group_id":99,"groups_to_display":[4,5,6,7,8]},"attachments":[{"content":"TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12","content_id":"Balance Sheet","disposition":"attachment","filename":"balance_001.pdf","type":"application/pdf"},{"content":"BwdW","content_id":"Banner","disposition":"inline","filename":"banner.png","type":"image/png"}],"batch_id":"sendgrid_batch_id","categories":["May","2016"],"content":[{"type":"text/plain","value":"some text here"},{"type":"text/html","value":"<html><body>some text here</body></html>"}],"custom_args":{"campaign":"welcome","weekday":"morning"},"from":{"email":"test@example.com"},"headers":{"X-Test3":"test3","X-Test4":"test4"},"ip_pool_name":"23","mail_settings":{"bcc":{"email":"test@example.com","enable":true},"bypass_list_management":{"enable":true},"footer":{"enable":true,"html":"<html><body>Footer Text</body></html>","text":"Footer Text"},"sandbox_mode":{"enable":true},"spam_check":{"enable":true,"post_to_url":"https://spamcatcher.sendgrid.com","threshold":1}},"personalizations":[{"bcc":[{"email":"test@example.com","name":"Example User"},{"email":"test@example.com","name":"Example User"}],"cc":[{"email":"test@example.com","name":"Example User"},{"email":"test@example.com","name":"Example User"}],"custom_args":{"type":"marketing","user_id":"343"},"headers":{"X-Mock":"False","X-Test":"True"},"send_at":1443636843,"subject":"Hello World from the Personalized SendGrid Python Library","substitutions":{"%city%":"Denver","%name%":"Example User"},"to":[{"email":"test@example.com","name":"Example User"},{"email":"test@example.com","name":"Example User"}]},{"bcc":[{"email":"test@example.com","name":"Example User"},{"email":"test@example.com","name":"Example User"}],"cc":[{"email":"test@example.com","name":"Example User"},{"email":"test@example.com","name":"Example User"}],"custom_args":{"type":"marketing","user_id":"343"},"headers":{"X-Mock":"False","X-Test":"True"},"send_at":1443636843,"subject":"Hello World from the Personalized SendGrid Python Library","substitutions":{"%city%":"Denver","%name%":"Example User"},"to":[{"email":"test@example.com","name":"Example User"},{"email":"test@example.com","name":"Example User"}]}],"reply_to":{"email":"test@example.com"},"sections":{"%section1%":"Substitution Text for Section 1","%section2%":"Substitution Text for Section 2"},"send_at":1443636842,"subject":"Hello World from the SendGrid Ruby Library","template_id":"13b8f94f-bcae-4ec6-b752-70d6cb59f932","tracking_settings":{"click_tracking":{"enable":false,"enable_text":false},"ganalytics":{"enable":true,"utm_campaign":"some campaign","utm_content":"some content","utm_medium":"some medium","utm_source":"some source","utm_term":"some term"},"open_tracking":{"enable":true,"substitution_tag":"Optional tag to replace with the open image in the body of the message"},"subscription_tracking":{"enable":true,"html":"html to insert into the text/html portion of the message","substitution_tag":"Optional tag to replace with the open image in the body of the message","text":"text to insert into the text/plain portion of the message"}}}'))
121
121
  end
122
+
123
+ def test_that_personalizations_is_empty_initially
124
+ mail = Mail.new
125
+ assert_equal([], mail.personalizations)
126
+ end
127
+
128
+ def test_that_contents_is_empty_initially
129
+ mail = Mail.new
130
+ assert_equal([], mail.contents)
131
+ end
132
+
133
+ def test_that_attachments_is_empty_initially
134
+ mail = Mail.new
135
+ assert_equal([], mail.attachments)
136
+ end
137
+
138
+ def test_that_categories_is_empty_initially
139
+ mail = Mail.new
140
+ assert_equal([], mail.categories)
141
+ end
142
+
143
+ def test_add_personalization
144
+ mail = Mail.new
145
+ mail.add_personalization('foo')
146
+ assert_equal(['foo'.to_json], mail.personalizations)
147
+ end
148
+
149
+ def test_add_content
150
+ mail = Mail.new
151
+ mail.add_content('foo')
152
+ assert_equal(['foo'.to_json], mail.contents)
153
+ end
154
+
155
+ def test_add_section
156
+ mail = Mail.new
157
+ mail.add_section(Section.new(key: '%section1%', value: 'Substitution Text for Section 1'))
158
+ expected_json = {
159
+ "sections"=>{
160
+ "%section1%"=>"Substitution Text for Section 1"
161
+ }
162
+ }
163
+ assert_equal mail.to_json, expected_json
164
+ mail.add_section(Section.new(key: '%section2%', value: 'Substitution Text for Section 2'))
165
+ expected_json = {
166
+ "sections"=>{
167
+ "%section1%"=>"Substitution Text for Section 1",
168
+ "%section2%"=>"Substitution Text for Section 2"
169
+ }
170
+ }
171
+ assert_equal mail.to_json, expected_json
172
+ end
173
+
174
+ def test_add_header
175
+ mail = Mail.new
176
+ mail.add_header(Header.new(key: 'X-Test3', value: 'test3'))
177
+ expected_json = {
178
+ "headers"=>{
179
+ "X-Test3"=>"test3"
180
+ }
181
+ }
182
+ assert_equal mail.to_json, expected_json
183
+ mail.add_header(Header.new(key: 'X-Test4', value: 'test4'))
184
+ expected_json = {
185
+ "headers"=>{
186
+ "X-Test3"=>"test3",
187
+ "X-Test4"=>"test4"
188
+ }
189
+ }
190
+ assert_equal mail.to_json, expected_json
191
+ end
192
+
193
+ def test_add_custom_arg
194
+ mail = Mail.new
195
+ mail.add_custom_arg(CustomArg.new(key: 'campaign 1', value: 'welcome 1'))
196
+ expected_json = {
197
+ "custom_args"=>{
198
+ "campaign 1"=>"welcome 1"
199
+ }
200
+ }
201
+ assert_equal mail.to_json, expected_json
202
+ mail.add_custom_arg(CustomArg.new(key: 'campaign 2', value: 'welcome 2'))
203
+ expected_json = {
204
+ "custom_args"=>{
205
+ "campaign 1"=>"welcome 1",
206
+ "campaign 2"=>"welcome 2"
207
+ }
208
+ }
209
+ assert_equal mail.to_json, expected_json
210
+ end
211
+
212
+ def test_add_attachment
213
+ mail = Mail.new
214
+ mail.add_attachment('foo')
215
+ assert_equal(['foo'.to_json], mail.attachments)
216
+ end
217
+
218
+ def test_add_valid_category
219
+ mail = Mail.new
220
+ category = Category.new(name: 'foo')
221
+ mail.add_category(category)
222
+ assert_equal(['foo'], mail.categories)
223
+ end
224
+
225
+ def test_add_more_than_1_valid_category
226
+ mail = Mail.new
227
+ category_1 = Category.new(name: 'foo')
228
+ category_2 = Category.new(name: 'bar')
229
+ mail.add_category(category_1)
230
+ mail.add_category(category_2)
231
+ assert_equal(['foo', 'bar'], mail.categories)
232
+ end
233
+
234
+ def test_add_invalid_category
235
+ mail = Mail.new
236
+ assert_raises(NoMethodError) do
237
+ mail.add_category('foo')
238
+ end
239
+ end
122
240
  end