Class: CLI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



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

def initialize
  @tracker = 0
  BillboardScraper.scrape_from_chart_page
end

Instance Attribute Details

#trackerObject

Returns the value of attribute tracker.



2
3
4
# File 'lib/top_100/cli.rb', line 2

def tracker
  @tracker
end

Instance Method Details

#callObject



10
11
12
13
14
# File 'lib/top_100/cli.rb', line 10

def call
  puts "Welcome to the Billboard Hot 100. Now outputting the top twenty songs..."
  display_chart
  present_options
end

#display_artist(name) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/top_100/cli.rb', line 67

def display_artist(name)
  artist = Artist.find_or_create(name)
  if artist.name == nil
    puts "Artist not found."
  else
    song_names = artist.songs.map {|song| song.name}
    puts "Name: #{artist.name}"
    puts "From: #{artist.location}"
    puts "Formed: #{artist.date} "
    puts "Currently Trending Songs: #{song_names.join(", ")}"
    puts "Bio: #{artist.bio}"
  end
  puts "--------------------------------"
end

#display_chartObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/top_100/cli.rb', line 56

def display_chart
  if self.tracker >= 100
    puts "There are no more songs to display."
  else
    20.times do
      display_song(Song.all[self.tracker])
      self.tracker += 1
    end
  end
end

#display_song(song) ⇒ Object



82
83
84
85
# File 'lib/top_100/cli.rb', line 82

def display_song(song)
  puts "##{song.rank}: #{song.name} by #{song.artist_name}."
  puts "--------------------------------"
end

#play_song(song) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/top_100/cli.rb', line 41

def play_song(song)
  if song.nil?
    puts "You've entered an invalid chart rank."
  else
    song.url = song.spotify_link
    #check if song has a valid url, copyright issues with certain songs
    if !!song.url
      puts "Playing song..."
      `open #{song.url}`
    else
      puts "Sorry, that artist does not have their song on Spotify."
    end
  end
end

#present_optionsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/top_100/cli.rb', line 16

def present_options
  puts "Options: 1. type in 'exit' to exit. 2. type in 'next' for the next twenty songs. 3. type 'song' to play a song sample. 4. type in the full artist title of a song to learn more about the main artist."
  choice = gets.chomp
  case choice
  when 'exit'
    puts "Now exiting..."
  when 'next'
    display_chart
    present_options
  when 'song'
    puts "Enter the chart number of the song you would like to play."
    rank = gets.chomp
    play_song(Song.find_by_rank(rank))
    present_options
  else
    begin
      display_artist(choice)
      # catches issues using OpenURI to access certain artist pages or issues with making frequent requests
      rescue OpenURI::HTTPError => error
        puts "Sorry, we're having trouble displaying this artist's details."
    end
    present_options
  end
end