class Rack::EnvRibbon

Constants

VERSION

Public Class Methods

new(app, opts = {}) click to toggle source
# File lib/rack/env_ribbon.rb, line 10
def initialize(app, opts = {})
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/env_ribbon.rb, line 14
def call(env)
  status, headers, body = @app.call(env)
  headers = HeaderHash.new(headers)

  if !STATUS_WITH_NO_ENTITY_BODY.include?(status) && headers[CONTENT_TYPE] =~ /\btext\/html\b/
    new_body = []
    new_content_length = 0

    body.each do |html|
      converter = HtmlConverter.new(html, app_env)
      next unless converter.valid_html?

      converter.insert_env_ribbon_into_body_tag!
      converter.insert_env_string_into_title_tag!
      converter.insert_env_ribbon_style_into_head_tag!

      result = converter.result

      new_body << result
      new_content_length += result.bytesize
    end

    new_body.compact!

    unless new_body.empty?
      body = new_body
      headers[CONTENT_LENGTH] &&= new_content_length
    end
  end

  [status, headers, body]
end

Private Instance Methods

app_env() click to toggle source
# File lib/rack/env_ribbon.rb, line 49
def app_env
  @app_env ||= ENV['RACK_ENV_RIBBON_ENV'] || ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development'
end