sendgrid-ruby 5.3.0 → 6.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/pr-lint.yml +15 -0
  3. data/.github/workflows/test-and-deploy.yml +120 -0
  4. data/.gitignore +2 -0
  5. data/.rubocop.yml +8 -0
  6. data/.rubocop_todo.yml +127 -0
  7. data/CHANGELOG.md +261 -8
  8. data/CODE_OF_CONDUCT.md +57 -25
  9. data/CONTRIBUTING.md +24 -71
  10. data/Dockerfile +14 -0
  11. data/FIRST_TIMERS.md +53 -0
  12. data/Gemfile +0 -1
  13. data/LICENSE +21 -0
  14. data/Makefile +14 -0
  15. data/PULL_REQUEST_TEMPLATE.md +31 -0
  16. data/README.md +39 -43
  17. data/Rakefile +3 -4
  18. data/TROUBLESHOOTING.md +41 -21
  19. data/UPGRADE.md +5 -0
  20. data/USAGE.md +1231 -1122
  21. data/examples/accesssettings/accesssettings.rb +9 -12
  22. data/examples/alerts/alerts.rb +8 -11
  23. data/examples/apikeys/apikeys.rb +12 -15
  24. data/examples/asm/asm.rb +27 -30
  25. data/examples/browsers/browsers.rb +0 -3
  26. data/examples/campaigns/campaigns.rb +29 -32
  27. data/examples/categories/categories.rb +0 -3
  28. data/examples/clients/clients.rb +1 -4
  29. data/examples/contactdb/contactdb.rb +63 -66
  30. data/examples/dataresidency/setregion.rb +48 -0
  31. data/examples/devices/devices.rb +0 -3
  32. data/examples/emailactivity/emailactivity.rb +52 -0
  33. data/examples/geo/geo.rb +0 -3
  34. data/examples/helpers/eventwebhook/example.rb +16 -0
  35. data/examples/helpers/mail/example.rb +30 -19
  36. data/examples/helpers/settings/example.rb +1 -1
  37. data/examples/helpers/stats/example.rb +4 -4
  38. data/examples/ips/ips.rb +31 -21
  39. data/examples/mail/mail.rb +73 -76
  40. data/examples/mailboxproviders/mailboxproviders.rb +0 -3
  41. data/examples/mailsettings/mailsettings.rb +21 -24
  42. data/examples/partnersettings/partnersettings.rb +3 -6
  43. data/examples/scopes/scopes.rb +49 -5
  44. data/examples/{whitelabel/whitelabel.rb → senderauthentication/senderauthentication.rb} +68 -71
  45. data/examples/senders/senders.rb +28 -31
  46. data/examples/stats/stats.rb +0 -3
  47. data/examples/subusers/subusers.rb +17 -20
  48. data/examples/suppression/suppression.rb +23 -26
  49. data/examples/templates/templates.rb +29 -31
  50. data/examples/trackingsettings/trackingsettings.rb +14 -17
  51. data/examples/user/user.rb +41 -44
  52. data/lib/rack/sendgrid_webhook_verification.rb +55 -0
  53. data/lib/sendgrid/base_interface.rb +57 -0
  54. data/lib/sendgrid/helpers/eventwebhook/eventwebhook.rb +50 -0
  55. data/lib/sendgrid/helpers/inbound/README.md +26 -9
  56. data/lib/sendgrid/helpers/inbound/app.rb +15 -3
  57. data/lib/sendgrid/helpers/inbound/public/index.html +2 -2
  58. data/lib/sendgrid/helpers/inbound/sample_data/default_data.txt +2 -2
  59. data/lib/sendgrid/helpers/inbound/sample_data/raw_data.txt +2 -2
  60. data/lib/sendgrid/helpers/inbound/sample_data/raw_data_with_attachments.txt +2 -2
  61. data/lib/sendgrid/helpers/inbound/send.rb +5 -5
  62. data/lib/sendgrid/helpers/ip_management/ip_management.rb +17 -0
  63. data/lib/sendgrid/helpers/mail/README.md +4 -4
  64. data/lib/sendgrid/helpers/mail/asm.rb +4 -18
  65. data/lib/sendgrid/helpers/mail/attachment.rb +30 -38
  66. data/lib/sendgrid/helpers/mail/bcc_settings.rb +4 -18
  67. data/lib/sendgrid/helpers/mail/bypass_list_management.rb +6 -18
  68. data/lib/sendgrid/helpers/mail/category.rb +2 -12
  69. data/lib/sendgrid/helpers/mail/click_tracking.rb +4 -18
  70. data/lib/sendgrid/helpers/mail/content.rb +4 -18
  71. data/lib/sendgrid/helpers/mail/custom_arg.rb +4 -10
  72. data/lib/sendgrid/helpers/mail/email.rb +10 -20
  73. data/lib/sendgrid/helpers/mail/footer.rb +5 -27
  74. data/lib/sendgrid/helpers/mail/ganalytics.rb +9 -55
  75. data/lib/sendgrid/helpers/mail/header.rb +4 -10
  76. data/lib/sendgrid/helpers/mail/mail.rb +37 -87
  77. data/lib/sendgrid/helpers/mail/mail_settings.rb +7 -25
  78. data/lib/sendgrid/helpers/mail/open_tracking.rb +4 -18
  79. data/lib/sendgrid/helpers/mail/personalization.rb +38 -27
  80. data/lib/sendgrid/helpers/mail/section.rb +4 -10
  81. data/lib/sendgrid/helpers/mail/spam_check.rb +5 -27
  82. data/lib/sendgrid/helpers/mail/subscription_tracking.rb +6 -36
  83. data/lib/sendgrid/helpers/mail/substitution.rb +4 -10
  84. data/lib/sendgrid/helpers/mail/tracking_settings.rb +6 -20
  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 +3 -3
  88. data/lib/sendgrid/helpers/settings/settings.rb +1 -1
  89. data/lib/sendgrid/helpers/settings/tracking_settings_dto.rb +3 -5
  90. data/lib/sendgrid/helpers/stats/metrics.rb +5 -7
  91. data/lib/sendgrid/helpers/stats/stats_response.rb +1 -3
  92. data/lib/sendgrid/sendgrid.rb +21 -0
  93. data/lib/sendgrid/twilio_email.rb +21 -0
  94. data/lib/sendgrid/version.rb +1 -1
  95. data/lib/sendgrid-ruby.rb +7 -1
  96. data/mail_helper_v3.md +21 -21
  97. data/sendgrid-ruby.gemspec +12 -12
  98. data/spec/fixtures/event_webhook.rb +22 -0
  99. data/spec/rack/sendgrid_webhook_verification_spec.rb +142 -0
  100. data/spec/sendgrid/helpers/eventwebhook/eventwebhook_spec.rb +105 -0
  101. data/spec/sendgrid/helpers/ip_management/ip_management_spec.rb +12 -0
  102. data/spec/sendgrid/helpers/settings/mail_settings_dto_spec.rb +3 -3
  103. data/spec/sendgrid/helpers/settings/partner_settings_dto_spec.rb +3 -3
  104. data/spec/sendgrid/helpers/settings/settings_spec.rb +2 -2
  105. data/spec/sendgrid/helpers/settings/tracking_settings_dto_spec.rb +3 -3
  106. data/spec/sendgrid/helpers/settings/user_settings_dto_spec.rb +3 -3
  107. data/spec/sendgrid/helpers/stats/email_stats_spec.rb +22 -23
  108. data/spec/sendgrid/helpers/stats/metrics_spec.rb +19 -20
  109. data/spec/sendgrid/helpers/stats/stats_response_spec.rb +22 -23
  110. data/spec/sendgrid/sendgrid_spec.rb +11 -0
  111. data/spec/sendgrid/twilio_email_spec.rb +11 -0
  112. data/spec/spec_helper.rb +3 -1
  113. data/static/img/github-fork.png +0 -0
  114. data/static/img/github-sign-up.png +0 -0
  115. data/test/sendgrid/helpers/mail/test_attachment.rb +33 -0
  116. data/test/sendgrid/helpers/mail/test_category.rb +0 -2
  117. data/test/sendgrid/helpers/mail/test_data_residency.rb +44 -0
  118. data/test/sendgrid/helpers/mail/test_email.rb +17 -10
  119. data/test/sendgrid/helpers/mail/test_mail.rb +126 -119
  120. data/test/sendgrid/helpers/mail/test_personalizations.rb +145 -92
  121. data/test/sendgrid/permissions/test_scopes.rb +36 -0
  122. data/test/sendgrid/test_sendgrid-ruby.rb +1961 -1979
  123. data/twilio_sendgrid_logo.png +0 -0
  124. data/use-cases/README.md +17 -0
  125. data/use-cases/domain-authentication.md +5 -0
  126. data/use-cases/email-statistics.md +52 -0
  127. data/use-cases/legacy-templates.md +98 -0
  128. data/use-cases/personalizations.md +34 -0
  129. data/use-cases/sms.md +39 -0
  130. data/use-cases/transactional-templates.md +111 -0
  131. data/use-cases/twilio-email.md +13 -0
  132. data/use-cases/twilio-setup.md +54 -0
  133. metadata +99 -45
  134. data/.codeclimate.yml +0 -21
  135. data/.github/ISSUE_TEMPLATE +0 -17
  136. data/.github/PULL_REQUEST_TEMPLATE +0 -24
  137. data/.travis.yml +0 -29
  138. data/LICENSE.txt +0 -22
  139. data/USE_CASES.md +0 -147
  140. data/docker/Dockerfile +0 -12
  141. data/docker/README.md +0 -30
  142. data/lib/sendgrid/client.rb +0 -35
  143. data/test/prism.sh +0 -42
@@ -2,22 +2,16 @@ require 'json'
2
2
 
3
3
  module SendGrid
4
4
  class CustomArg
5
+ attr_accessor :custom_arg
6
+
5
7
  def initialize(key: nil, value: nil)
6
8
  @custom_arg = {}
7
- (key.nil? || value.nil?) ? @custom_arg = nil : @custom_arg[key.to_s] = value.to_s
8
- end
9
-
10
- def custom_arg=(custom_arg)
11
- @custom_arg = custom_arg
12
- end
13
-
14
- def custom_arg
15
- @custom_arg
9
+ key.nil? || value.nil? ? @custom_arg = nil : @custom_arg[key.to_s] = value.to_s
16
10
  end
17
11
 
18
12
  def to_json(*)
19
13
  {
20
- 'custom_arg' => self.custom_arg
14
+ 'custom_arg' => custom_arg
21
15
  }.delete_if { |_, value| value.to_s.strip == '' }
22
16
  end
23
17
  end
@@ -2,7 +2,11 @@ require 'json'
2
2
 
3
3
  module SendGrid
4
4
  class Email
5
- def initialize(email: nil, name: nil)
5
+ attr_accessor :email, :name
6
+
7
+ # @param [String] email required e-mail address
8
+ # @param [String] name optionally personification
9
+ def initialize(email:, name: nil)
6
10
  if name
7
11
  @email = email
8
12
  @name = name
@@ -11,31 +15,17 @@ module SendGrid
11
15
  end
12
16
  end
13
17
 
14
- def email=(email)
15
- @email = email
16
- end
17
-
18
- def email
19
- @email
20
- end
21
-
22
- def name=(name)
23
- @name = name
24
- end
25
-
26
- def name
27
- @name
28
- end
29
-
30
18
  def split_email(email)
31
19
  split = /(?:(?<address>.+)\s)?<?(?<email>.+@[^>]+)>?/.match(email)
32
- return split[:email], split[:address]
20
+ raise ArgumentError, "email (#{email}) is invalid" unless split
21
+
22
+ [split[:email], split[:address]]
33
23
  end
34
24
 
35
25
  def to_json(*)
36
26
  {
37
- 'email' => self.email,
38
- 'name' => self.name
27
+ 'email' => email,
28
+ 'name' => name
39
29
  }.delete_if { |_, value| value.to_s.strip == '' }
40
30
  end
41
31
  end
@@ -2,41 +2,19 @@ require 'json'
2
2
 
3
3
  module SendGrid
4
4
  class Footer
5
- def initialize(enable: nil, text: nil, html: nil)
6
- @enable = enable
7
- @text = text
8
- @html = html
9
- end
5
+ attr_accessor :enable, :text, :html
10
6
 
11
- def enable=(enable)
7
+ def initialize(enable: nil, text: nil, html: nil)
12
8
  @enable = enable
13
- end
14
-
15
- def enable
16
- @enable
17
- end
18
-
19
- def text=(text)
20
9
  @text = text
21
- end
22
-
23
- def text
24
- @text
25
- end
26
-
27
- def html=(html)
28
10
  @html = html
29
11
  end
30
12
 
31
- def html
32
- @html
33
- end
34
-
35
13
  def to_json(*)
36
14
  {
37
- 'enable' => self.enable,
38
- 'text' => self.text,
39
- 'html' => self.html
15
+ 'enable' => enable,
16
+ 'text' => text,
17
+ 'html' => html
40
18
  }.delete_if { |_, value| value.to_s.strip == '' }
41
19
  end
42
20
  end
@@ -2,6 +2,8 @@ require 'json'
2
2
 
3
3
  module SendGrid
4
4
  class Ganalytics
5
+ attr_accessor :enable, :utm_source, :utm_medium, :utm_term, :utm_content, :utm_name, :utm_campaign
6
+
5
7
  def initialize(enable: nil, utm_source: nil, utm_medium: nil, utm_term: nil, utm_content: nil, utm_campaign: nil, utm_name: nil)
6
8
  @enable = enable
7
9
  @utm_source = utm_source
@@ -12,63 +14,15 @@ module SendGrid
12
14
  @utm_name = utm_name
13
15
  end
14
16
 
15
- def enable=(enable)
16
- @enable = enable
17
- end
18
-
19
- def enable
20
- @enable
21
- end
22
-
23
- def utm_source=(utm_source)
24
- @utm_source = utm_source
25
- end
26
-
27
- def utm_source
28
- @utm_source
29
- end
30
-
31
- def utm_medium=(utm_medium)
32
- @utm_medium = utm_medium
33
- end
34
-
35
- def utm_medium
36
- @utm_medium
37
- end
38
-
39
- def utm_term=(utm_term)
40
- @utm_term = utm_term
41
- end
42
-
43
- def utm_term
44
- @utm_term
45
- end
46
-
47
- def utm_content=(utm_content)
48
- @utm_content = utm_content
49
- end
50
-
51
- def utm_content
52
- @utm_content
53
- end
54
-
55
- def utm_campaign=(utm_campaign)
56
- @utm_campaign = utm_campaign
57
- end
58
-
59
- def utm_campaign
60
- @utm_campaign
61
- end
62
-
63
17
  def to_json(*)
64
18
  {
65
- 'enable' => self.enable,
66
- 'utm_source' => self.utm_source,
67
- 'utm_medium' => self.utm_medium,
68
- 'utm_term' => self.utm_term,
69
- 'utm_content' => self.utm_content,
70
- 'utm_campaign' => self.utm_campaign
19
+ 'enable' => enable,
20
+ 'utm_source' => utm_source,
21
+ 'utm_medium' => utm_medium,
22
+ 'utm_term' => utm_term,
23
+ 'utm_content' => utm_content,
24
+ 'utm_campaign' => utm_campaign
71
25
  }.delete_if { |_, value| value.to_s.strip == '' }
72
26
  end
73
27
  end
74
- end
28
+ end
@@ -2,22 +2,16 @@ require 'json'
2
2
 
3
3
  module SendGrid
4
4
  class Header
5
+ attr_accessor :header
6
+
5
7
  def initialize(key: nil, value: nil)
6
8
  @header = {}
7
- (key.nil? || value.nil?) ? @header = nil : @header[key] = value
8
- end
9
-
10
- def header=(header)
11
- @header = header
12
- end
13
-
14
- def header
15
- @header
9
+ key.nil? || value.nil? ? @header = nil : @header[key] = value
16
10
  end
17
11
 
18
12
  def to_json(*)
19
13
  {
20
- 'header' => self.header
14
+ 'header' => header
21
15
  }.delete_if { |_, value| value.to_s.strip == '' }
22
16
  end
23
17
  end
@@ -4,10 +4,13 @@ require 'json'
4
4
 
5
5
  module SendGrid
6
6
  class Mail
7
-
7
+ attr_accessor :subject, :ip_pool_name, :template_id, :send_at, :batch_id
8
8
  attr_reader :personalizations, :contents, :attachments, :categories, :sections, :headers, :custom_args
9
+ attr_writer :from, :asm, :mail_settings, :tracking_settings, :reply_to
9
10
 
10
- def initialize(from_email=nil, subj=nil, to_email=nil, cont=nil)
11
+ # We allow for all nil values here to create uninitialized Mail objects
12
+ # (e.g. <project-root>/use-cases/transactional-templates.md)
13
+ def initialize(from_email = nil, subj = nil, to_email = nil, cont = nil) # rubocop:disable Metrics/ParameterLists
11
14
  @from = nil
12
15
  @subject = nil
13
16
  @personalizations = []
@@ -26,32 +29,20 @@ module SendGrid
26
29
  @tracking_settings = nil
27
30
  @reply_to = nil
28
31
 
29
- if !(from_email.nil? && subj.nil? && to_email.nil? && cont.nil?)
30
- self.from = from_email
31
- self.subject = subj
32
- personalization = Personalization.new
33
- personalization.add_to(to_email)
34
- self.add_personalization(personalization)
35
- self.add_content(cont)
36
- end
37
- end
32
+ return if from_email.nil? && subj.nil? && to_email.nil? && cont.nil?
38
33
 
39
- def from=(from)
40
- @from = from
34
+ self.from = from_email
35
+ self.subject = subj
36
+ personalization = Personalization.new
37
+ personalization.add_to(to_email)
38
+ add_personalization(personalization)
39
+ add_content(cont)
41
40
  end
42
41
 
43
42
  def from
44
43
  @from.nil? ? nil : @from.to_json
45
44
  end
46
45
 
47
- def subject=(subject)
48
- @subject = subject
49
- end
50
-
51
- def subject
52
- @subject
53
- end
54
-
55
46
  def add_personalization(personalization)
56
47
  @personalizations << personalization.to_json
57
48
  end
@@ -60,6 +51,13 @@ module SendGrid
60
51
  @contents << content.to_json
61
52
  end
62
53
 
54
+ def check_for_secrets(patterns)
55
+ contents = @contents.map { |content| content['value'] }.join(' ')
56
+ patterns.each do |pattern|
57
+ raise SecurityError, 'Content contains sensitive information.' if contents.match(pattern)
58
+ end
59
+ end
60
+
63
61
  def add_attachment(attachment)
64
62
  @attachments << attachment.to_json
65
63
  end
@@ -68,14 +66,6 @@ module SendGrid
68
66
  @categories << category.name
69
67
  end
70
68
 
71
- def template_id=(template_id)
72
- @template_id = template_id
73
- end
74
-
75
- def template_id
76
- @template_id
77
- end
78
-
79
69
  def add_section(section)
80
70
  section = section.to_json
81
71
  @sections = @sections.merge(section['section'])
@@ -91,82 +81,42 @@ module SendGrid
91
81
  @custom_args = @custom_args.merge(custom_arg['custom_arg'])
92
82
  end
93
83
 
94
- def send_at=(send_at)
95
- @send_at = send_at
96
- end
97
-
98
- def send_at
99
- @send_at
100
- end
101
-
102
- def batch_id=(batch_id)
103
- @batch_id = batch_id
104
- end
105
-
106
- def batch_id
107
- @batch_id
108
- end
109
-
110
- def asm=(asm)
111
- @asm = asm
112
- end
113
-
114
84
  def asm
115
85
  @asm.nil? ? nil : @asm.to_json
116
86
  end
117
87
 
118
- def ip_pool_name=(ip_pool_name)
119
- @ip_pool_name = ip_pool_name
120
- end
121
-
122
- def ip_pool_name
123
- @ip_pool_name
124
- end
125
-
126
- def mail_settings=(mail_settings)
127
- @mail_settings = mail_settings
128
- end
129
-
130
88
  def mail_settings
131
89
  @mail_settings.nil? ? nil : @mail_settings.to_json
132
90
  end
133
91
 
134
- def tracking_settings=(tracking_settings)
135
- @tracking_settings = tracking_settings
136
- end
137
-
138
92
  def tracking_settings
139
93
  @tracking_settings.nil? ? nil : @tracking_settings.to_json
140
94
  end
141
95
 
142
- def reply_to=(reply_to)
143
- @reply_to = reply_to
144
- end
145
-
146
96
  def reply_to
147
97
  @reply_to.nil? ? nil : @reply_to.to_json
148
98
  end
149
99
 
150
100
  def to_json(*)
151
101
  {
152
- 'from' => self.from,
153
- 'subject' => self.subject,
154
- 'personalizations' => self.personalizations,
155
- 'content' => self.contents,
156
- 'attachments' => self.attachments,
157
- 'template_id' => self.template_id,
158
- 'sections' => self.sections,
159
- 'headers' => self.headers,
160
- 'categories' => self.categories,
161
- 'custom_args' => self.custom_args,
162
- 'send_at' => self.send_at,
163
- 'batch_id' => self.batch_id,
164
- 'asm' => self.asm,
165
- 'ip_pool_name' => self.ip_pool_name,
166
- 'mail_settings' => self.mail_settings,
167
- 'tracking_settings' => self.tracking_settings,
168
- 'reply_to' => self.reply_to
169
- }.delete_if { |_, value| value.to_s.strip == '' || value == [] || value == {}}
102
+ 'from' => from,
103
+ 'subject' => subject,
104
+ 'personalizations' => personalizations,
105
+ 'content' => contents,
106
+ 'attachments' => attachments,
107
+ 'template_id' => template_id,
108
+ 'sections' => sections,
109
+ 'headers' => headers,
110
+ 'categories' => categories,
111
+ 'custom_args' => custom_args,
112
+ 'send_at' => send_at,
113
+ 'batch_id' => batch_id,
114
+ 'asm' => asm,
115
+ 'ip_pool_name' => ip_pool_name,
116
+ 'mail_settings' => mail_settings,
117
+ 'tracking_settings' => tracking_settings,
118
+ 'reply_to' => reply_to
119
+ }.delete_if { |_, value| value.to_s.strip == '' || value == [] || value == {} }
170
120
  end
171
121
  end
172
122
  end
@@ -2,6 +2,8 @@ require 'json'
2
2
 
3
3
  module SendGrid
4
4
  class MailSettings
5
+ attr_writer :sandbox_mode, :footer, :bcc, :spam_check, :bypass_list_management
6
+
5
7
  def initialize
6
8
  @bcc = nil
7
9
  @bypass_list_management = nil
@@ -10,53 +12,33 @@ module SendGrid
10
12
  @spam_check = nil
11
13
  end
12
14
 
13
- def sandbox_mode=(sandbox_mode)
14
- @sandbox_mode = sandbox_mode
15
- end
16
-
17
15
  def sandbox_mode
18
16
  @sandbox_mode.nil? ? nil : @sandbox_mode.to_json
19
17
  end
20
18
 
21
- def bypass_list_management=(bypass_list_management)
22
- @bypass_list_management = bypass_list_management
23
- end
24
-
25
19
  def bypass_list_management
26
20
  @bypass_list_management.nil? ? nil : @bypass_list_management.to_json
27
21
  end
28
22
 
29
- def footer=(footer)
30
- @footer = footer
31
- end
32
-
33
23
  def footer
34
24
  @footer.nil? ? nil : @footer.to_json
35
25
  end
36
26
 
37
- def bcc=(bcc)
38
- @bcc = bcc
39
- end
40
-
41
27
  def bcc
42
28
  @bcc.nil? ? nil : @bcc.to_json
43
29
  end
44
30
 
45
- def spam_check=(spam_check)
46
- @spam_check = spam_check
47
- end
48
-
49
31
  def spam_check
50
32
  @spam_check.nil? ? nil : @spam_check.to_json
51
33
  end
52
34
 
53
35
  def to_json(*)
54
36
  {
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
37
+ 'bcc' => bcc,
38
+ 'bypass_list_management' => bypass_list_management,
39
+ 'footer' => footer,
40
+ 'sandbox_mode' => sandbox_mode,
41
+ 'spam_check' => spam_check
60
42
  }.delete_if { |_, value| value.to_s.strip == '' }
61
43
  end
62
44
  end
@@ -2,31 +2,17 @@ require 'json'
2
2
 
3
3
  module SendGrid
4
4
  class OpenTracking
5
+ attr_accessor :enable, :substitution_tag
6
+
5
7
  def initialize(enable: nil, substitution_tag: nil)
6
8
  @enable = enable
7
9
  @substitution_tag = substitution_tag
8
10
  end
9
11
 
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
12
  def to_json(*)
27
13
  {
28
- 'enable' => self.enable,
29
- 'substitution_tag' => self.substitution_tag
14
+ 'enable' => enable,
15
+ 'substitution_tag' => substitution_tag
30
16
  }.delete_if { |_, value| value.to_s.strip == '' }
31
17
  end
32
18
  end
@@ -2,12 +2,14 @@ require 'json'
2
2
 
3
3
  module SendGrid
4
4
  class Personalization
5
+ attr_reader :tos, :from, :ccs, :bccs, :headers, :substitutions, :custom_args,
6
+ :dynamic_template_data
5
7
 
6
- attr_reader :tos, :ccs, :bccs, :headers, :substitutions, :custom_args,
7
- :dynamic_template_data
8
+ attr_accessor :send_at, :subject
8
9
 
9
10
  def initialize
10
11
  @tos = []
12
+ @from = nil
11
13
  @ccs = []
12
14
  @bccs = []
13
15
  @subject = nil
@@ -19,23 +21,25 @@ module SendGrid
19
21
  end
20
22
 
21
23
  def add_to(to)
24
+ raise DuplicatePersonalizationError if duplicate?(to)
25
+
22
26
  @tos << to.to_json
23
27
  end
24
28
 
29
+ def add_from(from)
30
+ @from = from.to_json
31
+ end
32
+
25
33
  def add_cc(cc)
34
+ raise DuplicatePersonalizationError if duplicate?(cc)
35
+
26
36
  @ccs << cc.to_json
27
37
  end
28
38
 
29
39
  def add_bcc(bcc)
30
- @bccs << bcc.to_json
31
- end
32
-
33
- def subject=(subject)
34
- @subject = subject
35
- end
40
+ raise DuplicatePersonalizationError if duplicate?(bcc)
36
41
 
37
- def subject
38
- @subject
42
+ @bccs << bcc.to_json
39
43
  end
40
44
 
41
45
  def add_header(header)
@@ -57,26 +61,33 @@ module SendGrid
57
61
  @dynamic_template_data.merge!(dynamic_template_data)
58
62
  end
59
63
 
60
- def send_at=(send_at)
61
- @send_at = send_at
64
+ def to_json(*)
65
+ {
66
+ 'to' => tos,
67
+ 'from' => from,
68
+ 'cc' => ccs,
69
+ 'bcc' => bccs,
70
+ 'subject' => subject,
71
+ 'headers' => headers,
72
+ 'substitutions' => substitutions,
73
+ 'custom_args' => custom_args,
74
+ 'dynamic_template_data' => dynamic_template_data,
75
+ 'send_at' => send_at
76
+ }.delete_if { |_, value| value.to_s.strip == '' || value == [] || value == {} }
62
77
  end
63
78
 
64
- def send_at
65
- @send_at
66
- end
79
+ private
67
80
 
68
- def to_json(*)
69
- {
70
- 'to' => self.tos,
71
- 'cc' => self.ccs,
72
- 'bcc' => self.bccs,
73
- 'subject' => self.subject,
74
- 'headers' => self.headers,
75
- 'substitutions' => self.substitutions,
76
- 'custom_args' => self.custom_args,
77
- 'dynamic_template_data' => self.dynamic_template_data,
78
- 'send_at' => self.send_at
79
- }.delete_if { |_, value| value.to_s.strip == '' || value == [] || value == {}}
81
+ def duplicate?(addition)
82
+ additional_email = addition.email.downcase
83
+
84
+ [@tos, @ccs, @bccs].flatten.each do |elm|
85
+ return true if elm&.dig('email')&.downcase == additional_email
86
+ end
87
+
88
+ false
80
89
  end
81
90
  end
91
+
92
+ class DuplicatePersonalizationError < StandardError; end
82
93
  end
@@ -2,22 +2,16 @@ require 'json'
2
2
 
3
3
  module SendGrid
4
4
  class Section
5
+ attr_accessor :section
6
+
5
7
  def initialize(key: nil, value: nil)
6
8
  @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
9
+ key.nil? || value.nil? ? @section = nil : @section[key] = value
16
10
  end
17
11
 
18
12
  def to_json(*)
19
13
  {
20
- 'section' => self.section
14
+ 'section' => section
21
15
  }.delete_if { |_, value| value.to_s.strip == '' }
22
16
  end
23
17
  end
@@ -2,41 +2,19 @@ require 'json'
2
2
 
3
3
  module SendGrid
4
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
5
+ attr_accessor :enable, :threshold, :post_to_url
10
6
 
11
- def enable=(enable)
7
+ def initialize(enable: nil, threshold: nil, post_to_url: nil)
12
8
  @enable = enable
13
- end
14
-
15
- def enable
16
- @enable
17
- end
18
-
19
- def threshold=(threshold)
20
9
  @threshold = threshold
21
- end
22
-
23
- def threshold
24
- @threshold
25
- end
26
-
27
- def post_to_url=(post_to_url)
28
10
  @post_to_url = post_to_url
29
11
  end
30
12
 
31
- def post_to_url
32
- @post_to_url
33
- end
34
-
35
13
  def to_json(*)
36
14
  {
37
- 'enable' => self.enable,
38
- 'threshold' => self.threshold,
39
- 'post_to_url' => self.post_to_url
15
+ 'enable' => enable,
16
+ 'threshold' => threshold,
17
+ 'post_to_url' => post_to_url
40
18
  }.delete_if { |_, value| value.to_s.strip == '' }
41
19
  end
42
20
  end