Sha256: b584f3ffbceda36d5de60ecd0d7298ce625035432b6cd64758e0a4554e75e002

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

require 'rest-client'

module CrashHook
  class Crash
    include CrashHook::Request
    
    # Initialize a new Crash object
    #   config    => CrashHook::Configuration object
    #   exception => Exception raised from middleware
    #   env       => Environment variable
    #
    def initialize(config, exception=nil, env={})
      unless config.kind_of?(CrashHook::Configuration)
        raise ArgumentError, "CrashHook::Configuration required!"
      end
      
      raise ArgumentError, "Exception required!" if exception.nil?
      raise ArgumentError, "Environment required!" if env.nil?
      
      @config = config
      @payload = CrashHook::Payload.new(exception, env, @config.extra_params)
    end
    
    # Send notification to the endpoint
    def notify
      begin
        request(@config.method, @config.url, payload_data, @config.format)
        true
      rescue Exception => ex
        log_error(ex) if @config.has_logger?
        false
      end
    end
    
    private
    
    # Log CrashHook delivery error
    #
    def log_error(ex)
      if @config.logger.respond_to?(:error)
        @config.logger.error("CrashHook Error: #{ex.inspect}")
      elsif @config.logger.kind_of?(IO)
        @config.logger.puts("CrashHook Error: #{ex.inspect}")
      end
    end
    
    # Returns data formatted into config's format
    # 
    def payload_data
      case @config.format
      when :json
        @payload.to_json
      when :yaml
        @payload.to_yaml
      else
        @payload.to_hash
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
crash_hook-0.2.0 lib/crash_hook/crash.rb