class Rack::PixelFire

Public Class Methods

new(app) click to toggle source
# File lib/rack/pixel_fire.rb, line 5
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/pixel_fire.rb, line 9
def call(env)
  @status, @headers, @body = @app.call(env)
  return [@status, @headers, @body] unless html?
  response = ::Rack::Response.new([], @status, @headers)

  @body.each { |fragment| response.write inject(env, fragment) }
  @body.close if @body.respond_to?(:close)

  response.finish
end
html?() click to toggle source
# File lib/rack/pixel_fire.rb, line 20
def html?
  @headers['Content-Type'] =~ /html/
end
inject(env, response) click to toggle source
# File lib/rack/pixel_fire.rb, line 24
def inject(env, response)
  puts "TAGS: #{tags(env).inspect}"
  tags(env).each do |tag|
    response.sub! %r{</#{tag.target}>} do |m|
      tag.custom_html << m.to_s
    end
  end
  response
end
tags(env) click to toggle source
# File lib/rack/pixel_fire.rb, line 34
def tags(env)
  return [] if env["PATH_INFO"] =~ /\A.*\/pixel_fire.*\Z/
  ::PixelFire::Trigger.matches(env["PATH_INFO"]).map(&:tags).flatten.uniq
end