Class: Chatterbox::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/chatterbox/notification.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil) ⇒ Notification

Returns a new instance of Notification.



7
8
9
# File 'lib/chatterbox/notification.rb', line 7

def initialize(message = nil)
  @message = message
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



5
6
7
# File 'lib/chatterbox/notification.rb', line 5

def message
  @message
end

Instance Method Details

#add_rails_info(data) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/chatterbox/notification.rb', line 55

def add_rails_info(data)
  data.merge({
    :rails_env => rails_configuration.env,
    :rails_root => rails_configuration.root,
    :rails_version => rails_configuration.version
  })
end

#add_ruby_info(data) ⇒ Object



63
64
65
66
67
68
# File 'lib/chatterbox/notification.rb', line 63

def add_ruby_info(data)
  data.merge({
    :ruby_version  => ruby_version,
    :ruby_platform => ruby_platform
  })
end

#default_infoObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/chatterbox/notification.rb', line 28

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

#envObject



78
79
80
# File 'lib/chatterbox/notification.rb', line 78

def env
  ENV.to_hash
end

#exception_to_notice(hash) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/chatterbox/notification.rb', line 44

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

#normalize_message_to_hash(message) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/chatterbox/notification.rb', line 17

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

#noticeObject



11
12
13
14
15
# File 'lib/chatterbox/notification.rb', line 11

def notice
  hash = normalize_message_to_hash(message)
  hash = exception_to_notice(hash)
  default_info.merge(hash)
end

#rails_configurationObject



82
83
84
# File 'lib/chatterbox/notification.rb', line 82

def rails_configuration
  defined?(Rails) && Rails
end

#ruby_platformObject



74
75
76
# File 'lib/chatterbox/notification.rb', line 74

def ruby_platform
  RUBY_PLATFORM
end

#ruby_versionObject



70
71
72
# File 'lib/chatterbox/notification.rb', line 70

def ruby_version
  RUBY_VERSION
end

#string_to_notice(message) ⇒ Object



40
41
42
# File 'lib/chatterbox/notification.rb', line 40

def string_to_notice(message)
  { :summary => message }
end