class MediaPlayer::InputManager

Constants

INSTRUCTIONS

Shortcuts? maybe SHORTCUTS = %w(a sh pl st pa n pr e) SHORTCUT_MAPPINGS = {

'a' => 'add',
'sh' => 'shuffle',
'pl' => 'play',
'st' => 'stop',
'pa' => 'pause',
'n' => 'next',
'pr' => 'prev',
'e' => 'exit'

}

Attributes

player[R]

Public Class Methods

new(args={}) click to toggle source
# File lib/input_manager.rb, line 23
def initialize(args={})
  @player = args.fetch(:player)
end

Public Instance Methods

process(instruction) click to toggle source
# File lib/input_manager.rb, line 27
def process(instruction)
  if INSTRUCTIONS.include? instruction.split(' ').first
    begin
      if instruction.match(/add/)
        path = (instruction.split(' ') - ['add']).join(' ')
        type = File.ftype(path)
        if type == 'directory'
          Dir.glob("#{path}/*.mp3") { |file| @player.add_media(file) }
        elsif type == 'file'
          @player.add_media(path)
        end
      else
        send instruction
      end
    rescue => e
      return e.message
    end
  else
    return 'Invalid Instruction'
  end
end
start() click to toggle source

TODO: The only thing not covered by test

# File lib/input_manager.rb, line 50
def start
  while (input = gets.chomp) != 'exit'
    p process(input)
  end
  p process('stop')
end