Class: FootballApi::MatchSummary

Inherits:
Object
  • Object
show all
Defined in:
lib/football_api/match_summary.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ MatchSummary

For the teams goals, the api returns an empty array when there are no goals. Goals is an hash if there are goals to present.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/football_api/match_summary.rb', line 10

def initialize(hash = {})
  @id = hash[:id]

  @local_team_goals = parse_team_goals(hash[:localteam])
  @local_team_yellowcards = parse_cards(hash[:localteam][:yellowcards], :yellow)
  @local_team_redcards = parse_cards(hash[:localteam][:redcards], :red)

  @visitor_team_goals = parse_team_goals(hash[:visitorteam])
  @visitor_team_yellowcards = parse_cards(hash[:visitorteam][:yellowcards], :yellow)
  @visitor_team_redcards = parse_cards(hash[:visitorteam][:redcards], :red)
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/football_api/match_summary.rb', line 4

def id
  @id
end

#local_team_goalsObject

Returns the value of attribute local_team_goals.



4
5
6
# File 'lib/football_api/match_summary.rb', line 4

def local_team_goals
  @local_team_goals
end

#local_team_redcardsObject

Returns the value of attribute local_team_redcards.



4
5
6
# File 'lib/football_api/match_summary.rb', line 4

def local_team_redcards
  @local_team_redcards
end

#local_team_yellowcardsObject

Returns the value of attribute local_team_yellowcards.



4
5
6
# File 'lib/football_api/match_summary.rb', line 4

def local_team_yellowcards
  @local_team_yellowcards
end

#visitor_team_goalsObject

Returns the value of attribute visitor_team_goals.



4
5
6
# File 'lib/football_api/match_summary.rb', line 4

def visitor_team_goals
  @visitor_team_goals
end

#visitor_team_redcardsObject

Returns the value of attribute visitor_team_redcards.



4
5
6
# File 'lib/football_api/match_summary.rb', line 4

def visitor_team_redcards
  @visitor_team_redcards
end

#visitor_team_yellowcardsObject

Returns the value of attribute visitor_team_yellowcards.



4
5
6
# File 'lib/football_api/match_summary.rb', line 4

def visitor_team_yellowcards
  @visitor_team_yellowcards
end

Instance Method Details

#parse_cards(hash, type) ⇒ Object

If the cards struct has no members, its type is an array, so we must do the type validation before sending it to new. Like with the goals, the only way to iterpolate through all the records is with the hash key.



34
35
36
37
# File 'lib/football_api/match_summary.rb', line 34

def parse_cards(hash, type)
  return [] if !hash.is_a?(Hash) || !hash[:player]
  Array(hash[:player]).map { |card| FootballApi::Card.new(card.merge(type: type)) }
end

#parse_team_goals(hash = {}) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/football_api/match_summary.rb', line 22

def parse_team_goals(hash = {})
  return [] if !hash[:goals] || hash[:goals].is_a?(Array) || !hash[:goals][:player]

  Array(hash[:goals][:player]).map do |player|
    FootballApi::Goal.new(player.merge(score: player.to_s.to_i))
  end
end