Sha256: 5145795aba7bd7c7665009a1e7847a6091a5f3cca7602ed796bb635fcae7c482

Contents?: true

Size: 1.93 KB

Versions: 6

Compression:

Stored size: 1.93 KB

Contents

module Chatterbox
  
  class Notification
    
    attr_reader :message
    
    def initialize(message = nil)
      @message = message
    end
    
    def notice
      hash = normalize_message_to_hash(message)
      hash = exception_to_notice(hash)
      default_info.merge(hash)
    end
    
    def normalize_message_to_hash(message)
      case
      when Exception === message
        { :exception => message }
      when message.respond_to?(:to_hash)
        message.to_hash
      when message.respond_to?(:to_s)
        string_to_notice(message.to_s)
      end
    end
    
    def default_info
      default_info     = {
        :summary       => "N/A",
        :environment   => env,
        :ruby_version  => ruby_version,
        :ruby_platform => ruby_platform
      }
      default_info = add_ruby_info(default_info)
      default_info = add_rails_info(default_info) if rails_configuration
      default_info
    end
    
    def string_to_notice(message)
      { :summary => message }
    end
    
    def exception_to_notice(hash)
      return hash unless hash.key?(:exception)
      exception = hash[:exception]
      {
        :summary => "#{exception.class.name}: #{exception.message}",
        :error_class   => exception.class.name,
        :error_message => exception.message,
        :backtrace     => exception.backtrace,
      }.merge(hash)
    end
    
    def add_rails_info(data)
      data.merge({
        :rails_env => rails_configuration.env,
        :rails_root => rails_configuration.root,
        :rails_version => rails_configuration.version
      })
    end
    
    def add_ruby_info(data)
      data.merge({
        :ruby_version  => ruby_version,
        :ruby_platform => ruby_platform
      })
    end

    def ruby_version
      RUBY_VERSION
    end
    
    def ruby_platform
      RUBY_PLATFORM
    end
    
    def env
      ENV.to_hash
    end
    
    def rails_configuration
      defined?(Rails) && Rails
    end
    
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
relevance-chatterbox-0.3.0 lib/chatterbox/notification.rb
relevance-chatterbox-0.3.1 lib/chatterbox/notification.rb
relevance-chatterbox-0.3.2 lib/chatterbox/notification.rb
relevance-chatterbox-0.3.3 lib/chatterbox/notification.rb
chatterbox-0.4.0 lib/chatterbox/notification.rb
chatterbox-0.3.3 lib/chatterbox/notification.rb