Class: ESPNScraper::CLI

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

Overview

CLI Controller

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#articleObject

Returns the value of attribute article.



3
4
5
# File 'lib/ESPNScraper/cli.rb', line 3

def article
  @article
end

#sportObject

Returns the value of attribute sport.



3
4
5
# File 'lib/ESPNScraper/cli.rb', line 3

def sport
  @sport
end

#teamObject

Returns the value of attribute team.



3
4
5
# File 'lib/ESPNScraper/cli.rb', line 3

def team
  @team
end

#urlObject

Returns the value of attribute url.



3
4
5
# File 'lib/ESPNScraper/cli.rb', line 3

def url
  @url
end

Instance Method Details

#article_menuObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ESPNScraper/cli.rb', line 71

def article_menu
  options = ["1", "2", "3", "4", "5",
  "Select New Sport", "Select New Team", "Exit"]
  input = @prompt.select("Which article would you like to read?", options)
  if input.to_i.between?(1, 5)
    article_index = input.to_i - 1
    @article = Article.all[article_index]
    show_article_content
    exit_menu
  elsif input == "Select New Sport"
    Article.destroy_all
    run
  elsif input == "Select New Team"
    Article.destroy_all
    list_teams
    menu
  elsif input == "Exit"
    puts "Ending the program. See you again soon!".colorize(:yellow)
    exit
  end
end

#display_articlesObject



62
63
64
65
66
67
68
69
# File 'lib/ESPNScraper/cli.rb', line 62

def display_articles
  system "clear"
  Article.all.slice(0,5).each_with_index do |article, index|
    puts "#{index + 1}. #{article.title}".colorize(:light_blue).bold
    puts "  #{article.description} \n \n"
  end
  article_menu
end

#exit_menuObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ESPNScraper/cli.rb', line 104

def exit_menu
  options = ["Return to articles", "Select a new sport", "Select a new team in this league", "Exit"]
  input = @prompt.select("What would you like to do now?", options)
  if input == "Return to articles"
    display_articles
    article_menu
  elsif input == "Select a new sport"
    Article.destroy_all
    run
  elsif input == "Select a new team in this league"
    list_teams
  elsif input == "Exit"
    puts "Ending the program. See you again soon!".colorize(:yellow)
    exit
  end
end

#list_teamsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ESPNScraper/cli.rb', line 24

def list_teams
  system "clear"
  case self.sport
  when "mlb"
    all_teams = Teams.mlb_teams
  when "nba"
    all_teams = Teams.nba_teams
  when "nfl"
    all_teams = Teams.nfl_teams
  when "nhl"
    all_teams = Teams.nhl_teams
  end
  all_teams.each do |division, teams|
    puts division.to_s.sub(/_/, ' ').upcase.colorize(:magenta).bold
    teams.each do |team|
      team.each{|key,value| puts "  #{key.to_s.upcase} - #{value}"}
    end
  end
  menu
end


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ESPNScraper/cli.rb', line 45

def menu
  options = Teams.team_abbreviations(self.sport).collect{|team| team.upcase} + ["Select New Sport", "Exit"]
  input = @prompt.select("Select the team you'd like to read about.", options, filter: true).downcase
  if Teams.team_abbreviations(self.sport).include?(input)
    @team = input
    build_url
    self.sport == "nhl" ? Scraper.scrape_new_nhl_articles(self.url) : Scraper.scrape_new_articles(self.url)
    display_articles
  elsif input == "select new sport"
    Article.destroy_all
    run
  elsif input ==  "exit"
    puts "Ending the program. See you again soon!".colorize(:yellow)
    exit
  end
end

#runObject



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

def run
  @prompt = TTY::Prompt.new
  welcome
end

#show_article_contentObject



93
94
95
96
97
98
99
100
101
102
# File 'lib/ESPNScraper/cli.rb', line 93

def show_article_content
  system "clear"
  Scraper.get_content(self.article)
  puts "#{self.article.title} \n \n".colorize(:light_blue).bold
  if self.article.content.size > 0
    self.article.content.each {|p| puts "#{p.text} \n \n"}
  else
    puts "This is a link to an ESPN Radio report. To listen to the report vist: #{self.article.url}".colorize(:red)
  end
end

#welcomeObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ESPNScraper/cli.rb', line 10

def welcome
  system "clear"
  puts "Welcome to the ESPN Scraper, your source for all the latest news for the MLB, NFL, NHL, and NBA!".colorize(:blue)
  options = ["MLB", "NBA", "NFL", "NHL", "Exit"]
  input = @prompt.select("Select the sport you'd like to read about...", options).downcase
  if input == "exit"
    puts "Ending the program. See you again soon!".colorize(:yellow)
    exit
  else
    @sport = input
    list_teams
  end
end