Created
October 11, 2013 15:37
-
-
Save TrevMcKendrick/6936895 to your computer and use it in GitHub Desktop.
This file contains 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 artist_header(artist) | |
header = "\n---------------\n" | |
header += "#{artist}:\n" | |
header += "---------------" | |
puts header | |
end | |
def list_artist(artist, album_hash) | |
artist_header(artist) | |
album_hash[:albums].each do |album_name, songs_hash| | |
artist_list = "\n#{album_name}:\n\t" | |
artist_list += songs_hash[:songs].join("\n\t") | |
puts artist_list | |
end | |
end | |
def list_library | |
lib = full_library | |
lib.each do |artist, album_hash| | |
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.downcase == artist.to_s.gsub("_"," ").downcase | |
puts list_artist(artist, lib) | |
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,value| | |
value.each do |song| | |
if song.downcase == command.downcase | |
puts "Now Playing: #{artist.to_s.strip}: #{album} - #{song}\n\n" | |
return true | |
end | |
end | |
end | |
end | |
end | |
false | |
end | |
def not_found(command) | |
puts "I did not understand '#{command}'!\n\n" | |
true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment