class ZombieBattleground::Api::Responses::GetCardsResponse

Response validator for GetCards

Attributes

cards[R]

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

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

@example

response.deck #=> [ZombieBattleground::Api::Models::Card]

@api public

limit[R]

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

@return [Integer]

@example

response.limit #=> 100

@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 GetCardsResponse

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

@return [ZombieBattleground::Api::GetCardsResponse]

@example

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

@api public

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

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

Private Instance Methods

cards_contains_valid_cards() click to toggle source

Validator for cards in cards

@return [void]

@api private

# File lib/zombie_battleground/api/responses/get_cards_response.rb, line 138
def cards_contains_valid_cards
  @cards.each do |card|
    next if card.is_a?(ZombieBattleground::Api::Models::Card) &&
            card.valid? &&
            card.errors.size.zero?

    errors.add(:cards, 'cards must be an array of Card')
  end
end
cards_is_an_array_of_card() click to toggle source

Validator for cards attribute

@return [void]

@api private

# File lib/zombie_battleground/api/responses/get_cards_response.rb, line 119
def cards_is_an_array_of_card
  unless @cards.is_a?(Array)
    errors.add(:cards, 'Cards must be an array')
    return
  end

  if remove_invalid
    remove_invalid_cards
  else
    cards_contains_valid_cards
  end
end
remove_invalid_cards() click to toggle source

Removes invalid vards from card

@return [void]

@api private

# File lib/zombie_battleground/api/responses/get_cards_response.rb, line 154
def remove_invalid_cards
  @cards.select! do |card|
    card.is_a?(ZombieBattleground::Api::Models::Card) &&
      card.valid? &&
      card.errors.size.zero?
  end
end