8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/gdt/parser.rb', line 8
def self.parse(string)
string.gsub!("\r\n","\n")
string.split(/\n/).
delete_if { |line| line =~ /^\s*$/ }.
inject({}) do |h, line|
length, type, data = line.scan(/(\d{3})(\d{4})(.*)/).first
raise ParseError, "line does not match expected GDT format: '#{line}'" unless length && type && data
length = length.to_i
type = type.to_i
expected_length = 3 + 4 + data.length + 2
raise ParseError, "wrong length in GDT data: '#{line}'" unless length == expected_length
h[type] = data
h
end
end
|