Class: GrizzlyBer
Instance Method Summary collapse
- #[](index_or_tag) ⇒ Object
- #[]=(index_or_tag, value) ⇒ Object
- #each(&block) ⇒ Object
- #from_ber(byte_array) ⇒ Object
- #from_ber_hex_string(hex_string) ⇒ Object
- #hex_value_of_first_element_with_tag(tag) ⇒ Object
-
#initialize(hex_string = "") ⇒ GrizzlyBer
constructor
A new instance of GrizzlyBer.
- #remove!(tag) ⇒ Object
- #set_hex_value_for_tag(tag, value) ⇒ Object
- #set_value_for_tag(tag, value) ⇒ Object
- #size ⇒ Object
- #to_ber ⇒ Object
- #to_s(indent_size: 0) ⇒ Object
- #value_of_first_element_with_tag(tag) ⇒ Object
Constructor Details
#initialize(hex_string = "") ⇒ GrizzlyBer
Returns a new instance of GrizzlyBer.
107 108 109 110 111 |
# File 'lib/grizzly_ber.rb', line 107 def initialize(hex_string = "") raise ArgumentError, "hex_string must be a valid hex string" unless hex_string.is_a? String and hex_string.size.even? and hex_string =~ /^[0-9A-F]*$/ @elements = [] # is an array of GrizzlyBerElement from_ber_hex_string(hex_string) end |
Instance Method Details
#[](index_or_tag) ⇒ Object
132 133 134 135 |
# File 'lib/grizzly_ber.rb', line 132 def [](index_or_tag) return value_of_first_element_with_tag(index_or_tag) if index_or_tag.is_a? String @elements[index_or_tag] end |
#[]=(index_or_tag, value) ⇒ Object
137 138 139 140 141 |
# File 'lib/grizzly_ber.rb', line 137 def []=(index_or_tag,value) return set_value_for_tag(index_or_tag, value) if index_or_tag.is_a? String @elements[index].value = value @elements[index] end |
#each(&block) ⇒ Object
147 148 149 |
# File 'lib/grizzly_ber.rb', line 147 def each &block @elements.each &block end |
#from_ber(byte_array) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/grizzly_ber.rb', line 118 def from_ber(byte_array) raise ArgumentError, "byte_array must be an array of bytes" unless byte_array.is_a? Array and byte_array.all? {|byte| byte.is_a? Integer and byte <= 0xFF} while byte_array.size > 0 element = GrizzlyBerElement.new(byte_array) break if element.tag.nil? or element.value.nil? @elements << element end self end |
#from_ber_hex_string(hex_string) ⇒ Object
113 114 115 116 |
# File 'lib/grizzly_ber.rb', line 113 def from_ber_hex_string(hex_string) raise ArgumentError, "hex_string must be a valid hex string" unless hex_string.is_a? String and hex_string.size.even? and hex_string =~ /^[0-9A-F]*$/ self.from_ber [hex_string].pack("H*").unpack("C*") end |
#hex_value_of_first_element_with_tag(tag) ⇒ Object
157 158 159 160 |
# File 'lib/grizzly_ber.rb', line 157 def hex_value_of_first_element_with_tag(tag) first_tagged_element = value_of_first_element_with_tag(tag) first_tagged_element &&= first_tagged_element.pack("C*").unpack("H*").first.upcase end |
#remove!(tag) ⇒ Object
178 179 180 181 |
# File 'lib/grizzly_ber.rb', line 178 def remove!(tag) @elements = @elements.select {|element| element.tag != tag.upcase} self end |
#set_hex_value_for_tag(tag, value) ⇒ Object
174 175 176 |
# File 'lib/grizzly_ber.rb', line 174 def set_hex_value_for_tag(tag, value) set_value_for_tag(tag, [value].pack("H*").unpack("C*")) end |
#set_value_for_tag(tag, value) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/grizzly_ber.rb', line 162 def set_value_for_tag(tag, value) real_tag = GrizzlyTag.named(tag) ? GrizzlyTag.named(tag)[:tag] : tag.upcase first_tagged_element = @elements.find {|element| real_tag == element.tag} if first_tagged_element.nil? first_tagged_element = GrizzlyBerElement.new first_tagged_element.tag = real_tag @elements << first_tagged_element end first_tagged_element.value = value first_tagged_element end |
#size ⇒ Object
143 144 145 |
# File 'lib/grizzly_ber.rb', line 143 def size @elements.size end |
#to_ber ⇒ Object
128 129 130 |
# File 'lib/grizzly_ber.rb', line 128 def to_ber @elements.reduce("") {|ber_array, element| ber_array += element.to_ber} end |
#to_s(indent_size: 0) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/grizzly_ber.rb', line 183 def to_s(indent_size: 0) indent = " " * 3 * indent_size @elements.reduce("") do |output, element| info = GrizzlyTag.tagged(element.tag) || {:name => "Unknown Tag", :description => "Unknown"} output += "#{indent}#{element.tag}: #{info[:name]}\n" output += "#{indent} Description: #{info[:description]}\n" if element.value.is_a? GrizzlyBer output += element.value.to_s(indent_size: indent_size+1) else output += "#{indent} Value: #{element.value.pack("C*").unpack("H*").first}" if info[:format] == :string string_value = ", \"#{element.value.pack("C*")}\"" output += string_value if string_value.encoding == output.encoding #output is expected to be Encoding::UTF_8 but that default mustn't be forced here. end output += "\n" end end end |
#value_of_first_element_with_tag(tag) ⇒ Object
151 152 153 154 155 |
# File 'lib/grizzly_ber.rb', line 151 def value_of_first_element_with_tag(tag) first_tagged_element = @elements.find {|element| tag.upcase == element.tag} first_tagged_element ||= @elements.find {|element| element.tag == GrizzlyTag.named(tag)[:tag]} if GrizzlyTag.named(tag) first_tagged_element &&= first_tagged_element.value end |