class FootballApi::MatchSummary

Attributes

id[RW]
local_team_goals[RW]
local_team_redcards[RW]
local_team_yellowcards[RW]
visitor_team_goals[RW]
visitor_team_redcards[RW]
visitor_team_yellowcards[RW]

Public Class Methods

new(hash = {}) click to toggle source

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.

# 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

Public Instance Methods

parse_cards(hash, type) click to toggle source

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.

# 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 = {}) click to toggle source
# 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