Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow unique args to be set at the class level #7

Merged
merged 2 commits into from
May 14, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ gem "json"
# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
group :development do
# gem "shoulda", ">= 0"
gem "shoulda", ">= 0"
gem "bundler", "~> 1.0.0"
gem "jeweler", "~> 1.5.1"
# gem "rcov", ">= 0"
end

group :test do
gem "actionmailer", ">= 2.3.2"
end
49 changes: 49 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,18 +1,67 @@
GEM
remote: http://rubygems.org/
specs:
actionmailer (3.1.5)
actionpack (= 3.1.5)
mail (~> 2.3.3)
actionpack (3.1.5)
activemodel (= 3.1.5)
activesupport (= 3.1.5)
builder (~> 3.0.0)
erubis (~> 2.7.0)
i18n (~> 0.6)
rack (~> 1.3.6)
rack-cache (~> 1.2)
rack-mount (~> 0.8.2)
rack-test (~> 0.6.1)
sprockets (~> 2.0.4)
activemodel (3.1.5)
activesupport (= 3.1.5)
builder (~> 3.0.0)
i18n (~> 0.6)
activesupport (3.1.5)
multi_json (>= 1.0, < 1.3)
builder (3.0.0)
erubis (2.7.0)
git (1.2.5)
hike (1.2.1)
i18n (0.6.0)
jeweler (1.5.2)
bundler (~> 1.0.0)
git (>= 1.2.5)
rake
json (1.5.1)
mail (2.3.3)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.18)
multi_json (1.2.0)
polyglot (0.3.3)
rack (1.3.6)
rack-cache (1.2)
rack (>= 0.4)
rack-mount (0.8.3)
rack (>= 1.0.0)
rack-test (0.6.1)
rack (>= 1.0)
rake (0.8.7)
shoulda (2.10.3)
sprockets (2.0.4)
hike (~> 1.2)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
tilt (1.3.3)
treetop (1.4.10)
polyglot
polyglot (>= 0.3.1)

PLATFORMS
ruby

DEPENDENCIES
actionmailer (>= 2.3.2)
bundler (~> 1.0.0)
jeweler (~> 1.5.1)
json
shoulda
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ rescue LoadError
end
end

task :test => :check_dependencies
task :test

task :default => :test

Expand Down
22 changes: 17 additions & 5 deletions lib/sendgrid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def self.included(base)
base.class_eval do
class << self
attr_accessor :default_sg_category, :default_sg_options, :default_subscriptiontrack_text,
:default_footer_text, :default_spamcheck_score
:default_footer_text, :default_spamcheck_score, :default_sg_unique_args
end
attr_accessor :sg_category, :sg_options, :sg_disabled_options, :sg_recipients, :sg_substitutions, :subscriptiontrack_text, :footer_text, :spamcheck_score
end
Expand Down Expand Up @@ -59,6 +59,13 @@ def sendgrid_enable(*options)
options.each { |option| self.default_sg_options << option if VALID_OPTIONS.include?(option) }
end

# Sets unique args at the class level. Should be a hash
# of name, value pairs.
# { :some_unique_arg => "some_value"}
def sendgrid_unique_args(args)
self.default_sg_unique_args = args
end

# Sets the default text for subscription tracking (must be enabled).
# There are two options:
# 1. Add an unsubscribe link at the bottom of the email
Expand Down Expand Up @@ -129,10 +136,11 @@ def sendgrid_spamcheck_maxscore(score)
end

# Call within mailer method to set unique args for this email.
# Merged with class-level unique args, if any exist.
def sendgrid_unique_args(args)
@sg_unique_args = args
end

# only override the appropriate methods for the current ActionMailer version
if ActionMailer::Base.respond_to?(:mail)

Expand Down Expand Up @@ -211,8 +219,13 @@ def sendgrid_json_headers(mail)
end

# Set unique_args
if self.class.default_sg_unique_args && !self.class.default_sg_unique_args.empty?
header_opts[:unique_args] = self.class.default_sg_unique_args
end

if @sg_unique_args && !@sg_unique_args.empty?
header_opts[:unique_args] = @sg_unique_args
header_opts[:unique_args] ||= @sg_unique_args
header_opts[:unique_args].merge!(@sg_unique_args)
end

header_opts.to_json.gsub(/(["\]}])([,:])(["\[{])/, '\\1\\2 \\3')
Expand Down Expand Up @@ -264,5 +277,4 @@ def filters_hash_from_options(enabled_opts, disabled_opts)

return filters
end

end
end
18 changes: 16 additions & 2 deletions test/sendgrid_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
require 'test_helper'

class SendgridTest < Test::Unit::TestCase
def setup
def setup
ActionMailer::Base.delivery_method = :test
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.deliveries = []

@options = {
:to => ['test@example.com'],
:from => 'test@example.com',
Expand All @@ -18,7 +22,17 @@ def setup

should "require the same number of items in a substitution array as is in the recipient array" do
assert_raise ArgumentError do
test_email = SendgridCampaignTestMailer.create_test(@options)
test_email = SendgridCampaignTestMailer.create_test(@options).deliver
end
end

should "accept a hash of unique args at the class level" do
assert_equal ({ :test_arg => "test value" }), SendgridUniqueArgsMailer.default_sg_unique_args
end

should "pass unique args from both the mailer class and the mailer method through custom headers" do
SendgridUniqueArgsMailer.unique_args_test_email(@options).deliver
mail = ActionMailer::Base.deliveries.last
assert_match ({ :mailer_method_unique_arg => "some value", :test_arg => "test value" }.to_json.gsub(/(["\]}])([,:])(["\[{])/, '\\1\\2 \\3')), mail.header['X-SMTPAPI'].value
end
end
24 changes: 13 additions & 11 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'sendgrid'
require 'action_mailer'

class Test::Unit::TestCase
end
require 'sendgrid'

class SendgridCampaignTestMailer < ActionMailer::Base
include SendGrid
Expand All @@ -26,13 +23,9 @@ class SendgridCampaignTestMailer < ActionMailer::Base
# array must match the :to array
# :category the sendgrid category used for tracking
# :html_content, :text_content, :subject
def test(options)
def create_test(options)
handle_sendgrid_options(options)
recipients(options[:to])
from(options[:from])
reply_to(options[:reply_to])
subject(options[:subject])
body(options[:html_content])
mail(options)
end

protected
Expand All @@ -49,7 +42,16 @@ def handle_sendgrid_options(options)
end
end


sendgrid_category(options[:category])
end
end

class SendgridUniqueArgsMailer < ActionMailer::Base
include SendGrid
sendgrid_unique_args({ :test_arg => "test value" })

def unique_args_test_email(options)
sendgrid_unique_args({ :mailer_method_unique_arg => "some value" })
mail(options)
end
end