class Rack::EnvNotifier

Constants

VERSION

Public Class Methods

custom_css() click to toggle source
# File lib/rack/env_notifier.rb, line 6
def custom_css
  @custom_css
end
custom_css=(css) click to toggle source
# File lib/rack/env_notifier.rb, line 10
def custom_css=(css)
  @custom_css = css
end
message() click to toggle source
# File lib/rack/env_notifier.rb, line 14
def message
  @message
end
message=(msg) click to toggle source
# File lib/rack/env_notifier.rb, line 18
def message=(msg)
  @message = msg
end
new(app) click to toggle source
# File lib/rack/env_notifier.rb, line 49
def initialize(app)
  @app = app
end
notification() click to toggle source

Format notification based on custom_css value

# File lib/rack/env_notifier.rb, line 24
      def notification
        if @custom_css == true
          <<-EOF
<!-- Notify Start -->
<div id="env-notifier" class="#{@message.gsub(/[^a-z]/i, '-').gsub(/--*/, '-').gsub(/-$/, '')}">#{@message}</div>
<!-- Notify End -->
          EOF
        else
          <<-EOF
<!-- Notify Start -->
<div id="env-notifier" class="#{@message.gsub(/[^a-z]/i, '-').gsub(/--*/, '-').gsub(/-$/, '')}" style="position: fixed; top: 0; right: 0; left: 0; background: rgba(150, 50, 50, .7); color: #fff; text-align: center; font-size: 16px; font-weight: bold; padding: 2px; z-index: 999999">#{@message}</div>
<!-- Notify End -->
          EOF
        end
      end
notify=(ntf) click to toggle source
# File lib/rack/env_notifier.rb, line 40
def notify=(ntf)
  @notify = ntf
end
notify?() click to toggle source
# File lib/rack/env_notifier.rb, line 44
def notify?
  @notify
end

Public Instance Methods

_call(env) click to toggle source
# File lib/rack/env_notifier.rb, line 57
def _call(env)
  status, headers, body = @app.call(env)

  # Inject headers, notification, update content-length header

  if status == 200 and EnvNotifier.notify?

    # Inject notification

    if headers['Content-Type'] =~ %r{text/html} then
      injector = BodyInjector.new(body, EnvNotifier.notification)
      injector.inject!(env)

      # Inject header

      if injector.notification_added
        headers['X-EnvNotifier'] = EnvNotifier.message
      end

      # Update content-length header after the body is modified

      headers['Content-Length'] = injector.content_length.to_s
      [status, headers, injector.new_body]
    end
    [status, headers, body]
  else
    [status, headers, body]
  end
end
call(env) click to toggle source
# File lib/rack/env_notifier.rb, line 53
def call(env)
  dup._call(env)
end