Class: GamedayCliGem::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/gameday_cli_gem/game.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(team1 = nil, team2 = nil, league = nil, start_time = nil, news_url = nil) ⇒ Game

Returns a new instance of Game.



7
8
9
10
11
12
13
14
# File 'lib/gameday_cli_gem/game.rb', line 7

def initialize(team1 = nil, team2 = nil, league = nil, start_time = nil, news_url = nil)
  @team1 = team1
  @team2 = team2
  @league = league
  @start_time = start_time
  @news_url = news_url
  self.class.all << self unless self.class.all.include?(self)
end

Instance Attribute Details

#headlineObject

headline selector from doc



81
82
83
# File 'lib/gameday_cli_gem/game.rb', line 81

def headline
  @headline
end

#leagueObject

Returns the value of attribute league.



3
4
5
# File 'lib/gameday_cli_gem/game.rb', line 3

def league
  @league
end

#news_urlObject

Returns the value of attribute news_url.



3
4
5
# File 'lib/gameday_cli_gem/game.rb', line 3

def news_url
  @news_url
end

#start_timeObject

Returns the value of attribute start_time.



3
4
5
# File 'lib/gameday_cli_gem/game.rb', line 3

def start_time
  @start_time
end

#team1Object

Returns the value of attribute team1.



3
4
5
# File 'lib/gameday_cli_gem/game.rb', line 3

def team1
  @team1
end

#team2Object

Returns the value of attribute team2.



3
4
5
# File 'lib/gameday_cli_gem/game.rb', line 3

def team2
  @team2
end

Class Method Details

.allObject



57
58
59
# File 'lib/gameday_cli_gem/game.rb', line 57

def self.all
  @@all
end

.find(index) ⇒ Object



61
62
63
# File 'lib/gameday_cli_gem/game.rb', line 61

def self.find(index)
    self.all[index.to_i-1]
end

.new_from_index_page(game) ⇒ Object

Instantiates game objects from XML code passed by Scraper class. Conditionals determine initialize params



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gameday_cli_gem/game.rb', line 16

def self.new_from_index_page(game) # Instantiates game objects from XML code passed by Scraper class. Conditionals determine initialize params
if game.css(".game-link").text == "Preview"  #selects for upcoming games with previews
  self.new(
    game.css("div.media-body")[0].css("span").text,
    game.css("div.media-body")[1].css("span").text,
    game.attribute("data-league").text.upcase,
    game.css(".status-pregame").text.gsub(" ", "").gsub(/\n/, ""),
     "http://www.si.com#{game.css(".game-link").attribute("href").text}"
    )
elsif game.css(".game-link").text.include?("Recap")  #selects for games with recap, which must be finished
  self.new(
    game.css("div.media-body")[0].css("span").text,
    game.css("div.media-body")[1].css("span").text,
    game.attribute("data-league").text.upcase,
    "FIN",
    "http://www.si.com#{game.css(".game-link").attribute("href").text}"
    )
elsif game.at_css(".status-pregame")  #selects for games with a visible start time
   self.new(
     game.css("div.media-body")[0].css("span").text,
     game.css("div.media-body")[1].css("span").text,
     game.attribute("data-league").text.upcase,
     game.css(".status-pregame").text.gsub(" ", "").gsub(/\n/, "")
     )
elsif game.at_css(".status-active")  #selects for games which are active
    self.new(
      game.css("div.media-body")[0].css("span").text,
      game.css("div.media-body")[1].css("span").text,
      game.attribute("data-league").text.upcase,
      game.css(".status-active").text.gsub(/\s+/, "").split("|").join("| ")
      )
 elsif game.at_css(".status-final") #selects for any other completed games with no recap/preview or start time
    self.new(
      game.css("div.media-body")[0].css("span").text,
      game.css("div.media-body")[1].css("span").text,
      game.attribute("data-league").text.upcase,
      "FIN"
      )
    end
end

Instance Method Details

#docObject

document of the recap_url for games with an available recap



85
86
87
# File 'lib/gameday_cli_gem/game.rb', line 85

def doc  #document of the recap_url for games with an available recap
  Nokogiri::HTML(open(self.news_url))
end

#final_scoreObject

puts the final score of a completed game



72
73
74
75
76
77
78
79
# File 'lib/gameday_cli_gem/game.rb', line 72

def final_score  #puts the final score of a completed game
  if doc.at_css(".team-score")
    puts "Final Score >> #{doc.css(".team-name")[0].text}: #{doc.css(".team-score")[0].text.strip} || #{doc.css(".team-name")[1].text}: #{doc.css(".team-score")[1].text.strip}"
  else
    puts "Here's a quick preview!"
    puts ""
  end
end

#recapObject

puts a truncated recap article to terminal



65
66
67
68
69
70
# File 'lib/gameday_cli_gem/game.rb', line 65

def recap  #puts a truncated recap article to terminal
  puts doc.css(".article p")[0].text
  puts doc.css(".article p")[1].text
  puts doc.css(".article p")[2].text
  puts doc.css(".article p")[3].text
end