scottcreynolds
11/14/2013 - 8:00 PM

cli_jukebox_spec.rb

# Build a Jukebox
 
# When that program is run, it should introduce itself
# to the user and accept input from the user using the gets command.
 
# The jukebox should respond to 4 commands, help, play, list and exit.
 
# The help command should output instructions for the user
# on how to use the jukebox.
 
# The list command should output a list of songs that the
# user can play.
 
# the play command should accept a song, either by number/index
# or name. Once the user has indicated which song they want to
# play, the jukebox should output 'Playing The Phoenix - 1901'
# or whatever song name is important.
 
# if the user types in exit, the jukebox should say goodbye
# and the program should shut down.
 
# Think about the following things
 
# How to keep the program running until the exit command is
# executed (Hint: Loop maybe? Loop upon a condition)
 
# How to normalize the user's input so LIST and list are the
# same. (Hint, maybe downcase and strip it)
 
# How to give the songs an "index" so that when you list them
# out, you can refer to them by position so the user can just
# type play 1 and then you find the first song. (Hint, check
# out a method called each_with_index)
 
def list(songs)
  songs.each_with_index do |song, i|
    puts "#{i+1}. #{song}"
  end
end
 
def input
  gets.downcase.strip
end
 
def play(songs)
  song_selection = input
  
  begin
    song_selection = Integer(song_selection)
  rescue
  end
 
  if song_selection.is_a?(Integer)
    puts "Playing #{songs[song_selection.to_i-1]}..."
  else
    puts song_selection if songs.include?(song_selection)
  end
  
end

def do_exit
  puts "Goodbye!"
end

def show_help
  puts "I support help, play, exit and list"
end

def run
  songs = [
    "The Phoenix - 1901",
    "Tokyo Police Club - Wait Up",
    "Sufjan Stevens - Too Much",
    "The Naked and the Famous - Young Blood",
    "(Far From) Home - Tiga",
    "The Cults - Abducted",
    "The Phoenix - Consolation Prizes"
  ]
  loop do
    puts "Please enter a command:"
    command = input
    case command
    when "play"
      puts "What song would you like to play? By # please."
      list(songs)
      play(songs)
    when "exit"
      do_exit
      break
    when "list"
      list(songs)
    when "help"
      show_help
    else
      puts "What are you talking about?"
    end
  end  
end
require_relative './jukebox'

RSpec.configure do |config|
  config.color_enabled = true
  config.tty = true
  # Use the specified formatter
  config.formatter = :documentation # :progress, :html, :textmate
end

def capture_stdout(&block)
  original_stdout = $stdout
  $stdout = fake = StringIO.new
  begin
    yield
  ensure
    $stdout = original_stdout
  end
  fake.string
end
songs = [
    "The Phoenix - 1901",
    "Tokyo Police Club - Wait Up",
    "Sufjan Stevens - Too Much",
    "The Naked and the Famous - Young Blood",
    "(Far From) Home - Tiga",
    "The Cults - Abducted",
    "The Phoenix - Consolation Prizes"
  ]
describe "CLI Jukebox" do

  context "with commands" do
    it "responds to 'HELP'" do
      output = capture_stdout {show_help}
      output.should match /I support help, play, exit and list/
    end

    it "responds to 'LIST'" do
      output = capture_stdout { list(songs) }
      output.should match /The Phoenix - 1901/
    end

    it "responds to 'PLAY'" do
      pending "this"
    end

    it "responds to 'EXIT'" do
    	output = capture_stdout { do_exit }
    	output.should match /Goodbye!/
    end
  end

end