Skip to content

Commit

Permalink
Cleaned up Cards extensions after having tried to use it
Browse files Browse the repository at this point in the history
Bump zombie_battleground-api to 0.5.0

Update default docs link

Refreshed after version bump
  • Loading branch information
John Shields committed Feb 16, 2019
1 parent 6f935ed commit 363f38e
Show file tree
Hide file tree
Showing 16 changed files with 304 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
zombie_battleground-api (0.4.1)
zombie_battleground-api (0.5.0)
activerecord
faraday
json
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Or install it yourself as:

## Usage

See the [API documentation](https://www.rubydoc.info/gems/zombie_battleground-api/0.4.1).
See the [API documentation](https://www.rubydoc.info/gems/zombie_battleground-api/0.5.0).
Every API call returns a response object that contains a complete modeled Ruby object of the response.

Use the singleton class `ZombieBattleground::Api`. The singleton has additional extentions, the client implements the public API as is.
Expand Down
14 changes: 13 additions & 1 deletion lib/zombie_battleground/api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ def initialize
# @param hero_id [Integer] Optionally filter on Deck hero_id
# @param primary_skill_id [Integer] Optionally filter on Deck primary_skill_id
# @param secondary_skill_id [Integer] Optionally filter on Deck secondary_skill_id
# @param version [String] Optionally filter on Deck version
# @param version [string] optionally filter on deck version
# @param page [Integer] Used for pagination of query results
# @param limit [Integer] Used for pagination of query results. Max 100
# @param remove_invalid [Boolean] Remove invalid decks from response. Default: true
#
# @return [ZombieBattleground::Api::Responses::GetDecksResponse]
#
Expand All @@ -73,12 +74,15 @@ def initialize
#
# @api public
def decks_request(**args)
remove_invalid = args[:remove_invalid].nil? ? true : args.delete(:remove_invalid)

request = ZombieBattleground::Api::Requests::GetDecksRequest.new
args.each { |key, val| request.send("#{key}=", val) }
raise ZombieBattleground::Api::Errors::InvalidInput, request.errors.messages unless request.valid?

response = connection(uri: request.uri, params: request.params).get
decks = ZombieBattleground::Api::Responses::GetDecksResponse.new(response)
decks.remove_invalid = remove_invalid
raise ZombieBattleground::Api::Errors::InvalidResponse.new(response, decks) unless decks.valid?

decks
Expand Down Expand Up @@ -121,6 +125,7 @@ def deck_request(**args)
# @param winner_id [String] Optionally filter on Match winner_id
# @param page [Integer] Used for pagination of query results
# @param limit [Integer] Used for pagination of query results. Max 100
# @param remove_invalid [Boolean] Remove invalid decks from response. Default: true
#
# @return [ZombieBattleground::Api::Responses::GetMatchesResponse]
#
Expand All @@ -132,12 +137,15 @@ def deck_request(**args)
#
# @api public
def matches_request(**args)
remove_invalid = args[:remove_invalid].nil? ? true : args.delete(:remove_invalid)

request = ZombieBattleground::Api::Requests::GetMatchesRequest.new
args.each { |key, val| request.send("#{key}=", val) }
raise ZombieBattleground::Api::Errors::InvalidInput, request.errors.messages unless request.valid?

response = connection(uri: request.uri, params: request.params).get
matches = ZombieBattleground::Api::Responses::GetMatchesResponse.new(response)
matches.remove_invalid = remove_invalid
raise ZombieBattleground::Api::Errors::InvalidResponse.new(response, matches) unless matches.valid?

matches
Expand Down Expand Up @@ -186,6 +194,7 @@ def match_request(**args)
# @param cost [Integer] Optionally filter on Card cost
# @param page [Integer] Used for pagination of query results
# @param limit [Integer] Used for pagination of query results. Max 100
# @param remove_invalid [Boolean] Remove invalid decks from response. Default: true
#
# @return [ZombieBattleground::Api::Responses::GetCardsResponse]
#
Expand All @@ -197,12 +206,15 @@ def match_request(**args)
#
# @api public
def cards_request(**args)
remove_invalid = args[:remove_invalid].nil? ? true : args.delete(:remove_invalid)

request = ZombieBattleground::Api::Requests::GetCardsRequest.new
args.each { |key, val| request.send("#{key}=", val) }
raise ZombieBattleground::Api::Errors::InvalidInput, request.errors.messages unless request.valid?

response = connection(uri: request.uri, params: request.params).get
cards = ZombieBattleground::Api::Responses::GetCardsResponse.new(response)
cards.remove_invalid = remove_invalid
raise ZombieBattleground::Api::Errors::InvalidResponse.new(response, cards) unless cards.valid?

cards
Expand Down
4 changes: 4 additions & 0 deletions lib/zombie_battleground/api/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ class Api
##
# Max items per page
PAGE_MAX = 100

##
# Required amount of cards in deck
DECK_REQUIRED_CARDS_COUNT = 30
end
end
76 changes: 60 additions & 16 deletions lib/zombie_battleground/api/extensions/cards.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'yaml'

require 'zombie_battleground/api/constants'

module ZombieBattleground
Expand Down Expand Up @@ -34,7 +36,7 @@ module Cards
# @api public
def all_cards(**args)
args.delete(:limit) # query as many as possible
return enum_for(:all_cards) unless block_given?
return enum_for(:all_cards, args) unless block_given?

page = 1

Expand All @@ -60,7 +62,7 @@ def all_cards(**args)
#
# @api public
def card_kinds
all_cards.map(&:kind).uniq
load_cards_data['kinds']
end

##
Expand All @@ -74,7 +76,7 @@ def card_kinds
#
# @api public
def card_ranks
all_cards.map(&:rank).uniq
load_cards_data['ranks']
end

##
Expand All @@ -88,7 +90,21 @@ def card_ranks
#
# @api public
def card_sets
all_cards.map(&:set).uniq
load_cards_data['sets']
end

##
# Fetches all card factions
#
# @return [Array<String>]
#
# @example
# factions = ZombieBattleground::Api.card_factions
# # => [String]
#
# @api public
def card_factions
load_cards_data['factions']
end

##
Expand All @@ -102,7 +118,7 @@ def card_sets
#
# @api public
def card_types
all_cards.map(&:type).uniq
load_cards_data['types']
end

##
Expand All @@ -116,7 +132,7 @@ def card_types
#
# @api public
def card_rarities
all_cards.map(&:rarity).uniq
load_cards_data['rarities']
end

##
Expand All @@ -127,11 +143,11 @@ def card_rarities
# @return [Array<ZombieBattleground::Api::Models::Card>]
#
# @example
# cards = ZombieBattleground::Api.cards_by_kind(kind: "SPELL")
# cards = ZombieBattleground::Api.cards_by_kind("SPELL")
# # => [ZombieBattleground::Api::Models::Card]
#
# @api public
def cards_by_kind(kind:)
def cards_by_kind(kind)
all_cards(kind: kind).to_a
end

Expand All @@ -143,11 +159,11 @@ def cards_by_kind(kind:)
# @return [Array<ZombieBattleground::Api::Models::Card>]
#
# @example
# cards = ZombieBattleground::Api.cards_by_rank(rank: "COMMANDER")
# cards = ZombieBattleground::Api.cards_by_rank("COMMANDER")
# # => [ZombieBattleground::Api::Models::Card]
#
# @api public
def cards_by_rank(rank:)
def cards_by_rank(rank)
all_cards(rank: rank).to_a
end

Expand All @@ -159,14 +175,30 @@ def cards_by_rank(rank:)
# @return [Array<ZombieBattleground::Api::Models::Card>]
#
# @example
# cards = ZombieBattleground::Api.cards_by_set(set: "WATER")
# cards = ZombieBattleground::Api.cards_by_set("WATER")
# # => [ZombieBattleground::Api::Models::Card]
#
# @api public
def cards_by_set(set:)
def cards_by_set(set)
all_cards(set: set).to_a
end

##
# Fetches all cards with selected faction
#
# @param faction [String]
#
# @return [Array<ZombieBattleground::Api::Models::Card>]
#
# @example
# cards = ZombieBattleground::Api.cards_by_faction("WATER")
# # => [ZombieBattleground::Api::Models::Card]
#
# @api public
def cards_by_faction(faction)
all_cards(set: faction).to_a
end

##
# Fetches all cards with selected type
#
Expand All @@ -175,11 +207,11 @@ def cards_by_set(set:)
# @return [Array<ZombieBattleground::Api::Models::Card>]
#
# @example
# cards = ZombieBattleground::Api.cards_by_type(type: "WALKER")
# cards = ZombieBattleground::Api.cards_by_type("WALKER")
# # => [ZombieBattleground::Api::Models::Card]
#
# @api public
def cards_by_type(type:)
def cards_by_type(type)
all_cards(type: type).to_a
end

Expand All @@ -191,13 +223,25 @@ def cards_by_type(type:)
# @return [Array<ZombieBattleground::Api::Models::Card>]
#
# @example
# cards = ZombieBattleground::Api.cards_by_rarity(rarity: "")
# cards = ZombieBattleground::Api.cards_by_rarity("")
# # => [ZombieBattleground::Api::Models::Card]
#
# @api public
def cards_by_rarity(rarity:)
def cards_by_rarity(rarity)
all_cards(rarity: rarity).to_a
end

private

##
# Loads the helper data for cards
#
# @return [Hash]
#
# @api private
def load_cards_data
@load_cards_data ||= YAML.safe_load(File.read(File.join(__dir__, 'cards.yml')))
end
end
end
end
Expand Down
49 changes: 49 additions & 0 deletions lib/zombie_battleground/api/extensions/cards.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---

# API doesn't provide an endpoint for enumerating card metadata

# https://zombiebattleground.io/index.php/Type
types:
- 'WALKER' # standard
- 'FERAL' # haste
- 'HEAVY' # taunt

# Didn't find docs for these yet, duplicating what API is serving now
kinds:
- 'CREATURE'
- 'SPELL'

# https://zombiebattleground.io/index.php/Rank
# implies rarity for creature cards
ranks:
- 'MINION'
- 'OFFICER'
- 'GENERAL'
- 'COMMANDER'

# Didn't find docs for these yet, duplicating what API is serving now
sets:
- 'AIR'
- 'EARTH'
- 'FIRE'
- 'LIFE'
- 'TOXIC'
- 'WATER'
- 'ITEM'
- 'OTHERS'

# https://zombiebattleground.io/index.php/Faction
# factions are a subset of sets
factions:
- 'AIR'
- 'EARTH'
- 'FIRE'
- 'LIFE'
- 'TOXIC'
- 'WATER'

# https://zombiebattleground.io/index.php/File:Card_description.png
# https://zombiebattleground.io/index.php/Rarity
# This one is strange, API returns empty strings right now
rarities:
- ''
17 changes: 17 additions & 0 deletions lib/zombie_battleground/api/models/deck.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'active_record'

require 'zombie_battleground/api/constants'
require 'zombie_battleground/api/validation_helper'
require 'zombie_battleground/api/models/simple_card'

Expand Down Expand Up @@ -245,7 +246,23 @@ def cards_is_an_array_of_simple_card
card.errors.size.zero?

errors.add(:cards, 'cards must be an array of SimpleCard')
break
end

cards_has_required_count
end

##
# Validator for correct number of cards in deck
#
# @return [void]
#
# @api private
def cards_has_required_count
return if errors.messages.size.zero? &&
cards.sum(&:amount) == DECK_REQUIRED_CARDS_COUNT

errors.add(:cards, 'cards must add up to 30')
end
end
end
Expand Down

0 comments on commit 363f38e

Please sign in to comment.