Class: Top100::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
9
# File 'lib/top_100/cli.rb', line 5

def initialize
  @tracker = 0
  @scraper = Top100::BillboardScraper.new
  @current_hits = self.scraper.scrape_from_chart_page
end

Instance Attribute Details

#current_hitsObject

Returns the value of attribute current_hits.



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

def current_hits
  @current_hits
end

#scraperObject

Returns the value of attribute scraper.



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

def scraper
  @scraper
end

#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



12
13
14
15
16
# File 'lib/top_100/cli.rb', line 12

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

#display_artist(rank) ⇒ Object



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

def display_artist(rank)
  artist = Top100::Artist.create_artist(rank)
  if artist == nil
    puts "Invalid input"
  else
    artist.display_details
  end
  puts "--------------------------------"
end

#display_chartObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/top_100/cli.rb', line 18

def display_chart
  if self.tracker >= 100
    puts "There are no more songs to display."
  else
    20.times do
      hit = self.current_hits[self.tracker]
      puts "##{hit[:current_rank]}: #{hit[:song_name]} by #{hit[:song_artist]}. Previously number #{hit[:last_week_rank]} last week."
      puts "--------------------------------"
      self.tracker += 1
    end
  end
end

#present_optionsObject



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

def present_options
  puts "Options: 1. type in 'exit' to exit. 2. type in 'next' for the next twenty songs. 3. type in the number of a song for an artist you would like to learn more about."
  choice = gets.chomp
  if choice == 'exit'
    puts "Now exiting..."
  elsif choice == 'next'
    display_chart
    present_options
  elsif choice.match(/\d+/)
    display_artist(choice.match(/\d+/)[0])
    present_options
  else
    puts "Unknown command. Try again."
    present_options
  end
end