Skip to content

Commit

Permalink
Merge pull request #70 from kernkw/settings_helper
Browse files Browse the repository at this point in the history
Adds an account settings management helper object
  • Loading branch information
thinkingserious authored Apr 17, 2017
2 parents 9d1bbf1 + 57ecb71 commit 1210708
Show file tree
Hide file tree
Showing 20 changed files with 291 additions and 4 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ notifications:
format: html
notify: false
script:
- rake
- rake test
- rake spec
before_install:
- gem update bundler

3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
source 'http://rubygems.org'

gemspec

gem 'ruby_http_client'

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ Please see [our helper](https://github.com/sendgrid/sendgrid-ruby/tree/master/li
- [Example Code](https://github.com/sendgrid/sendgrid-ruby/tree/master/examples)
- [How-to: Migration from v2 to v3](https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/how_to_migrate_from_v2_to_v3_mail_send.html)
- [v3 Web API Mail Send Helper](https://github.com/sendgrid/sendgrid-ruby/tree/master/lib/sendgrid/helpers/mail) - build a request object payload for a v3 /mail/send API call.
- [Settings Helper](https://github.com/sendgrid/sendgrid-ruby/tree/master/lib/sendgrid/helpers/settings)

<a name="use_cases"></a>
# Use Cases
Expand Down
7 changes: 6 additions & 1 deletion Rakefile
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]

23 changes: 23 additions & 0 deletions examples/helpers/settings/example.rb
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
3 changes: 2 additions & 1 deletion lib/sendgrid-ruby.rb
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'
14 changes: 14 additions & 0 deletions lib/sendgrid/helpers/settings/README.md
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)
13 changes: 13 additions & 0 deletions lib/sendgrid/helpers/settings/mail_settings_dto.rb
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
13 changes: 13 additions & 0 deletions lib/sendgrid/helpers/settings/partner_settings_dto.rb
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
28 changes: 28 additions & 0 deletions lib/sendgrid/helpers/settings/settings.rb
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
24 changes: 24 additions & 0 deletions lib/sendgrid/helpers/settings/tracking_settings_dto.rb
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
13 changes: 13 additions & 0 deletions lib/sendgrid/helpers/settings/user_settings_dto.rb
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
2 changes: 1 addition & 1 deletion lib/sendgrid/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SendGrid
VERSION = '4.2.1'
end
end
3 changes: 3 additions & 0 deletions sendgrid-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ Gem::Specification.new do |spec|
spec.add_dependency 'ruby_http_client', '~> 3.0'
spec.add_dependency 'sinatra', '~> 1.4.7'
spec.add_development_dependency 'rake', '~> 0'
spec.add_development_dependency 'rspec'
spec.add_development_dependency 'pry'
spec.add_development_dependency 'faker'
spec.add_development_dependency 'minitest', '~> 5.9'
end
32 changes: 32 additions & 0 deletions spec/sendgrid/helpers/settings/mail_settings_dto_spec.rb
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 spec/sendgrid/helpers/settings/partner_settings_dto_spec.rb
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
25 changes: 25 additions & 0 deletions spec/sendgrid/helpers/settings/settings_spec.rb
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 spec/sendgrid/helpers/settings/tracking_settings_dto_spec.rb
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
24 changes: 24 additions & 0 deletions spec/sendgrid/helpers/settings/user_settings_dto_spec.rb
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
10 changes: 10 additions & 0 deletions spec/spec_helper.rb
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

0 comments on commit 1210708

Please sign in to comment.