Created
October 13, 2013 17:32
-
-
Save rosiehoyem/6964949 to your computer and use it in GitHub Desktop.
Debug TODO, Day 12
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_hash| | |
artist_list += "\n#{album_name}:\n\t" | |
artist_list += songs_hash[:songs].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.downcase.to_sym | |
parsed = false | |
if lib.has_key?(cmd) | |
puts list_artist(command, lib[cmd]) | |
parsed = true | |
else | |
lib.each do |artist, album_hash| #added album to _hash | |
if command.downcase == artist.to_s.gsub("_"," ").downcase | |
puts list_artist(artist, album_hash) #added album to _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, songs_hash| | |
songs_hash.each do |songs, song_array| | |
song_array.each do |song| | |
if song.downcase == command.downcase | |
puts "Now Playing: #{artist}: #{album} - #{song}\n\n" | |
return true | |
end | |
end | |
end | |
end | |
end | |
end | |
false | |
end | |
def not_found(command) | |
puts "I did not understand '#{command}'!\n\n" | |
true | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative './jukebox.rb' | |
def run | |
puts "Welcome to Ruby Console Jukebox!" | |
command = "" | |
while command.downcase != "exit" do | |
puts "Enter a command to continue. Type 'help' for a list of commands." | |
command = get_command | |
run_command(command) unless command.downcase == "exit" | |
end | |
end | |
def get_command | |
gets.strip | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def full_library | |
{ | |
:"U2" => { | |
:albums => { | |
:"The Joshua Tree" => { | |
:songs => ["With or Without You", "Still Haven't Found What I'm Looking For", "Bullet the Blue Sky"] | |
}, | |
:"Zooropa" => { | |
:songs => ["Numb"] | |
} | |
} | |
}, | |
:"Talking Heads" => { | |
:albums => { | |
:"Fear of Music" => { | |
:songs => ["Life During Wartime", "Heaven"] | |
}, | |
:"Speaking in Tongues" => { | |
:songs => ["This Must Be the Place (Naive Melody)", "Burning Down the House"] | |
} | |
} | |
}, | |
:"Huey Lewis and the News" => { | |
:albums => { | |
:"Sports" => { | |
:songs => ["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