class Panier::Application::InputReader
The InputReader
is responsible for parsing raw input data.
Constants
- CELLS_PER_LINE
- HEADER
Public Class Methods
new(product_service = nil)
click to toggle source
# File lib/panier/application/input_reader.rb, line 14 def initialize(product_service = nil) @product_service = product_service || Panier::Domain::ProductService.new end
Public Instance Methods
parse_input(input)
click to toggle source
# File lib/panier/application/input_reader.rb, line 19 def parse_input(input) line_items = input.lines.reject(&:blank?).map do |line| parse_line(line) end line_items.reject(&:nil?) end
parse_line(line)
click to toggle source
# File lib/panier/application/input_reader.rb, line 26 def parse_line(line) return nil if line.match(HEADER) parsed = CSV.parse_line(line) unless parsed.count == CELLS_PER_LINE fail ArgumentError, 'invalid input' end quantity = Integer(parsed[0]) name = parsed[1].strip price = Money.new(Float(parsed[2]) * 100) product = @product_service.find_by_name_and_price(name, price) product.present? ? LineItem.new(product, quantity) : nil end