class Hbci::Parser
Constants
- BINARY_ELEMENT_LENGTH_REGEX
Binary Elements may contain unescaped delimiters. Thus they are not terminated by regular delimiters. But their content is preceeded with its length surrounded by '@'s. e.g.:
'@6@mydata' or '@12@mydatamydata'
The BINARY_ELEMENT_LENGTH_REGEx matches only the length.
- ELEMENT_DELIMITER
- ELEMENT_GROUP_DELIMITER
- ELEMENT_REGEX
ELEMENT_REGEX
matches everything until a a delimiter that is not escapedNODE EXPLANATION
( group and capture to \1:
.*? any character except \n (0 or more times (matching the least amount possible))
(?= look ahead to see if there is:
(?<! look behind to see if there is not:
\? '?'
) end of look-behind
[:+'] any character of: ':', '+', '''
) end of look-ahead
) end of \1
- SEGMENT_DELIMITER
Attributes
scanner[R]
segments[RW]
Public Class Methods
new(string)
click to toggle source
# File lib/hbci/parser.rb, line 49 def initialize(string) @scanner = StringScanner.new(string) @segments = [] add_segment add_element_group end
parse(string)
click to toggle source
# File lib/hbci/parser.rb, line 45 def self.parse(string) new(string).parse end
Public Instance Methods
parse()
click to toggle source
# File lib/hbci/parser.rb, line 56 def parse parse_element while scanner.rest_size > 1 parse_delimiter parse_element end segments end
Private Instance Methods
add_element_group()
click to toggle source
# File lib/hbci/parser.rb, line 103 def add_element_group current_segment << [] end
add_segment()
click to toggle source
# File lib/hbci/parser.rb, line 99 def add_segment segments << [] end
binary_element_ahead?()
click to toggle source
# File lib/hbci/parser.rb, line 95 def binary_element_ahead? scanner.peek(1) == '@' && scanner.check(BINARY_ELEMENT_LENGTH_REGEX) end
current_element_group()
click to toggle source
# File lib/hbci/parser.rb, line 111 def current_element_group current_segment.last end
current_segment()
click to toggle source
# File lib/hbci/parser.rb, line 107 def current_segment segments.last end
parse_binary_element()
click to toggle source
# File lib/hbci/parser.rb, line 88 def parse_binary_element scanner.scan(BINARY_ELEMENT_LENGTH_REGEX) binary = scanner.rest.byteslice(0, scanner[1].to_i) scanner.pos = scanner.pos + scanner[1].to_i current_element_group << binary end
parse_delimiter()
click to toggle source
# File lib/hbci/parser.rb, line 76 def parse_delimiter delimiter = scanner.getch return if scanner.eos? if delimiter == ELEMENT_GROUP_DELIMITER add_element_group elsif delimiter == SEGMENT_DELIMITER add_segment add_element_group end end
parse_element()
click to toggle source
# File lib/hbci/parser.rb, line 72 def parse_element binary_element_ahead? ? parse_binary_element : parse_regular_element end
parse_regular_element()
click to toggle source
# File lib/hbci/parser.rb, line 67 def parse_regular_element str = scanner.scan(ELEMENT_REGEX) current_element_group << str.gsub('??', '?').gsub('?:', ':').gsub("?'", "'").gsub('?+', '+') end