Class: Chatterbox::ExceptionNotification::Presenter

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Presenter

Returns a new instance of Presenter.



12
13
14
15
# File 'lib/chatterbox/exception_notification/presenter.rb', line 12

def initialize(options = {})
  @options = options
  @config = options[:config]
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/chatterbox/exception_notification/presenter.rb', line 6

def options
  @options
end

Class Method Details

.render(options) ⇒ Object



8
9
10
# File 'lib/chatterbox/exception_notification/presenter.rb', line 8

def self.render(options)
  new(options).to_message
end

Instance Method Details

#error_detailsObject



27
28
29
# File 'lib/chatterbox/exception_notification/presenter.rb', line 27

def error_details
  [:error_message, :request, :backtrace, :environment, :ruby_info, :rails_info, :chatterbox_info]
end

#render_array(object) ⇒ Object



65
66
67
# File 'lib/chatterbox/exception_notification/presenter.rb', line 65

def render_array(object)
  render_object(object.join("\n"))
end

#render_bodyObject



31
32
33
34
35
36
37
# File 'lib/chatterbox/exception_notification/presenter.rb', line 31

def render_body
  processed_keys = []
  extra_sections = options.keys - error_details
  body = render_sections(extra_sections, processed_keys)
  body << render_sections(error_details, processed_keys)
  body
end

#render_hash(hsh) ⇒ Object

renders hashes with keys in alpha-sorted order



74
75
76
77
78
79
80
81
82
83
# File 'lib/chatterbox/exception_notification/presenter.rb', line 74

def render_hash(hsh)
  str = ""
  indiff_hsh = hsh.with_indifferent_access
  indiff_hsh.keys.sort.each do |key|
    str << "#{key}: "
    value = indiff_hsh[key]
    PP::pp(value, str)
  end
  str
end

#render_obj(object) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/chatterbox/exception_notification/presenter.rb', line 57

def render_obj(object)
  case object
  when Hash then render_hash(object)
  when Array then render_array(object)
  else render_object(object)
  end
end

#render_object(object) ⇒ Object



69
70
71
# File 'lib/chatterbox/exception_notification/presenter.rb', line 69

def render_object(object)
  "#{object}\n"
end

#render_section(key, processed_keys = []) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/chatterbox/exception_notification/presenter.rb', line 47

def render_section(key, processed_keys = [])
  processed_keys << key
  return nil unless options.key?(key)
  output = "#{key.to_s.titleize}\n"
  output << "----------\n"
  output << render_obj(options[key])
  output << "\n"
  output
end

#render_sections(keys, already_processed) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/chatterbox/exception_notification/presenter.rb', line 39

def render_sections(keys, already_processed)
  keys.inject(String.new) do |str, key|
    output = render_section(key, already_processed)
    str << output if output
    str
  end
end

#summaryObject



17
18
19
# File 'lib/chatterbox/exception_notification/presenter.rb', line 17

def summary
  options.delete(:summary)
end

#to_messageObject



21
22
23
24
25
# File 'lib/chatterbox/exception_notification/presenter.rb', line 21

def to_message
  { :summary => summary, 
    :body => render_body,
    :config => @config }
end