Skip to content

Commit 92a9db2

Browse files
committed
Adds settings module to help users manage settings
1 parent 77731f2 commit 92a9db2

17 files changed

Lines changed: 299 additions & 1 deletion

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
source 'http://rubygems.org'
22

3+
gemspec
4+
35
gem 'ruby_http_client'

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,27 @@ puts response.status_code
8181
puts response.body
8282
puts response.headers
8383
```
84+
## Manage Account Settings
85+
86+
```ruby
87+
require 'sendgrid-ruby'
88+
include SendGrid
89+
90+
sg_client = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']).client
91+
settings = SendGrid::Settings.new(sendgrid_client: sg_client)
92+
93+
# Fetch settings
94+
response = settings.bcc
95+
puts response.status_code
96+
puts response.body
97+
puts response.headers
98+
99+
# Update bcc settings
100+
response = settings.update_bcc(enabled: true, email: "email@example.com")
101+
puts response.status_code
102+
puts response.body
103+
puts response.headers
104+
```
84105

85106
## General v3 Web API Usage
86107

examples/settings/settings.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require 'sendgrid-ruby'
2+
include SendGrid
3+
4+
sg_client = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']).client
5+
settings = SendGrid::Settings.new(sendgrid_client: sg_client)
6+
7+
# Fetch settings
8+
response = settings.bcc
9+
puts response.status_code
10+
puts response.body
11+
puts response.headers
12+
13+
14+
# Turn on bcc settings
15+
response = settings.update_bcc(enabled: true, email: "email@example.com")
16+
puts response.status_code
17+
puts response.body
18+
puts response.headers
19+
20+
# Turn off bcc settings
21+
response = settings.update_bcc(enabled: false)
22+
puts response.status_code
23+
puts response.body
24+
puts response.headers

lib/sendgrid-ruby.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
require_relative 'sendgrid/client'
22
require_relative 'sendgrid/version'
3-
require_relative 'sendgrid/helpers/mail/mail'
3+
require_relative 'sendgrid/helpers/mail/mail'
4+
require_relative 'sendgrid/settings/settings'

lib/sendgrid/settings/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
**This module allows you to quickly and easily build a Settings object for retreiving or updating you SendGrid Settings.**
2+
3+
# Quick Start
4+
5+
Run the [example](https://github.com/sendgrid/sendgrid-ruby/tree/master/examples/settings/settings) (make sure you have set your environment variable to include your SENDGRID_API_KEY).
6+
7+
```bash
8+
ruby examples/settings/settings.rb
9+
```
10+
11+
## Usage
12+
13+
- See the [example](https://github.com/sendgrid/sendgrid-ruby/tree/master/examples/settings/settings) for a complete working example.
14+
- [Documentation](https://sendgrid.com/docs/API_Reference/Web_API_v3/Settings/index.html)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module SendGrid
2+
class MailSettings
3+
attr_reader :bcc, :address_whitelist, :bounce_purge, :footer, :forward_spam, :forward_bounce, :plain_content, :spam_check, :template
4+
5+
def self.fetch(sendgrid_client:, name:, query_params:)
6+
sendgrid_client.mail_settings.public_send(name).get(query_params: query_params)
7+
end
8+
9+
def self.update(sendgrid_client:, name:, request_body:)
10+
sendgrid_client.mail_settings.public_send(name).patch(request_body: request_body)
11+
end
12+
end
13+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module SendGrid
2+
class PartnerSettings
3+
attr_reader :new_relic
4+
5+
def self.fetch(sendgrid_client:, name:, query_params:)
6+
sendgrid_client.partner_settings.public_send(name).get(query_params: query_params)
7+
end
8+
9+
def self.update(sendgrid_client:, name:, request_body:)
10+
sendgrid_client.partner_settings.public_send(name).patch(request_body: request_body)
11+
end
12+
end
13+
end

lib/sendgrid/settings/settings.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require_relative 'mail_settings'
2+
require_relative 'partner_settings'
3+
require_relative 'tracking_settings'
4+
require_relative 'user_settings'
5+
6+
module SendGrid
7+
class Settings
8+
attr_accessor :sendgrid_client
9+
10+
SETTING_TYPES = [SendGrid::MailSettings, SendGrid::TrackingSettings,
11+
SendGrid::PartnerSettings, SendGrid::UserSettings]
12+
13+
def initialize(sendgrid_client:)
14+
@sendgrid_client = sendgrid_client
15+
end
16+
17+
SETTING_TYPES.each do |setting_type|
18+
setting_type.instance_methods(false).each do |name|
19+
define_method(name) do |**args|
20+
setting_type.fetch(sendgrid_client: sendgrid_client, name: name, query_params: args)
21+
end
22+
define_method("update_#{name}") do |**args|
23+
setting_type.update(sendgrid_client: sendgrid_client, name: name, request_body: args)
24+
end
25+
end
26+
end
27+
end
28+
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module SendGrid
2+
class TrackingSettings
3+
attr_reader :open, :click, :google_analytics, :subscription
4+
alias :click_tracking :click
5+
alias :open_tracking :open
6+
alias :subscription_tracking :subscription
7+
8+
def self.fetch(sendgrid_client:, name:, query_params:)
9+
name = scrub_alias_names(name.to_s)
10+
sendgrid_client.tracking_settings.public_send(name).get(query_params: query_params)
11+
end
12+
13+
def self.update(sendgrid_client:, name:, request_body:)
14+
name = scrub_alias_names(name.to_s)
15+
sendgrid_client.tracking_settings.public_send(name).patch(request_body: request_body)
16+
end
17+
18+
private
19+
20+
def self.scrub_alias_names(name)
21+
name.gsub(/_tracking/, '')
22+
end
23+
end
24+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module SendGrid
2+
class UserSettings
3+
attr_reader :enforced_tls
4+
5+
def self.fetch(sendgrid_client:, name:, query_params:)
6+
sendgrid_client.user.settings.public_send(name).get(query_params: query_params)
7+
end
8+
9+
def self.update(sendgrid_client:, name:, request_body:)
10+
sendgrid_client.user.settings.public_send(name).patch(request_body: request_body)
11+
end
12+
end
13+
end

0 commit comments

Comments
 (0)