Sha256: 09a4871d0fb45eae270c4c7be7d2ff0f75290e63a5965e89af5d0c140f642f3c

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

module Revepast
	module Parser
		require "revepast/parser/eft"
		require "revepast/parser/cargo_scan"
		class Utils
			def sanitize(str)
				result = []
				str.each_line do |line|
					line = line.chomp.gsub /^$\n/, ''
					unless line.nil? || line == ""
						result << line
					end
				end
				return result
			end

			def regex_match_lines(regex, lines)
				matches = Array.new
				bad_lines = Array.new
				lines.each do |line|
					if line.match(regex)
						a = line.match(regex)
						matches.push(a.captures)
					else
						bad_lines.push(line.chomp)
					end
				end
				return matches, bad_lines
			end

			def parse_listing(lines)
				# 10 x Cargo Scanner II | 10x Cargo Scanner II | 10 Cargo Scanner II
				listing_re = /^([\d,'\.]+?) ?x? ([\S ]+)$/
				# Cargo Scanner II x10 | Cargo Scanner II x 10 | Cargo Scanner II 10
				listing_re2 = /^([\S ]+?) x? ?([\d,'\.]+)$/
				# Cargo Scanner II
				listing_re3 = /^([\S ]+)$/

				matches, bad_lines = regex_match_lines(listing_re, lines)
				matches2, bad_lines2 = regex_match_lines(listing_re2, bad_lines)
				matches3, bad_lines3 = regex_match_lines(listing_re3, bad_lines2)

				items = Hash.new { |h, k| h[k] = 0 }
				matches.each do |count, name|
					items[name.strip] += count.to_i
				end
				matches2.each do |name, count|
					items[name.strip] += count.to_i
				end
				matches3.each do |res|
					items[res[0].strip] += 1
				end
				results = []
				items.each do |name, quantity|
					results << {
						'name' => name,
						'quantity' => quantity
						}
				end
				return results, bad_lines3
			end

		end
	end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
revepast-1.0.2 lib/revepast/parser.rb
revepast-0.1.0 lib/revepast/parser.rb
revepast-0.0.3 lib/revepast/parser.rb