Class: Rack::PixelFire

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/pixel_fire.rb

Defined Under Namespace

Classes: Railtie

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ PixelFire



5
6
7
# File 'lib/rack/pixel_fire.rb', line 5

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# 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?Boolean



20
21
22
# File 'lib/rack/pixel_fire.rb', line 20

def html?
  @headers['Content-Type'] =~ /html/
end

#inject(env, response) ⇒ Object



24
25
26
27
28
29
30
31
32
# 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) ⇒ Object



34
35
36
37
# 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