class ZombieBattleground::Api::Responses::GetMatchesResponse

Response validator for GetMatches

Attributes

limit[R]

@!attribute [r] limit the limit of results for the page

@return [Integer]

@example

response.limit #=> 100

@api public

matches[R]

@!attribute [r] matches the matches found for the page and limit

@return [Array<ZombieBattleground::Api::Models::Match>]

@example

response.matches #=> [ZombieBattleground::Api::Models::Match]

@api public

page[R]

@!attribute [r] page the page number of the results

@return [Integer]

@example

response.page #=> 1

@api public

remove_invalid[RW]

@!attribute [a] remove_invalid flag to remove objects in response that are invalid during validation

@return [Boolean]

@example

response.remove_invalid = true

@api public

total[R]

@!attribute [r] total the total number of results available

@return [Integer]

@example

response.total #=> 1505

@api public

Public Class Methods

new(response) click to toggle source

Creates a new GetMatchesResponse

@param response [Faraday::Response] Faraday response from endpoint

@return [ZombieBattleground::Api::GetMatchesResponse]

@example

response = ZombieBattleground::Api::GetMatchesResponse.new(faraday_response)
# => ZombieBattleground::Api::GetMatchesResponse

@api public

# File lib/zombie_battleground/api/responses/get_matches_response.rb, line 97
def initialize(response)
  handle_errors(response)

  JSON.parse(response.body).each do |key, value|
    if key == 'matches'
      instance_variable_set(
        "@#{key}".to_sym, value.map { |match| ZombieBattleground::Api::Models::Match.new(match) }
      )
    else
      instance_variable_set("@#{key}".to_sym, value)
    end
  end
end

Private Instance Methods

matches_contains_valid_matches() click to toggle source

Validator for matches in matches

@return [void]

@api private

# File lib/zombie_battleground/api/responses/get_matches_response.rb, line 138
def matches_contains_valid_matches
  @matches.each do |match|
    next if match.is_a?(ZombieBattleground::Api::Models::Match) &&
            match.valid? &&
            match.errors.size.zero?

    errors.add(:matches, 'matches must be an array of Match')
  end
end
matches_is_an_array_of_match() click to toggle source

Validator for matches attribute

@return [void]

@api private

# File lib/zombie_battleground/api/responses/get_matches_response.rb, line 119
def matches_is_an_array_of_match
  unless @matches.is_a?(Array)
    errors.add(:matches, 'Matchs must be an array')
    return
  end

  if remove_invalid
    remove_invalid_matches
  else
    matches_contains_valid_matches
  end
end
remove_invalid_matches() click to toggle source

Removes invalid vards from match

@return [void]

@api private

# File lib/zombie_battleground/api/responses/get_matches_response.rb, line 154
def remove_invalid_matches
  @matches.select! do |match|
    match.is_a?(ZombieBattleground::Api::Models::Match) &&
      match.valid? &&
      match.errors.size.zero?
  end
end