Skip to content

Instantly share code, notes, and snippets.

@irmiller22
Last active December 24, 2015 14:29
Show Gist options
  • Save irmiller22/6813319 to your computer and use it in GitHub Desktop.
Save irmiller22/6813319 to your computer and use it in GitHub Desktop.
require_relative './song_library.rb'
def jukebox(command)
if command.downcase == "list"
list_library
else
parse_command(command)
end
end
def list_artist(artist, album_hash)
artist_list = "\n---------------\n"
artist_list += "#{artist}:\n"
artist_list += "---------------"
album_hash[:albums].each do |album_name, songs_array|
artist_list += "\n#{album_name}:\n\t"
artist_list += songs_array.join("\n\t")
end
artist_list
end
def list_library
lib = full_library
lib.each do |artist, album_hash|
puts list_artist(artist, album_hash)
end
end
def parse_command(command)
parse_artist(command, full_library) || play_song(command, full_library) || not_found(command)
end
def parse_artist(command, lib)
cmd = command.to_sym
parsed = false
if lib.has_key?(cmd)
puts list_artist(command, lib[cmd])
parsed = false
else
lib.each do |artist, hash|
if command == artist.to_s.downcase
puts list_artist(artist, hash)
parsed = true
break
end
end
end
parsed
end
def play_song(command, lib)
lib.each do |artist, hash|
hash.each do |album_name, albums_hash|
albums_hash.each do |album, song_array|
song_array.each do |song|
if song.downcase == command
puts "Now Playing: #{artist}: #{album} - #{song}\n"
return true
end
end
end
end
end
false
end
def not_found(command)
puts "I did not understand '#{command}'!\n\n"
true
end
require_relative './jukebox.rb'
def run
puts "Welcome to Ruby Console Jukebox!"
puts "Enter a command to continue. Type 'help' for a list of commands."
command = get_command
while command.downcase != "exit" do
run_command(command)
puts "Enter a command to continue. Type 'help' for a list of commands.\n"
command = get_command unless command == "exit"
end
end
def get_command
gets.strip.downcase
end
def run_command(command)
case command
when "help"
show_help
else
jukebox(command)
end
end
def show_help
help = "Never worked a jukebox, eh? Pretty standard. Available commands are:\n"
help += "'help' - shows this menu\n"
help += "'list' - lists the whole song library\n"
help += "or you can enter an artist's name to show that artist's songs\n"
puts help
end
run
def full_library
{
:"U2" => {
:albums => {
:"The Joshua Tree" =>
["With or Without You", "Still Haven't Found What I'm Looking For", "Bullet the Blue Sky"],
:"Zooropa" =>
["Numb"]
}
},
:"Talking Heads" => {
:albums => {
:"Fear of Music" =>
["Life During Wartime", "Heaven"],
:"Speaking in Tongues" =>
["This Must Be the Place (Naive Melody)", "Burning Down the House"]
}
},
:"Huey Lewis and the News" => {
:albums => {
:"Sports" =>
["I Want a New Drug", "If This is It", "Heart of Rock and Roll"]
}
}
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment