Class: YankeeScore::Game

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

Constant Summary collapse

PRE_GAME_STATE =
["Pre-Game","Preview", "Warmup"]
END_GAME_STATE =
["Final", "Postponed", "Game Over"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(home_team, away_team) ⇒ Game

Returns a new instance of Game.



13
14
15
16
# File 'lib/yankee_score/game.rb', line 13

def initialize(home_team, away_team)
  @home_team = home_team
  @away_team = away_team
end

Instance Attribute Details

#away_teamObject

Returns the value of attribute away_team.



2
3
4
# File 'lib/yankee_score/game.rb', line 2

def away_team
  @away_team
end

#home_teamObject

Returns the value of attribute home_team.



2
3
4
# File 'lib/yankee_score/game.rb', line 2

def home_team
  @home_team
end

#inningObject

Returns the value of attribute inning.



2
3
4
# File 'lib/yankee_score/game.rb', line 2

def inning
  @inning
end

#inning_stateObject

Returns the value of attribute inning_state.



2
3
4
# File 'lib/yankee_score/game.rb', line 2

def inning_state
  @inning_state
end

#scoreObject

Returns the value of attribute score.



2
3
4
# File 'lib/yankee_score/game.rb', line 2

def score
  @score
end

#start_timeObject

Returns the value of attribute start_time.



2
3
4
# File 'lib/yankee_score/game.rb', line 2

def start_time
  @start_time
end

#statusObject

Returns the value of attribute status.



2
3
4
# File 'lib/yankee_score/game.rb', line 2

def status
  @status
end

Class Method Details

.allObject



20
21
22
# File 'lib/yankee_score/game.rb', line 20

def self.all
  @@all
end

.create_from_json(game_hash) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/yankee_score/game.rb', line 32

def self.create_from_json(game_hash)
    game = self.new(YankeeScore::Team.new(game_hash[:home_name_abbrev]), YankeeScore::Team.new(game_hash[:away_name_abbrev]))

    game.start_time = game_hash[:time]
    game.status = game_hash[:status][:status]
    game.inning = game_hash[:status][:inning]
    game.inning_state = game_hash[:status][:inning_state]


    if game_hash.has_key?(:linescore)
      game.home_team.runs = game_hash[:linescore][:r][:home]
      game.away_team.runs = game_hash[:linescore][:r][:away]
      game.score = "#{game.away_team.runs}-#{game.home_team.runs}"
    end

    game.save
end

.find_team_by_abbrev(team_abbrev) ⇒ Object



51
52
53
54
55
# File 'lib/yankee_score/game.rb', line 51

def self.find_team_by_abbrev(team_abbrev)
  self.all.select do |team|
    team_abbrev == team.home_team.name || team_abbrev == team.away_team.name
  end
end

.reset_all!Object



24
25
26
# File 'lib/yankee_score/game.rb', line 24

def self.reset_all!
  @@all.clear
end

Instance Method Details

#is_active?Boolean

Returns:

  • (Boolean)


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

def is_active?
  self.inning.to_i >= 1 && !is_over? && !PRE_GAME_STATE.include?(status)
end

#is_over?Boolean

Returns:

  • (Boolean)


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

def is_over?
  END_GAME_STATE.include?(self.status)
end

#saveObject



28
29
30
# File 'lib/yankee_score/game.rb', line 28

def save
  @@all << self
end