class Trainworks::FileParser
FileParser
is responsible for parsing the input file @example
AB5, cd99, LetterLetterPositiveNumbers
Constants
- SINGLE_TUPLE_REGEX
captures, for example, AB99
Public Class Methods
new(file_path)
click to toggle source
Converts the object into textual markup given a specific format. @param file_path [String] the path for input file
# File lib/trainworks/file_parser.rb, line 13 def initialize(file_path) @raw_content = File.read(file_path) end
Public Instance Methods
parse()
click to toggle source
@return [Array<Route>] array of {Route}s @raise [InvalidRailroadInputFormat] if the tuple doesn't match SINGLE_TUPLE_REGEX
# File lib/trainworks/file_parser.rb, line 19 def parse clean_string(@raw_content).split(',').map do |route_string| matched_route_string = route_string.match(SINGLE_TUPLE_REGEX) raise InvalidRailroadInputFormat, route_string if matched_route_string.nil? Route.new( from: matched_route_string[:from], to: matched_route_string[:to], distance: matched_route_string[:distance] ) end end
Private Instance Methods
clean_string(text)
click to toggle source
removes special characters from the input except letters, numbers and commas @private
# File lib/trainworks/file_parser.rb, line 36 def clean_string(text) text.gsub(/[^0-9A-Za-z\,]/, '') end