Skip to content

Commit

Permalink
feat: geolocation setter in sendgrid-ruby for GDPR compliance (#496)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
manisha1997 authored Nov 23, 2023
1 parent e17be75 commit 780e7a0
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
48 changes: 48 additions & 0 deletions examples/dataresidency/setregion.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'sendgrid-ruby'

# Example 1
# Sending using "global" data residency

from = SendGrid::Email.new(email: '[email protected]')
to = SendGrid::Email.new(email: '[email protected]')
subject = 'Sending with Twilio SendGrid is Fun'
content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby')
mail = SendGrid::Mail.new(from, subject, to, content)
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
sg.sendgrid_data_residency(region: "global")
puts sg.host
response = sg.client.mail._('send').post(request_body: mail.to_json)
puts response.status_code
puts response.body
puts response.headers

# Example 2
# Sending using "eu" data residency

from = SendGrid::Email.new(email: '[email protected]')
to = SendGrid::Email.new(email: '[email protected]')
subject = 'Sending with Twilio SendGrid is Fun'
content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby')
mail = SendGrid::Mail.new(from, subject, to, content)
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY_EU'])
sg.sendgrid_data_residency(region: 'eu')
puts sg.host
response = sg.client.mail._('send').post(request_body: mail.to_json)
puts response.status_code
puts response.body
puts response.headers

# Example 3
# Sending using no data residency

from = SendGrid::Email.new(email: '[email protected]')
to = SendGrid::Email.new(email: '[email protected]')
subject = 'Sending with Twilio SendGrid is Fun'
content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby')
mail = SendGrid::Mail.new(from, subject, to, content)
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
puts sg.host
response = sg.client.mail._('send').post(request_body: mail.to_json)
puts response.status_code
puts response.body
puts response.headers
17 changes: 17 additions & 0 deletions lib/sendgrid/base_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,21 @@ def initialize(auth:, host:, request_headers: nil, version: nil, impersonate_sub
request_headers: @request_headers,
http_options: @http_options)
end

# Client libraries contain setters for specifying region/edge.
# This supports global and eu regions only. This set will likely expand in the future.
# Global is the default residency (or region)
# Global region means the message will be sent through https://api.sendgrid.com
# EU region means the message will be sent through https://api.eu.sendgrid.com
# Parameters:
# - region(String) : specify the region. Currently supports "global" and "eu"
def sendgrid_data_residency(region:)
region_host_dict = { "eu" => 'https://api.eu.sendgrid.com', "global" => 'https://api.sendgrid.com' }
raise ArgumentError, "region can only be \"eu\" or \"global\"" if region.nil? || !region_host_dict.key?(region)

@host = region_host_dict[region]
@client = SendGrid::Client.new(host: "#{@host}/#{@version}",
request_headers: @request_headers,
http_options: @http_options)
end
end
44 changes: 44 additions & 0 deletions test/sendgrid/helpers/mail/test_data_residency.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require_relative '../../../../lib/sendgrid-ruby'
require 'minitest/autorun'

class TestDataResidency < Minitest::Test
include SendGrid

def setup
@global_email = 'https://api.sendgrid.com'
@eu_email = 'https://api.eu.sendgrid.com'
end

def test_with_global_data_residency
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
sg.sendgrid_data_residency(region: 'global')
assert_equal @global_email, sg.host
end

def test_with_global_eu_residency
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
sg.sendgrid_data_residency(region: 'eu')
assert_equal @eu_email, sg.host
end

def test_with_global_nil_residency
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
assert_raises(ArgumentError) do
sg.sendgrid_data_residency(region: nil)
end
end

def test_with_global_invalid_residency
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
assert_raises(ArgumentError) do
sg.sendgrid_data_residency(region: "abc")
end
end

def test_with_global_empty_residency
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
assert_raises(ArgumentError) do
sg.sendgrid_data_residency(region: "")
end
end
end

0 comments on commit 780e7a0

Please sign in to comment.