class Rsirts::Parser

Constants

ZMAP_VALUES

Public Class Methods

parse(path) click to toggle source
# File lib/rsirts/parser.rb, line 9
def self.parse path
  new.parse path
end

Public Instance Methods

parse(path) click to toggle source
# File lib/rsirts/parser.rb, line 13
def parse path
  map_depth(
    File.new(path)
    .readlines
    .map { |line| line.chomp }
    .tap { |lines| check_format lines }
  )
end

Private Instance Methods

check_format(lines) click to toggle source
# File lib/rsirts/parser.rb, line 24
def check_format lines
  lines.each_with_index do |line,i|
    line_length ||= line.length
    raise ParseError, "Length of line #{i+1} does not match previous lines" if line.length != line_length
    raise ParseError, "Invalid character at line #{i+1} position #{$~.offset(0)[0]}" if line[/[^-0-9]/]
  end
end
map_depth(lines) click to toggle source
# File lib/rsirts/parser.rb, line 32
def map_depth lines
  lines.map do |line|
    line.each_char.to_a.map do |c|
      ZMAP_VALUES[c]
    end
  end
end