Module: PacketGen::Inspect Private

Defined in:
lib/packetgen/inspect.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Inspect module provides methods to help writing inspect

Author:

  • Sylvain Daubert

Constant Summary collapse

INSPECT_MAX_WIDTH =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Maximum number of characters on a line for INSPECT

70
INSPECT_FMT_ATTR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Format to inspect attribute

"%10s %12s: %s\n"

Class Method Summary collapse

Class Method Details

.dashed_line(name, level = 1) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a dashed line with obj class writing in it

Parameters:

  • name (String)
  • level (Integer) (defaults to: 1)

Returns:

  • (String)


18
19
20
21
# File 'lib/packetgen/inspect.rb', line 18

def self.dashed_line(name, level=1)
  str = '--' * level << " #{name} "
  str << '-' * (INSPECT_MAX_WIDTH - str.length) << "\n"
end

.inspect_attribute(attr, value, level = 1) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Format an attribute for #inspect. 3 cases are handled:

  • attribute value is a Types::Int: show value as integer and in hexdecimal format,

  • attribute value responds to #to_human: call it,

  • else, #to_s is used to format attribute value.

Parameters:

  • attr (Symbol)

    attribute name

  • value (Object)

    attribute value

  • level (Integer) (defaults to: 1)

Returns:

  • (String)


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/packetgen/inspect.rb', line 45

def self.inspect_attribute(attr, value, level=1)
  str = shift_level(level)
  val = if value.is_a? Types::Int
          int_dec_hex(value, value.to_s.size * 2)
        elsif value.respond_to? :to_human
          value.to_human
        else
          value.to_s.inspect
        end
  str << INSPECT_FMT_ATTR % [value.class.to_s.sub(/.*::/, ''), attr, val]
end

.inspect_body(body) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • body (#to_s)

Returns:

  • (String)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/packetgen/inspect.rb', line 59

def self.inspect_body(body)
  return '' if body.nil? or body.empty?
  str = dashed_line('Body', 2)
  str << (0..15).to_a.map { |v| " %02d" % v}.join << "\n"
  str << '-' * INSPECT_MAX_WIDTH << "\n"
  if body.size > 0
    (body.size / 16 + 1).times do |i|
      octets = body.to_s[i*16, 16].unpack('C*')
      o_str = octets.map { |v| " %02x" % v}.join
      str << o_str
      str << ' ' * (3*16 - o_str.size) unless o_str.size >= 3*16
      str << '  ' << octets.map { |v| v < 128 && v > 13 ? v.chr : '.' }.join
      str << "\n"
    end
  end
  str << '-' * INSPECT_MAX_WIDTH << "\n"
end

.int_dec_hex(value, hexsize) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • value (#to_i)
  • hexsize (Integer)

Returns:

  • (String)


31
32
33
# File 'lib/packetgen/inspect.rb', line 31

def self.int_dec_hex(value, hexsize)
  "%-10s (0x%0#{hexsize}x)" % [value.to_i, value.to_i]
end

.shift_level(level = 1) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


24
25
26
# File 'lib/packetgen/inspect.rb', line 24

def self.shift_level(level=1)
  '  ' + '  ' * level
end