lib/ahoy_email/processor.rb in ahoy_email-0.2.2 vs lib/ahoy_email/processor.rb in ahoy_email-0.2.3
- old
+ new
@@ -7,19 +7,22 @@
@mailer = mailer
end
def process
action_name = mailer.action_name.to_sym
- if options[:message] and (!options[:only] or options[:only].include?(action_name)) and !options[:except].to_a.include?(action_name)
+ if options[:message] && (!options[:only] || options[:only].include?(action_name)) && !options[:except].to_a.include?(action_name)
@ahoy_message = AhoyEmail.message_model.new
ahoy_message.token = generate_token
ahoy_message.to = message.to.join(", ") if ahoy_message.respond_to?(:to=)
ahoy_message.user = options[:user]
track_open if options[:open]
- track_links if options[:utm_params] or options[:click]
+ track_links if options[:utm_params] || options[:click]
+ ahoy_message.utm_source = options[:utm_source] if ahoy_message.respond_to?(:utm_source=)
+ ahoy_message.utm_medium = options[:utm_medium] if ahoy_message.respond_to?(:utm_medium=)
+ ahoy_message.utm_campaign = options[:utm_campaign] if ahoy_message.respond_to?(:utm_campaign=)
ahoy_message.mailer = options[:mailer] if ahoy_message.respond_to?(:mailer=)
ahoy_message.subject = message.subject if ahoy_message.respond_to?(:subject=)
ahoy_message.content = message.to_s if ahoy_message.respond_to?(:content=)
ahoy_message.save
@@ -90,21 +93,21 @@
body = (message.html_part || message).body
doc = Nokogiri::HTML(body.raw_source)
doc.css("a[href]").each do |link|
# utm params first
- if options[:utm_params] and !skip_attribute?(link, "utm-params")
+ if options[:utm_params] && !skip_attribute?(link, "utm-params")
uri = Addressable::URI.parse(link["href"])
params = uri.query_values || {}
- %w[utm_source utm_medium utm_term utm_content utm_campaign].each do |key|
+ %w(utm_source utm_medium utm_term utm_content utm_campaign).each do |key|
params[key] ||= options[key.to_sym] if options[key.to_sym]
end
uri.query_values = params
link["href"] = uri.to_s
end
- if options[:click] and !skip_attribute?(link, "click")
+ if options[:click] && !skip_attribute?(link, "click")
signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha1"), AhoyEmail.secret_token, link["href"])
link["href"] =
url_for(
controller: "ahoy/messages",
action: "click",
@@ -131,17 +134,23 @@
link.remove_attribute(attribute)
true
elsif link["href"].to_s =~ /unsubscribe/i
# try to avoid unsubscribe links
true
+ elsif link["href"].to_s.start_with?("mailto:")
+ # mailto's shouldn't go through a redirect
+ true
else
false
end
end
- def url_for(options)
- AhoyEmail::Engine.routes.url_helpers.url_for((ActionMailer::Base.default_url_options || {}).merge(options))
+ def url_for(opt)
+ opt = (ActionMailer::Base.default_url_options || {})
+ .merge(options[:url_options])
+ .merge(opt)
+ AhoyEmail::Engine.routes.url_helpers.url_for(opt)
end
# not a fan of quiet errors
# but tracking should *not* break
# email delivery in production
@@ -150,8 +159,7 @@
$stderr.puts e
else
raise e
end
end
-
end
end