zombie_battleground-api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +62 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +21 -0
  5. data/.travis.yml +5 -0
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Dockerfile +33 -0
  8. data/Gemfile +8 -0
  9. data/Gemfile.lock +85 -0
  10. data/LICENSE +21 -0
  11. data/LICENSE.txt +21 -0
  12. data/README.md +57 -0
  13. data/Rakefile +16 -0
  14. data/bin/console +12 -0
  15. data/bin/setup +8 -0
  16. data/docker-compose.yml +4 -0
  17. data/lib/zombie_battleground/api/client.rb +114 -0
  18. data/lib/zombie_battleground/api/errors.rb +24 -0
  19. data/lib/zombie_battleground/api/models/card.rb +46 -0
  20. data/lib/zombie_battleground/api/models/deck.rb +65 -0
  21. data/lib/zombie_battleground/api/models/match.rb +47 -0
  22. data/lib/zombie_battleground/api/models/simple_card.rb +27 -0
  23. data/lib/zombie_battleground/api/requests/get_card_request.rb +27 -0
  24. data/lib/zombie_battleground/api/requests/get_cards_request.rb +40 -0
  25. data/lib/zombie_battleground/api/requests/get_deck_request.rb +30 -0
  26. data/lib/zombie_battleground/api/requests/get_decks_request.rb +36 -0
  27. data/lib/zombie_battleground/api/requests/get_match_request.rb +30 -0
  28. data/lib/zombie_battleground/api/requests/get_matches_request.rb +34 -0
  29. data/lib/zombie_battleground/api/requests/request_helper.rb +20 -0
  30. data/lib/zombie_battleground/api/responses/get_card_response.rb +38 -0
  31. data/lib/zombie_battleground/api/responses/get_cards_response.rb +54 -0
  32. data/lib/zombie_battleground/api/responses/get_deck_response.rb +38 -0
  33. data/lib/zombie_battleground/api/responses/get_decks_response.rb +54 -0
  34. data/lib/zombie_battleground/api/responses/get_match_response.rb +38 -0
  35. data/lib/zombie_battleground/api/responses/get_matches_response.rb +54 -0
  36. data/lib/zombie_battleground/api/responses/response_helper.rb +28 -0
  37. data/lib/zombie_battleground/api/validation_helper.rb +347 -0
  38. data/lib/zombie_battleground/api/version.rb +7 -0
  39. data/lib/zombie_battleground/api.rb +40 -0
  40. data/zombie_battleground-api.gemspec +40 -0
  41. metadata +196 -0
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,4 @@
1
+ version: '3'
2
+ services:
3
+ zombie_battleground-api:
4
+ build: .
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+
5
+ require 'zombie_battleground/api/errors'
6
+
7
+ require 'zombie_battleground/api/requests/get_decks_request'
8
+ require 'zombie_battleground/api/responses/get_decks_response'
9
+ require 'zombie_battleground/api/requests/get_deck_request'
10
+ require 'zombie_battleground/api/responses/get_deck_response'
11
+ require 'zombie_battleground/api/requests/get_matches_request'
12
+ require 'zombie_battleground/api/responses/get_matches_response'
13
+ require 'zombie_battleground/api/requests/get_match_request'
14
+ require 'zombie_battleground/api/responses/get_match_response'
15
+ require 'zombie_battleground/api/requests/get_cards_request'
16
+ require 'zombie_battleground/api/responses/get_cards_response'
17
+ require 'zombie_battleground/api/requests/get_card_request'
18
+ require 'zombie_battleground/api/responses/get_card_response'
19
+
20
+ module ZombieBattleground
21
+ class Api
22
+ ##
23
+ # The API Client for Zombie Battleground
24
+ class Client
25
+ def initialize
26
+ @endpoint = 'https://api.loom.games/zb'
27
+ @api_version = 'v1'
28
+ end
29
+
30
+ def connection(uri:, params:)
31
+ Faraday.new(
32
+ url: "#{@endpoint}/#{@api_version}/#{uri}",
33
+ params: params,
34
+ headers: {
35
+ 'User-Agent' => "RubyGem-zombie_battleground-api_#{VERSION}",
36
+ 'Content-Type' => 'application/json'
37
+ }
38
+ )
39
+ end
40
+
41
+ def decks(**args)
42
+ request = ZombieBattleground::Api::GetDecksRequest.new
43
+ args.each { |key, val| request.send("#{key}=", val) }
44
+ raise ZombieBattleground::Api::Errors::InvalidInput, request.errors.messages unless request.valid?
45
+
46
+ response = connection(uri: request.uri, params: request.params).get
47
+ decks = ZombieBattleground::Api::GetDecksResponse.new(response)
48
+ raise ZombieBattleground::Api::Errors::InvalidResponse.new(response, decks) unless decks.valid?
49
+
50
+ decks
51
+ end
52
+
53
+ def deck(**args)
54
+ request = ZombieBattleground::Api::GetDeckRequest.new
55
+ args.each { |key, val| request.send("#{key}=", val) }
56
+ raise ZombieBattleground::Api::Errors::InvalidInput, request.errors.messages unless request.valid?
57
+
58
+ response = connection(uri: request.uri, params: request.params).get
59
+ deck = ZombieBattleground::Api::GetDeckResponse.new(response)
60
+ raise ZombieBattleground::Api::Errors::InvalidResponse.new(response, deck) unless deck.valid?
61
+
62
+ deck
63
+ end
64
+
65
+ def matches(**args)
66
+ request = ZombieBattleground::Api::GetMatchesRequest.new
67
+ args.each { |key, val| request.send("#{key}=", val) }
68
+ raise ZombieBattleground::Api::Errors::InvalidInput, request.errors.messages unless request.valid?
69
+
70
+ response = connection(uri: request.uri, params: request.params).get
71
+ matches = ZombieBattleground::Api::GetMatchesResponse.new(response)
72
+ raise ZombieBattleground::Api::Errors::InvalidResponse.new(response, matches) unless matches.valid?
73
+
74
+ matches
75
+ end
76
+
77
+ def match(**args)
78
+ request = ZombieBattleground::Api::GetMatchRequest.new
79
+ args.each { |key, val| request.send("#{key}=", val) }
80
+ raise ZombieBattleground::Api::Errors::InvalidInput, request.errors.messages unless request.valid?
81
+
82
+ response = connection(uri: request.uri, params: request.params).get
83
+ match = ZombieBattleground::Api::GetMatchResponse.new(response)
84
+ raise ZombieBattleground::Api::Errors::InvalidResponse.new(response, match) unless match.valid?
85
+
86
+ match
87
+ end
88
+
89
+ def cards(**args)
90
+ request = ZombieBattleground::Api::GetCardsRequest.new
91
+ args.each { |key, val| request.send("#{key}=", val) }
92
+ raise ZombieBattleground::Api::Errors::InvalidInput, request.errors.messages unless request.valid?
93
+
94
+ response = connection(uri: request.uri, params: request.params).get
95
+ cards = ZombieBattleground::Api::GetCardsResponse.new(response)
96
+ raise ZombieBattleground::Api::Errors::InvalidResponse.new(response, cards) unless cards.valid?
97
+
98
+ cards
99
+ end
100
+
101
+ def card(**args)
102
+ request = ZombieBattleground::Api::GetCardRequest.new
103
+ args.each { |key, val| request.send("#{key}=", val) }
104
+ raise ZombieBattleground::Api::Errors::InvalidInput, request.errors.messages unless request.valid?
105
+
106
+ response = connection(uri: request.uri, params: request.params).get
107
+ card = ZombieBattleground::Api::GetCardResponse.new(response)
108
+ raise ZombieBattleground::Api::Errors::InvalidResponse.new(response, card) unless card.valid?
109
+
110
+ card
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZombieBattleground
4
+ class Api
5
+ class Errors
6
+ class BadRequest < StandardError; end
7
+ class InternalServerError < StandardError; end
8
+ class InvalidInput < StandardError; end
9
+ class NotFound < StandardError; end
10
+ class ServiceUnavailable < StandardError; end
11
+ class UnknownResponse < StandardError; end
12
+
13
+ ##
14
+ # Creates an error with the Faraday response and the context for the error
15
+ class InvalidResponse < StandardError
16
+ def initialize(response, context)
17
+ @response = response
18
+ @context = context
19
+ super(context.errors.messages.to_s)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+
5
+ require 'zombie_battleground/api/validation_helper'
6
+
7
+ module ZombieBattleground
8
+ class Api
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)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+
5
+ require 'zombie_battleground/api/validation_helper'
6
+ require 'zombie_battleground/api/models/simple_card'
7
+
8
+ module ZombieBattleground
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)
45
+ end
46
+ end
47
+ end
48
+
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
54
+
55
+ @cards.each do |card|
56
+ next if card.is_a?(ZombieBattleground::Api::SimpleCard) &&
57
+ card.valid? &&
58
+ card.errors.size.zero?
59
+
60
+ errors.add(:cards, 'cards must be an array of SimpleCard')
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+
5
+ require 'zombie_battleground/api/validation_helper'
6
+
7
+ module ZombieBattleground
8
+ class Api
9
+ ##
10
+ # Validator for Match
11
+ class Match
12
+ include ActiveModel::Validations
13
+ include ZombieBattleground::Api::ValidationHelper
14
+
15
+ attr_reader :id, :created_at, :updated_at, :player1_id, :player2_id, :player1_accepted,
16
+ :player2_accepted, :player1_deck_id, :player2_deck_id, :status, :version,
17
+ :random_seed, :winner_id, :block_height
18
+
19
+ validate :id_is_a_non_negative_integer
20
+ validate :created_at_is_a_time_and_not_null
21
+ validate :updated_at_is_a_time_and_not_null
22
+ validate :player1_id_is_a_string_and_not_null
23
+ validate :player2_id_is_a_string_and_not_null
24
+ validates :player1_accepted, inclusion: { in: [true, false] }
25
+ validates :player2_accepted, inclusion: { in: [true, false] }
26
+ validate :player1_deck_id_is_a_non_negative_integer
27
+ validate :player2_deck_id_is_a_non_negative_integer
28
+ validate :status_is_a_string_and_not_null
29
+ validate :version_is_a_string_and_not_null
30
+ validate :random_seed_is_a_non_negative_integer
31
+ validate :winner_id_is_a_string_and_not_null
32
+ validate :block_height_is_a_non_negative_integer_and_not_null
33
+
34
+ def initialize(match)
35
+ match.each do |key, value|
36
+ next if value.nil? # this is an illegal response, match id 1 is bogus
37
+
38
+ if %w[created_at updated_at].include?(key)
39
+ instance_variable_set("@#{key}".to_sym, Time.parse(value))
40
+ else
41
+ instance_variable_set("@#{key}".to_sym, value)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+
5
+ require 'zombie_battleground/api/validation_helper'
6
+
7
+ module ZombieBattleground
8
+ class Api
9
+ ##
10
+ # Validator for SimpleCard (only returned from GetDeck(s))
11
+ class SimpleCard
12
+ include ActiveModel::Validations
13
+ include ZombieBattleground::Api::ValidationHelper
14
+
15
+ attr_reader :card_name, :amount
16
+
17
+ validate :card_name_is_a_string_and_not_null
18
+ validate :amount_is_a_non_negative_integer_and_not_null
19
+
20
+ def initialize(card)
21
+ card.each do |key, value|
22
+ instance_variable_set("@#{key}".to_sym, value)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+
5
+ require 'zombie_battleground/api/validation_helper'
6
+ require 'zombie_battleground/api/requests/request_helper'
7
+
8
+ module ZombieBattleground
9
+ class Api
10
+ ##
11
+ # Request validator for GetCard
12
+ class GetCardRequest
13
+ include ActiveModel::Validations
14
+ include ZombieBattleground::Api::ValidationHelper
15
+ include ZombieBattleground::Api::RequestHelper
16
+
17
+ attr_accessor :mould_id, :version
18
+
19
+ validate :mould_id_is_a_string_and_not_null
20
+ validate :version_is_a_string_and_not_null
21
+
22
+ def uri
23
+ 'card'
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+
5
+ require 'zombie_battleground/api/validation_helper'
6
+ require 'zombie_battleground/api/requests/request_helper'
7
+
8
+ module ZombieBattleground
9
+ class Api
10
+ ##
11
+ # Request validator for GetCards
12
+ class GetCardsRequest
13
+ include ActiveModel::Validations
14
+ include ZombieBattleground::Api::ValidationHelper
15
+ include ZombieBattleground::Api::RequestHelper
16
+
17
+ attr_accessor :id, :mould_id, :version, :kind, :set, :name, :rank, :type, :rarity,
18
+ :damage, :health, :cost, :page, :limit
19
+
20
+ validate :id_is_a_non_negative_integer
21
+ validate :mould_id_is_a_string
22
+ validate :version_is_a_string
23
+ validate :kind_is_a_string
24
+ validate :set_is_a_string
25
+ validate :name_is_a_string
26
+ validate :rank_is_a_string
27
+ validate :type_is_a_string
28
+ validate :rarity_is_a_string
29
+ validate :damage_is_a_non_negative_integer
30
+ validate :health_is_a_non_negative_integer
31
+ validate :cost_is_a_non_negative_integer
32
+ validate :page_is_a_non_negative_integer
33
+ validate :limit_is_a_non_negative_integer
34
+
35
+ def uri
36
+ 'cards'
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+
5
+ require 'zombie_battleground/api/validation_helper'
6
+ require 'zombie_battleground/api/requests/request_helper'
7
+
8
+ module ZombieBattleground
9
+ class Api
10
+ ##
11
+ # Request validator for GetDeck
12
+ class GetDeckRequest
13
+ include ActiveModel::Validations
14
+ include ZombieBattleground::Api::ValidationHelper
15
+ include ZombieBattleground::Api::RequestHelper
16
+
17
+ attr_accessor :id
18
+
19
+ validate :id_is_a_non_negative_integer
20
+
21
+ def uri
22
+ "deck/#{id}"
23
+ end
24
+
25
+ def params
26
+ {}
27
+ end
28
+ end
29
+ end
30
+ end