zombie_battleground-api 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -0
  3. data/Gemfile.lock +29 -11
  4. data/README.md +4 -0
  5. data/Rakefile +62 -5
  6. data/lib/zombie_battleground/api.rb +108 -8
  7. data/lib/zombie_battleground/api/client.rb +157 -23
  8. data/lib/zombie_battleground/api/errors.rb +28 -0
  9. data/lib/zombie_battleground/api/models/card.rb +272 -32
  10. data/lib/zombie_battleground/api/models/deck.rb +234 -46
  11. data/lib/zombie_battleground/api/models/match.rb +211 -33
  12. data/lib/zombie_battleground/api/models/simple_card.rb +47 -11
  13. data/lib/zombie_battleground/api/requests/get_card_request.rb +45 -10
  14. data/lib/zombie_battleground/api/requests/get_cards_request.rb +203 -27
  15. data/lib/zombie_battleground/api/requests/get_deck_request.rb +43 -13
  16. data/lib/zombie_battleground/api/requests/get_decks_request.rb +141 -23
  17. data/lib/zombie_battleground/api/requests/get_match_request.rb +43 -13
  18. data/lib/zombie_battleground/api/requests/get_matches_request.rb +125 -21
  19. data/lib/zombie_battleground/api/requests/request_helper.rb +24 -10
  20. data/lib/zombie_battleground/api/responses/get_card_response.rb +50 -16
  21. data/lib/zombie_battleground/api/responses/get_cards_response.rb +103 -33
  22. data/lib/zombie_battleground/api/responses/get_deck_response.rb +49 -17
  23. data/lib/zombie_battleground/api/responses/get_decks_response.rb +103 -33
  24. data/lib/zombie_battleground/api/responses/get_match_response.rb +49 -17
  25. data/lib/zombie_battleground/api/responses/get_matches_response.rb +103 -33
  26. data/lib/zombie_battleground/api/responses/response_helper.rb +32 -16
  27. data/lib/zombie_battleground/api/validation_helper.rb +497 -0
  28. data/lib/zombie_battleground/api/version.rb +5 -1
  29. data/zombie_battleground-api.gemspec +6 -0
  30. metadata +71 -1
@@ -2,18 +2,32 @@
2
2
 
3
3
  module ZombieBattleground
4
4
  class Api
5
- ##
6
- # Provides generic methods for request validator classes
7
- module RequestHelper
8
- BLACKLISTED_INSTANCE_VARIABLES = %w[errors validation_context].freeze
5
+ class Requests
6
+ ##
7
+ # Provides generic methods for request validator classes
8
+ module RequestHelper
9
+ ##
10
+ # Instance variables to skip when computing the model's query paramaters
11
+ BLACKLISTED_INSTANCE_VARIABLES = %w[errors validation_context].freeze
9
12
 
10
- def params
11
- instance_variables.map do |var|
12
- normalized_var = var.to_s.gsub(/^@/, '')
13
- next if BLACKLISTED_INSTANCE_VARIABLES.include?(normalized_var)
13
+ ##
14
+ # Computes the model's query paramaters
15
+ #
16
+ # @return [Hash]
17
+ #
18
+ # @example
19
+ # query_params = model.params
20
+ # query_params # => Hash
21
+ #
22
+ # @api public
23
+ def params
24
+ instance_variables.map do |var|
25
+ normalized_var = var.to_s.gsub(/^@/, '')
26
+ next if BLACKLISTED_INSTANCE_VARIABLES.include?(normalized_var)
14
27
 
15
- [normalized_var, instance_variable_get(var)]
16
- end.compact.to_h
28
+ [normalized_var, instance_variable_get(var)]
29
+ end.compact.to_h
30
+ end
17
31
  end
18
32
  end
19
33
  end
@@ -10,28 +10,62 @@ require 'zombie_battleground/api/responses/response_helper'
10
10
  module ZombieBattleground
11
11
  class Api
12
12
  ##
13
- # Response validator for GetCard
14
- class GetCardResponse
15
- include ActiveModel::Validations
16
- include ZombieBattleground::Api::ValidationHelper
17
- include ZombieBattleground::Api::ResponseHelper
13
+ # Namespace for Responses
14
+ class Responses
15
+ ##
16
+ # Response validator for GetCard
17
+ class GetCardResponse
18
+ include ActiveModel::Validations
19
+ include ZombieBattleground::Api::ValidationHelper
20
+ include ZombieBattleground::Api::Responses::ResponseHelper
18
21
 
19
- attr_reader :card
22
+ ##
23
+ # @!attribute [r] card
24
+ # the card
25
+ #
26
+ # @return [ZombieBattleground::Api::Deck]
27
+ #
28
+ # @example
29
+ # response.card #=> ZombieBattleground::Api::Deck
30
+ #
31
+ # @api public
32
+ attr_reader :card
20
33
 
21
- validate :card_is_a_card
34
+ validate :card_is_a_card
22
35
 
23
- def initialize(response)
24
- handle_errors(response)
36
+ ##
37
+ # Creates a new GetCardResponse
38
+ #
39
+ # @param response [Faraday::Response] Faraday response from endpoint
40
+ #
41
+ # @return [ZombieBattleground::Api::GetCardResponse]
42
+ #
43
+ # @example
44
+ # response = ZombieBattleground::Api::GetCardResponse.new(faraday_response)
45
+ # # => ZombieBattleground::Api::GetCardResponse
46
+ #
47
+ # @api public
48
+ def initialize(response)
49
+ handle_errors(response)
25
50
 
26
- @card = ZombieBattleground::Api::Card.new(JSON.parse(response.body))
27
- end
51
+ @card = ZombieBattleground::Api::Models::Card.new(JSON.parse(response.body))
52
+ end
53
+
54
+ private
28
55
 
29
- def card_is_a_card
30
- return if @card.is_a?(ZombieBattleground::Api::Card) &&
31
- @card.valid? &&
32
- @card.errors.size.zero?
56
+ ##
57
+ # Validator for card attribute
58
+ #
59
+ # @return [void]
60
+ #
61
+ # @api private
62
+ def card_is_a_card
63
+ return if @card.is_a?(ZombieBattleground::Api::Models::Card) &&
64
+ @card.valid? &&
65
+ @card.errors.size.zero?
33
66
 
34
- errors.add(:card, 'card must be a Card')
67
+ errors.add(:card, 'card must be a Card')
68
+ end
35
69
  end
36
70
  end
37
71
  end
@@ -9,44 +9,114 @@ require 'zombie_battleground/api/responses/response_helper'
9
9
 
10
10
  module ZombieBattleground
11
11
  class Api
12
- ##
13
- # Response validator for GetCards
14
- class GetCardsResponse
15
- include ActiveModel::Validations
16
- include ZombieBattleground::Api::ValidationHelper
17
- include ZombieBattleground::Api::ResponseHelper
18
-
19
- attr_reader :total, :page, :limit, :cards
20
-
21
- validate :total_is_a_non_negative_integer
22
- validate :page_is_a_non_negative_integer
23
- validate :limit_is_a_non_negative_integer
24
- validate :cards_is_an_array_of_card
25
-
26
- def initialize(response)
27
- handle_errors(response)
28
-
29
- JSON.parse(response.body).each do |key, value|
30
- if key == 'cards'
31
- instance_variable_set("@#{key}".to_sym, value.map { |card| ZombieBattleground::Api::Card.new(card) })
32
- else
33
- instance_variable_set("@#{key}".to_sym, value)
12
+ class Responses
13
+ ##
14
+ # Response validator for GetCards
15
+ class GetCardsResponse
16
+ include ActiveModel::Validations
17
+ include ZombieBattleground::Api::ValidationHelper
18
+ include ZombieBattleground::Api::Responses::ResponseHelper
19
+
20
+ ##
21
+ # @!attribute [r] total
22
+ # the total number of results available
23
+ #
24
+ # @return [Integer]
25
+ #
26
+ # @example
27
+ # response.total #=> 1505
28
+ #
29
+ # @api public
30
+ attr_reader :total
31
+
32
+ ##
33
+ # @!attribute [r] page
34
+ # the page number of the results
35
+ #
36
+ # @return [Integer]
37
+ #
38
+ # @example
39
+ # response.page #=> 1
40
+ #
41
+ # @api public
42
+ attr_reader :page
43
+
44
+ ##
45
+ # @!attribute [r] limit
46
+ # the limit of results for the page
47
+ #
48
+ # @return [Integer]
49
+ #
50
+ # @example
51
+ # response.limit #=> 100
52
+ #
53
+ # @api public
54
+ attr_reader :limit
55
+
56
+ ##
57
+ # @!attribute [r] cards
58
+ # the deck found for the page and limit
59
+ #
60
+ # @return [Array<ZombieBattleground::Api::Models::Card>]
61
+ #
62
+ # @example
63
+ # response.deck #=> [ZombieBattleground::Api::Models::Card]
64
+ #
65
+ # @api public
66
+ attr_reader :cards
67
+
68
+ validate :total_is_a_non_negative_integer
69
+ validate :page_is_a_non_negative_integer
70
+ validate :limit_is_a_non_negative_integer
71
+ validate :cards_is_an_array_of_card
72
+
73
+ ##
74
+ # Creates a new GetCardsResponse
75
+ #
76
+ # @param response [Faraday::Response] Faraday response from endpoint
77
+ #
78
+ # @return [ZombieBattleground::Api::GetCardsResponse]
79
+ #
80
+ # @example
81
+ # response = ZombieBattleground::Api::GetCardsResponse.new(faraday_response)
82
+ # # => ZombieBattleground::Api::GetCardsResponse
83
+ #
84
+ # @api public
85
+ def initialize(response)
86
+ handle_errors(response)
87
+
88
+ JSON.parse(response.body).each do |key, value|
89
+ if key == 'cards'
90
+ instance_variable_set(
91
+ "@#{key}".to_sym, value.map { |card| ZombieBattleground::Api::Models::Card.new(card) }
92
+ )
93
+ else
94
+ instance_variable_set("@#{key}".to_sym, value)
95
+ end
34
96
  end
35
97
  end
36
- end
37
98
 
38
- def cards_is_an_array_of_card
39
- unless @cards.is_a?(Array)
40
- errors.add(:cards, 'Cards must be an array')
41
- return
42
- end
99
+ private
43
100
 
44
- @cards.each do |card|
45
- next if card.is_a?(ZombieBattleground::Api::Card) &&
46
- card.valid? &&
47
- card.errors.size.zero?
101
+ ##
102
+ # Validator for cards attribute
103
+ #
104
+ # @return [void]
105
+ #
106
+ # @api private
107
+ def cards_is_an_array_of_card
108
+ unless @cards.is_a?(Array)
109
+ errors.add(:cards, 'Cards must be an array')
110
+ return
111
+ end
112
+
113
+ @cards.each do |card|
114
+ next if card.is_a?(ZombieBattleground::Api::Models::Card) &&
115
+ card.valid? &&
116
+ card.errors.size.zero?
48
117
 
49
- errors.add(:cards, 'cards must be an array of Card')
118
+ errors.add(:cards, 'cards must be an array of Card')
119
+ end
50
120
  end
51
121
  end
52
122
  end
@@ -9,29 +9,61 @@ require 'zombie_battleground/api/responses/response_helper'
9
9
 
10
10
  module ZombieBattleground
11
11
  class Api
12
- ##
13
- # Response validator for GetDeck
14
- class GetDeckResponse
15
- include ActiveModel::Validations
16
- include ZombieBattleground::Api::ValidationHelper
17
- include ZombieBattleground::Api::ResponseHelper
12
+ class Responses
13
+ ##
14
+ # Response validator for GetDeck
15
+ class GetDeckResponse
16
+ include ActiveModel::Validations
17
+ include ZombieBattleground::Api::ValidationHelper
18
+ include ZombieBattleground::Api::Responses::ResponseHelper
18
19
 
19
- attr_reader :deck
20
+ ##
21
+ # @!attribute [r] deck
22
+ # the deck
23
+ #
24
+ # @return [ZombieBattleground::Api::Models::Deck]
25
+ #
26
+ # @example
27
+ # response.deck #=> ZombieBattleground::Api::Models::Deck
28
+ #
29
+ # @api public
30
+ attr_reader :deck
20
31
 
21
- validate :deck_is_a_deck
32
+ validate :deck_is_a_deck
22
33
 
23
- def initialize(response)
24
- handle_errors(response)
34
+ ##
35
+ # Creates a new GetDeckResponse
36
+ #
37
+ # @param response [Faraday::Response] Faraday response from endpoint
38
+ #
39
+ # @return [ZombieBattleground::Api::GetDeckResponse]
40
+ #
41
+ # @example
42
+ # response = ZombieBattleground::Api::GetDeckResponse.new(faraday_response)
43
+ # # => ZombieBattleground::Api::GetDeckResponse
44
+ #
45
+ # @api public
46
+ def initialize(response)
47
+ handle_errors(response)
25
48
 
26
- @deck = ZombieBattleground::Api::Deck.new(JSON.parse(response.body))
27
- end
49
+ @deck = ZombieBattleground::Api::Models::Deck.new(JSON.parse(response.body))
50
+ end
51
+
52
+ private
28
53
 
29
- def deck_is_a_deck
30
- return if @deck.is_a?(ZombieBattleground::Api::Deck) &&
31
- @deck.valid? &&
32
- @deck.errors.size.zero?
54
+ ##
55
+ # Validator for deck attribute
56
+ #
57
+ # @return [void]
58
+ #
59
+ # @api private
60
+ def deck_is_a_deck
61
+ return if @deck.is_a?(ZombieBattleground::Api::Models::Deck) &&
62
+ @deck.valid? &&
63
+ @deck.errors.size.zero?
33
64
 
34
- errors.add(:deck, 'deck must be a Deck')
65
+ errors.add(:deck, 'deck must be a Deck')
66
+ end
35
67
  end
36
68
  end
37
69
  end
@@ -9,44 +9,114 @@ require 'zombie_battleground/api/responses/response_helper'
9
9
 
10
10
  module ZombieBattleground
11
11
  class Api
12
- ##
13
- # Response validator for GetDecks
14
- class GetDecksResponse
15
- include ActiveModel::Validations
16
- include ZombieBattleground::Api::ValidationHelper
17
- include ZombieBattleground::Api::ResponseHelper
18
-
19
- attr_reader :total, :page, :limit, :decks
20
-
21
- validate :total_is_a_non_negative_integer
22
- validate :page_is_a_non_negative_integer
23
- validate :limit_is_a_non_negative_integer
24
- validate :decks_is_an_array_of_deck
25
-
26
- def initialize(response)
27
- handle_errors(response)
28
-
29
- JSON.parse(response.body).each do |key, value|
30
- if key == 'decks'
31
- instance_variable_set("@#{key}".to_sym, value.map { |deck| ZombieBattleground::Api::Deck.new(deck) })
32
- else
33
- instance_variable_set("@#{key}".to_sym, value)
12
+ class Responses
13
+ ##
14
+ # Response validator for GetDecks
15
+ class GetDecksResponse
16
+ include ActiveModel::Validations
17
+ include ZombieBattleground::Api::ValidationHelper
18
+ include ZombieBattleground::Api::Responses::ResponseHelper
19
+
20
+ ##
21
+ # @!attribute [r] total
22
+ # the total number of results available
23
+ #
24
+ # @return [Integer]
25
+ #
26
+ # @example
27
+ # response.total #=> 1505
28
+ #
29
+ # @api public
30
+ attr_reader :total
31
+
32
+ ##
33
+ # @!attribute [r] page
34
+ # the page number of the results
35
+ #
36
+ # @return [Integer]
37
+ #
38
+ # @example
39
+ # response.page #=> 1
40
+ #
41
+ # @api public
42
+ attr_reader :page
43
+
44
+ ##
45
+ # @!attribute [r] limit
46
+ # the limit of results for the page
47
+ #
48
+ # @return [Integer]
49
+ #
50
+ # @example
51
+ # response.limit #=> 100
52
+ #
53
+ # @api public
54
+ attr_reader :limit
55
+
56
+ ##
57
+ # @!attribute [r] decks
58
+ # the deck found for the page and limit
59
+ #
60
+ # @return [Array<ZombieBattleground::Api::Models::Deck>]
61
+ #
62
+ # @example
63
+ # response.deck #=> [ZombieBattleground::Api::Models::Deck]
64
+ #
65
+ # @api public
66
+ attr_reader :decks
67
+
68
+ validate :total_is_a_non_negative_integer
69
+ validate :page_is_a_non_negative_integer
70
+ validate :limit_is_a_non_negative_integer
71
+ validate :decks_is_an_array_of_deck
72
+
73
+ ##
74
+ # Creates a new GetDecksResponse
75
+ #
76
+ # @param response [Faraday::Response] Faraday response from endpoint
77
+ #
78
+ # @return [ZombieBattleground::Api::GetDecksResponse]
79
+ #
80
+ # @example
81
+ # response = ZombieBattleground::Api::GetDecksResponse.new(faraday_response)
82
+ # # => ZombieBattleground::Api::GetDecksResponse
83
+ #
84
+ # @api public
85
+ def initialize(response)
86
+ handle_errors(response)
87
+
88
+ JSON.parse(response.body).each do |key, value|
89
+ if key == 'decks'
90
+ instance_variable_set(
91
+ "@#{key}".to_sym, value.map { |deck| ZombieBattleground::Api::Models::Deck.new(deck) }
92
+ )
93
+ else
94
+ instance_variable_set("@#{key}".to_sym, value)
95
+ end
34
96
  end
35
97
  end
36
- end
37
98
 
38
- def decks_is_an_array_of_deck
39
- unless @decks.is_a?(Array)
40
- errors.add(:decks, 'Decks must be an array')
41
- return
42
- end
99
+ private
43
100
 
44
- @decks.each do |deck|
45
- next if deck.is_a?(ZombieBattleground::Api::Deck) &&
46
- deck.valid? &&
47
- deck.errors.size.zero?
101
+ ##
102
+ # Validator for decks attribute
103
+ #
104
+ # @return [void]
105
+ #
106
+ # @api private
107
+ def decks_is_an_array_of_deck
108
+ unless @decks.is_a?(Array)
109
+ errors.add(:decks, 'Decks must be an array')
110
+ return
111
+ end
112
+
113
+ @decks.each do |deck|
114
+ next if deck.is_a?(ZombieBattleground::Api::Models::Deck) &&
115
+ deck.valid? &&
116
+ deck.errors.size.zero?
48
117
 
49
- errors.add(:decks, 'decks must be an array of Deck')
118
+ errors.add(:decks, 'decks must be an array of Deck')
119
+ end
50
120
  end
51
121
  end
52
122
  end