module Gateway::Feature::Performance

Public Class Methods

included(klass) click to toggle source
# File lib/gateway/feature/performance.rb, line 4
def self.included(klass)
  klass.class_eval do
    class_attribute :profiler
  end if klass.is_a?(Class)
end

Public Instance Methods

profiler() click to toggle source
# File lib/gateway/feature/performance.rb, line 10
def profiler
  @profiler ||= (options[:profiler] || self.class.profiler)
end

Protected Instance Methods

error_message(error) click to toggle source
# File lib/gateway/feature/performance.rb, line 50
def error_message(error)
  "#{error.class.name} - #{error.message}"
end
error_status(error) click to toggle source
# File lib/gateway/feature/performance.rb, line 46
def error_status(error)
  error.respond_to?(:status) ? error.status : 500
end
success_message(resp) click to toggle source
# File lib/gateway/feature/performance.rb, line 42
def success_message(resp)
  "OK"
end
success_status(resp) click to toggle source
# File lib/gateway/feature/performance.rb, line 38
def success_status(resp)
  200
end
with_perf(action, req, opts={}, &block) click to toggle source
# File lib/gateway/feature/performance.rb, line 16
def with_perf(action, req, opts={}, &block)
  return block.call if opts[:perf] == false

  start_time = Time.now

  begin
    resp = block.call
  ensure
    if e = $!
      status = error_status(e)
      desc   = error_message(e)
    else
      status = success_status(resp)
      desc   = success_message(resp)
    end

    duration = Time.now - start_time
    req = "#{action.to_s.upcase} #{req}"
    profiler.performance(name.to_s, duration, status, desc, req)
  end
end