module Mailings class OpenPixel def self.call(html, recipient) new(html, recipient).call end def initialize(html, recipient) @html = html.to_s @recipient = recipient end def call return @html unless trackable? url = tracking_url return @html if url.blank? tag = %() if @html.match?(/<\/body>/i) @html.sub(/<\/body>/i, "#{tag}") else "#{@html}#{tag}" end end private def trackable? @recipient.respond_to?(:tracking_token) && @recipient.tracking_token.present? end def tracking_url Rails.application.routes.url_helpers.mail_open_url(@recipient.tracking_token, **PublicUrl.options) end end end