Class: Rack::EnvNotifier

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/env_notifier.rb,
lib/rack/env_notifier/body_injector.rb,
lib/rack-env-notifier.rb

Defined Under Namespace

Classes: BodyInjector

Constant Summary collapse

VERSION =
"0.0.5"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ EnvNotifier

Returns a new instance of EnvNotifier.



49
50
51
# File 'lib/rack/env_notifier.rb', line 49

def initialize(app)
  @app = app
end

Class Method Details

.custom_cssObject



6
7
8
# File 'lib/rack/env_notifier.rb', line 6

def custom_css
  @custom_css
end

.custom_css=(css) ⇒ Object



10
11
12
# File 'lib/rack/env_notifier.rb', line 10

def custom_css=(css)
  @custom_css = css
end

.messageObject



14
15
16
# File 'lib/rack/env_notifier.rb', line 14

def message
  @message
end

.message=(msg) ⇒ Object



18
19
20
# File 'lib/rack/env_notifier.rb', line 18

def message=(msg)
  @message = msg
end

.notificationObject

Format notification based on custom_css value



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Object



40
41
42
# File 'lib/rack/env_notifier.rb', line 40

def notify=(ntf)
  @notify = ntf
end

.notify?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/rack/env_notifier.rb', line 44

def notify?
  @notify
end

Instance Method Details

#_call(env) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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) ⇒ Object



53
54
55
# File 'lib/rack/env_notifier.rb', line 53

def call(env)
  dup._call(env)
end