class Formatters::BasicFormatter
Basic formatter of message body
Attributes
message[RW]
Message to format
msg_content_hashed[RW]
Content hashed
Public Class Methods
escape_chars(msg_string)
click to toggle source
Escapes characters which Python's eval() cannot load that is esp. 0, r, n. Use a range, to be safe.
# File lib/formatters/basic_formatter.rb, line 94 def self.escape_chars(msg_string) if msg_string.nil? nil else msg_string.each_codepoint.map do |s| if s < 32 || s > 126 format('\\u%04x', s) else s.chr end end.join end end
new(message, msg_content_hashed=false)
click to toggle source
Public Instance Methods
format_value(value)
click to toggle source
Format value according to type
Parameters¶ ↑
- value
-
value to format
Returns¶ ↑
value formatted as string
# File lib/formatters/basic_formatter.rb, line 43 def format_value(value) # Switch by class of value case value # Boolean value when TrueClass, FalseClass value ? "True" : "False" # Numeric or Range value when Integer, Float, Numeric, Range #, Bignum, Fixnum are deprecated value # Array value when Array # Array for formatted items of array help_array = [] # Each item in array needs to be formatted value.each do |item| # Format array item help_array.push(format_value(item)) end "[#{help_array.join(", ")}]" # Dictionary/hash value when Hash # Array for formatted items of hash help_array = [] # Each key-value pair needs to be formatted value.each do |key, val| # Format key-value pair of item help_array.push("#{format_value(key)}: #{format_value(val)}") end "{#{help_array.join(", ")}}" # String or symbol value when Symbol value.size > 0 ? "'#{value}'" : "None" when String value.size > 0 ? "'#{value.gsub(/'/, %q(\\\'))}'" : "None" # Nil value when NilClass "None" # Other or unknown type else raise TypeError, "Unknown value type" end # case end
print()
click to toggle source
Prints formatted message body to stdout
# File lib/formatters/basic_formatter.rb, line 87 def print() # Print formatted body to stdout puts format_value(@msg_content_hashed ? StringUtils.sha1_hash(@message.body) : @message.body) end