class DMAPParser::Parser

The DMAPParser class parses DMAP responses

Constants

ParseError

Public Class Methods

new(response) click to toggle source
# File lib/dmapparser/parser.rb, line 14
def initialize(response)
  @response = response
  @response = StringIO.new(response) unless @response.is_a? IO
  @response.set_encoding(Encoding::BINARY)
end
parse(response) click to toggle source
# File lib/dmapparser/parser.rb, line 10
def self.parse(response)
  new(response).parse
end

Public Instance Methods

parse() click to toggle source
# File lib/dmapparser/parser.rb, line 20
def parse
  fail ParseError if @response.nil? || @response.size < 8
  ret = TagContainer.new(read_key)
  fail ParseError if ret.type && !ret.type.container?
  ret.value = parse_container(read_length)
  ret
end

Private Instance Methods

build_tag(key, length) click to toggle source
# File lib/dmapparser/parser.rb, line 58
def build_tag(key, length)
  tag = TagDefinition[key] ||
        TagDefinition.new(key, :unknown, "unknown (#{length})")
  if tag.container?
    TagContainer.new(tag, parse_container(length))
  else
    Tag.new(tag, Converter.decode(tag.type, read_bytes(length)))
  end
end
bytes_available?(num = 1) click to toggle source
# File lib/dmapparser/parser.rb, line 34
def bytes_available?(num = 1)
  bytes_left - num >= 0
end
bytes_left() click to toggle source
# File lib/dmapparser/parser.rb, line 30
def bytes_left
  @response.size - @response.pos
end
parse_container(container_length) click to toggle source
# File lib/dmapparser/parser.rb, line 51
def parse_container(container_length)
  values = []
  end_pos = container_length + @response.pos
  values << build_tag(read_key, read_length) until @response.pos == end_pos
  values
end
read_bytes(length) click to toggle source
# File lib/dmapparser/parser.rb, line 38
def read_bytes(length)
  fail ParseError, 'Not enough data available' unless bytes_available?(length)
  @response.read(length)
end
read_key() click to toggle source
# File lib/dmapparser/parser.rb, line 47
def read_key
  read_bytes(4)
end
read_length() click to toggle source
# File lib/dmapparser/parser.rb, line 43
def read_length
  Converter.bin_to_int(read_bytes(4))
end