class MauxRobot::Parser

Constants

ALLOWED_ORDERS

Public Instance Methods

parse(line_input) click to toggle source
# File lib/maux_robot/parser.rb, line 7
def parse(line_input)
  clean_input = line_input.strip.squeeze(" ").split(" ")
  order = sanitize_order(clean_input.shift)
  return unless order

  command = {order: order}

  if clean_input.any?
    arguments = sanitize_arguments(order, clean_input.join(""))
    command[:arguments] = arguments
  end

  command
end

Private Instance Methods

sanitize_arguments(order, arguments) click to toggle source
# File lib/maux_robot/parser.rb, line 29
def sanitize_arguments(order, arguments)
  case order
  when :place
    arguments = arguments.delete(" ").split(",")
    {x: arguments[0], y: arguments[1], face: arguments[2]}
  when :report
    {format_type: arguments.downcase.to_sym}
  end
end
sanitize_order(input_string) click to toggle source
# File lib/maux_robot/parser.rb, line 24
def sanitize_order(input_string)
  order = input_string.downcase.to_sym
  order if ALLOWED_ORDERS.include?(order)
end