Class: Pulse::Rack

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

Overview

Middleware for Rack applications. Any errors raised by the upstream application will be delivered to Pulse and re-raised.

Synopsis:

require 'rack'
require 'projectlocker_pulse'

Pulse.configure do |config|
  config.api_key = 'my_api_key'
end

app = Rack::Builder.app do
  run lambda { |env| raise "Rack down" }
end

use Pulse::Rack
run app

Use a standard Pulse.configure call to configure your api key.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Rack

Returns a new instance of Rack.



23
24
25
# File 'lib/pulse/rack.rb', line 23

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pulse/rack.rb', line 39

def call(env)
  begin
    response = @app.call(env)
  rescue Exception => raised
    env['pulse.error_id'] = notify_pulse(raised,env)
    raise
  end

  if env['rack.exception']
    env['pulse.error_id'] = notify_pulse(env['rack.exception'],env)
  end

  response
end

#ignored_user_agent?(env) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
# File 'lib/pulse/rack.rb', line 27

def ignored_user_agent?(env)
  true if Pulse.
    configuration.
    ignore_user_agent.
    flatten.
    any? { |ua| ua === env['HTTP_USER_AGENT'] }
end

#notify_pulse(exception, env) ⇒ Object



35
36
37
# File 'lib/pulse/rack.rb', line 35

def notify_pulse(exception,env)
  Pulse.notify_or_ignore(exception,:rack_env => env) unless ignored_user_agent?(env)
end