class PlanBCD::Rails::Middleware

Public Class Methods

new(app) click to toggle source
# File lib/planbcd/rails/middleware.rb, line 7
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/planbcd/rails/middleware.rb, line 11
def call(env)
  status, headers, response = @app.call(env)

  headers = HeaderHash.new(headers)

  if headers['Content-Type'].to_s =~ %r{^text/html;}i && PlanBCD::Rails.configuration.auto_insert
    begin
      response = insert_js(response)
    rescue PlanBCD::Rails::Exception => e
      $stderr.write "Failed to insert planbcd tag: #{e.message}\n"
    end
  end

  [status, headers, response]
end

Private Instance Methods

insert_js(response) click to toggle source
# File lib/planbcd/rails/middleware.rb, line 29
def insert_js(response)
  body = ""
  response.each do |r|
    body.concat r.to_s
  end
  [body.sub(/(<head[^>]*>)/i, "\\1#{pbcd_init}")]
end