class Panier::Application::CLI

A class responsible for handling the command line interface input.

Constants

EXIT_FAILURE
EXIT_SUCCESS

Public Class Methods

new() click to toggle source
# File lib/panier/application/cli.rb, line 12
def initialize
  I18n.enforce_available_locales = false
  @service = SalesTaxService.new
end

Public Instance Methods

run() click to toggle source

The main application loop.

# File lib/panier/application/cli.rb, line 20
def run
  begin
    print_welcome
    loop do
      prompt_for_input
    end
  rescue SignalException, Interrupt
    puts "\nExiting..."
  end

  EXIT_SUCCESS
end

Private Instance Methods

print_welcome() click to toggle source

Prints a welcome message and instructions about how to quit.

process_input(input) click to toggle source

Given a complete set of input, prints a receipt.

# File lib/panier/application/cli.rb, line 62
def process_input(input)
  begin
    puts @service.evaluate_input(input)
  rescue ArgumentError
    $stderr.puts 'The input was invalid.'
  end
  puts
end
prompt_for_input() click to toggle source

Asks the user for input and processes it when given.

# File lib/panier/application/cli.rb, line 46
def prompt_for_input
  puts 'Enter some sample input, then leave a blank line to proceed.'

  input = []
  $stdin.each do |line|
    break if line.nil? || line.chomp.empty?
    input << line.chomp
  end

  input = input.join("\n")
  process_input(input) unless input.empty?
end