class ActionMailerPixel::Interceptor

Public Class Methods

new(mailer) click to toggle source
# File lib/action_mailer_pixel/interceptor.rb, line 3
def initialize(mailer)
  @mailer = mailer
end

Public Instance Methods

setup_pixel() click to toggle source
# File lib/action_mailer_pixel/interceptor.rb, line 7
def setup_pixel
  if html?
    @email_view = EmailView.create(recipient: recipient, campaign: campaign)

    raw_source = (message.html_part || message).body.raw_source
    regex = /<\/body>/i

    if raw_source.match(regex)
      raw_source.gsub!(regex, "#{pixel_html}\\0")
    else
      raw_source << pixel_html
    end
  end
end

Private Instance Methods

campaign() click to toggle source
# File lib/action_mailer_pixel/interceptor.rb, line 36
def campaign
  configuration.campaign
end
configuration() click to toggle source
# File lib/action_mailer_pixel/interceptor.rb, line 28
def configuration
  @mailer.open_pixel_configuration
end
html?() click to toggle source
# File lib/action_mailer_pixel/interceptor.rb, line 55
def html?
  (message.html_part || message).content_type.include? 'text/html'
end
message() click to toggle source
# File lib/action_mailer_pixel/interceptor.rb, line 24
def message
  @mailer.message
end
pixel_html() click to toggle source
# File lib/action_mailer_pixel/interceptor.rb, line 51
def pixel_html
  ActionController::Base.helpers.image_tag(pixel_url, size: '1x1', alt: '')
end
pixel_url() click to toggle source
# File lib/action_mailer_pixel/interceptor.rb, line 40
def pixel_url
  options = (ActionMailer::Base.default_url_options || {})
              .merge({
                controller: 'action_mailer_pixel/pixel',
                action: 'open',
                token: @email_view.token,
                format: 'png'
              })
  ActionMailerPixel::Engine.routes.url_helpers.url_for(options)
end
recipient() click to toggle source
# File lib/action_mailer_pixel/interceptor.rb, line 32
def recipient
  configuration.recipient
end