class AhoyEmail::Processor

Constants

UTM_PARAMETERS

Attributes

mailer[R]
options[R]

Public Class Methods

new(mailer, options) click to toggle source
# File lib/ahoy_email/processor.rb, line 7
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

Public Instance Methods

perform() click to toggle source
# File lib/ahoy_email/processor.rb, line 15
def perform
  track_links if options[:utm_params] || options[:click]
  track_message if options[:message]
  message.ahoy_options = options
end

Protected Instance Methods

campaign() click to toggle source

return nil if false

# File lib/ahoy_email/processor.rb, line 146
def campaign
  options[:campaign] || nil
end
html_part?() click to toggle source
# File lib/ahoy_email/processor.rb, line 108
def html_part?
  (message.html_part || message).content_type =~ /html/
end
message() click to toggle source
# File lib/ahoy_email/processor.rb, line 23
def message
  mailer.message
end
parse_uri(href) click to toggle source

Parse href attribute Return uri if valid, nil otherwise

# File lib/ahoy_email/processor.rb, line 133
def parse_uri(href)
  # to_s prevent to return nil from this method
  Addressable::URI.heuristic_parse(href.to_s) rescue nil
end
parser_class() click to toggle source

use document instead of fragment github.com/ankane/ahoy_email/pull/150

# File lib/ahoy_email/processor.rb, line 97
def parser_class
  case options[:html5]
  when true
    Nokogiri::HTML5::Document
  when false
    Nokogiri::HTML4::Document
  else
    Nokogiri::HTML::Document
  end
end
skip_attribute?(link, suffix) click to toggle source
# File lib/ahoy_email/processor.rb, line 112
def skip_attribute?(link, suffix)
  attribute = "data-skip-#{suffix}"
  if link[attribute]
    # remove it
    link.remove_attribute(attribute)
    true
  elsif link["href"].to_s =~ /unsubscribe/i && !options[:unsubscribe_links]
    # try to avoid unsubscribe links
    true
  else
    false
  end
end
token() click to toggle source
# File lib/ahoy_email/processor.rb, line 27
def token
  @token ||= SecureRandom.urlsafe_base64(32).gsub(/[\-_]/, "").first(32)
end
track_message() click to toggle source
# File lib/ahoy_email/processor.rb, line 31
def track_message
  data = {
    mailer: options[:mailer],
    extra: options[:extra],
    user: options[:user]
  }

  if options[:click]
    data[:token] = token if AhoyEmail.save_token
    data[:campaign] = campaign
  end

  if options[:utm_params]
    UTM_PARAMETERS.map(&:to_sym).each do |k|
      data[k] = options[k] if options[k]
    end
  end

  mailer.message.ahoy_data = data
end
trackable?(uri) click to toggle source

Filter trackable URIs, i.e. absolute one with http

# File lib/ahoy_email/processor.rb, line 127
def trackable?(uri)
  uri && uri.absolute? && %w(http https).include?(uri.scheme)
end
url_for(opt) click to toggle source
# File lib/ahoy_email/processor.rb, line 138
def url_for(opt)
  opt = (mailer.default_url_options || {})
        .merge(options[:url_options])
        .merge(opt)
  AhoyEmail::Engine.routes.url_helpers.url_for(opt)
end