Sha256: e7aa67ff657f25f50c40168d02aef0c3842404ea84d88032ac628344ffebeb01

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

module Pulse
  # 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.
  class Rack
    def initialize(app)
      @app = app
    end

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

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

    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
projectlocker_pulse-1.0.0 lib/pulse/rack.rb
projectlocker_pulse-0.2.1 lib/pulse/rack.rb