dmapparser 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8f70493e234f76cac85f3a07af469d45eefe839c
4
- data.tar.gz: 4a3ae53c0f24a30d0fbd7552c4b2a1abfa6d0cec
3
+ metadata.gz: 68b7396a6898eeefafa26e430022b49d3b281115
4
+ data.tar.gz: d0188b16664b041ca72ba713dafa45aafbf919d5
5
5
  SHA512:
6
- metadata.gz: 46c2fc9701f4b715acf4f9a168c713834d249a37a270ecaceb556a5ba5c18a57dfcc041c03eb5e5e32a2861c4c61111b4548c6f2a21ff1ea2584456c1c44091e
7
- data.tar.gz: 79ed8bd31dd10179e8d912ab83a02c85a59fa47d739a0474c70b2d273152ca624eafaa7ee3357e11b0da5553db01c963b455fccd48b3daf659a3710ed346be1f
6
+ metadata.gz: 7a339caec989bebaecb8792e3b3ddd09fca8305822a32690bb5da872af5efc68e5362f94781a51279dd8cd3064b88b85a4f8f123ebfa5350c984c5a9a4b2491e
7
+ data.tar.gz: d09b9ba744cee11dbe7ea60c49607905aef822d9c61bb23e5352b2c337813a675517e655a54d3673bd2d8de222aadb61053f5aed4bbc935cb2dc2857674c1d20
data/Gemfile CHANGED
@@ -4,8 +4,8 @@ source 'https://rubygems.org'
4
4
  gemspec
5
5
 
6
6
  group :test do
7
- gem 'minitest', '~> 5.2.2'
7
+ gem 'minitest', '~> 5.3.1'
8
8
  gem 'coveralls', require: false
9
- gem 'rubocop', '~> 0.18.0'
9
+ gem 'rubocop', '~> 0.20.0'
10
10
  gem 'simplecov', require: false
11
11
  end
data/Rakefile CHANGED
@@ -16,7 +16,7 @@ task :rubocop do
16
16
  puts "Running Rubocop #{Rubocop::Version::STRING}"
17
17
  args = FileList['**/*.rb', 'Rakefile', 'dmapparser.gemspec', 'Gemfile']
18
18
  cli = Rubocop::CLI.new
19
- fail unless cli.run(['-a'] + args) == 0
19
+ cli.run(args)
20
20
  end
21
21
 
22
22
  task default: :test
data/dmapparser.gemspec CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.version = DMAPParser::VERSION
9
9
  spec.authors = ['Jurriaan Pruis']
10
10
  spec.email = ['email@jurriaanpruis.nl']
11
- spec.summary = %q{Parses DMAP data}
11
+ spec.summary = %q(Parses DMAP data)
12
12
  spec.homepage = 'https://github.com/jurriaan/dmapparser'
13
13
  spec.license = 'MIT'
14
14
  spec.platform = Gem::Platform::RUBY
@@ -16,7 +16,7 @@ module DMAPParser
16
16
  tag = TagDefinition[method]
17
17
  return super if tag.nil?
18
18
 
19
- if block_given?
19
+ if block_given? || tag.container?
20
20
  fail "Tag #{tag} is not a container type" unless tag.container?
21
21
  build_container(tag, &block)
22
22
  else
@@ -35,7 +35,7 @@ module DMAPParser
35
35
  end
36
36
  end
37
37
 
38
- def build_container(tag , &block)
38
+ def build_container(tag, &block)
39
39
  @dmap_stack << TagContainer.new(tag)
40
40
  instance_exec(&block)
41
41
  if @dmap_stack.length > 1
@@ -3,7 +3,7 @@ module DMAPParser
3
3
  class Converter
4
4
  class << self
5
5
  def date_to_bin(data)
6
- int_to_bin(value.to_i)
6
+ int_to_bin(data.to_i)
7
7
  end
8
8
 
9
9
  def bin_to_byte(data)
@@ -11,7 +11,7 @@ module DMAPParser
11
11
  end
12
12
 
13
13
  def bin_to_long(data)
14
- (bin_to_int(data[0..3]) << 32) + bin_to_int(data[4..7])
14
+ (bin_to_int(data[0..3]) << 32) | bin_to_int(data[4..7])
15
15
  end
16
16
 
17
17
  def bin_to_int(data)
@@ -31,7 +31,7 @@ module DMAPParser
31
31
  end
32
32
 
33
33
  def bin_to_hex(data)
34
- data.bytes.reduce('') { |a, e| a + sprintf('%02X', e) }
34
+ data.bytes.reduce('') { |a, e| a + format('%02X', e) }
35
35
  end
36
36
 
37
37
  def bin_to_date(data)
@@ -51,7 +51,7 @@ module DMAPParser
51
51
  end
52
52
 
53
53
  def long_to_bin(data)
54
- [data >> 32, data & 0xfffffff].pack 'NN'
54
+ [data >> 32, data & 0xFFFFFFFF].pack 'NN'
55
55
  end
56
56
 
57
57
  def short_to_bin(data)
@@ -59,7 +59,7 @@ module DMAPParser
59
59
  end
60
60
 
61
61
  def version_to_bin(data)
62
- data.split('.').pack 'nCC'
62
+ data.split('.').map { |part| part.to_i }.pack 'nCC'
63
63
  end
64
64
 
65
65
  def hex_to_bin(data)
@@ -108,7 +108,6 @@ module DMAPParser
108
108
  if respond_to? decode_method
109
109
  send(decode_method, data)
110
110
  else
111
- warn "Decoder: Unknown type #{type}"
112
111
  decode_unknown(data)
113
112
  end
114
113
  end
@@ -118,8 +117,7 @@ module DMAPParser
118
117
  if respond_to? encode_method
119
118
  send(encode_method, data)
120
119
  else
121
- warn "Encoder: Unknown type #{type}"
122
- data
120
+ data # This only works for strings..
123
121
  end
124
122
  end
125
123
  end
@@ -14,14 +14,13 @@ module DMAPParser
14
14
  def initialize(response)
15
15
  @response = response
16
16
  @response = StringIO.new(response) unless @response.is_a? IO
17
- @response.set_encoding(Encoding::BINARY) # Use unicode
17
+ @response.set_encoding(Encoding::BINARY)
18
18
  end
19
19
 
20
20
  def parse
21
21
  return nil if @response.nil? || @response.size == 0
22
22
  fail ParseError if @response.size < 8
23
- ret = TagContainer.new
24
- ret.type = TagDefinition[read_key]
23
+ ret = TagContainer.new(read_key)
25
24
  fail ParseError if ret.type && !ret.type.container?
26
25
  ret.value = parse_container(read_length)
27
26
  ret
@@ -1,6 +1,14 @@
1
1
  module DMAPParser
2
2
  # The Tag class
3
3
  class Tag < Struct.new(:type, :value)
4
+ def initialize(type, value)
5
+ unless type.is_a? TagDefinition
6
+ type = TagDefinition[type] ||
7
+ TagDefinition.new(type, :unknown, "unknown (#{length})")
8
+ end
9
+ super
10
+ end
11
+
4
12
  def to_s
5
13
  "#<#{self.class.name} #{type}>"
6
14
  end
@@ -1,8 +1,8 @@
1
1
  module DMAPParser
2
2
  # The TagContainer class is a Tag which contains other Tags
3
3
  class TagContainer < Tag
4
- def initialize(type = nil, value = [])
5
- super type, value
4
+ def initialize(type, value = [])
5
+ super
6
6
  end
7
7
 
8
8
  def inspect(level = 0)
@@ -14,12 +14,8 @@ module DMAPParser
14
14
  def get_value(key)
15
15
  return value[key] if key.is_a? Fixnum
16
16
  tag = get_tag(key)
17
-
18
- if tag.type.container?
19
- tag
20
- elsif !tag.nil?
21
- tag.value
22
- end
17
+ return unless tag
18
+ tag.type.container? ? tag : tag.value
23
19
  end
24
20
 
25
21
  def get_tag(key)
@@ -39,7 +35,7 @@ module DMAPParser
39
35
  elsif has?(method)
40
36
  get_value(method)
41
37
  else
42
- super
38
+ nil
43
39
  end
44
40
  end
45
41
 
@@ -1,4 +1,4 @@
1
1
  # DMAPParser handles the parsing of DMAP data
2
2
  module DMAPParser
3
- VERSION = '0.0.2'
3
+ VERSION = '0.1.0'
4
4
  end
@@ -0,0 +1,38 @@
1
+ require_relative '../../test_helper'
2
+
3
+ describe DMAPParser::Converter do
4
+ it 'should return the original output again' do
5
+ time = Time.now
6
+ klass = DMAPParser::Converter
7
+ klass.bin_to_date(klass.date_to_bin(time)).to_i.must_equal time.to_i
8
+ klass.bin_to_bool(klass.bool_to_bin(true)).must_equal true
9
+ klass.bin_to_int(klass.int_to_bin(90000)).must_equal 90000
10
+ klass.bin_to_byte(klass.byte_to_bin(190)).must_equal 190
11
+ klass.bin_to_long(klass.long_to_bin(3**25)).must_equal 3**25
12
+ klass.bin_to_version(klass.version_to_bin('1322.200.3')).must_equal '1322.200.3'
13
+ klass.bin_to_short(klass.short_to_bin(19007)).must_equal 19007
14
+ end
15
+
16
+ it 'should automatically convert numerics' do
17
+ klass = DMAPParser::Converter
18
+ byte = klass.byte_to_bin(244)
19
+ short = klass.short_to_bin(244)
20
+ int = klass.int_to_bin(244)
21
+ long = klass.long_to_bin(244)
22
+ klass.data_to_numeric(byte).must_equal(244)
23
+ klass.data_to_numeric(short).must_equal(244)
24
+ klass.data_to_numeric(int).must_equal(244)
25
+ klass.data_to_numeric(long).must_equal(244)
26
+ end
27
+
28
+ it 'should decode unknown types' do
29
+ klass = DMAPParser::Converter
30
+ byte = klass.byte_to_bin(244)
31
+ klass.decode(:ZZZZ, byte).must_equal 244
32
+ end
33
+
34
+ it 'should encode unknown strings' do
35
+ klass = DMAPParser::Converter
36
+ klass.encode(:ZZZZ, 'aaa').must_equal 'aaa'
37
+ end
38
+ end
@@ -3,8 +3,11 @@ require_relative '../../test_helper'
3
3
  describe DMAPParser::Parser do
4
4
  it 'should raise a ParserError when given invalid data' do
5
5
  invalid = []
6
- invalid << (0..255).map { |c| c.chr }.join # random data
6
+ 20.times do
7
+ invalid << (0..255).map { |c| c.chr }.join # random data
8
+ end
7
9
  invalid << "rand\x00\x00\x00\x01" # non correct size
10
+ invalid << "mcon\x00\x00\x00\x05rand\x09" # wrong tag size
8
11
  invalid << "msup\x00\x00\x00\x01\x01" # not a container!
9
12
  invalid << "rand\x00\x00\x00\x01\x01" # not a container!
10
13
  invalid.each do |data|
@@ -0,0 +1,14 @@
1
+ require_relative '../../test_helper'
2
+
3
+ describe DMAPParser::TagContainer do
4
+ it 'should respond to known tags' do
5
+ subject = DMAPParser::TagContainer.new(:mcon,
6
+ [DMAPParser::Tag.new(:minm, 1337)])
7
+ subject.respond_to?(:unknown).must_equal false
8
+ subject.respond_to?(:minm).must_equal true
9
+ subject.minm.must_equal 1337
10
+ subject.unknown.must_equal nil
11
+ subject.inspect.must_include('minm')
12
+ subject.inspect.must_include('1337')
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ require_relative '../../test_helper'
2
+
3
+ describe DMAPParser::Tag do
4
+ it 'should return the correct value' do
5
+ subject = DMAPParser::Tag.new(:minm, 'Test thing')
6
+ subject.value.must_equal 'Test thing'
7
+ subject.type.must_equal DMAPParser::TagDefinition[:minm]
8
+ end
9
+
10
+ it 'should be recognizable as a tag' do
11
+ subject = DMAPParser::Tag.new(:minm, 'Test thing')
12
+ subject.to_s.must_equal('#<DMAPParser::Tag minm (dmap.itemname: string)>')
13
+ end
14
+ end
data/test/test_helper.rb CHANGED
@@ -1,12 +1,11 @@
1
1
  require 'simplecov'
2
2
  require 'coveralls'
3
+ Coveralls.wear!
3
4
  SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
4
- SimpleCov::Formatter::HTMLFormatter,
5
- Coveralls::SimpleCov::Formatter
5
+ SimpleCov::Formatter::HTMLFormatter,
6
+ Coveralls::SimpleCov::Formatter
6
7
  ]
7
- SimpleCov.start do
8
- # add_filter '/test/'
9
- end
8
+ SimpleCov.start
10
9
  gem 'minitest'
11
10
  require 'minitest/autorun'
12
11
  require 'minitest/hell'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dmapparser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jurriaan Pruis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-02 00:00:00.000000000 Z
11
+ date: 2014-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -83,6 +83,7 @@ files:
83
83
  - lib/dmapparser/version.rb
84
84
  - test/.rubocop.yml
85
85
  - test/lib/dmapparser/builder_test.rb
86
+ - test/lib/dmapparser/converter_test.rb
86
87
  - test/lib/dmapparser/parser_test.rb
87
88
  - test/lib/dmapparser/tag_container_test.rb
88
89
  - test/lib/dmapparser/tag_test.rb
@@ -116,6 +117,7 @@ summary: Parses DMAP data
116
117
  test_files:
117
118
  - test/.rubocop.yml
118
119
  - test/lib/dmapparser/builder_test.rb
120
+ - test/lib/dmapparser/converter_test.rb
119
121
  - test/lib/dmapparser/parser_test.rb
120
122
  - test/lib/dmapparser/tag_container_test.rb
121
123
  - test/lib/dmapparser/tag_test.rb