Module: EISCP::Parser::ISCPParser

Defined in:
lib/eiscp/parser/iscp_parser.rb

Overview

This module parses an ISCP string and returns a Message object

Constant Summary collapse

REGEX =

Regexp for parsing ISCP messages

/(?<start>!)?(?<unit_type>(\d|x))?(?<command>[A-Z]{3})\s?(?<value>.*?)(?<terminator>[[:cntrl:]]*$)/.freeze

Class Method Summary collapse

Class Method Details

.parse(string) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/eiscp/parser/iscp_parser.rb', line 10

def self.parse(string)
  match = string.match(REGEX)

  # Convert MatchData to Hash
  hash = Hash[match.names.zip(match.captures)]

  # Remove nil and blank values
  hash.delete_if { |_, v| v.nil? || v == '' }

  # Convert keys to symbols
  hash = hash.each_with_object({}) do |(k, v), memo|
    memo[k.to_sym] = v
  end

  Message.new(**hash)
end