Class: CMF::Builder
- Inherits:
-
Object
- Object
- CMF::Builder
- Defined in:
- lib/cmf/builder.rb
Overview
Instances of the Builder
class can create CMF messages.
Basic usage:
b = CMF::Builder.new
b.add(0, "value0")
b.add(1, "value1")
The CMF message can be output in octet form (one character per byte):
b.to_octet
or hex form (two characters per byte):
b.to_hex
Method calls can be chained together:
CMF::Builder.new.add(0, "value0").add(1, "value1").to_hex
A dictionary can be used to refer to tags by name rather than number:
b = CMF::Builder.new([:tag0, :tag1])
b.add(:tag0, "value0")
b.add(:tag1, "value1")
Messages can be built from an object:
b = CMF::Builder.new([:tag0, :tag1])
b.build({tag0: "value0", tag1: "value1"})
Instance Attribute Summary collapse
-
#dictionary ⇒ Hash
readonly
The dictionary mapping tag names to numbers.
Instance Method Summary collapse
-
#add(tag, value) ⇒ Builder
Adds a (tag, value) pair to the CMF message.
-
#add_bool(tag, value) ⇒ Builder
Adds a (tag, boolean value) pair to the CMF message.
-
#add_bytes(tag, value) ⇒ Builder
Adds a (tag, byte_array value) pair to the CMF message.
-
#add_double(tag, value) ⇒ Builder
(also: #add_float)
Adds a (tag, float value) pair to the CMF message.
-
#add_int(tag, value) ⇒ Builder
Adds a (tag, integer value) pair to the CMF message.
-
#add_string(tag, value) ⇒ Builder
Adds a (tag, string value) pair to the CMF message.
-
#build(obj) ⇒ Builder
Adds multiple (tag, value) pairs to the CMF message.
-
#initialize(dictionary = nil) ⇒ Builder
constructor
Creates a new instance of Builder.
-
#reset ⇒ Builder
Resets the CMF message.
-
#to_hex ⇒ String
A hex string, every 2 characters representing one byte of the CMF message.
-
#to_octet ⇒ String
An octet string, each character representing one byte of the CMF message.
Constructor Details
#initialize(dictionary = nil) ⇒ Builder
Creates a new instance of CMF::Builder.
41 42 43 44 |
# File 'lib/cmf/builder.rb', line 41 def initialize(dictionary = nil) @dictionary = Dictionary.validate(dictionary) reset end |
Instance Attribute Details
#dictionary ⇒ Hash (readonly)
Returns The dictionary mapping tag names to numbers.
35 36 37 |
# File 'lib/cmf/builder.rb', line 35 def dictionary @dictionary end |
Instance Method Details
#add(tag, value) ⇒ Builder
Adds a (tag, value) pair to the CMF message.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/cmf/builder.rb', line 82 def add(tag, value) case value when Integer add_int(tag, value) when String if value.encoding == Encoding::BINARY add_bytes(tag, value) else add_string(tag, value) end when TrueClass, FalseClass add_bool(tag, value) when Float add_double(tag, value) else add_string(tag, value) end self end |
#add_bool(tag, value) ⇒ Builder
Adds a (tag, boolean value) pair to the CMF message.
159 160 161 162 163 |
# File 'lib/cmf/builder.rb', line 159 def add_bool(tag, value) write_tag(tag, value ? Type::BOOL_TRUE : Type::BOOL_FALSE) self end |
#add_bytes(tag, value) ⇒ Builder
Adds a (tag, byte_array value) pair to the CMF message.
144 145 146 147 148 149 150 151 |
# File 'lib/cmf/builder.rb', line 144 def add_bytes(tag, value) value = value.to_s write_tag(tag, Type::BYTE_ARRAY) Varint.serialize(@io, value.bytesize) @io << value self end |
#add_double(tag, value) ⇒ Builder Also known as: add_float
Adds a (tag, float value) pair to the CMF message.
171 172 173 174 175 176 |
# File 'lib/cmf/builder.rb', line 171 def add_double(tag, value) write_tag(tag, Type::DOUBLE) @io << [value.to_f].pack('E') self end |
#add_int(tag, value) ⇒ Builder
Adds a (tag, integer value) pair to the CMF message.
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/cmf/builder.rb', line 109 def add_int(tag, value) value = value.to_i type = Type::POSITIVE_NUMBER if value < 0 type = Type::NEGATIVE_NUMBER value *= -1 end write_tag(tag, type) Varint.serialize(@io, value.abs) self end |
#add_string(tag, value) ⇒ Builder
Adds a (tag, string value) pair to the CMF message.
129 130 131 132 133 134 135 136 |
# File 'lib/cmf/builder.rb', line 129 def add_string(tag, value) value = value.to_s write_tag(tag, Type::STRING) Varint.serialize(@io, value.bytesize) @io << value self end |
#build(obj) ⇒ Builder
Adds multiple (tag, value) pairs to the CMF message.
62 63 64 65 66 67 68 69 70 |
# File 'lib/cmf/builder.rb', line 62 def build(obj) obj.each do |key, values| Array(values).each do |value| add(key, value) end end self end |
#reset ⇒ Builder
Resets the CMF message.
49 50 51 52 53 |
# File 'lib/cmf/builder.rb', line 49 def reset @io = StringIO.new(String.new) # A StringIO with ASCII-8BIT encoding self end |
#to_hex ⇒ String
Returns A hex string, every 2 characters representing one byte of the CMF message.
187 188 189 |
# File 'lib/cmf/builder.rb', line 187 def to_hex to_octet.unpack('H*').first end |
#to_octet ⇒ String
Returns An octet string, each character representing one byte of the CMF message.
181 182 183 |
# File 'lib/cmf/builder.rb', line 181 def to_octet @io.string end |