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,17 +2,45 @@
2
2
 
3
3
  module ZombieBattleground
4
4
  class Api
5
+ ##
6
+ # Error classes for the API
5
7
  class Errors
8
+ ##
9
+ # Raised if the endpoint responds with a 400
6
10
  class BadRequest < StandardError; end
11
+
12
+ ##
13
+ # Raised if the endpoint responds with a 500
7
14
  class InternalServerError < StandardError; end
15
+
16
+ ##
17
+ # Raised if a request validation fails
8
18
  class InvalidInput < StandardError; end
19
+
20
+ ##
21
+ # Raised if the endpoint responds with a 404
9
22
  class NotFound < StandardError; end
23
+
24
+ ##
25
+ # Raised if the endpoint responds with a 503
10
26
  class ServiceUnavailable < StandardError; end
27
+
28
+ ##
29
+ # Raised if the response fails validation
11
30
  class UnknownResponse < StandardError; end
12
31
 
13
32
  ##
14
33
  # Creates an error with the Faraday response and the context for the error
15
34
  class InvalidResponse < StandardError
35
+ ##
36
+ # Creates an exception class with the Faraday response and error context
37
+ #
38
+ # @param response [Faraday::Response] response from the endpoint
39
+ # @param context [Class] parsed response object
40
+ #
41
+ # @return [ZombieBattleground::Api::Errors::InvalidResponse]
42
+ #
43
+ # @api private
16
44
  def initialize(response, context)
17
45
  @response = response
18
46
  @context = context
@@ -7,38 +7,278 @@ require 'zombie_battleground/api/validation_helper'
7
7
  module ZombieBattleground
8
8
  class Api
9
9
  ##
10
- # Validator for Card
11
- class Card
12
- include ActiveModel::Validations
13
- include ZombieBattleground::Api::ValidationHelper
14
-
15
- attr_reader :id, :mould_id, :version, :kind, :set, :name, :description, :flavor_text,
16
- :picture, :rank, :type, :rarity, :frame, :damage, :health, :cost,
17
- :ability, :block_height, :image_url
18
-
19
- validate :id_is_a_non_negative_integer
20
- validate :mould_id_is_a_string_and_not_null
21
- validate :version_is_a_string_and_not_null
22
- validate :kind_is_a_string_and_not_null
23
- validate :set_is_a_string_and_not_null
24
- validate :name_is_a_string_and_not_null
25
- validate :description_is_a_string_and_not_null
26
- validate :rank_is_a_string_and_not_null
27
- validate :type_is_a_string_and_not_null
28
- validate :rarity_is_a_string_and_not_null
29
- validate :frame_is_a_string_and_not_null
30
- validate :damage_is_a_non_negative_integer_and_not_null
31
- validate :health_is_a_non_negative_integer_and_not_null
32
- validate :cost_is_a_non_negative_integer_and_not_null
33
- validate :ability_is_a_string_and_not_null
34
- validate :block_height_is_a_non_negative_integer_and_not_null
35
- validate :image_url_is_a_string_and_not_null
36
-
37
- def initialize(card)
38
- card.each do |key, value|
39
- next if value.nil? # this is an illegal response, card id 1 is bogus
40
-
41
- instance_variable_set("@#{key}".to_sym, value)
10
+ # Namespace for Models
11
+ class Models
12
+ ##
13
+ # Validator for Card
14
+ class Card
15
+ include ActiveModel::Validations
16
+ include ZombieBattleground::Api::ValidationHelper
17
+
18
+ ##
19
+ # @!attribute [r] id
20
+ # the Cards's id
21
+ #
22
+ # @return [Integer]
23
+ #
24
+ # @example
25
+ # card.id #=> 1
26
+ #
27
+ # @api public
28
+ attr_reader :id
29
+
30
+ ##
31
+ # @!attribute [r] mould_id
32
+ # the Cards's mould_id
33
+ #
34
+ # @return [String]
35
+ #
36
+ # @example
37
+ # card.mould_id #=> "1"
38
+ #
39
+ # @api public
40
+ attr_reader :mould_id
41
+
42
+ ##
43
+ # @!attribute [r] version
44
+ # the Cards's version
45
+ #
46
+ # @return [String]
47
+ #
48
+ # @example
49
+ # card.version #=> "v3"
50
+ #
51
+ # @api public
52
+ attr_reader :version
53
+
54
+ ##
55
+ # @!attribute [r] kind
56
+ # the Cards's kind
57
+ #
58
+ # @return [String]
59
+ #
60
+ # @example
61
+ # card.kind #=> "CREATURE"
62
+ #
63
+ # @api public
64
+ attr_reader :kind
65
+
66
+ ##
67
+ # @!attribute [r] set
68
+ # the Cards's set
69
+ #
70
+ # @return [String]
71
+ #
72
+ # @example
73
+ # card.set #=> "AIR"
74
+ #
75
+ # @api public
76
+ attr_reader :set
77
+
78
+ ##
79
+ # @!attribute [r] name
80
+ # the Cards's name
81
+ #
82
+ # @return [String]
83
+ #
84
+ # @example
85
+ # card.name #=> "Whizpar"
86
+ #
87
+ # @api public
88
+ attr_reader :name
89
+
90
+ ##
91
+ # @!attribute [r] description
92
+ # the Cards's description
93
+ #
94
+ # @return [String]
95
+ #
96
+ # @example
97
+ # card.description #=> "<b>Attack:</b> +1 damage to Water zombies"
98
+ #
99
+ # @api public
100
+ attr_reader :description
101
+
102
+ ##
103
+ # @!attribute [r] flavor_text
104
+ # the Cards's flavor_text
105
+ #
106
+ # @return [String]
107
+ #
108
+ # @example
109
+ # card.flavor_text #=> "The unfriendly ghost..."
110
+ #
111
+ # @api public
112
+ attr_reader :flavor_text
113
+
114
+ ##
115
+ # @!attribute [r] picture
116
+ # the Cards's picture
117
+ #
118
+ # @return [String]
119
+ #
120
+ # @example
121
+ # card.picture #=> "Whizpar"
122
+ #
123
+ # @api public
124
+ attr_reader :picture
125
+
126
+ ##
127
+ # @!attribute [r] rank
128
+ # the Cards's rank
129
+ #
130
+ # @return [String]
131
+ #
132
+ # @example
133
+ # card.rank #=> "MINION"
134
+ #
135
+ # @api public
136
+ attr_reader :rank
137
+
138
+ ##
139
+ # @!attribute [r] type
140
+ # the Cards's type
141
+ #
142
+ # @return [String]
143
+ #
144
+ # @example
145
+ # card.type #=> "WALKER"
146
+ #
147
+ # @api public
148
+ attr_reader :type
149
+
150
+ ##
151
+ # @!attribute [r] rarity
152
+ # the Cards's rarity
153
+ #
154
+ # @return [String]
155
+ #
156
+ # @example
157
+ # card.rarity #=> ""
158
+ #
159
+ # @api public
160
+ attr_reader :rarity
161
+
162
+ ##
163
+ # @!attribute [r] frame
164
+ # the Cards's frame
165
+ #
166
+ # @return [String]
167
+ #
168
+ # @example
169
+ # card.frame #=> ""
170
+ #
171
+ # @api public
172
+ attr_reader :frame
173
+
174
+ ##
175
+ # @!attribute [r] damage
176
+ # the Cards's damage
177
+ #
178
+ # @return [Integer]
179
+ #
180
+ # @example
181
+ # card.damage #=> 1
182
+ #
183
+ # @api public
184
+ attr_reader :damage
185
+
186
+ ##
187
+ # @!attribute [r] health
188
+ # the Cards's health
189
+ #
190
+ # @return [Integer]
191
+ #
192
+ # @example
193
+ # card.health #=> 1
194
+ #
195
+ # @api public
196
+ attr_reader :health
197
+
198
+ ##
199
+ # @!attribute [r] cost
200
+ # the Cards's cost
201
+ #
202
+ # @return [Integer]
203
+ #
204
+ # @example
205
+ # card.cost #=> 1
206
+ #
207
+ # @api public
208
+ attr_reader :cost
209
+
210
+ ##
211
+ # @!attribute [r] ability
212
+ # the Cards's ability
213
+ #
214
+ # @return [String]
215
+ #
216
+ # @example
217
+ # card.ability #=> ""
218
+ #
219
+ # @api public
220
+ attr_reader :ability
221
+
222
+ ##
223
+ # @!attribute [r] block_height
224
+ # the Cards's block_height
225
+ #
226
+ # @return [Integer]
227
+ #
228
+ # @example
229
+ # card.block_height #=> 0
230
+ #
231
+ # @api public
232
+ attr_reader :block_height
233
+
234
+ ##
235
+ # @!attribute [r] image_url
236
+ # the Cards's image_url
237
+ #
238
+ # @return [String]
239
+ #
240
+ # @example
241
+ # card.image_url #=> "https://loom.games/img/cards/001.png"
242
+ #
243
+ # @api public
244
+ attr_reader :image_url
245
+
246
+ validate :id_is_a_non_negative_integer
247
+ validate :mould_id_is_a_string_and_not_null
248
+ validate :version_is_a_string_and_not_null
249
+ validate :kind_is_a_string_and_not_null
250
+ validate :set_is_a_string_and_not_null
251
+ validate :name_is_a_string_and_not_null
252
+ validate :description_is_a_string_and_not_null
253
+ validate :rank_is_a_string_and_not_null
254
+ validate :type_is_a_string_and_not_null
255
+ validate :rarity_is_a_string_and_not_null
256
+ validate :frame_is_a_string_and_not_null
257
+ validate :damage_is_a_non_negative_integer_and_not_null
258
+ validate :health_is_a_non_negative_integer_and_not_null
259
+ validate :cost_is_a_non_negative_integer_and_not_null
260
+ validate :ability_is_a_string_and_not_null
261
+ validate :block_height_is_a_non_negative_integer_and_not_null
262
+ validate :image_url_is_a_string_and_not_null
263
+
264
+ ##
265
+ # Creates a new Card
266
+ #
267
+ # @param card [Hash] Parsed JSON response
268
+ #
269
+ # @return [ZombieBattleground::Api::Card]
270
+ #
271
+ # @example
272
+ # card = ZombieBattleground::Api::Card.new(parsed_json)
273
+ # # => ZombieBattleground::Api::Card
274
+ #
275
+ # @api public
276
+ def initialize(card)
277
+ card.each do |key, value|
278
+ next if value.nil? # this is an illegal response, card id 1 is bogus
279
+
280
+ instance_variable_set("@#{key}".to_sym, value)
281
+ end
42
282
  end
43
283
  end
44
284
  end
@@ -7,57 +7,245 @@ require 'zombie_battleground/api/models/simple_card'
7
7
 
8
8
  module ZombieBattleground
9
9
  class Api
10
- ##
11
- # Validator for Deck
12
- class Deck
13
- include ActiveModel::Validations
14
- include ZombieBattleground::Api::ValidationHelper
15
-
16
- attr_reader :id, :created_at, :updated_at, :user_id, :deck_id, :name, :hero_id,
17
- :cards, :primary_skill_id, :secondary_skill_id, :version, :sender_address,
18
- :block_height, :is_deleted
19
-
20
- validate :id_is_a_non_negative_integer
21
- validate :created_at_is_a_time_and_not_null
22
- validate :updated_at_is_a_time_and_not_null
23
- validate :user_id_is_a_string_and_not_null
24
- validate :deck_id_is_a_non_negative_integer_and_not_null
25
- validate :name_is_a_string_and_not_null
26
- validate :hero_id_is_a_non_negative_integer_and_not_null
27
- validate :cards_is_an_array_of_simple_card
28
- validate :primary_skill_id_is_a_non_negative_integer_and_not_null
29
- validate :secondary_skill_id_is_a_non_negative_integer_and_not_null
30
- validate :version_is_a_string_and_not_null
31
- validate :sender_address_is_a_string_and_not_null
32
- validate :block_height_is_a_non_negative_integer_and_not_null
33
- validates :is_deleted, inclusion: { in: [true, false] }
34
-
35
- def initialize(deck)
36
- deck.each do |key, value|
37
- next if value.nil? # this is an illegal response, deck id 1 is bogus
38
-
39
- if key == 'cards'
40
- instance_variable_set("@#{key}".to_sym, value.map { |card| ZombieBattleground::Api::SimpleCard.new(card) })
41
- elsif %w[created_at updated_at].include?(key)
42
- instance_variable_set("@#{key}".to_sym, Time.parse(value))
43
- else
44
- instance_variable_set("@#{key}".to_sym, value)
10
+ class Models
11
+ ##
12
+ # Validator for Deck
13
+ class Deck
14
+ include ActiveModel::Validations
15
+ include ZombieBattleground::Api::ValidationHelper
16
+
17
+ ##
18
+ # @!attribute [r] id
19
+ # Deck's id
20
+ #
21
+ # @return [Integer]
22
+ #
23
+ # @example
24
+ # deck.id #=> 1812
25
+ #
26
+ # @api public
27
+ attr_reader :id
28
+
29
+ ##
30
+ # @!attribute [r] created_at
31
+ # the Deck's created_at time
32
+ #
33
+ # @return [Time]
34
+ #
35
+ # @example
36
+ # deck.created_at #=> Time
37
+ #
38
+ # @api public
39
+ attr_reader :created_at
40
+
41
+ ##
42
+ # @!attribute [r] updated_at
43
+ # the Deck's updated_at time
44
+ #
45
+ # @return [Time]
46
+ #
47
+ # @example
48
+ # deck.updated_at #=> Time
49
+ #
50
+ # @api public
51
+ attr_reader :updated_at
52
+
53
+ ##
54
+ # @!attribute [r] user_id
55
+ # the Deck's user_id
56
+ #
57
+ # @return [String]
58
+ #
59
+ # @example
60
+ # deck.user_id #=> "ZombieSlayer_5773"
61
+ #
62
+ # @api public
63
+ attr_reader :user_id
64
+
65
+ ##
66
+ # @!attribute [r] deck_id
67
+ # the Deck's deck_id
68
+ #
69
+ # @return [Integer]
70
+ #
71
+ # @example
72
+ # deck.deck_id #=> 4
73
+ #
74
+ # @api public
75
+ attr_reader :deck_id
76
+
77
+ ##
78
+ # @!attribute [r] name
79
+ # the Deck's name
80
+ #
81
+ # @return [String]
82
+ #
83
+ # @example
84
+ # deck.name #=> "Leaf Midrange"
85
+ #
86
+ # @api public
87
+ attr_reader :name
88
+
89
+ ##
90
+ # @!attribute [r] hero_id
91
+ # the Deck's hero_id
92
+ #
93
+ # @return [Integer]
94
+ #
95
+ # @example
96
+ # deck.hero_id #=> 5
97
+ #
98
+ # @api public
99
+ attr_reader :hero_id
100
+
101
+ ##
102
+ # @!attribute [r] cards
103
+ # the Deck's cards
104
+ #
105
+ # @return [Array<ZombieBattleground::Api::Models::SimpleCard>]
106
+ #
107
+ # @example
108
+ # deck.cards #=> [ZombieBattleground::Api::Models::SimpleCard]
109
+ #
110
+ # @api public
111
+ attr_reader :cards
112
+
113
+ ##
114
+ # @!attribute [r] primary_skill_id
115
+ # the Deck's primary_skill_id
116
+ #
117
+ # @return [Integer]
118
+ #
119
+ # @example
120
+ # deck.primary_skill_id #=> 19
121
+ #
122
+ # @api public
123
+ attr_reader :primary_skill_id
124
+
125
+ ##
126
+ # @!attribute [r] secondary_skill_id
127
+ # the Deck's secondary_skill_id
128
+ #
129
+ # @return [Integer]
130
+ #
131
+ # @example
132
+ # deck.secondary_skill_id #=> 18
133
+ #
134
+ # @api public
135
+ attr_reader :secondary_skill_id
136
+
137
+ ##
138
+ # @!attribute [r] version
139
+ # the Deck's version
140
+ #
141
+ # @return [String]
142
+ #
143
+ # @example
144
+ # deck.version #=> "v5"
145
+ #
146
+ # @api public
147
+ attr_reader :version
148
+
149
+ ##
150
+ # @!attribute [r] sender_address
151
+ # the Deck's sender_address
152
+ #
153
+ # @return [String]
154
+ #
155
+ # @example
156
+ # deck.sender_address #=> "0x62bb8C4452c3Fa7E9eEd6D9Ceaa4d3fe8E18E249"
157
+ #
158
+ # @api public
159
+ attr_reader :sender_address
160
+
161
+ ##
162
+ # @!attribute [r] block_height
163
+ # the Deck's block_height
164
+ #
165
+ # @return [Integer]
166
+ #
167
+ # @example
168
+ # deck.block_height #=> 939237
169
+ #
170
+ # @api public
171
+ attr_reader :block_height
172
+
173
+ ##
174
+ # @!attribute [r] is_deleted
175
+ # the Deck's is_deleted
176
+ #
177
+ # @return [Boolean]
178
+ #
179
+ # @example
180
+ # deck.is_deleted #=> false
181
+ #
182
+ # @api public
183
+ attr_reader :is_deleted
184
+
185
+ validate :id_is_a_non_negative_integer
186
+ validate :created_at_is_a_time_and_not_null
187
+ validate :updated_at_is_a_time_and_not_null
188
+ validate :user_id_is_a_string_and_not_null
189
+ validate :deck_id_is_a_non_negative_integer_and_not_null
190
+ validate :name_is_a_string_and_not_null
191
+ validate :hero_id_is_a_non_negative_integer_and_not_null
192
+ validate :cards_is_an_array_of_simple_card
193
+ validate :primary_skill_id_is_a_non_negative_integer_and_not_null
194
+ validate :secondary_skill_id_is_a_non_negative_integer_and_not_null
195
+ validate :version_is_a_string_and_not_null
196
+ validate :sender_address_is_a_string_and_not_null
197
+ validate :block_height_is_a_non_negative_integer_and_not_null
198
+ validates :is_deleted, inclusion: { in: [true, false] }
199
+
200
+ ##
201
+ # Creates a new Deck
202
+ #
203
+ # @param card [Hash] Parsed JSON response
204
+ #
205
+ # @return [ZombieBattleground::Api::Deck]
206
+ #
207
+ # @example
208
+ # deck = ZombieBattleground::Api::Deck.new(parsed_json)
209
+ # # => ZombieBattleground::Api::Deck
210
+ #
211
+ # @api public
212
+ def initialize(deck)
213
+ deck.each do |key, value|
214
+ next if value.nil? # this is an illegal response, deck id 1 is bogus
215
+
216
+ if key == 'cards'
217
+ instance_variable_set(
218
+ "@#{key}".to_sym, value.map { |card| ZombieBattleground::Api::Models::SimpleCard.new(card) }
219
+ )
220
+ elsif %w[created_at updated_at].include?(key)
221
+ instance_variable_set("@#{key}".to_sym, Time.parse(value))
222
+ else
223
+ instance_variable_set("@#{key}".to_sym, value)
224
+ end
45
225
  end
46
226
  end
47
- end
48
227
 
49
- def cards_is_an_array_of_simple_card
50
- unless @cards.is_a?(Array)
51
- errors.add(:cards, 'cards must be an Array')
52
- return
53
- end
228
+ private
54
229
 
55
- @cards.each do |card|
56
- next if card.is_a?(ZombieBattleground::Api::SimpleCard) &&
57
- card.valid? &&
58
- card.errors.size.zero?
230
+ ##
231
+ # Validator for cards attribute
232
+ #
233
+ # @return [void]
234
+ #
235
+ # @api private
236
+ def cards_is_an_array_of_simple_card
237
+ unless @cards.is_a?(Array)
238
+ errors.add(:cards, 'cards must be an Array')
239
+ return
240
+ end
59
241
 
60
- errors.add(:cards, 'cards must be an array of SimpleCard')
242
+ @cards.each do |card|
243
+ next if card.is_a?(ZombieBattleground::Api::Models::SimpleCard) &&
244
+ card.valid? &&
245
+ card.errors.size.zero?
246
+
247
+ errors.add(:cards, 'cards must be an array of SimpleCard')
248
+ end
61
249
  end
62
250
  end
63
251
  end