Sha256: 89c3ce2d1de68f438fca47ebdfe62ead02fb24a9cbf66cc44b03fa2b5777b317

Contents?: true

Size: 1.71 KB

Versions: 6

Compression:

Stored size: 1.71 KB

Contents

require 'yaml'

module Chatterbox::ExceptionNotification
  class Presenter
    attr_reader :options
    
    def self.render(options)
      new(options).to_message
    end
    
    def initialize(options = {})
      @options = options
      @config = options[:config]
    end
    
    def summary
      options.delete(:summary)
    end
    
    def to_message
      { :message => { :summary => summary, :body => body },
        :config => @config }
    end
    
    def section_order
      [:error_message, :request, :backtrace, :environment, :ruby_info, :rails_info]
    end
    
    def body
      body = ""
      section_order.each do |section|
        output = render_section(section)
        body << output if output
      end
      body
    end
    
    def render_section(key)
      return nil unless options.key?(key)
      output = key.to_s.titleize
      output << "\n"
      output << "----------\n"
      output << "#{inspect_value(options[key])}\n\n"
      output
    end
    
    # Taken from exception_notification - thanks Jamis.
    def inspect_value(value)
      object_to_yaml(value).strip
    end

    def object_to_yaml(object)
      result = ""
      result << render_obj(object)
      result
    end
    
    def render_obj(object)
      if object.is_a?(Hash)
        render_hash(object)
      else
        render_non_hash(object)
      end
    end
    
    def render_non_hash(object)
      object.to_yaml.sub(/^---\s*\n?/, "")
    end
    
    # renders hashes with keys in sorted order
    def render_hash(hsh)
      str = ""
      indiff_hsh = hsh.with_indifferent_access
      indiff_hsh.keys.sort.each do |key|
        value = indiff_hsh[key]
        str << "#{key}: #{render_obj(value)}"
      end
      str
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
chatterbox-0.6.1 lib/chatterbox/exception_notification/presenter.rb
chatterbox-0.6.0 lib/chatterbox/exception_notification/presenter.rb
chatterbox-0.5.4 lib/chatterbox/exception_notification/presenter.rb
chatterbox-0.5.3 lib/chatterbox/exception_notification/presenter.rb
chatterbox-0.5.1 lib/chatterbox/exception_notification/presenter.rb
chatterbox-0.5.0 lib/chatterbox/exception_notification/presenter.rb