Skip to content

Commit 780e7a0

Browse files
authored
feat: geolocation setter in sendgrid-ruby for GDPR compliance (#496)
* feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance
1 parent e17be75 commit 780e7a0

3 files changed

Lines changed: 109 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require 'sendgrid-ruby'
2+
3+
# Example 1
4+
# Sending using "global" data residency
5+
6+
from = SendGrid::Email.new(email: 'example@abc.com')
7+
to = SendGrid::Email.new(email: 'example@abc.com')
8+
subject = 'Sending with Twilio SendGrid is Fun'
9+
content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby')
10+
mail = SendGrid::Mail.new(from, subject, to, content)
11+
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
12+
sg.sendgrid_data_residency(region: "global")
13+
puts sg.host
14+
response = sg.client.mail._('send').post(request_body: mail.to_json)
15+
puts response.status_code
16+
puts response.body
17+
puts response.headers
18+
19+
# Example 2
20+
# Sending using "eu" data residency
21+
22+
from = SendGrid::Email.new(email: 'example@abc.com')
23+
to = SendGrid::Email.new(email: 'example@abc.com')
24+
subject = 'Sending with Twilio SendGrid is Fun'
25+
content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby')
26+
mail = SendGrid::Mail.new(from, subject, to, content)
27+
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY_EU'])
28+
sg.sendgrid_data_residency(region: 'eu')
29+
puts sg.host
30+
response = sg.client.mail._('send').post(request_body: mail.to_json)
31+
puts response.status_code
32+
puts response.body
33+
puts response.headers
34+
35+
# Example 3
36+
# Sending using no data residency
37+
38+
from = SendGrid::Email.new(email: 'example@abc.com')
39+
to = SendGrid::Email.new(email: 'example@abc.com')
40+
subject = 'Sending with Twilio SendGrid is Fun'
41+
content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby')
42+
mail = SendGrid::Mail.new(from, subject, to, content)
43+
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
44+
puts sg.host
45+
response = sg.client.mail._('send').post(request_body: mail.to_json)
46+
puts response.status_code
47+
puts response.body
48+
puts response.headers

lib/sendgrid/base_interface.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,21 @@ def initialize(auth:, host:, request_headers: nil, version: nil, impersonate_sub
3737
request_headers: @request_headers,
3838
http_options: @http_options)
3939
end
40+
41+
# Client libraries contain setters for specifying region/edge.
42+
# This supports global and eu regions only. This set will likely expand in the future.
43+
# Global is the default residency (or region)
44+
# Global region means the message will be sent through https://api.sendgrid.com
45+
# EU region means the message will be sent through https://api.eu.sendgrid.com
46+
# Parameters:
47+
# - region(String) : specify the region. Currently supports "global" and "eu"
48+
def sendgrid_data_residency(region:)
49+
region_host_dict = { "eu" => 'https://api.eu.sendgrid.com', "global" => 'https://api.sendgrid.com' }
50+
raise ArgumentError, "region can only be \"eu\" or \"global\"" if region.nil? || !region_host_dict.key?(region)
51+
52+
@host = region_host_dict[region]
53+
@client = SendGrid::Client.new(host: "#{@host}/#{@version}",
54+
request_headers: @request_headers,
55+
http_options: @http_options)
56+
end
4057
end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require_relative '../../../../lib/sendgrid-ruby'
2+
require 'minitest/autorun'
3+
4+
class TestDataResidency < Minitest::Test
5+
include SendGrid
6+
7+
def setup
8+
@global_email = 'https://api.sendgrid.com'
9+
@eu_email = 'https://api.eu.sendgrid.com'
10+
end
11+
12+
def test_with_global_data_residency
13+
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
14+
sg.sendgrid_data_residency(region: 'global')
15+
assert_equal @global_email, sg.host
16+
end
17+
18+
def test_with_global_eu_residency
19+
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
20+
sg.sendgrid_data_residency(region: 'eu')
21+
assert_equal @eu_email, sg.host
22+
end
23+
24+
def test_with_global_nil_residency
25+
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
26+
assert_raises(ArgumentError) do
27+
sg.sendgrid_data_residency(region: nil)
28+
end
29+
end
30+
31+
def test_with_global_invalid_residency
32+
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
33+
assert_raises(ArgumentError) do
34+
sg.sendgrid_data_residency(region: "abc")
35+
end
36+
end
37+
38+
def test_with_global_empty_residency
39+
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
40+
assert_raises(ArgumentError) do
41+
sg.sendgrid_data_residency(region: "")
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)