sendgrid-ruby 1.1.6 → 6.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (136) hide show
  1. checksums.yaml +5 -5
  2. data/.codeclimate.yml +21 -0
  3. data/.env_sample +1 -3
  4. data/.gitignore +1 -0
  5. data/.travis.yml +28 -17
  6. data/CHANGELOG.md +231 -1
  7. data/CODE_OF_CONDUCT.md +73 -0
  8. data/CONTRIBUTING.md +214 -0
  9. data/Gemfile +5 -4
  10. data/ISSUE_TEMPLATE.md +26 -0
  11. data/LICENSE.md +21 -0
  12. data/Makefile +7 -0
  13. data/PULL_REQUEST_TEMPLATE.md +31 -0
  14. data/README.md +161 -225
  15. data/Rakefile +9 -2
  16. data/TROUBLESHOOTING.md +137 -0
  17. data/UPGRADE.md +5 -0
  18. data/USAGE.md +5037 -0
  19. data/USE_CASES.md +377 -0
  20. data/config.ru +4 -0
  21. data/docker/Dockerfile +12 -0
  22. data/docker/README.md +30 -0
  23. data/examples/accesssettings/accesssettings.rb +83 -0
  24. data/examples/alerts/alerts.rb +62 -0
  25. data/examples/apikeys/apikeys.rb +84 -0
  26. data/examples/asm/asm.rb +173 -0
  27. data/examples/browsers/browsers.rb +16 -0
  28. data/examples/campaigns/campaigns.rb +153 -0
  29. data/examples/categories/categories.rb +36 -0
  30. data/examples/clients/clients.rb +27 -0
  31. data/examples/contactdb/contactdb.rb +395 -0
  32. data/examples/devices/devices.rb +16 -0
  33. data/examples/geo/geo.rb +16 -0
  34. data/examples/helpers/mail/example.rb +147 -0
  35. data/examples/helpers/settings/example.rb +23 -0
  36. data/examples/helpers/stats/example.rb +42 -0
  37. data/examples/ips/ips.rb +167 -0
  38. data/examples/mail/mail.rb +173 -0
  39. data/examples/mailboxproviders/mailboxproviders.rb +16 -0
  40. data/examples/mailsettings/mailsettings.rb +219 -0
  41. data/examples/partnersettings/partnersettings.rb +39 -0
  42. data/examples/scopes/scopes.rb +61 -0
  43. data/examples/senderauthentication/senderauthentication.rb +310 -0
  44. data/examples/senders/senders.rb +98 -0
  45. data/examples/stats/stats.rb +16 -0
  46. data/examples/subusers/subusers.rb +169 -0
  47. data/examples/suppression/suppression.rb +201 -0
  48. data/examples/templates/templates.rb +129 -0
  49. data/examples/trackingsettings/trackingsettings.rb +110 -0
  50. data/examples/user/user.rb +293 -0
  51. data/gemfiles/Sinatra_1.gemfile +6 -0
  52. data/gemfiles/Sinatra_2.gemfile +6 -0
  53. data/lib/sendgrid/client.rb +33 -57
  54. data/lib/sendgrid/helpers/inbound/README.md +98 -0
  55. data/lib/sendgrid/helpers/inbound/app.rb +32 -0
  56. data/lib/sendgrid/helpers/inbound/config.yml +26 -0
  57. data/lib/sendgrid/helpers/inbound/public/index.html +10 -0
  58. data/lib/sendgrid/helpers/inbound/sample_data/default_data.txt +58 -0
  59. data/lib/sendgrid/helpers/inbound/sample_data/raw_data.txt +57 -0
  60. data/lib/sendgrid/helpers/inbound/sample_data/raw_data_with_attachments.txt +298 -0
  61. data/lib/sendgrid/helpers/inbound/send.rb +26 -0
  62. data/lib/sendgrid/helpers/ip_management/ip_management.rb +17 -0
  63. data/lib/sendgrid/helpers/mail/README.md +14 -0
  64. data/lib/sendgrid/helpers/mail/asm.rb +33 -0
  65. data/lib/sendgrid/helpers/mail/attachment.rb +86 -0
  66. data/lib/sendgrid/helpers/mail/bcc_settings.rb +33 -0
  67. data/lib/sendgrid/helpers/mail/bypass_list_management.rb +43 -0
  68. data/lib/sendgrid/helpers/mail/category.rb +20 -0
  69. data/lib/sendgrid/helpers/mail/click_tracking.rb +33 -0
  70. data/lib/sendgrid/helpers/mail/content.rb +20 -0
  71. data/lib/sendgrid/helpers/mail/custom_arg.rb +24 -0
  72. data/lib/sendgrid/helpers/mail/email.rb +29 -0
  73. data/lib/sendgrid/helpers/mail/footer.rb +43 -0
  74. data/lib/sendgrid/helpers/mail/ganalytics.rb +74 -0
  75. data/lib/sendgrid/helpers/mail/header.rb +24 -0
  76. data/lib/sendgrid/helpers/mail/mail.rb +140 -0
  77. data/lib/sendgrid/helpers/mail/mail_settings.rb +63 -0
  78. data/lib/sendgrid/helpers/mail/open_tracking.rb +33 -0
  79. data/lib/sendgrid/helpers/mail/personalization.rb +82 -0
  80. data/lib/sendgrid/helpers/mail/section.rb +24 -0
  81. data/lib/sendgrid/helpers/mail/spam_check.rb +43 -0
  82. data/lib/sendgrid/helpers/mail/subscription_tracking.rb +53 -0
  83. data/lib/sendgrid/helpers/mail/substitution.rb +24 -0
  84. data/lib/sendgrid/helpers/mail/tracking_settings.rb +53 -0
  85. data/lib/sendgrid/helpers/permissions/scope.rb +28 -0
  86. data/lib/sendgrid/helpers/permissions/scopes.yml +309 -0
  87. data/lib/sendgrid/helpers/settings/README.md +14 -0
  88. data/lib/sendgrid/helpers/settings/mail_settings_dto.rb +13 -0
  89. data/lib/sendgrid/helpers/settings/partner_settings_dto.rb +13 -0
  90. data/lib/sendgrid/helpers/settings/settings.rb +28 -0
  91. data/lib/sendgrid/helpers/settings/tracking_settings_dto.rb +24 -0
  92. data/lib/sendgrid/helpers/settings/user_settings_dto.rb +13 -0
  93. data/lib/sendgrid/helpers/stats/email_stats.rb +46 -0
  94. data/lib/sendgrid/helpers/stats/metrics.rb +35 -0
  95. data/lib/sendgrid/helpers/stats/stats_response.rb +31 -0
  96. data/lib/sendgrid/version.rb +1 -1
  97. data/lib/sendgrid-ruby.rb +27 -6
  98. data/mail_helper_v3.md +390 -0
  99. data/sendgrid-ruby.gemspec +13 -17
  100. data/spec/sendgrid/helpers/ip_management/ip_management_spec.rb +12 -0
  101. data/spec/sendgrid/helpers/settings/mail_settings_dto_spec.rb +32 -0
  102. data/spec/sendgrid/helpers/settings/partner_settings_dto_spec.rb +24 -0
  103. data/spec/sendgrid/helpers/settings/settings_spec.rb +25 -0
  104. data/spec/sendgrid/helpers/settings/tracking_settings_dto_spec.rb +27 -0
  105. data/spec/sendgrid/helpers/settings/user_settings_dto_spec.rb +24 -0
  106. data/spec/sendgrid/helpers/stats/email_stats_spec.rb +112 -0
  107. data/spec/sendgrid/helpers/stats/metrics_spec.rb +46 -0
  108. data/spec/sendgrid/helpers/stats/stats_response_spec.rb +76 -0
  109. data/spec/spec_helper.rb +10 -1
  110. data/test/prism.sh +42 -0
  111. data/test/sendgrid/helpers/mail/test_attachment.rb +35 -0
  112. data/test/sendgrid/helpers/mail/test_category.rb +27 -0
  113. data/test/sendgrid/helpers/mail/test_email.rb +34 -0
  114. data/test/sendgrid/helpers/mail/test_mail.rb +261 -0
  115. data/test/sendgrid/helpers/mail/test_personalizations.rb +161 -0
  116. data/test/sendgrid/permissions/test_scopes.rb +38 -0
  117. data/test/sendgrid/test_sendgrid-ruby.rb +2751 -0
  118. metadata +149 -115
  119. data/.rspec +0 -2
  120. data/.rubocop.yml +0 -30
  121. data/FETCH_HEAD +0 -0
  122. data/Guardfile +0 -10
  123. data/LICENSE.txt +0 -22
  124. data/example.rb +0 -41
  125. data/lib/sendgrid/exceptions.rb +0 -7
  126. data/lib/sendgrid/mail.rb +0 -182
  127. data/lib/sendgrid/recipient.rb +0 -29
  128. data/lib/sendgrid/response.rb +0 -14
  129. data/lib/sendgrid/template.rb +0 -26
  130. data/lib/sendgrid/template_mailer.rb +0 -59
  131. data/spec/lib/sendgrid/client_spec.rb +0 -87
  132. data/spec/lib/sendgrid/mail_spec.rb +0 -151
  133. data/spec/lib/sendgrid/recipient_spec.rb +0 -91
  134. data/spec/lib/sendgrid/template_mailer_spec.rb +0 -86
  135. data/spec/lib/sendgrid/template_spec.rb +0 -61
  136. data/spec/lib/sendgrid_spec.rb +0 -7
@@ -0,0 +1,112 @@
1
+ require 'spec_helper'
2
+
3
+ describe SendGrid::EmailStats do
4
+ let(:sg_client) { SendGrid::API.new(api_key: 'abcd').client }
5
+ let(:stats) { SendGrid::EmailStats.new(sendgrid_client: sg_client) }
6
+ let(:sg_response) { double('SendGrid::Response') }
7
+
8
+ let(:sample_response) do
9
+ [{
10
+ "date" => "2017-10-01",
11
+ "stats" => [
12
+ {"metrics" =>
13
+ {
14
+ "blocks" => 101,
15
+ "bounce_drops" => 102,
16
+ "bounces" => 103,
17
+ "clicks" => 104,
18
+ "deferred" => 105,
19
+ "delivered" => 106,
20
+ "invalid_emails" => 107,
21
+ "opens" => 108,
22
+ "processed" => 109,
23
+ "requests" => 110,
24
+ "spam_report_drops" => 111,
25
+ "spam_reports" => 112,
26
+ "unique_clicks" => 113,
27
+ "unique_opens" => 114,
28
+ "unsubscribe_drops" => 115,
29
+ "unsubscribes" => 116
30
+ }
31
+ }
32
+ ]
33
+ }]
34
+ end
35
+
36
+ let(:error_response) do
37
+ {
38
+ "errors" => [
39
+ {
40
+ "message" => "end_date should be a YYYY-MM-DD formatted date"
41
+ }
42
+ ]
43
+ }
44
+ end
45
+
46
+ describe '.new' do
47
+ it 'initializes with SendGrid::Client' do
48
+ expect(stats).to be_a SendGrid::EmailStats
49
+ end
50
+ end
51
+
52
+ describe 'successful response' do
53
+ before do
54
+ allow_any_instance_of(SendGrid::Response).to receive(:body) { sample_response.to_json }
55
+ end
56
+
57
+ describe '#by_day' do
58
+ it 'fetches data aggregated by day' do
59
+ day_stats = stats.by_day('2017-10-01', '2017-10-02')
60
+ day_metrics = day_stats.metrics.first
61
+
62
+ expect(day_metrics).to be_a SendGrid::Metrics
63
+ expect(day_metrics.date.to_s).to eq('2017-10-01')
64
+ expect(day_metrics.requests).to eq(110)
65
+ expect(day_metrics.clicks).to eq(104)
66
+ expect(day_metrics.bounces).to eq(103)
67
+ expect(day_metrics.opens).to eq(108)
68
+ end
69
+ end
70
+
71
+ describe '#by_week' do
72
+ it 'fetches data aggregated by week' do
73
+ day_stats = stats.by_week('2017-10-01', '2017-10-12')
74
+ day_metrics = day_stats.metrics.first
75
+
76
+ expect(day_metrics).to be_a SendGrid::Metrics
77
+ expect(day_metrics.date.to_s).to eq('2017-10-01')
78
+ expect(day_metrics.requests).to eq(110)
79
+ expect(day_metrics.clicks).to eq(104)
80
+ expect(day_metrics.bounces).to eq(103)
81
+ expect(day_metrics.opens).to eq(108)
82
+ end
83
+ end
84
+
85
+ describe '#by_month' do
86
+ it 'fetches data aggregated by month' do
87
+ day_stats = stats.by_month('2017-10-01', '2017-11-01')
88
+ day_metrics = day_stats.metrics.first
89
+
90
+ expect(day_metrics).to be_a SendGrid::Metrics
91
+ expect(day_metrics.date.to_s).to eq('2017-10-01')
92
+ expect(day_metrics.requests).to eq(110)
93
+ expect(day_metrics.clicks).to eq(104)
94
+ expect(day_metrics.bounces).to eq(103)
95
+ expect(day_metrics.opens).to eq(108)
96
+ end
97
+ end
98
+ end
99
+
100
+ describe 'error response' do
101
+ before do
102
+ allow_any_instance_of(SendGrid::Response).to receive(:body) { error_response.to_json }
103
+ end
104
+
105
+ it 'fetches data aggregated by month' do
106
+ day_stats = stats.by_month('2017-10-01', '2017-10-02')
107
+
108
+ expect(day_stats.errors).to include('end_date should be a YYYY-MM-DD formatted date')
109
+ expect(day_stats.error?).to be_truthy
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe SendGrid::Metrics do
4
+ let(:params) do
5
+ {
6
+ "date" => "2017-10-01",
7
+ "blocks" => 101,
8
+ "bounce_drops" => 102,
9
+ "bounces" => 103,
10
+ "clicks" => 104,
11
+ "deferred" => 105,
12
+ "delivered" => 106,
13
+ "invalid_emails" => 107,
14
+ "opens" => 108,
15
+ "processed" => 109,
16
+ "requests" => 110,
17
+ "spam_report_drops" => 111,
18
+ "spam_reports" => 112,
19
+ "unique_clicks" => 113,
20
+ "unique_opens" => 114,
21
+ "unsubscribe_drops" => 115,
22
+ "unsubscribes" => 116
23
+ }
24
+ end
25
+
26
+ subject { described_class.new(params) }
27
+
28
+ it 'initializes with metric parameters' do
29
+ expect(subject).to be_a SendGrid::Metrics
30
+ end
31
+
32
+ it 'returns date as object' do
33
+ expect(subject.date).to be_a Date
34
+ end
35
+
36
+ %w(
37
+ blocks bounce_drops bounces clicks deferred delivered invalid_emails
38
+ opens processed requests spam_report_drops spam_reports unique_clicks
39
+ unique_opens unsubscribe_drops unsubscribes
40
+ ).each do |attribute|
41
+ it "responds to #{attribute}" do
42
+ expect(subject).to respond_to(attribute.to_sym)
43
+ end
44
+ end
45
+
46
+ end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ describe SendGrid::StatsResponse do
4
+ let(:params) do
5
+ [{
6
+ "date" => "2017-10-01",
7
+ "stats" => [
8
+ {"metrics" =>
9
+ {
10
+ "blocks" => 101,
11
+ "bounce_drops" => 102,
12
+ "bounces" => 103,
13
+ "clicks" => 104,
14
+ "deferred" => 105,
15
+ "delivered" => 106,
16
+ "invalid_emails" => 107,
17
+ "opens" => 108,
18
+ "processed" => 109,
19
+ "requests" => 110,
20
+ "spam_report_drops" => 111,
21
+ "spam_reports" => 112,
22
+ "unique_clicks" => 113,
23
+ "unique_opens" => 114,
24
+ "unsubscribe_drops" => 115,
25
+ "unsubscribes" => 116
26
+ }
27
+ }
28
+ ]
29
+ }]
30
+ end
31
+
32
+ subject { described_class.new(params) }
33
+
34
+ it 'initialized with JSON response body' do
35
+ expect(subject).to be_a SendGrid::StatsResponse
36
+ end
37
+
38
+ describe '#metrics' do
39
+ it 'returns an array of metrics' do
40
+ metric = subject.metrics.first
41
+
42
+ expect(subject.metrics).to be_a Array
43
+ expect(metric.date.to_s).to eq('2017-10-01')
44
+ expect(metric.requests).to eq(110)
45
+ expect(metric.clicks).to eq(104)
46
+ expect(metric.bounces).to eq(103)
47
+ expect(metric.opens).to eq(108)
48
+ end
49
+ end
50
+
51
+ context 'errors' do
52
+ let(:error_params) do
53
+ {
54
+ "errors" => [
55
+ {
56
+ "message" => "end_date should be a YYYY-MM-DD formatted date"
57
+ }
58
+ ]
59
+ }
60
+ end
61
+
62
+ subject { described_class.new(error_params) }
63
+
64
+ describe '#errors' do
65
+ it 'shows a list of errors' do
66
+ expect(subject.errors).to include('end_date should be a YYYY-MM-DD formatted date')
67
+ end
68
+ end
69
+
70
+ describe '#error?' do
71
+ it 'returns true if there is an error' do
72
+ expect(subject.error?).to be_truthy
73
+ end
74
+ end
75
+ end
76
+ end
data/spec/spec_helper.rb CHANGED
@@ -1 +1,10 @@
1
- require_relative '../lib/sendgrid-ruby'
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'pry'
4
+ require 'faker'
5
+
6
+ RSpec.configure do |config|
7
+ Dir["#{File.dirname(__FILE__)}/../lib/sendgrid-ruby.rb"].sort.each { |ext| require ext }
8
+
9
+ config.color = true
10
+ end
data/test/prism.sh ADDED
@@ -0,0 +1,42 @@
1
+ #!/bin/bash
2
+
3
+ install () {
4
+
5
+ set -eu
6
+
7
+ UNAME=$(uname)
8
+ ARCH=$(uname -m)
9
+ if [ "$UNAME" != "Linux" ] && [ "$UNAME" != "Darwin" ] && [ "$ARCH" != "x86_64" ] && [ "$ARCH" != "i686" ]; then
10
+ echo "Sorry, OS/Architecture not supported: ${UNAME}/${ARCH}. Download binary from https://github.com/stoplightio/prism/releases"
11
+ exit 1
12
+ fi
13
+
14
+ if [ "$UNAME" = "Darwin" ] ; then
15
+ OSX_ARCH=$(uname -m)
16
+ if [ "${OSX_ARCH}" = "x86_64" ] ; then
17
+ PLATFORM="darwin_amd64"
18
+ fi
19
+ elif [ "$UNAME" = "Linux" ] ; then
20
+ LINUX_ARCH=$(uname -m)
21
+ if [ "${LINUX_ARCH}" = "i686" ] ; then
22
+ PLATFORM="linux_386"
23
+ elif [ "${LINUX_ARCH}" = "x86_64" ] ; then
24
+ PLATFORM="linux_amd64"
25
+ fi
26
+ fi
27
+
28
+ #LATEST=$(curl -s https://api.github.com/repos/stoplightio/prism/tags | grep -Eo '"name":.*?[^\\]",' | head -n 1 | sed 's/[," ]//g' | cut -d ':' -f 2)
29
+ LATEST="v0.2.7"
30
+ URL="https://github.com/stoplightio/prism/releases/download/$LATEST/prism_$PLATFORM"
31
+ DEST=./prism/bin/prism
32
+
33
+ if [ -z $LATEST ] ; then
34
+ echo "Error requesting. Download binary from ${URL}"
35
+ exit 1
36
+ else
37
+ curl -L $URL -o $DEST
38
+ chmod +x $DEST
39
+ fi
40
+ }
41
+
42
+ install
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ require_relative "../../../../lib/sendgrid/helpers/mail/attachment"
3
+ include SendGrid
4
+ require "json"
5
+ require "minitest/autorun"
6
+
7
+ class TestAttachment < Minitest::Test
8
+ SAMPLE_INPUT = """Es blüht so grün wie Blüten blüh'n im Frühling
9
+ Es blüht so grün wie Blüten blüh'n im Frühling
10
+ Es blüht so grün wie Blüten blüh'n im Frühling
11
+ Es blüht so grün wie Blüten blüh'n im Frühling
12
+ Es blüht so grün wie Blüten blüh'n im Frühling
13
+ """.force_encoding('UTF-8').encode
14
+
15
+ def setup
16
+ end
17
+
18
+ def test_io_enocding
19
+ attachment = Attachment.new
20
+ attachment.content = StringIO.new(SAMPLE_INPUT)
21
+
22
+ expected = {
23
+ "content" => "RXMgYmzDvGh0IHNvIGdyw7xuIHdpZSBCbMO8dGVuIGJsw7xoJ24gaW0gRnLD\nvGhsaW5nCkVzIGJsw7xodCBzbyBncsO8biB3aWUgQmzDvHRlbiBibMO8aCdu\nIGltIEZyw7xobGluZwpFcyBibMO8aHQgc28gZ3LDvG4gd2llIEJsw7x0ZW4g\nYmzDvGgnbiBpbSBGcsO8aGxpbmcKRXMgYmzDvGh0IHNvIGdyw7xuIHdpZSBC\nbMO8dGVuIGJsw7xoJ24gaW0gRnLDvGhsaW5nCkVzIGJsw7xodCBzbyBncsO8\nbiB3aWUgQmzDvHRlbiBibMO8aCduIGltIEZyw7xobGluZwo=\n"
24
+ }
25
+
26
+ json = attachment.to_json
27
+
28
+ # Double check that the decoded json matches original input.
29
+ decoded = Base64.decode64(json["content"]).force_encoding('UTF-8').encode
30
+
31
+ assert_equal(decoded, SAMPLE_INPUT)
32
+
33
+ assert_equal(json, expected)
34
+ end
35
+ 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
@@ -0,0 +1,34 @@
1
+ require_relative '../../../../lib/sendgrid/helpers/mail/email'
2
+ require 'minitest/autorun'
3
+
4
+ class TestEmail < Minitest::Test
5
+
6
+ include SendGrid
7
+
8
+ def test_split_email_full_email
9
+ @email = Email.new(email: "Example User <test1@example.com>")
10
+ expected_json = {
11
+ "email"=>"test1@example.com",
12
+ "name"=>"Example User"
13
+ }
14
+ assert_equal @email.to_json, expected_json
15
+ end
16
+
17
+ def test_split_email_only_email
18
+ @email = Email.new(email: "test1@example.com")
19
+ expected_json = {
20
+ "email"=>"test1@example.com",
21
+ }
22
+ assert_equal @email.to_json, expected_json
23
+ end
24
+
25
+ def test_split_email_name_and_email
26
+ @email = Email.new(name: "Example User", email: "test1@example.com")
27
+ expected_json = {
28
+ "email"=>"test1@example.com",
29
+ "name"=>"Example User"
30
+ }
31
+ assert_equal @email.to_json, expected_json
32
+ end
33
+
34
+ end
@@ -0,0 +1,261 @@
1
+ require_relative "../../../../lib/sendgrid/helpers/mail/mail"
2
+ require_relative "../../../../lib/sendgrid/client"
3
+ include SendGrid
4
+ require "json"
5
+ require 'minitest/autorun'
6
+
7
+ class TestMail < Minitest::Test
8
+ def setup
9
+ end
10
+
11
+ def test_hello_world
12
+ from = Email.new(email: 'test@example.com')
13
+ to = Email.new(email: 'test@example.com')
14
+ subject = 'Sending with Twilio SendGrid is Fun'
15
+ content = Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby')
16
+ mail = SendGrid::Mail.new(from, subject, to, content)
17
+
18
+ assert_equal(mail.to_json, JSON.parse('{"from":{"email":"test@example.com"}, "subject":"Sending with Twilio SendGrid is Fun", "personalizations":[{"to":[{"email":"test@example.com"}]}], "content":[{"type":"text/plain", "value":"and easy to do anywhere, even with Ruby"}]}'))
19
+ end
20
+
21
+ def test_kitchen_sink
22
+ mail = SendGrid::Mail.new
23
+ mail.from = Email.new(email: "test@example.com")
24
+ mail.subject = "Hello World from the Twilio SendGrid Ruby Library"
25
+ personalization = Personalization.new
26
+ personalization.add_to(Email.new(email: "test@example.com", name: "Example User"))
27
+ personalization.add_to(Email.new(email: "test@example.com", name: "Example User"))
28
+ personalization.add_cc(Email.new(email: "test@example.com", name: "Example User"))
29
+ personalization.add_cc(Email.new(email: "test@example.com", name: "Example User"))
30
+ personalization.add_bcc(Email.new(email: "test@example.com", name: "Example User"))
31
+ personalization.add_bcc(Email.new(email: "test@example.com", name: "Example User"))
32
+ personalization.subject = "Hello World from the Personalized Twilio SendGrid Ruby Library"
33
+ personalization.add_header(Header.new(key: "X-Test", value: "True"))
34
+ personalization.add_header(Header.new(key: "X-Mock", value: "False"))
35
+ personalization.add_substitution(Substitution.new(key: "%name%", value: "Example User"))
36
+ personalization.add_substitution(Substitution.new(key: "%city%", value: "Denver"))
37
+ personalization.add_custom_arg(CustomArg.new(key: "user_id", value: "343"))
38
+ personalization.add_custom_arg(CustomArg.new(key: "type", value: "marketing"))
39
+ personalization.send_at = 1443636843
40
+ mail.add_personalization(personalization)
41
+
42
+ personalization2 = Personalization.new
43
+ personalization2.add_to(Email.new(email: "test@example.com", name: "Example User"))
44
+ personalization2.add_to(Email.new(email: "test@example.com", name: "Example User"))
45
+ personalization2.add_cc(Email.new(email: "test@example.com", name: "Example User"))
46
+ personalization2.add_cc(Email.new(email: "test@example.com", name: "Example User"))
47
+ personalization2.add_bcc(Email.new(email: "test@example.com", name: "Example User"))
48
+ personalization2.add_bcc(Email.new(email: "test@example.com", name: "Example User"))
49
+ personalization2.subject = "Hello World from the Personalized Twilio SendGrid Ruby Library"
50
+ personalization2.add_header(Header.new(key: "X-Test", value: "True"))
51
+ personalization2.add_header(Header.new(key: "X-Mock", value: "False"))
52
+ personalization2.add_substitution(Substitution.new(key: "%name%", value: "Example User"))
53
+ personalization2.add_substitution(Substitution.new(key: "%city%", value: "Denver"))
54
+ personalization2.add_custom_arg(CustomArg.new(key: "user_id", value: "343"))
55
+ personalization2.add_custom_arg(CustomArg.new(key: "type", value: "marketing"))
56
+ personalization2.send_at = 1443636843
57
+ mail.add_personalization(personalization2)
58
+
59
+ mail.add_content(Content.new(type: "text/plain", value: "some text here"))
60
+ mail.add_content(Content.new(type: "text/html", value: "<html><body>some text here</body></html>"))
61
+
62
+ attachment = Attachment.new
63
+ attachment.content = "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12"
64
+ attachment.type = "application/pdf"
65
+ attachment.filename = "balance_001.pdf"
66
+ attachment.disposition = "attachment"
67
+ attachment.content_id = "Balance Sheet"
68
+
69
+ mail.add_attachment(attachment)
70
+
71
+ attachment2 = Attachment.new
72
+ attachment2.content = "BwdW"
73
+ attachment2.type = "image/png"
74
+ attachment2.filename = "banner.png"
75
+ attachment2.disposition = "inline"
76
+ attachment2.content_id = "Banner"
77
+ mail.add_attachment(attachment2)
78
+
79
+ mail.template_id = "13b8f94f-bcae-4ec6-b752-70d6cb59f932"
80
+
81
+ mail.add_section(Section.new(key: "%section1%", value: "Substitution Text for Section 1"))
82
+ mail.add_section(Section.new(key: "%section2%", value: "Substitution Text for Section 2"))
83
+
84
+ mail.add_header(Header.new(key: "X-Test3", value: "test3"))
85
+ mail.add_header(Header.new(key: "X-Test4", value: "test4"))
86
+
87
+ mail.add_category(Category.new(name: "May"))
88
+ mail.add_category(Category.new(name: "2016"))
89
+
90
+ mail.add_custom_arg(CustomArg.new(key: "campaign", value: "welcome"))
91
+ mail.add_custom_arg(CustomArg.new(key: "weekday", value: "morning"))
92
+
93
+ mail.send_at = 1443636842
94
+
95
+ mail.batch_id = "sendgrid_batch_id"
96
+
97
+ mail.asm = ASM.new(group_id: 99, groups_to_display: [4,5,6,7,8])
98
+
99
+ mail.ip_pool_name = "23"
100
+
101
+ mail_settings = MailSettings.new
102
+ mail_settings.bcc = BccSettings.new(enable: true, email: "test@example.com")
103
+ mail_settings.bypass_list_management = BypassListManagement.new(enable: true)
104
+ mail_settings.footer = Footer.new(enable: true, text: "Footer Text", html: "<html><body>Footer Text</body></html>")
105
+ mail_settings.sandbox_mode = SandBoxMode.new(enable: true)
106
+ mail_settings.spam_check = SpamCheck.new(enable: true, threshold: 1, post_to_url: "https://spamcatcher.sendgrid.com")
107
+ mail.mail_settings = mail_settings
108
+
109
+ tracking_settings = TrackingSettings.new
110
+ tracking_settings.click_tracking = ClickTracking.new(enable: false, enable_text: false)
111
+ tracking_settings.open_tracking = OpenTracking.new(enable: true, substitution_tag: "Optional tag to replace with the open image in the body of the message")
112
+ 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")
113
+ tracking_settings.ganalytics = Ganalytics.new(enable: true, utm_source: "some source", utm_medium: "some medium", utm_term: "some term", utm_content: "some content", utm_campaign: "some campaign")
114
+ mail.tracking_settings = tracking_settings
115
+
116
+ mail.reply_to = Email.new(email: "test@example.com")
117
+
118
+ 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 Twilio SendGrid Ruby 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 Twilio SendGrid Ruby 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 Twilio 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"}}}'))
119
+ end
120
+
121
+ def test_that_personalizations_is_empty_initially
122
+ mail = SendGrid::Mail.new
123
+ assert_equal([], mail.personalizations)
124
+ end
125
+
126
+ def test_that_contents_is_empty_initially
127
+ mail = SendGrid::Mail.new
128
+ assert_equal([], mail.contents)
129
+ end
130
+
131
+ def test_that_attachments_is_empty_initially
132
+ mail = SendGrid::Mail.new
133
+ assert_equal([], mail.attachments)
134
+ end
135
+
136
+ def test_that_categories_is_empty_initially
137
+ mail = SendGrid::Mail.new
138
+ assert_equal([], mail.categories)
139
+ end
140
+
141
+ def test_add_personalization
142
+ mail = SendGrid::Mail.new
143
+ mail.add_personalization('foo')
144
+ assert_equal(['foo'.to_json], mail.personalizations)
145
+ end
146
+
147
+ def test_add_content
148
+ mail = SendGrid::Mail.new
149
+ mail.add_content('foo')
150
+ assert_equal(['foo'.to_json], mail.contents)
151
+ end
152
+
153
+ def test_add_section
154
+ mail = SendGrid::Mail.new
155
+ mail.add_section(Section.new(key: '%section1%', value: 'Substitution Text for Section 1'))
156
+ expected_json = {
157
+ "sections"=>{
158
+ "%section1%"=>"Substitution Text for Section 1"
159
+ }
160
+ }
161
+ assert_equal mail.to_json, expected_json
162
+ mail.add_section(Section.new(key: '%section2%', value: 'Substitution Text for Section 2'))
163
+ expected_json = {
164
+ "sections"=>{
165
+ "%section1%"=>"Substitution Text for Section 1",
166
+ "%section2%"=>"Substitution Text for Section 2"
167
+ }
168
+ }
169
+ assert_equal mail.to_json, expected_json
170
+ end
171
+
172
+ def test_add_header
173
+ mail = SendGrid::Mail.new
174
+ mail.add_header(Header.new(key: 'X-Test3', value: 'test3'))
175
+ expected_json = {
176
+ "headers"=>{
177
+ "X-Test3"=>"test3"
178
+ }
179
+ }
180
+ assert_equal mail.to_json, expected_json
181
+ mail.add_header(Header.new(key: 'X-Test4', value: 'test4'))
182
+ expected_json = {
183
+ "headers"=>{
184
+ "X-Test3"=>"test3",
185
+ "X-Test4"=>"test4"
186
+ }
187
+ }
188
+ assert_equal mail.to_json, expected_json
189
+ end
190
+
191
+ def test_add_custom_arg
192
+ mail = SendGrid::Mail.new
193
+ mail.add_custom_arg(CustomArg.new(key: 'campaign 1', value: 'welcome 1'))
194
+ expected_json = {
195
+ "custom_args"=>{
196
+ "campaign 1"=>"welcome 1"
197
+ }
198
+ }
199
+ assert_equal mail.to_json, expected_json
200
+ mail.add_custom_arg(CustomArg.new(key: 'campaign 2', value: 'welcome 2'))
201
+ expected_json = {
202
+ "custom_args"=>{
203
+ "campaign 1"=>"welcome 1",
204
+ "campaign 2"=>"welcome 2"
205
+ }
206
+ }
207
+ assert_equal mail.to_json, expected_json
208
+ end
209
+
210
+ def test_add_non_string_custom_arg
211
+ mail = Mail.new
212
+ mail.add_custom_arg(CustomArg.new(key: "Integer", value: 1))
213
+ mail.add_custom_arg(CustomArg.new(key: "Array", value: [1, "a", true]))
214
+ mail.add_custom_arg(CustomArg.new(key: "Hash", value: {"a" => 1, "b" => 2}))
215
+ expected_json = {
216
+ "custom_args"=>{
217
+ "Integer"=>"1",
218
+ "Array"=>"[1, \"a\", true]",
219
+ "Hash"=>"{\"a\"=>1, \"b\"=>2}",
220
+ }
221
+ }
222
+ assert_equal mail.to_json, expected_json
223
+ end
224
+
225
+ def test_add_attachment
226
+ mail = SendGrid::Mail.new
227
+ mail.add_attachment('foo')
228
+ assert_equal(['foo'.to_json], mail.attachments)
229
+ end
230
+
231
+ def test_add_valid_category
232
+ mail = SendGrid::Mail.new
233
+ category = Category.new(name: 'foo')
234
+ mail.add_category(category)
235
+ assert_equal(['foo'], mail.categories)
236
+ end
237
+
238
+ def test_add_more_than_1_valid_category
239
+ mail = SendGrid::Mail.new
240
+ category_1 = Category.new(name: 'foo')
241
+ category_2 = Category.new(name: 'bar')
242
+ mail.add_category(category_1)
243
+ mail.add_category(category_2)
244
+ assert_equal(['foo', 'bar'], mail.categories)
245
+ end
246
+
247
+ def test_add_invalid_category
248
+ mail = SendGrid::Mail.new
249
+ assert_raises(NoMethodError) do
250
+ mail.add_category('foo')
251
+ end
252
+ end
253
+
254
+ def test_check_for_secrets
255
+ mail = Mail.new
256
+ mail.add_content(Content.new(type: 'text/plain', value: 'Sensitive information: SG.a123b456'))
257
+ assert_raises(SecurityError) do
258
+ mail.check_for_secrets([/SG.[a-zA-Z0-9_-]*/])
259
+ end
260
+ end
261
+ end