Class: MusicExplorer::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/music_explorer/cli.rb

Instance Method Summary collapse

Instance Method Details

#artist_options(artist) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/music_explorer/cli.rb', line 48

def artist_options(artist)
  while true
    puts
    puts "Please select an option (1-4)"
    puts "1. Display #{artist.name}'s top tracks"
    puts "2. Display #{artist.name}'s genres"
    puts "3. Display albums by #{artist.name}"
    puts "4. Display artists related to #{artist.name}"
    puts "5. Go back to main menu"
    puts
    case get_numeric_input
      when 1
        display_top_tracks(artist)
      when 2
        display_genres(artist)
      when 3
        display_albums(artist)
      when 4
        display_related_artists(artist)
      when 5
        program_options
    end
  end
end

#callObject



4
5
6
7
8
# File 'lib/music_explorer/cli.rb', line 4

def call
  #Welcome message, initially called by the executable program
  puts "Welcome to the Music Explorer!"
  program_options
end

#display_albums(artist) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/music_explorer/cli.rb', line 97

def display_albums(artist)
  if artist.albums
    puts
    puts "#{artist.name}'s releases include the following albums (up to 20):"
    print_numbered_list(artist.albums.uniq)
    puts
  else
    puts
    puts "Albums not available for #{artist.name}"
    puts
  end
end

#display_artist(artist) ⇒ Object



42
43
44
45
46
# File 'lib/music_explorer/cli.rb', line 42

def display_artist(artist)
  puts
  puts "Your search matched #{artist.name}"
  puts
end

#display_genres(artist) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/music_explorer/cli.rb', line 85

def display_genres(artist)
  if artist.genres
    puts
    puts "Spotify classifies #{artist.name} as having the following genres:"
    print_numbered_list(artist.genres)
  puts
  else
    puts
    puts "Genres not available for #{artist.name}"
  end
end


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/music_explorer/cli.rb', line 110

def display_related_artists(artist)
  if artist.related_artists
    puts
    puts "Here is a list of artists related to #{artist.name}:"
    print_numbered_list(artist.related_artists)
    puts "Would you like to learn more about these artists? (Y/N)"
    user_choice = get_user_input.capitalize
    if user_choice[0] == "Y"
      explore_related_artists(artist.related_artists)
    end
  else
    puts
    puts "Related artists not available for #{artist.name}"
    puts
  end
end

#display_top_tracks(artist) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/music_explorer/cli.rb', line 73

def display_top_tracks(artist)
  if artist.top_tracks
    puts
    puts "#{artist.name}'s top 10 tracks on Spotify are:"
    print_numbered_list(artist.top_tracks)
  puts
  else
    puts
    puts "Top tracks not available for #{artist.name}"
  end
end

#end_programObject



163
164
165
166
# File 'lib/music_explorer/cli.rb', line 163

def end_program
  puts "Goodbye!"
  exit
end


127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/music_explorer/cli.rb', line 127

def explore_related_artists(related_artists)
  puts
  puts "Choose one of the above artists by number"
  user_input = get_numeric_input
  if user_input >=1 && user_input <= 20
    puts
    search_artists(related_artists[user_input - 1])
  else
    puts "Number out of range (1-20)"
    explore_related_artists(related_artists)
  end
end

#get_numeric_inputObject



153
154
155
# File 'lib/music_explorer/cli.rb', line 153

def get_numeric_input
  get_user_input.to_i
end

#get_user_inputObject



149
150
151
# File 'lib/music_explorer/cli.rb', line 149

def get_user_input
  gets.strip
end


157
158
159
160
161
# File 'lib/music_explorer/cli.rb', line 157

def print_numbered_list(array)
  array.each_with_index do |item, index|
    puts "#{index + 1}. #{item}"
  end
end

#program_optionsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/music_explorer/cli.rb', line 10

def program_options
  while true
    puts
    puts "Please select an option (1-3)"
    puts "1. Search for an artist"
    puts "2. View all artists you have explored"
    puts "3. Leave program"
    puts
    user_choice = get_numeric_input
    case user_choice
    when 1
      search_artists
    when 2
      view_all_artists
    when 3
      end_program
    end
  end
end

#search_artists(artist_query = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/music_explorer/cli.rb', line 30

def search_artists(artist_query = nil)
  if artist_query == nil
    puts
    puts "What artist would you like more info about?"
    puts
    artist_query = self.get_user_input
  end
  artist = MusicExplorer::Artist.create_artist(artist_query)
  display_artist(artist)
  artist_options(artist)
end

#view_all_artistsObject



140
141
142
143
144
145
146
147
# File 'lib/music_explorer/cli.rb', line 140

def view_all_artists
  puts
  puts "Artists explored in this session:"
  MusicExplorer::Artist.all.sort_by {|artist| artist.name}.each_with_index do |artist, index|
    puts "#{artist.name}"
  end
  puts
end