Class: Formatters::BasicFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/formatters/basic_formatter.rb

Overview

Basic formatter of message body

Direct Known Subclasses

DictFormatter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, msg_content_hashed = false) ⇒ BasicFormatter

Initialization of basic formatter

Basic formatter arguments

message

message to format



32
33
34
35
36
# File 'lib/formatters/basic_formatter.rb', line 32

def initialize(message, msg_content_hashed=false)
  # Save message
  @message = message
  @msg_content_hashed = msg_content_hashed
end

Instance Attribute Details

#messageObject

Message to format



25
26
27
# File 'lib/formatters/basic_formatter.rb', line 25

def message
  @message
end

#msg_content_hashedObject

Content hashed



27
28
29
# File 'lib/formatters/basic_formatter.rb', line 27

def msg_content_hashed
  @msg_content_hashed
end

Class Method Details

.escape_chars(msg_string) ⇒ Object

Escapes characters which Python’s eval() cannot load that is esp. 0, r, n. Use a range, to be safe.



94
95
96
97
98
99
100
101
102
103
104
105
106
# 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

Instance Method Details

#format_value(value) ⇒ Object

Format value according to type

Parameters

value

value to format

Returns

value formatted as string



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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

Prints formatted message body to stdout



87
88
89
90
# 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