Class: Ponch::Interceptor

Inherits:
Object
  • Object
show all
Defined in:
lib/ponch/interceptor.rb

Class Method Summary collapse

Class Method Details

.delivering_email(message) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ponch/interceptor.rb', line 6

def self.delivering_email(message)
  if html_part = (message.html_part || (message.content_type =~ /text\/html/ && message))
    delivery = Ponch::Delivery.create_from_mail message

    message_body = html_part.body.to_s

    html_doc = Nokogiri::HTML(message_body)

    #intercept_links
    html_doc.search("a[href*='#{Ponch.config.url_options[:host]}']").each do |link|
      href = link.attr("href")
      uri = URI.parse(href)
      query = Rack::Utils.parse_nested_query uri.query
      query[:ponch] = delivery.code
      uri.query = query.to_query
      link.attributes["href"].value = uri.to_s
    end

    pixel_url = Rails.application.routes.url_helpers.ponch_pixel_url(delivery.code, Ponch.config.url_options)
    tracking_pixel = "<img src=\"#{pixel_url}\" />"
    html_doc.at("body").add_child(tracking_pixel)

    #set the HTML part with tracking pixel at the end
    html_part.body =  html_doc.to_s
  end
  message
end