lib/ahoy_email/processor.rb in ahoy_email-0.2.4 vs lib/ahoy_email/processor.rb in ahoy_email-0.3.0

- old
+ new

@@ -8,48 +8,48 @@ @message = message @mailer = mailer end def process - action_name = mailer.action_name.to_sym - 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] + safely do + action_name = mailer.action_name.to_sym + 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 = Array(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] || options[:click] + track_open if options[:open] + track_links if options[:utm_params] || options[:click] - 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.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=) - UTM_PARAMETERS.each do |k| - ahoy_message.send("#{k}=", options[k.to_sym]) if ahoy_message.respond_to?("#{k}=") - end + UTM_PARAMETERS.each do |k| + ahoy_message.send("#{k}=", options[k.to_sym]) if ahoy_message.respond_to?("#{k}=") + end - ahoy_message.assign_attributes(options[:extra] || {}) + ahoy_message.assign_attributes(options[:extra] || {}) - ahoy_message.save - message["Ahoy-Message-Id"] = ahoy_message.id.to_s + ahoy_message.save + message["Ahoy-Message-Id"] = ahoy_message.id.to_s + end end - rescue => e - report_error(e) end def track_send - if (message_id = message["Ahoy-Message-Id"]) - ahoy_message = AhoyEmail.message_model.where(id: message_id.to_s).first - if ahoy_message - ahoy_message.sent_at = Time.now - ahoy_message.save + safely do + if (message_id = message["Ahoy-Message-Id"]) + ahoy_message = AhoyEmail.message_model.where(id: message_id.to_s).first + if ahoy_message + ahoy_message.sent_at = Time.now + ahoy_message.save + end + message["Ahoy-Message-Id"] = nil end - message["Ahoy-Message-Id"] = nil end - rescue => e - report_error(e) end protected def options @@ -97,13 +97,14 @@ if html_part? body = (message.html_part || message).body doc = Nokogiri::HTML(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") - uri = Addressable::URI.parse(link["href"]) params = uri.query_values || {} UTM_PARAMETERS.each do |key| params[key] ||= options[key.to_sym] if options[key.to_sym] end uri.query_values = params @@ -139,32 +140,30 @@ 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 + # Filter trackable URIs, i.e. absolute one with http + def trackable?(uri) + uri && uri.absolute? && %w(http https).include?(uri.scheme) + end + + # Parse href attribute + # Return uri if valid, nil otherwise + def parse_uri(href) + # to_s prevent to return nil from this method + Addressable::URI.parse(href.to_s) rescue nil + end + 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 - def report_error(e) - if Rails.env.production? - $stderr.puts e - else - raise e - end end end end