sendgrid-ruby 1.1.6 → 6.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. checksums.yaml +5 -5
  2. data/.env_sample +1 -3
  3. data/.github/ISSUE_TEMPLATE/config.yml +10 -0
  4. data/.github/workflows/test-and-deploy.yml +120 -0
  5. data/.gitignore +3 -0
  6. data/.rubocop.yml +5 -27
  7. data/.rubocop_todo.yml +127 -0
  8. data/CHANGELOG.md +417 -1
  9. data/CODE_OF_CONDUCT.md +73 -0
  10. data/CONTRIBUTING.md +202 -0
  11. data/Dockerfile +14 -0
  12. data/FIRST_TIMERS.md +79 -0
  13. data/Gemfile +4 -4
  14. data/ISSUE_TEMPLATE.md +30 -0
  15. data/LICENSE +21 -0
  16. data/Makefile +14 -0
  17. data/PULL_REQUEST_TEMPLATE.md +31 -0
  18. data/README.md +153 -229
  19. data/Rakefile +9 -3
  20. data/TROUBLESHOOTING.md +151 -0
  21. data/UPGRADE.md +5 -0
  22. data/USAGE.md +5144 -0
  23. data/config.ru +4 -0
  24. data/examples/accesssettings/accesssettings.rb +80 -0
  25. data/examples/alerts/alerts.rb +59 -0
  26. data/examples/apikeys/apikeys.rb +81 -0
  27. data/examples/asm/asm.rb +170 -0
  28. data/examples/browsers/browsers.rb +13 -0
  29. data/examples/campaigns/campaigns.rb +150 -0
  30. data/examples/categories/categories.rb +33 -0
  31. data/examples/clients/clients.rb +24 -0
  32. data/examples/contactdb/contactdb.rb +392 -0
  33. data/examples/devices/devices.rb +13 -0
  34. data/examples/emailactivity/emailactivity.rb +52 -0
  35. data/examples/geo/geo.rb +13 -0
  36. data/examples/helpers/eventwebhook/example.rb +16 -0
  37. data/examples/helpers/mail/example.rb +158 -0
  38. data/examples/helpers/settings/example.rb +23 -0
  39. data/examples/helpers/stats/example.rb +42 -0
  40. data/examples/ips/ips.rb +164 -0
  41. data/examples/mail/mail.rb +170 -0
  42. data/examples/mailboxproviders/mailboxproviders.rb +13 -0
  43. data/examples/mailsettings/mailsettings.rb +216 -0
  44. data/examples/partnersettings/partnersettings.rb +36 -0
  45. data/examples/scopes/scopes.rb +59 -0
  46. data/examples/senderauthentication/senderauthentication.rb +307 -0
  47. data/examples/senders/senders.rb +95 -0
  48. data/examples/stats/stats.rb +13 -0
  49. data/examples/subusers/subusers.rb +166 -0
  50. data/examples/suppression/suppression.rb +198 -0
  51. data/examples/templates/templates.rb +127 -0
  52. data/examples/trackingsettings/trackingsettings.rb +107 -0
  53. data/examples/user/user.rb +290 -0
  54. data/gemfiles/Sinatra_1.gemfile +6 -0
  55. data/gemfiles/Sinatra_2.gemfile +6 -0
  56. data/lib/rack/sendgrid_webhook_verification.rb +55 -0
  57. data/lib/sendgrid/base_interface.rb +40 -0
  58. data/lib/sendgrid/helpers/eventwebhook/eventwebhook.rb +50 -0
  59. data/lib/sendgrid/helpers/inbound/README.md +98 -0
  60. data/lib/sendgrid/helpers/inbound/app.rb +32 -0
  61. data/lib/sendgrid/helpers/inbound/config.yml +26 -0
  62. data/lib/sendgrid/helpers/inbound/public/index.html +10 -0
  63. data/lib/sendgrid/helpers/inbound/sample_data/default_data.txt +58 -0
  64. data/lib/sendgrid/helpers/inbound/sample_data/raw_data.txt +57 -0
  65. data/lib/sendgrid/helpers/inbound/sample_data/raw_data_with_attachments.txt +298 -0
  66. data/lib/sendgrid/helpers/inbound/send.rb +26 -0
  67. data/lib/sendgrid/helpers/ip_management/ip_management.rb +17 -0
  68. data/lib/sendgrid/helpers/mail/README.md +14 -0
  69. data/lib/sendgrid/helpers/mail/asm.rb +19 -0
  70. data/lib/sendgrid/helpers/mail/attachment.rb +55 -0
  71. data/lib/sendgrid/helpers/mail/bcc_settings.rb +19 -0
  72. data/lib/sendgrid/helpers/mail/bypass_list_management.rb +31 -0
  73. data/lib/sendgrid/helpers/mail/category.rb +18 -0
  74. data/lib/sendgrid/helpers/mail/click_tracking.rb +19 -0
  75. data/lib/sendgrid/helpers/mail/content.rb +19 -0
  76. data/lib/sendgrid/helpers/mail/custom_arg.rb +18 -0
  77. data/lib/sendgrid/helpers/mail/email.rb +32 -0
  78. data/lib/sendgrid/helpers/mail/footer.rb +21 -0
  79. data/lib/sendgrid/helpers/mail/ganalytics.rb +28 -0
  80. data/lib/sendgrid/helpers/mail/header.rb +18 -0
  81. data/lib/sendgrid/helpers/mail/mail.rb +122 -0
  82. data/lib/sendgrid/helpers/mail/mail_settings.rb +45 -0
  83. data/lib/sendgrid/helpers/mail/open_tracking.rb +19 -0
  84. data/lib/sendgrid/helpers/mail/personalization.rb +93 -0
  85. data/lib/sendgrid/helpers/mail/section.rb +18 -0
  86. data/lib/sendgrid/helpers/mail/spam_check.rb +21 -0
  87. data/lib/sendgrid/helpers/mail/subscription_tracking.rb +23 -0
  88. data/lib/sendgrid/helpers/mail/substitution.rb +18 -0
  89. data/lib/sendgrid/helpers/mail/tracking_settings.rb +39 -0
  90. data/lib/sendgrid/helpers/permissions/scope.rb +28 -0
  91. data/lib/sendgrid/helpers/permissions/scopes.yml +309 -0
  92. data/lib/sendgrid/helpers/settings/README.md +14 -0
  93. data/lib/sendgrid/helpers/settings/mail_settings_dto.rb +13 -0
  94. data/lib/sendgrid/helpers/settings/partner_settings_dto.rb +13 -0
  95. data/lib/sendgrid/helpers/settings/settings.rb +28 -0
  96. data/lib/sendgrid/helpers/settings/tracking_settings_dto.rb +22 -0
  97. data/lib/sendgrid/helpers/settings/user_settings_dto.rb +13 -0
  98. data/lib/sendgrid/helpers/stats/email_stats.rb +46 -0
  99. data/lib/sendgrid/helpers/stats/metrics.rb +33 -0
  100. data/lib/sendgrid/helpers/stats/stats_response.rb +29 -0
  101. data/lib/sendgrid/sendgrid.rb +21 -0
  102. data/lib/sendgrid/twilio_email.rb +21 -0
  103. data/lib/sendgrid/version.rb +1 -1
  104. data/lib/sendgrid-ruby.rb +32 -7
  105. data/mail_helper_v3.md +390 -0
  106. data/sendgrid-ruby.gemspec +16 -20
  107. data/spec/fixtures/event_webhook.rb +22 -0
  108. data/spec/rack/sendgrid_webhook_verification_spec.rb +142 -0
  109. data/spec/sendgrid/helpers/eventwebhook/eventwebhook_spec.rb +105 -0
  110. data/spec/sendgrid/helpers/ip_management/ip_management_spec.rb +12 -0
  111. data/spec/sendgrid/helpers/settings/mail_settings_dto_spec.rb +32 -0
  112. data/spec/sendgrid/helpers/settings/partner_settings_dto_spec.rb +24 -0
  113. data/spec/sendgrid/helpers/settings/settings_spec.rb +25 -0
  114. data/spec/sendgrid/helpers/settings/tracking_settings_dto_spec.rb +27 -0
  115. data/spec/sendgrid/helpers/settings/user_settings_dto_spec.rb +24 -0
  116. data/spec/sendgrid/helpers/stats/email_stats_spec.rb +111 -0
  117. data/spec/sendgrid/helpers/stats/metrics_spec.rb +45 -0
  118. data/spec/sendgrid/helpers/stats/stats_response_spec.rb +75 -0
  119. data/spec/sendgrid/sendgrid_spec.rb +11 -0
  120. data/spec/sendgrid/twilio_email_spec.rb +11 -0
  121. data/spec/spec_helper.rb +12 -1
  122. data/static/img/github-fork.png +0 -0
  123. data/static/img/github-sign-up.png +0 -0
  124. data/test/sendgrid/helpers/mail/test_attachment.rb +33 -0
  125. data/test/sendgrid/helpers/mail/test_category.rb +25 -0
  126. data/test/sendgrid/helpers/mail/test_email.rb +41 -0
  127. data/test/sendgrid/helpers/mail/test_mail.rb +260 -0
  128. data/test/sendgrid/helpers/mail/test_personalizations.rb +214 -0
  129. data/test/sendgrid/permissions/test_scopes.rb +36 -0
  130. data/test/sendgrid/test_sendgrid-ruby.rb +2729 -0
  131. data/twilio_sendgrid_logo.png +0 -0
  132. data/use-cases/README.md +17 -0
  133. data/use-cases/domain-authentication.md +5 -0
  134. data/use-cases/email-statistics.md +52 -0
  135. data/use-cases/legacy-templates.md +98 -0
  136. data/use-cases/personalizations.md +34 -0
  137. data/use-cases/sms.md +39 -0
  138. data/use-cases/transactional-templates.md +111 -0
  139. data/use-cases/twilio-email.md +13 -0
  140. data/use-cases/twilio-setup.md +54 -0
  141. metadata +187 -112
  142. data/.rspec +0 -2
  143. data/.travis.yml +0 -20
  144. data/FETCH_HEAD +0 -0
  145. data/Guardfile +0 -10
  146. data/LICENSE.txt +0 -22
  147. data/example.rb +0 -41
  148. data/lib/sendgrid/client.rb +0 -62
  149. data/lib/sendgrid/exceptions.rb +0 -7
  150. data/lib/sendgrid/mail.rb +0 -182
  151. data/lib/sendgrid/recipient.rb +0 -29
  152. data/lib/sendgrid/response.rb +0 -14
  153. data/lib/sendgrid/template.rb +0 -26
  154. data/lib/sendgrid/template_mailer.rb +0 -59
  155. data/spec/lib/sendgrid/client_spec.rb +0 -87
  156. data/spec/lib/sendgrid/mail_spec.rb +0 -151
  157. data/spec/lib/sendgrid/recipient_spec.rb +0 -91
  158. data/spec/lib/sendgrid/template_mailer_spec.rb +0 -86
  159. data/spec/lib/sendgrid/template_spec.rb +0 -61
  160. data/spec/lib/sendgrid_spec.rb +0 -7
@@ -0,0 +1,105 @@
1
+ require 'spec_helper'
2
+ require './spec/fixtures/event_webhook'
3
+
4
+ describe SendGrid::EventWebhook do
5
+ describe '.verify_signature' do
6
+ it 'verifies a valid signature' do
7
+ unless skip_jruby
8
+ expect(verify(
9
+ Fixtures::EventWebhook::PUBLIC_KEY,
10
+ Fixtures::EventWebhook::PAYLOAD,
11
+ Fixtures::EventWebhook::SIGNATURE,
12
+ Fixtures::EventWebhook::TIMESTAMP
13
+ )).to be true
14
+ end
15
+ end
16
+
17
+ it 'rejects a bad key' do
18
+ unless skip_jruby
19
+ expect(verify(
20
+ Fixtures::EventWebhook::FAILING_PUBLIC_KEY,
21
+ Fixtures::EventWebhook::PAYLOAD,
22
+ Fixtures::EventWebhook::SIGNATURE,
23
+ Fixtures::EventWebhook::TIMESTAMP
24
+ )).to be false
25
+ end
26
+ end
27
+
28
+ it 'rejects a bad payload' do
29
+ unless skip_jruby
30
+ expect(verify(
31
+ Fixtures::EventWebhook::PUBLIC_KEY,
32
+ 'payload',
33
+ Fixtures::EventWebhook::SIGNATURE,
34
+ Fixtures::EventWebhook::TIMESTAMP
35
+ )).to be false
36
+ end
37
+ end
38
+
39
+ it 'rejects a bad signature' do
40
+ unless skip_jruby
41
+ expect(verify(
42
+ Fixtures::EventWebhook::PUBLIC_KEY,
43
+ Fixtures::EventWebhook::PAYLOAD,
44
+ Fixtures::EventWebhook::FAILING_SIGNATURE,
45
+ Fixtures::EventWebhook::TIMESTAMP
46
+ )).to be false
47
+ end
48
+ end
49
+
50
+ it 'rejects a bad timestamp' do
51
+ unless skip_jruby
52
+ expect(verify(
53
+ Fixtures::EventWebhook::PUBLIC_KEY,
54
+ Fixtures::EventWebhook::PAYLOAD,
55
+ Fixtures::EventWebhook::SIGNATURE,
56
+ 'timestamp'
57
+ )).to be false
58
+ end
59
+ end
60
+
61
+ it 'rejects a missing signature' do
62
+ unless skip_jruby
63
+ expect(verify(
64
+ Fixtures::EventWebhook::PUBLIC_KEY,
65
+ Fixtures::EventWebhook::PAYLOAD,
66
+ nil,
67
+ Fixtures::EventWebhook::TIMESTAMP
68
+ )).to be false
69
+ end
70
+ end
71
+
72
+ it 'throws an error when using jruby' do
73
+ if skip_jruby
74
+ expect do
75
+ verify(
76
+ Fixtures::EventWebhook::PUBLIC_KEY,
77
+ Fixtures::EventWebhook::PAYLOAD,
78
+ Fixtures::EventWebhook::SIGNATURE,
79
+ Fixtures::EventWebhook::TIMESTAMP
80
+ )
81
+ end.to raise_error(SendGrid::EventWebhook::NotSupportedError)
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ describe SendGrid::EventWebhookHeader do
88
+ it 'sets the signature header constant' do
89
+ expect(SendGrid::EventWebhookHeader::SIGNATURE).to eq("HTTP_X_TWILIO_EMAIL_EVENT_WEBHOOK_SIGNATURE")
90
+ end
91
+
92
+ it 'sets the timestamp header constant' do
93
+ expect(SendGrid::EventWebhookHeader::TIMESTAMP).to eq("HTTP_X_TWILIO_EMAIL_EVENT_WEBHOOK_TIMESTAMP")
94
+ end
95
+ end
96
+
97
+ def verify(public_key, payload, signature, timestamp)
98
+ ew = SendGrid::EventWebhook.new
99
+ ec_public_key = ew.convert_public_key_to_ecdsa(public_key)
100
+ ew.verify_signature(ec_public_key, payload, signature, timestamp)
101
+ end
102
+
103
+ def skip_jruby
104
+ RUBY_PLATFORM == 'java'
105
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe SendGrid::IpManagement do
4
+ let(:sendgrid_client) { SendGrid::API.new(api_key: 'fake_key').client }
5
+ let(:ip_management) { SendGrid::IpManagement.new(sendgrid_client: sendgrid_client) }
6
+
7
+ describe '.new' do
8
+ it 'initializes correctly' do
9
+ expect(ip_management).to be_a SendGrid::IpManagement
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe SendGrid::MailSettingsDto do
4
+ let(:sendgrid_client) { SendGrid::API.new(api_key: 'fake_key').client }
5
+ let(:mail_settings) { SendGrid::MailSettingsDto }
6
+ let(:setting_name) { 'bcc' }
7
+ let(:setting_params) { { email: Faker::Internet.email, enabled: rand(1..100).even? } }
8
+
9
+ it { should respond_to :bcc }
10
+ it { should respond_to :address_whitelist }
11
+ it { should respond_to :bounce_purge }
12
+ it { should respond_to :footer }
13
+ it { should respond_to :forward_spam }
14
+ it { should respond_to :forward_bounce }
15
+ it { should respond_to :plain_content }
16
+ it { should respond_to :spam_check }
17
+ it { should respond_to :template }
18
+
19
+ describe '.fetch' do
20
+ it 'calls get on sendgrid_client' do
21
+ args = { sendgrid_client: sendgrid_client, name: setting_name, query_params: {} }
22
+ expect(mail_settings.fetch(**args)).to be_a SendGrid::Response
23
+ end
24
+ end
25
+
26
+ describe '.update' do
27
+ it 'calls patch on sendgrid_client' do
28
+ args = { sendgrid_client: sendgrid_client, name: setting_name, request_body: setting_params }
29
+ expect(mail_settings.update(**args)).to be_a SendGrid::Response
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe SendGrid::PartnerSettingsDto do
4
+ let(:sendgrid_client) { SendGrid::API.new(api_key: 'fake_key').client }
5
+ let(:partner_settings) { SendGrid::PartnerSettingsDto }
6
+ let(:setting_name) { 'new_relic' }
7
+ let(:setting_params) { { license_key: 'key', enabled: rand(1..100).even? } }
8
+
9
+ it { should respond_to :new_relic }
10
+
11
+ describe '.fetch' do
12
+ it 'calls get on sendgrid_client' do
13
+ args = { sendgrid_client: sendgrid_client, name: setting_name, query_params: {} }
14
+ expect(partner_settings.fetch(**args)).to be_a SendGrid::Response
15
+ end
16
+ end
17
+
18
+ describe '.update' do
19
+ it 'calls patch on sendgrid_client' do
20
+ args = { sendgrid_client: sendgrid_client, name: setting_name, request_body: setting_params }
21
+ expect(partner_settings.update(**args)).to be_a SendGrid::Response
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe SendGrid::Settings do
4
+ let(:sendgrid_client) { SendGrid::API.new(api_key: 'fake_key').client }
5
+ let(:settings) { SendGrid::Settings.new(sendgrid_client: sendgrid_client) }
6
+
7
+ describe '.new' do
8
+ it 'initializes correctly' do
9
+ expect(settings).to be_a SendGrid::Settings
10
+ end
11
+ end
12
+
13
+ describe '.bcc' do
14
+ it 'fetches bcc data' do
15
+ expect(settings.bcc).to be_a SendGrid::Response
16
+ end
17
+ end
18
+
19
+ describe '.update_bcc' do
20
+ it 'updates bcc' do
21
+ bcc_response = settings.update_bcc(enabled: true, email: 'email@example.com')
22
+ expect(bcc_response).to be_a SendGrid::Response
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe SendGrid::TrackingSettingsDto do
4
+ let(:sendgrid_client) { SendGrid::API.new(api_key: 'fake_key').client }
5
+ let(:tracking_settings) { SendGrid::TrackingSettingsDto }
6
+ let(:setting_name) { 'open_tracking' }
7
+ let(:setting_params) { { enabled: rand(1..100).even? } }
8
+
9
+ it { should respond_to :open_tracking }
10
+ it { should respond_to :click_tracking }
11
+ it { should respond_to :google_analytics }
12
+ it { should respond_to :subscription_tracking }
13
+
14
+ describe '.fetch' do
15
+ it 'calls get on sendgrid_client' do
16
+ args = { sendgrid_client: sendgrid_client, name: setting_name, query_params: {} }
17
+ expect(tracking_settings.fetch(**args)).to be_a SendGrid::Response
18
+ end
19
+ end
20
+
21
+ describe '.update' do
22
+ it 'calls patch on sendgrid_client' do
23
+ args = { sendgrid_client: sendgrid_client, name: setting_name, request_body: setting_params }
24
+ expect(tracking_settings.update(**args)).to be_a SendGrid::Response
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe SendGrid::UserSettingsDto do
4
+ let(:sendgrid_client) { SendGrid::API.new(api_key: 'fake_key').client }
5
+ let(:user_settings) { SendGrid::UserSettingsDto }
6
+ let(:setting_name) { 'enforced_tls' }
7
+ let(:setting_params) { { require_tls: rand(1..100).even? } }
8
+
9
+ it { should respond_to :enforced_tls }
10
+
11
+ describe '.fetch' do
12
+ it 'calls get on sendgrid_client' do
13
+ args = { sendgrid_client: sendgrid_client, name: setting_name, query_params: {} }
14
+ expect(user_settings.fetch(**args)).to be_a SendGrid::Response
15
+ end
16
+ end
17
+
18
+ describe '.update' do
19
+ it 'calls patch on sendgrid_client' do
20
+ args = { sendgrid_client: sendgrid_client, name: setting_name, request_body: setting_params }
21
+ expect(user_settings.update(**args)).to be_a SendGrid::Response
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,111 @@
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
+ end
34
+
35
+ let(:error_response) do
36
+ {
37
+ 'errors' => [
38
+ {
39
+ 'message' => 'end_date should be a YYYY-MM-DD formatted date'
40
+ }
41
+ ]
42
+ }
43
+ end
44
+
45
+ describe '.new' do
46
+ it 'initializes with SendGrid::Client' do
47
+ expect(stats).to be_a SendGrid::EmailStats
48
+ end
49
+ end
50
+
51
+ describe 'successful response' do
52
+ before do
53
+ allow_any_instance_of(SendGrid::Response).to receive(:body) { sample_response.to_json }
54
+ end
55
+
56
+ describe '#by_day' do
57
+ it 'fetches data aggregated by day' do
58
+ day_stats = stats.by_day('2017-10-01', '2017-10-02')
59
+ day_metrics = day_stats.metrics.first
60
+
61
+ expect(day_metrics).to be_a SendGrid::Metrics
62
+ expect(day_metrics.date.to_s).to eq('2017-10-01')
63
+ expect(day_metrics.requests).to eq(110)
64
+ expect(day_metrics.clicks).to eq(104)
65
+ expect(day_metrics.bounces).to eq(103)
66
+ expect(day_metrics.opens).to eq(108)
67
+ end
68
+ end
69
+
70
+ describe '#by_week' do
71
+ it 'fetches data aggregated by week' do
72
+ day_stats = stats.by_week('2017-10-01', '2017-10-12')
73
+ day_metrics = day_stats.metrics.first
74
+
75
+ expect(day_metrics).to be_a SendGrid::Metrics
76
+ expect(day_metrics.date.to_s).to eq('2017-10-01')
77
+ expect(day_metrics.requests).to eq(110)
78
+ expect(day_metrics.clicks).to eq(104)
79
+ expect(day_metrics.bounces).to eq(103)
80
+ expect(day_metrics.opens).to eq(108)
81
+ end
82
+ end
83
+
84
+ describe '#by_month' do
85
+ it 'fetches data aggregated by month' do
86
+ day_stats = stats.by_month('2017-10-01', '2017-11-01')
87
+ day_metrics = day_stats.metrics.first
88
+
89
+ expect(day_metrics).to be_a SendGrid::Metrics
90
+ expect(day_metrics.date.to_s).to eq('2017-10-01')
91
+ expect(day_metrics.requests).to eq(110)
92
+ expect(day_metrics.clicks).to eq(104)
93
+ expect(day_metrics.bounces).to eq(103)
94
+ expect(day_metrics.opens).to eq(108)
95
+ end
96
+ end
97
+ end
98
+
99
+ describe 'error response' do
100
+ before do
101
+ allow_any_instance_of(SendGrid::Response).to receive(:body) { error_response.to_json }
102
+ end
103
+
104
+ it 'fetches data aggregated by month' do
105
+ day_stats = stats.by_month('2017-10-01', '2017-10-02')
106
+
107
+ expect(day_stats.errors).to include('end_date should be a YYYY-MM-DD formatted date')
108
+ expect(day_stats.error?).to be_truthy
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,45 @@
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
+ end
@@ -0,0 +1,75 @@
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
+ end
30
+
31
+ subject { described_class.new(params) }
32
+
33
+ it 'initialized with JSON response body' do
34
+ expect(subject).to be_a SendGrid::StatsResponse
35
+ end
36
+
37
+ describe '#metrics' do
38
+ it 'returns an array of metrics' do
39
+ metric = subject.metrics.first
40
+
41
+ expect(subject.metrics).to be_a Array
42
+ expect(metric.date.to_s).to eq('2017-10-01')
43
+ expect(metric.requests).to eq(110)
44
+ expect(metric.clicks).to eq(104)
45
+ expect(metric.bounces).to eq(103)
46
+ expect(metric.opens).to eq(108)
47
+ end
48
+ end
49
+
50
+ context 'errors' do
51
+ let(:error_params) do
52
+ {
53
+ 'errors' => [
54
+ {
55
+ 'message' => 'end_date should be a YYYY-MM-DD formatted date'
56
+ }
57
+ ]
58
+ }
59
+ end
60
+
61
+ subject { described_class.new(error_params) }
62
+
63
+ describe '#errors' do
64
+ it 'shows a list of errors' do
65
+ expect(subject.errors).to include('end_date should be a YYYY-MM-DD formatted date')
66
+ end
67
+ end
68
+
69
+ describe '#error?' do
70
+ it 'returns true if there is an error' do
71
+ expect(subject.error?).to be_truthy
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe SendGrid::API do
4
+ describe '.new' do
5
+ it 'initializes correctly' do
6
+ mail_client = SendGrid::API.new(api_key: 'fake_key')
7
+ expect(mail_client.request_headers['Authorization']).to eq('Bearer fake_key')
8
+ expect(mail_client.host).to eq('https://api.sendgrid.com')
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe TwilioEmail::API do
4
+ describe '.new' do
5
+ it 'initializes correctly' do
6
+ mail_client = TwilioEmail::API.new(username: 'username', password: 'password')
7
+ expect(mail_client.request_headers['Authorization']).to eq('Basic dXNlcm5hbWU6cGFzc3dvcmQ=')
8
+ expect(mail_client.host).to eq('https://email.twilio.com')
9
+ end
10
+ end
11
+ end
data/spec/spec_helper.rb CHANGED
@@ -1 +1,12 @@
1
- require_relative '../lib/sendgrid-ruby'
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+ require 'rubygems'
4
+ require 'bundler/setup'
5
+ require 'pry'
6
+ require 'faker'
7
+
8
+ RSpec.configure do |config|
9
+ Dir["#{File.dirname(__FILE__)}/../lib/sendgrid-ruby.rb"].sort.each { |ext| require ext }
10
+
11
+ config.color = true
12
+ end
Binary file
Binary file
@@ -0,0 +1,33 @@
1
+ require_relative "../../../../lib/sendgrid/helpers/mail/attachment"
2
+ include SendGrid
3
+ require "json"
4
+ require "minitest/autorun"
5
+
6
+ class TestAttachment < Minitest::Test
7
+ SAMPLE_INPUT = "Es blüht so grün wie Blüten blüh'n im Frühling
8
+ 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
+ ".force_encoding('UTF-8').encode
13
+
14
+ def setup; end
15
+
16
+ def test_io_enocding
17
+ attachment = Attachment.new
18
+ attachment.content = StringIO.new(SAMPLE_INPUT)
19
+
20
+ expected = {
21
+ "content" => "RXMgYmzDvGh0IHNvIGdyw7xuIHdpZSBCbMO8dGVuIGJsw7xoJ24gaW0gRnLD\nvGhsaW5nCkVzIGJsw7xodCBzbyBncsO8biB3aWUgQmzDvHRlbiBibMO8aCdu\nIGltIEZyw7xobGluZwpFcyBibMO8aHQgc28gZ3LDvG4gd2llIEJsw7x0ZW4g\nYmzDvGgnbiBpbSBGcsO8aGxpbmcKRXMgYmzDvGh0IHNvIGdyw7xuIHdpZSBC\nbMO8dGVuIGJsw7xoJ24gaW0gRnLDvGhsaW5nCkVzIGJsw7xodCBzbyBncsO8\nbiB3aWUgQmzDvHRlbiBibMO8aCduIGltIEZyw7xobGluZwo=\n"
22
+ }
23
+
24
+ json = attachment.to_json
25
+
26
+ # Double check that the decoded json matches original input.
27
+ decoded = Base64.decode64(json["content"]).force_encoding('UTF-8').encode
28
+
29
+ assert_equal(decoded, SAMPLE_INPUT)
30
+
31
+ assert_equal(json, expected)
32
+ end
33
+ end
@@ -0,0 +1,25 @@
1
+ require_relative '../../../../lib/sendgrid/helpers/mail/mail'
2
+ require 'minitest/autorun'
3
+
4
+ class TestCategory < Minitest::Test
5
+ include SendGrid
6
+
7
+ def setup
8
+ @category = Category.new(name: 'foo')
9
+ end
10
+
11
+ def test_aliases
12
+ assert_equal @category.method(:name), @category.method(:category)
13
+ end
14
+
15
+ def test_name
16
+ assert_equal @category.name, 'foo'
17
+ end
18
+
19
+ def test_to_json
20
+ expected_json = {
21
+ 'category' => 'foo'
22
+ }
23
+ assert_equal @category.to_json, expected_json
24
+ end
25
+ end
@@ -0,0 +1,41 @@
1
+ require_relative '../../../../lib/sendgrid/helpers/mail/email'
2
+ require 'minitest/autorun'
3
+
4
+ class TestEmail < Minitest::Test
5
+ include SendGrid
6
+
7
+ def test_split_email_full_email
8
+ @email = Email.new(email: 'Example User <test1@example.com>')
9
+ expected_json = {
10
+ 'email' => 'test1@example.com',
11
+ 'name' => 'Example User'
12
+ }
13
+ assert_equal @email.to_json, expected_json
14
+ end
15
+
16
+ def test_split_email_only_email
17
+ @email = Email.new(email: 'test1@example.com')
18
+ expected_json = {
19
+ 'email' => 'test1@example.com'
20
+ }
21
+ assert_equal @email.to_json, expected_json
22
+ end
23
+
24
+ def test_split_email_name_and_email
25
+ @email = Email.new(name: 'Example User', email: 'test1@example.com')
26
+ expected_json = {
27
+ 'email' => 'test1@example.com',
28
+ 'name' => 'Example User'
29
+ }
30
+ assert_equal @email.to_json, expected_json
31
+ end
32
+
33
+ def test_mandatory_email_missing
34
+ assert_raises(ArgumentError) { Email.new(email: nil) }
35
+ assert_raises(ArgumentError) { Email.new(email: "") }
36
+ end
37
+
38
+ def test_invalid_email
39
+ assert_raises(ArgumentError) { Email.new(email: "some-invalid-string") }
40
+ end
41
+ end