lib/ahoy_email/processor.rb in ahoy_email-1.0.3 vs lib/ahoy_email/processor.rb in ahoy_email-1.1.0
- old
+ new
@@ -56,11 +56,13 @@
mailer.message.ahoy_data = data
end
def track_open
if html_part?
- raw_source = (message.html_part || message).body.raw_source
+ part = message.html_part || message
+ raw_source = part.body.raw_source
+
regex = /<\/body>/i
url =
url_for(
controller: "ahoy/messages",
action: "open",
@@ -69,22 +71,22 @@
)
pixel = ActionController::Base.helpers.image_tag(url, size: "1x1", alt: "")
# try to add before body tag
if raw_source.match(regex)
- raw_source.gsub!(regex, "#{pixel}\\0")
+ part.body = raw_source.gsub(regex, "#{pixel}\\0")
else
- raw_source << pixel
+ part.body = raw_source + pixel
end
end
end
def track_links
if html_part?
- body = (message.html_part || message).body
+ part = message.html_part || message
- doc = Nokogiri::HTML(body.raw_source)
+ doc = Nokogiri::HTML(part.body.raw_source)
doc.css("a[href]").each do |link|
uri = parse_uri(link["href"])
next unless trackable?(uri)
# utm params first
if options[:utm_params] && !skip_attribute?(link, "utm-params")
@@ -96,10 +98,12 @@
uri.query_values = params
link["href"] = uri.to_s
end
if options[:click] && !skip_attribute?(link, "click")
+ raise "Secret token is empty" unless AhoyEmail.secret_token
+
# TODO sign more than just url and transition to HMAC-SHA256
signature = OpenSSL::HMAC.hexdigest("SHA1", AhoyEmail.secret_token, link["href"])
link["href"] =
url_for(
controller: "ahoy/messages",
@@ -109,11 +113,10 @@
signature: signature
)
end
end
- # hacky
- body.raw_source.sub!(body.raw_source, doc.to_s)
+ part.body = doc.to_s
end
end
def html_part?
(message.html_part || message).content_type =~ /html/