-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from kernkw/settings_helper
Adds an account settings management helper object
- Loading branch information
Showing
20 changed files
with
291 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
source 'http://rubygems.org' | ||
|
||
gemspec | ||
|
||
gem 'ruby_http_client' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
require 'rake/testtask' | ||
require 'bundler/gem_tasks' | ||
require 'rspec/core/rake_task' | ||
|
||
Rake::TestTask.new do |t| | ||
t.libs << 'test' | ||
t.test_files = FileList['test/sendgrid/test*.rb', 'test/sendgrid/helpers/mail/test*.rb'] | ||
t.verbose = true | ||
end | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
|
||
desc "Run tests" | ||
task :default => :test | ||
task default: [:spec, :test] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'sendgrid-ruby' | ||
include SendGrid | ||
|
||
sg_client = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']).client | ||
settings = SendGrid::Settings.new(sendgrid_client: sg_client) | ||
|
||
# Fetch settings | ||
response = settings.bcc | ||
puts response.status_code | ||
puts response.body | ||
puts response.headers | ||
|
||
# Turn on bcc settings | ||
response = settings.update_bcc(enabled: true, email: "[email protected]") | ||
puts response.status_code | ||
puts response.body | ||
puts response.headers | ||
|
||
# Turn off bcc settings | ||
response = settings.update_bcc(enabled: false) | ||
puts response.status_code | ||
puts response.body | ||
puts response.headers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
require_relative 'sendgrid/client' | ||
require_relative 'sendgrid/version' | ||
require_relative 'sendgrid/helpers/mail/mail' | ||
require_relative 'sendgrid/helpers/mail/mail' | ||
require_relative 'sendgrid/helpers/settings/settings' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
**This module allows you to quickly and easily build a Settings object for retreiving or updating you SendGrid Settings.** | ||
|
||
# Quick Start | ||
|
||
Run the [example](https://github.com/sendgrid/sendgrid-ruby/tree/master/examples/helpers/settings) (make sure you have set your environment variable to include your SENDGRID_API_KEY). | ||
|
||
```bash | ||
ruby examples/helpers/settings/example.rb | ||
``` | ||
|
||
## Usage | ||
|
||
- See the [example](https://github.com/sendgrid/sendgrid-ruby/tree/master/examples/helpers/settings) for a complete working example. | ||
- [Documentation](https://sendgrid.com/docs/API_Reference/Web_API_v3/Settings/index.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module SendGrid | ||
class MailSettingsDto | ||
attr_reader :bcc, :address_whitelist, :bounce_purge, :footer, :forward_spam, :forward_bounce, :plain_content, :spam_check, :template | ||
|
||
def self.fetch(sendgrid_client:, name:, query_params:) | ||
sendgrid_client.mail_settings.public_send(name).get(query_params: query_params) | ||
end | ||
|
||
def self.update(sendgrid_client:, name:, request_body:) | ||
sendgrid_client.mail_settings.public_send(name).patch(request_body: request_body) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module SendGrid | ||
class PartnerSettingsDto | ||
attr_reader :new_relic | ||
|
||
def self.fetch(sendgrid_client:, name:, query_params:) | ||
sendgrid_client.partner_settings.public_send(name).get(query_params: query_params) | ||
end | ||
|
||
def self.update(sendgrid_client:, name:, request_body:) | ||
sendgrid_client.partner_settings.public_send(name).patch(request_body: request_body) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require_relative 'mail_settings_dto' | ||
require_relative 'partner_settings_dto' | ||
require_relative 'tracking_settings_dto' | ||
require_relative 'user_settings_dto' | ||
|
||
module SendGrid | ||
class Settings | ||
attr_accessor :sendgrid_client | ||
|
||
SETTING_TYPES = [SendGrid::MailSettingsDto, SendGrid::TrackingSettingsDto, | ||
SendGrid::PartnerSettingsDto, SendGrid::UserSettingsDto] | ||
|
||
def initialize(sendgrid_client:) | ||
@sendgrid_client = sendgrid_client | ||
end | ||
|
||
SETTING_TYPES.each do |setting_type| | ||
setting_type.instance_methods(false).each do |name| | ||
define_method(name) do |**args| | ||
setting_type.fetch(sendgrid_client: sendgrid_client, name: name, query_params: args) | ||
end | ||
define_method("update_#{name}") do |**args| | ||
setting_type.update(sendgrid_client: sendgrid_client, name: name, request_body: args) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module SendGrid | ||
class TrackingSettingsDto | ||
attr_reader :open, :click, :google_analytics, :subscription | ||
alias :click_tracking :click | ||
alias :open_tracking :open | ||
alias :subscription_tracking :subscription | ||
|
||
def self.fetch(sendgrid_client:, name:, query_params:) | ||
name = scrub_alias_names(name.to_s) | ||
sendgrid_client.tracking_settings.public_send(name).get(query_params: query_params) | ||
end | ||
|
||
def self.update(sendgrid_client:, name:, request_body:) | ||
name = scrub_alias_names(name.to_s) | ||
sendgrid_client.tracking_settings.public_send(name).patch(request_body: request_body) | ||
end | ||
|
||
private | ||
|
||
def self.scrub_alias_names(name) | ||
name.gsub(/_tracking/, '') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module SendGrid | ||
class UserSettingsDto | ||
attr_reader :enforced_tls | ||
|
||
def self.fetch(sendgrid_client:, name:, query_params:) | ||
sendgrid_client.user.settings.public_send(name).get(query_params: query_params) | ||
end | ||
|
||
def self.update(sendgrid_client:, name:, request_body:) | ||
sendgrid_client.user.settings.public_send(name).patch(request_body: request_body) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module SendGrid | ||
VERSION = '4.2.1' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require 'spec_helper' | ||
|
||
describe SendGrid::MailSettingsDto do | ||
let(:sendgrid_client) { SendGrid::API.new(api_key: 'fake_key').client } | ||
let(:mail_settings) { SendGrid::MailSettingsDto } | ||
let(:setting_name) { 'bcc' } | ||
let(:setting_params) { {email: Faker::Internet.email, enabled: rand(1..100).even?} } | ||
|
||
it { should respond_to :bcc } | ||
it { should respond_to :address_whitelist } | ||
it { should respond_to :bounce_purge } | ||
it { should respond_to :footer } | ||
it { should respond_to :forward_spam } | ||
it { should respond_to :forward_bounce } | ||
it { should respond_to :plain_content } | ||
it { should respond_to :spam_check } | ||
it { should respond_to :template } | ||
|
||
describe '.fetch' do | ||
it 'calls get on sendgrid_client' do | ||
args = { sendgrid_client: sendgrid_client, name: setting_name, query_params: {} } | ||
expect(mail_settings.fetch(args)).to be_a SendGrid::Response | ||
end | ||
end | ||
|
||
describe '.update' do | ||
it 'calls patch on sendgrid_client' do | ||
args = { sendgrid_client: sendgrid_client, name: setting_name, request_body: setting_params } | ||
expect(mail_settings.update(args)).to be_a SendGrid::Response | ||
end | ||
end | ||
end |
24 changes: 24 additions & 0 deletions
24
spec/sendgrid/helpers/settings/partner_settings_dto_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'spec_helper' | ||
|
||
describe SendGrid::PartnerSettingsDto do | ||
let(:sendgrid_client) { SendGrid::API.new(api_key: 'fake_key').client } | ||
let(:partner_settings) { SendGrid::PartnerSettingsDto } | ||
let(:setting_name) { 'new_relic' } | ||
let(:setting_params) { {license_key: 'key', enabled: rand(1..100).even?} } | ||
|
||
it { should respond_to :new_relic } | ||
|
||
describe '.fetch' do | ||
it 'calls get on sendgrid_client' do | ||
args = { sendgrid_client: sendgrid_client, name: setting_name, query_params: {} } | ||
expect(partner_settings.fetch(args)).to be_a SendGrid::Response | ||
end | ||
end | ||
|
||
describe '.update' do | ||
it 'calls patch on sendgrid_client' do | ||
args = { sendgrid_client: sendgrid_client, name: setting_name, request_body: setting_params } | ||
expect(partner_settings.update(args)).to be_a SendGrid::Response | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'spec_helper' | ||
|
||
describe SendGrid::Settings do | ||
let(:sendgrid_client) { SendGrid::API.new(api_key: 'fake_key').client } | ||
let(:settings) { SendGrid::Settings.new(sendgrid_client: sendgrid_client) } | ||
|
||
describe '.new' do | ||
it 'initializes correctly' do | ||
expect(settings).to be_a SendGrid::Settings | ||
end | ||
end | ||
|
||
describe '.bcc' do | ||
it 'fetches bcc data' do | ||
expect(settings.bcc).to be_a SendGrid::Response | ||
end | ||
end | ||
|
||
describe '.update_bcc' do | ||
it 'updates bcc' do | ||
bcc_response = settings.update_bcc(enabled: true, email: "[email protected]") | ||
expect(bcc_response).to be_a SendGrid::Response | ||
end | ||
end | ||
end |
27 changes: 27 additions & 0 deletions
27
spec/sendgrid/helpers/settings/tracking_settings_dto_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require 'spec_helper' | ||
|
||
describe SendGrid::TrackingSettingsDto do | ||
let(:sendgrid_client) { SendGrid::API.new(api_key: 'fake_key').client } | ||
let(:tracking_settings) { SendGrid::TrackingSettingsDto } | ||
let(:setting_name) { 'open_tracking' } | ||
let(:setting_params) { {enabled: rand(1..100).even?} } | ||
|
||
it { should respond_to :open_tracking } | ||
it { should respond_to :click_tracking } | ||
it { should respond_to :google_analytics } | ||
it { should respond_to :subscription_tracking } | ||
|
||
describe '.fetch' do | ||
it 'calls get on sendgrid_client' do | ||
args = { sendgrid_client: sendgrid_client, name: setting_name, query_params: {} } | ||
expect(tracking_settings.fetch(args)).to be_a SendGrid::Response | ||
end | ||
end | ||
|
||
describe '.update' do | ||
it 'calls patch on sendgrid_client' do | ||
args = { sendgrid_client: sendgrid_client, name: setting_name, request_body: setting_params } | ||
expect(tracking_settings.update(args)).to be_a SendGrid::Response | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'spec_helper' | ||
|
||
describe SendGrid::UserSettingsDto do | ||
let(:sendgrid_client) { SendGrid::API.new(api_key: 'fake_key').client } | ||
let(:user_settings) { SendGrid::UserSettingsDto } | ||
let(:setting_name) { 'enforced_tls' } | ||
let(:setting_params) { {require_tls: rand(1..100).even?} } | ||
|
||
it { should respond_to :enforced_tls } | ||
|
||
describe '.fetch' do | ||
it 'calls get on sendgrid_client' do | ||
args = { sendgrid_client: sendgrid_client, name: setting_name, query_params: {} } | ||
expect(user_settings.fetch(args)).to be_a SendGrid::Response | ||
end | ||
end | ||
|
||
describe '.update' do | ||
it 'calls patch on sendgrid_client' do | ||
args = { sendgrid_client: sendgrid_client, name: setting_name, request_body: setting_params } | ||
expect(user_settings.update(args)).to be_a SendGrid::Response | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require 'rubygems' | ||
require 'bundler/setup' | ||
require 'pry' | ||
require 'faker' | ||
|
||
RSpec.configure do |config| | ||
Dir["#{File.dirname(__FILE__)}/../lib/sendgrid-ruby.rb"].sort.each { |ext| require ext } | ||
|
||
config.color = true | ||
end |