Class: CMF::Parser
- Inherits:
-
Object
- Object
- CMF::Parser
- Defined in:
- lib/cmf/parser.rb
Overview
Instances of the Parser
class can parse CMF messages.
Basic usage:
p = CMF::Parser.new
p.parse("\x04\r") # {0=>true, 1=>false}
p.parse_hex("040d") # {0=>true, 1=>false}
Using a dictionary:
p = CMF::Parser.new([:tag0, :tag1])
p.parse_hex("040d") # {:tag0=>true, :tag1=>false}
If a tag is found multiple times in the message, its values will be stored in an array in the parsed object.
CMF::Parser.new.parse_hex("040504") # {0=>[true, false, true]}
Using #next_pair:
p = CMF::Parser.new
p. = "040d"
p.next_pair # [0, true]
p.next_pair # [1, false]
p.next_pair # nil
Using #each:
p = CMF::Parser.new
p.messge_hex = "040d"
p.each { |k, v| puts "#{k}: #{v}" }
Instance Attribute Summary collapse
-
#inverted_dictionary ⇒ Hash
readonly
The inverted dictionary, mapping numbers to tag names.
Instance Method Summary collapse
-
#each {|tag, value| ... } ⇒ Enumerator
Calls the given block once for each pair found in the message.
-
#initialize(dictionary = nil) ⇒ Parser
constructor
Creates a new instance of Parser.
-
#message=(message) ⇒ Object
Sets a new CMF message.
-
#message_hex=(message_hex) ⇒ Object
Sets a new CMF message from a hex string.
-
#next_pair ⇒ Array?
Returns the next pair in the message, or nil if the whole message has been parsed.
-
#parse(message = nil) ⇒ Hash
Parses a CMF message into an object.
-
#parse_hex(message_hex) ⇒ Hash
Parses a hex-encoded CMF message into an object.
Constructor Details
permalink #initialize(dictionary = nil) ⇒ Parser
Creates a new instance of CMF::Parser.
42 43 44 45 |
# File 'lib/cmf/parser.rb', line 42 def initialize(dictionary = nil) @inverted_dictionary = Dictionary.validate(dictionary).invert @io = StringIO.new end |
Instance Attribute Details
permalink #inverted_dictionary ⇒ Hash (readonly)
Returns The inverted dictionary, mapping numbers to tag names.
36 37 38 |
# File 'lib/cmf/parser.rb', line 36 def inverted_dictionary @inverted_dictionary end |
Instance Method Details
permalink #each {|tag, value| ... } ⇒ Enumerator
Calls the given block once for each pair found in the message.
104 105 106 107 108 109 110 111 |
# File 'lib/cmf/parser.rb', line 104 def each return to_enum(:each) unless block_given? loop do pair = next_pair break if pair.nil? yield(pair[0], pair[1]) end end |
permalink #message=(message) ⇒ Object
Sets a new CMF message.
50 51 52 |
# File 'lib/cmf/parser.rb', line 50 def () @io = StringIO.new() end |
permalink #message_hex=(message_hex) ⇒ Object
Sets a new CMF message from a hex string.
57 58 59 |
# File 'lib/cmf/parser.rb', line 57 def () self. = [].pack('H*') end |
permalink #next_pair ⇒ Array?
Returns the next pair in the message, or nil if the whole message has been parsed.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/cmf/parser.rb', line 125 def next_pair return nil if @io.eof? byte = @io.getbyte type = byte & 0x07 tag = byte >> 3 if tag == 31 tag = Varint.deserialize(@io) end if @inverted_dictionary[tag] tag = @inverted_dictionary[tag] end case type when Type::POSITIVE_NUMBER [tag, Varint.deserialize(@io)] when Type::NEGATIVE_NUMBER [tag, -Varint.deserialize(@io)] when Type::STRING, Type::BYTE_ARRAY length = Varint.deserialize(@io) s = @io.read(length) s.bytesize == length or raise MalformedMessageError, "Unexpected end of stream" s = s.force_encoding(Encoding::UTF_8) if type == Type::STRING [tag, s] when Type::BOOL_TRUE [tag, true] when Type::BOOL_FALSE [tag, false] when Type::DOUBLE s = @io.read(8) s.bytesize == 8 or raise MalformedMessageError, "Unexpected end of stream" [tag, s.unpack('E').first] else raise MalformedMessageError, "Unknown type" end end |
permalink #parse(message = nil) ⇒ Hash
Parses a CMF message into an object.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/cmf/parser.rb', line 71 def parse( = nil) self. = if obj = {} each do |key, value| if obj.has_key?(key) obj[key] = Array(obj[key]) obj[key] << value else obj[key] = value end end obj end |
permalink #parse_hex(message_hex) ⇒ Hash
Parses a hex-encoded CMF message into an object.
91 92 93 94 95 |
# File 'lib/cmf/parser.rb', line 91 def parse_hex() self. = parse end |