lib/ahoy_email/processor.rb in ahoy_email-0.5.2 vs lib/ahoy_email/processor.rb in ahoy_email-1.0.0

- old
+ new

@@ -1,87 +1,70 @@ module AhoyEmail class Processor - attr_reader :message, :mailer, :ahoy_message + attr_reader :mailer, :options UTM_PARAMETERS = %w(utm_source utm_medium utm_term utm_content utm_campaign) - def initialize(message, mailer = nil) - @message = message + def initialize(mailer, options) @mailer = mailer + @options = options + + unknown_keywords = options.keys - AhoyEmail.default_options.keys + raise ArgumentError, "unknown keywords: #{unknown_keywords.join(", ")}" if unknown_keywords.any? end - def process - Safely.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] + def perform + track_open if options[:open] + track_links if options[:utm_params] || options[:click] + track_message + end - track_open if options[:open] - track_links if options[:utm_params] || options[:click] + protected - 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=) + def message + mailer.message + end - UTM_PARAMETERS.each do |k| - ahoy_message.send("#{k}=", options[k.to_sym]) if ahoy_message.respond_to?("#{k}=") - end + def token + @token ||= SecureRandom.urlsafe_base64(32).gsub(/[\-_]/, "").first(32) + end - ahoy_message.assign_attributes(options[:extra] || {}) + def track_message + data = { + mailer: options[:mailer], + extra: options[:extra] + } - ahoy_message.save! - message["Ahoy-Message-Id"] = ahoy_message.id.to_s - end + user = options[:user] + if user + data[:user_type] = user.model_name.name + id = user.id + data[:user_id] = id.is_a?(Integer) ? id : id.to_s end - end - def track_send - Safely.safely do - if (message_id = message["Ahoy-Message-Id"]) && message.perform_deliveries - 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 + if options[:open] || options[:click] + data[:token] = token end - end - protected - - def options - @options ||= begin - options = AhoyEmail.options.merge(mailer.class.ahoy_options) - if mailer.ahoy_options - options = options.except(:only, :except).merge(mailer.ahoy_options) + if options[:utm_params] + UTM_PARAMETERS.each do |k| + data[k] = options[k.to_sym] if options[k.to_sym] end - options.each do |k, v| - if v.respond_to?(:call) - options[k] = v.call(message, mailer) - end - end - options end - end - def generate_token - SecureRandom.urlsafe_base64(32).gsub(/[\-_]/, "").first(32) + mailer.message["Ahoy-Message"] = data.to_json end def track_open if html_part? raw_source = (message.html_part || message).body.raw_source regex = /<\/body>/i url = url_for( controller: "ahoy/messages", action: "open", - id: ahoy_message.token, + id: token, format: "gif" ) pixel = ActionController::Base.helpers.image_tag(url, size: "1x1", alt: "") # try to add before body tag @@ -111,16 +94,17 @@ uri.query_values = params link["href"] = uri.to_s end if options[:click] && !skip_attribute?(link, "click") - signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha1"), AhoyEmail.secret_token, link["href"]) + # 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", action: "click", - id: ahoy_message.token, + id: token, url: link["href"], signature: signature ) end end @@ -155,14 +139,10 @@ # Parse href attribute # Return uri if valid, nil otherwise def parse_uri(href) # to_s prevent to return nil from this method - if options[:heuristic_parse] - Addressable::URI.heuristic_parse(href.to_s) rescue nil - else - Addressable::URI.parse(href.to_s) rescue nil - end + Addressable::URI.heuristic_parse(href.to_s) rescue nil end def url_for(opt) opt = (ActionMailer::Base.default_url_options || {}) .merge(options[:url_options])