Skip to content

Commit 89e54ed

Browse files
author
tkbky
committed
Fix unexpected Mail #categories, #categories= behavior
1 parent 417eb48 commit 89e54ed

3 files changed

Lines changed: 72 additions & 11 deletions

File tree

lib/sendgrid/helpers/mail/mail.rb

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -475,23 +475,28 @@ def to_json(*)
475475
end
476476

477477
class Category
478+
attr_accessor :name
479+
478480
def initialize(name: nil)
479-
@category = name
481+
@name = name
480482
end
481483

482-
def category=(category)
483-
@category = category
484+
def name=(name)
485+
@name = name
484486
end
485487

486-
def category
487-
@category
488+
def name
489+
@name
488490
end
489491

490492
def to_json(*)
491493
{
492-
'category' => self.category
494+
'category' => name
493495
}.delete_if { |_, value| value.to_s.strip == '' }
494496
end
497+
498+
alias :category :name
499+
alias :category= :name=
495500
end
496501

497502
class Section
@@ -899,13 +904,12 @@ def headers
899904
end
900905

901906
def categories=(category)
902-
@categories = @categories.nil? ? [] : @categories
903-
category = category.to_json
904-
@categories = @categories.push(category['category'])
907+
raise ArgumentError.new("#{category.inspect} is not Category") unless category.is_a?(Category)
908+
(@categories ||= []) << category.name
905909
end
906910

907911
def categories
908-
@categories
912+
@categories ||= []
909913
end
910914

911915
def custom_args=(custom_args)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

test/sendgrid/helpers/mail/test_mail.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_hello_world
1717
mail.personalizations = personalization
1818
mail.contents = Content.new(type: "text/plain", value: "some text here")
1919
mail.contents = Content.new(type: "text/html", value: "<html><body>some text here</body></html>")
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"}'))
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", "categories": []}'))
2121
end
2222

2323
def test_kitchen_sink
@@ -119,4 +119,34 @@ def test_kitchen_sink
119119

120120
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"}}}'))
121121
end
122+
123+
def test_that_categories_is_empty_initially
124+
mail = Mail.new
125+
assert_equal(mail.categories, [])
126+
end
127+
128+
def test_assign_categories_with_non_category_argument
129+
mail = Mail.new
130+
invalid_category = 'foo'
131+
assert_raises(ArgumentError, "#{invalid_category.inspect} is not Category") do
132+
mail.categories = invalid_category
133+
end
134+
end
135+
136+
def test_assign_categories_with_valid_argument
137+
mail = Mail.new
138+
valid_category = Category.new(name: 'foo')
139+
mail.categories = valid_category
140+
assert_equal(mail.categories, ['foo'])
141+
end
142+
143+
def test_assign_categories_with_more_than_one_valid_arguments
144+
mail = Mail.new
145+
valid_category_1 = Category.new(name: 'foo')
146+
mail.categories = valid_category_1
147+
assert_equal(mail.categories, ['foo'])
148+
valid_category_2 = Category.new(name: 'bar')
149+
mail.categories = valid_category_2
150+
assert_equal(mail.categories, ['foo', 'bar'])
151+
end
122152
end

0 commit comments

Comments
 (0)