battlerite 0.1.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 582d85872b524d4fff8c3cbd74010f4cb9828fb2
4
- data.tar.gz: '0382244b51c915dcf39f99ab0227fd99971776b9'
3
+ metadata.gz: 8247cf7160956fa03ac8e8c05c3a4cdc0eca2c06
4
+ data.tar.gz: e8080bc6dbdf10f6695edf9aed86ffef6ee084e8
5
5
  SHA512:
6
- metadata.gz: 81092aa2b6da933b92b8f2e6cee6eff8a9892583b062c433561be4ecbe83da1b97e8e429932f02156167248ebdfbbd531ad2c2cd545eb0ac14d9dbd8000ad881
7
- data.tar.gz: 8aa08afde2924bc364393595afb6d2ebad5ff518b46f7902dbcfb0e1bd4631408204d697c57e03a063949944f2b741f10859031e659153410db76470baa0d285
6
+ metadata.gz: ae148a6c1f8bb97c2fc72537c43b2d41536f4c27e2738230133fb371dca5eb865de0bb6dbaa1d47bf6feaa70ff2b288c4a9d7df19d12e855a434d62d1cebeaa0
7
+ data.tar.gz: 9b92b6de1502f64f8981c12970b915954f842d646c7fc5773d74917b14feba706e86a911d48603d39312638faf4c4a816cdf93318bbe3443cc949d2d6167a853
@@ -11,6 +11,11 @@ module Battlerite
11
11
  Instance.new
12
12
  end
13
13
 
14
+ def self.visualize models
15
+ models = [models] unless models.respond_to? :to_ary
16
+ puts models.map { |model| model.to_s_tree }
17
+ end
18
+
14
19
  class Instance
15
20
 
16
21
  attr_reader :api
@@ -20,9 +25,7 @@ module Battlerite
20
25
 
21
26
  @api.public_methods(false).each do |method|
22
27
  define_singleton_method method do |*args|
23
- puts "sending #{method} with #{args}"
24
28
  result = @api.send(method, *args)
25
- puts "result: #{result}"
26
29
  Muncher.munch @api.send(method, *args)
27
30
  end
28
31
  end
@@ -1,4 +1,4 @@
1
-
1
+ require_relative "errors"
2
2
 
3
3
  module Battlerite
4
4
  class Api
@@ -15,13 +15,35 @@ module Battlerite
15
15
  def get_matches opt={}
16
16
  uri = URI(uris[:matches])
17
17
  a = uri.query.nil? ? [] : URI.decode_www_form(uri.query)
18
+ a << ["page[offset]", opt[:page_offset] || 0]
19
+ a << ["page[limit]", opt[:page_limit] || 5]
20
+ a << ["sort", opt[:sort] || "createdAt"]
21
+ a << ["filter[createdAt-start]", opt[:created_at_start] || (Time.now - (28 * 60 * 60 * 24))]
22
+ a << ["filter[createdAt-end]", opt[:created_at_end] || (Time.now)]
23
+ a << ["filter[gameMode]", opt[:game_mode]] if opt[:game_mode]
24
+ a << ["filter[playerIds]", opt[:player_ids]] if opt[:player_ids]
25
+ if opt[:player_names]
26
+ raise NotImplementedError, "Battlerite API 1.0 has not implemented player data yet. Check http://battlerite-docs.readthedocs.io for more information."
27
+ end
28
+ if opt[:team_names]
29
+ raise NotImplementedError, "Battlerite API 1.0 has not implemented team data yet. Check http://battlerite-docs.readthedocs.io for more information."
30
+ end
31
+ #a << ["filter[playerNames]", opt[:player_names]] if opt[:player_names]
32
+ #a << ["filter[teamNames]", opt[:team_names]] if opt[:team_names]
33
+ uri.query = URI.encode_www_form(a)
34
+ get uri
35
+ end
36
+
37
+ def get_matches_options opt
38
+ a = []
18
39
  a << ["page[offset]", opt[:offset] || 0]
19
40
  a << ["page[limit]", opt[:limit] || 5]
20
41
  a << ["sort", opt[:sort] || "createdAt"]
42
+ a << ["filter[createdAt-start]", opt[:start_date] || (Time.now - (28 * 60 * 60 * 24))]
43
+ a << ["filter[createdAt-end]", opt[:end_date] || (Time.now - (28 * 60 * 60 * 24))]
21
44
  a << ["filter[gameMode]", opt[:game_mode]] if opt[:game_mode]
22
45
  a << ["filter[teamNames]", opt[:team_names]] if opt[:team_names]
23
- uri.query = URI.encode_www_form(a)
24
- get uri
46
+ a
25
47
  end
26
48
 
27
49
  def get_match id
@@ -53,6 +75,7 @@ module Battlerite
53
75
  http.request(req)
54
76
  end
55
77
 
78
+ raise Battlerite::UnauthorizedError.new(res) if res.code.to_i == 401
56
79
  raise Battlerite::ApiError.new(res) if res.code.to_i >= 400
57
80
 
58
81
  JSON.parse(res.body)
@@ -8,6 +8,11 @@ module Battlerite
8
8
  def initialize data, msg="Two or more records exist with the same ID."
9
9
  @id = data[:id]
10
10
  @records = data[:records]
11
+ @message = msg
12
+ end
13
+
14
+ def message
15
+ @message
11
16
  end
12
17
 
13
18
  end
@@ -16,10 +21,24 @@ module Battlerite
16
21
 
17
22
  attr_reader :res
18
23
 
19
- def initialize res, msg="Error received from Battlerite API"
24
+ def initialize res
20
25
  @res = res
21
26
  end
22
27
 
28
+ def message
29
+ "Error code #{@res.code} received from Battlerite API. Response body: #{@res.body}"
30
+ end
31
+
23
32
  end
24
33
 
34
+ class UnauthorizedError < ApiError
35
+
36
+ def message
37
+ "Unauthorized (#{@res.code}) error from Battlerite API. Did you set ENV[\"BATTLERITE_API_KEY\"]?"
38
+ end
39
+
40
+ end
41
+
42
+
25
43
  end
44
+
@@ -1,7 +1,62 @@
1
1
  module Battlerite
2
2
 
3
3
  class Battlerite::Model
4
- attr_reader :id
4
+
5
+ @@dig = true
6
+
7
+ def self.enable_dynamic_dig enabled
8
+ @@dig = enabled
9
+ end
10
+
11
+ def self.alias_attribute new, old
12
+ @aliases ||= {}
13
+ @aliases[new] = old
14
+ end
15
+
16
+ def self.aliases
17
+ @aliases ||= {}
18
+ end
19
+
20
+ attr_reader :id, :relationships, :blob
21
+
22
+ def initialize params
23
+ @relationships = params.delete :relationships
24
+ @blob = params.delete :blob
25
+
26
+ params.each { |k, v| self.instance_variable_set :"@#{k}", v }
27
+
28
+ @relationships.each { |k, v| self.define_singleton_method(k.to_sym) { v } }
29
+
30
+ self.class.aliases.each { |new, old| self.define_singleton_method(new.to_sym) { |*args, &block| self.public_send(old, *args, &block) } }
31
+ end
32
+
33
+ def method_missing method, *args, &block
34
+ return dig(method, *args, &block) unless dig_path(method).empty? || @@dig == false
35
+ super(method, *args, &block)
36
+ end
37
+
38
+ def respond_to? method, *args, &block
39
+ return !dig_path(method).empty? || super(method, *args, &block) if @@dig
40
+ return super(method, *args, &block)
41
+ end
42
+
43
+ def method(method)
44
+ if !dig_path(method).empty? && @@dig
45
+ super(:dig)
46
+ else
47
+ super(method)
48
+ end
49
+ end
50
+
51
+ def dig_path(method)
52
+ return [self] if @relationships.keys.include?(method)
53
+ return [self] if self.class.aliases.keys.include?(method)
54
+ return @relationships.values.map { |v| v.map { |i| i.dig_path(method) } }.compact.flatten
55
+ end
56
+
57
+ def dig(method, *args, &block)
58
+ dig_path(method).map { |v| v.public_send(method, *args, &block) }.flatten
59
+ end
5
60
 
6
61
  def to_s
7
62
  "#{self.class.name.split("::").last}: #{id[0,5]}..."
@@ -15,6 +70,7 @@ module Battlerite
15
70
 
16
71
  end
17
72
 
18
- Dir[File.join(File.dirname(__FILE__), "models", "*.rb")].each { |file| require file }
19
73
 
20
74
  end
75
+
76
+ Dir[File.join(File.dirname(__FILE__), "models", "*.rb")].each { |file| require file }
@@ -9,15 +9,11 @@ module Battlerite
9
9
  created: Time.parse(asset["attributes"]["createdAt"]),
10
10
  description: asset["attributes"]["description"],
11
11
  name: asset["attributes"]["name"],
12
+ relationships: relationships,
12
13
  blob: blob,
13
14
  )
14
15
  end
15
16
 
16
- def initialize params
17
- params.each { |k, v| self.instance_variable_set :"@#{k}", v }
18
- end
19
-
20
17
  end
21
18
 
22
-
23
19
  end
@@ -21,12 +21,6 @@ module Battlerite
21
21
  )
22
22
  end
23
23
 
24
-
25
- def initialize params
26
- params.each { |k, v| self.instance_variable_set :"@#{k}", v }
27
- end
28
-
29
24
  end
30
25
 
31
-
32
26
  end
@@ -1,46 +1,41 @@
1
1
  module Battlerite
2
2
 
3
- class Participant < Battlerite::Model
3
+ class Participant < Battlerite::Model
4
+
5
+ attr_reader :actor, :ability_uses, :attachment, :damage_done, :damage_received, :deaths, :disables_done, :disables_received, :emote, :energy_gained, :energy_used, :healing_done, :healing_received
6
+ attr_reader :kills, :mount, :outfit, :score, :side, :time_alive, :user_id, :relationships, :blob
7
+ alias_attribute :players, :player
8
+
9
+ def self.munch participant, relationships, blob
10
+ attr = participant["attributes"]
11
+ stats = attr["stats"]
12
+ p = self.new(
13
+ id: participant["id"],
14
+ actor: attr["actor"],
15
+ ability_uses: stats["abilityUses"],
16
+ attachment: stats["attachment"],
17
+ damage_done: stats["damageDone"],
18
+ damage_received: stats["damageReceived"],
19
+ deaths: stats["deaths"],
20
+ disables_done: stats["disablesDone"],
21
+ disables_received: stats["disablesReceived"],
22
+ emote: stats["emote"],
23
+ energy_gained: stats["energyGained"],
24
+ energy_used: stats["energyUsed"],
25
+ healing_done: stats["healingDone"],
26
+ healing_received: stats["healingReceived"],
27
+ kills: stats["kills"],
28
+ mount: stats["mount"],
29
+ outfit: stats["outfit"],
30
+ score: stats["score"],
31
+ side: stats["side"],
32
+ time_alive: stats["timeAlive"],
33
+ user_id: stats["userID"],
34
+ relationships: relationships,
35
+ blob: blob,
36
+ )
37
+ end
4
38
 
5
- attr_reader :actor, :ability_uses, :attachment, :damage_done, :damage_received, :deaths, :disables_done, :disables_received, :emote, :energy_gained, :energy_used, :healing_done, :healing_received
6
- attr_reader :kills, :mount, :outfit, :score, :side, :time_alive, :user_id, :relationships, :blob
7
-
8
- def self.munch participant, relationships, blob
9
- attr = participant["attributes"]
10
- stats = attr["stats"]
11
- p = self.new(
12
- id: participant["id"],
13
- actor: attr["actor"],
14
- ability_uses: stats["abilityUses"],
15
- attachment: stats["attachment"],
16
- damage_done: stats["damageDone"],
17
- damage_received: stats["damageReceived"],
18
- deaths: stats["deaths"],
19
- disables_done: stats["disablesDone"],
20
- disables_received: stats["disablesReceived"],
21
- emote: stats["emote"],
22
- energy_gained: stats["energyGained"],
23
- energy_used: stats["energyUsed"],
24
- healing_done: stats["healingDone"],
25
- healing_received: stats["healingReceived"],
26
- kills: stats["kills"],
27
- mount: stats["mount"],
28
- outfit: stats["outfit"],
29
- score: stats["score"],
30
- side: stats["side"],
31
- time_alive: stats["timeAlive"],
32
- user_id: stats["userID"],
33
- relationships: relationships,
34
- blob: blob,
35
- )
36
- end
37
-
38
- def initialize params
39
- params.each { |k, v| self.instance_variable_set :"@#{k}", v }
40
39
  end
41
40
 
42
41
  end
43
-
44
-
45
-
46
- end
@@ -22,10 +22,6 @@ module Battlerite
22
22
  raise NotImplementedError, "Battlerite API 1.0 has not implemented player data yet. Check http://battlerite-docs.readthedocs.io for more information."
23
23
  end
24
24
 
25
- def initialize params
26
- params.each { |k, v| self.instance_variable_set :"@#{k}", v }
27
- end
28
-
29
25
  end
30
26
 
31
27
  end
@@ -15,11 +15,6 @@ module Battlerite
15
15
  )
16
16
  end
17
17
 
18
- def initialize params
19
- params.each { |k, v| self.instance_variable_set :"@#{k}", v }
20
- end
21
-
22
18
  end
23
19
 
24
-
25
20
  end
@@ -10,14 +10,11 @@ module Battlerite
10
10
  duration: round["attributes"]["duration"],
11
11
  ordinal: round["attributes"]["ordinal"],
12
12
  winning_team: round["attributes"]["stats"]["winningTeam"],
13
+ relationships: relationships,
14
+ blob: blob,
13
15
  )
14
16
  end
15
17
 
16
- def initialize params
17
- params.each { |k, v| self.instance_variable_set :"@#{k}", v }
18
- end
19
-
20
18
  end
21
19
 
22
-
23
20
  end
@@ -6,7 +6,6 @@ module Battlerite
6
6
  def self.munch blob
7
7
  data = blob["data"].respond_to?(:to_ary) ? blob["data"] : [blob["data"]]
8
8
  data.each_with_object([]) do |datum, results|
9
- #results << self.public_send(:"munch_#{datum["type"].downcase}", datum, blob)
10
9
  results << self.munch_agnostic(datum, blob)
11
10
  end
12
11
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: battlerite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - eiko kokuma
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-30 00:00:00.000000000 Z
11
+ date: 2017-12-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: API wrapper for Battlerite
13
+ description: API wrapper for Battlerite, with convenient but flexible wrapper Models.
14
14
  email: kokumeiko@gmail.com
15
15
  executables: []
16
16
  extensions: []
@@ -47,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
47
  version: '0'
48
48
  requirements: []
49
49
  rubyforge_project:
50
- rubygems_version: 2.6.13
50
+ rubygems_version: 2.6.8
51
51
  signing_key:
52
52
  specification_version: 4
53
53
  summary: API wrapper for Battlerite