battlerite 0.0.1 → 0.1.1

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: 2f7a82859d01beb9daa5fbfd2c478a4b7bf11344
4
- data.tar.gz: 4266d52717767b3086612419abb3bf16102d2f45
3
+ metadata.gz: 582d85872b524d4fff8c3cbd74010f4cb9828fb2
4
+ data.tar.gz: '0382244b51c915dcf39f99ab0227fd99971776b9'
5
5
  SHA512:
6
- metadata.gz: 93d9122789fab95b2dc14fbc3e14cffd8865a767a78d4dbae1a0a85c41212adde3ba6882b6735c492816cc662e8428c13e574f7db81370c64669c65174dde001
7
- data.tar.gz: e2265be74aab8ffb4ea1e35f6f95e88b084c50a25dd981d5189a2b1311aacda229d147cb8757bce10fd6256f6cd97ab06bc502e344262546ff27e8bd55724a70
6
+ metadata.gz: 81092aa2b6da933b92b8f2e6cee6eff8a9892583b062c433561be4ecbe83da1b97e8e429932f02156167248ebdfbbd531ad2c2cd545eb0ac14d9dbd8000ad881
7
+ data.tar.gz: 8aa08afde2924bc364393595afb6d2ebad5ff518b46f7902dbcfb0e1bd4631408204d697c57e03a063949944f2b741f10859031e659153410db76470baa0d285
@@ -1,64 +1,35 @@
1
1
  require "net/http"
2
2
  require "json"
3
- require "pry"
3
+ require "time"
4
4
 
5
+ require_relative "battlerite/api"
6
+ require_relative "battlerite/muncher"
5
7
 
6
- class Battlerite
7
-
8
- def uris
9
- @uris ||= {
10
- matches: "https://api.dc01.gamelockerapp.com/shards/global/matches",
11
- match: "https://api.dc01.gamelockerapp.com/shards/global/matches/:match_id",
12
- players: "https://api.dc01.gamelockerapp.com/shards/global/players",
13
- player: "https://api.dc01.gamelockerapp.com/shards/na/players/:player_id",
14
- }
15
- end
16
-
17
- def get_matches opt={}
18
- uri = URI(uris[:matches])
19
- a = uri.query.nil? ? [] : URI.decode_www_form(uri.query)
20
- a << ["page[offset]", opt[:offset] || 0]
21
- a << ["page[limit]", opt[:limit] || 5]
22
- a << ["sort", opt[:sort] || "createdAt"]
23
- uri.query = URI.encode_www_form(a)
24
- get uri
25
- end
8
+ module Battlerite
26
9
 
27
- def get_match id
28
- uri = URI(uris[:match].gsub(":match_id", id))
29
- get uri
10
+ def self.new
11
+ Instance.new
30
12
  end
31
13
 
32
- def get_players opt={}
33
- uri = URI(uris[:players])
34
- a = uri.query.nil? ? [] : URI.decode_www_form(uri.query)
35
- a << ["filter[playerNames]", opt[:player_names]] unless opt[:player_names].nil?
36
- a << ["filter[playerIds]", opt[:player_ids]] unless opt[:player_ids].nil?
37
- uri.query = URI.encode_www_form(a)
38
- get uri
39
- end
14
+ class Instance
40
15
 
41
- def get_player id
42
- uri = URI(uris[:player].gsub(":player_id", id))
43
- get uri
44
- end
45
-
16
+ attr_reader :api
46
17
 
47
- def get uri
48
- req = Net::HTTP::Get.new(uri)
49
- req["Authorization"] = api_key
50
- req["Accept"] = "application/vnd.api+json"
18
+ def initialize
19
+ @api = Api.new
51
20
 
52
- res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
53
- http.request(req)
21
+ @api.public_methods(false).each do |method|
22
+ define_singleton_method method do |*args|
23
+ puts "sending #{method} with #{args}"
24
+ result = @api.send(method, *args)
25
+ puts "result: #{result}"
26
+ Muncher.munch @api.send(method, *args)
27
+ end
28
+ end
54
29
  end
55
-
56
- JSON.parse(res.body)
57
- end
58
-
59
- def api_key
60
- ENV["BATTLERITE_API_KEY"]
30
+
61
31
  end
62
32
 
63
33
  end
64
34
 
35
+
@@ -0,0 +1,66 @@
1
+
2
+
3
+ module Battlerite
4
+ class Api
5
+
6
+ def uris
7
+ @@uris ||= {
8
+ matches: "https://api.dc01.gamelockerapp.com/shards/global/matches",
9
+ match: "https://api.dc01.gamelockerapp.com/shards/global/matches/:match_id",
10
+ players: "https://api.dc01.gamelockerapp.com/shards/global/players",
11
+ player: "https://api.dc01.gamelockerapp.com/shards/na/players/:player_id",
12
+ }
13
+ end
14
+
15
+ def get_matches opt={}
16
+ uri = URI(uris[:matches])
17
+ a = uri.query.nil? ? [] : URI.decode_www_form(uri.query)
18
+ a << ["page[offset]", opt[:offset] || 0]
19
+ a << ["page[limit]", opt[:limit] || 5]
20
+ a << ["sort", opt[:sort] || "createdAt"]
21
+ a << ["filter[gameMode]", opt[:game_mode]] if opt[:game_mode]
22
+ a << ["filter[teamNames]", opt[:team_names]] if opt[:team_names]
23
+ uri.query = URI.encode_www_form(a)
24
+ get uri
25
+ end
26
+
27
+ def get_match id
28
+ uri = URI(uris[:match].gsub(":match_id", id))
29
+ get uri
30
+ end
31
+
32
+ def get_players opt={}
33
+ uri = URI(uris[:players])
34
+ a = uri.query.nil? ? [] : URI.decode_www_form(uri.query)
35
+ a << ["filter[playerNames]", opt[:player_names]] unless opt[:player_names].nil?
36
+ a << ["filter[playerIds]", opt[:player_ids]] unless opt[:player_ids].nil?
37
+ uri.query = URI.encode_www_form(a)
38
+ get uri
39
+ end
40
+
41
+ def get_player id
42
+ uri = URI(uris[:player].gsub(":player_id", id))
43
+ get uri
44
+ end
45
+
46
+
47
+ def get uri
48
+ req = Net::HTTP::Get.new(uri)
49
+ req["Authorization"] = api_key
50
+ req["Accept"] = "application/vnd.api+json"
51
+
52
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
53
+ http.request(req)
54
+ end
55
+
56
+ raise Battlerite::ApiError.new(res) if res.code.to_i >= 400
57
+
58
+ JSON.parse(res.body)
59
+ end
60
+
61
+ def api_key
62
+ ENV["BATTLERITE_API_KEY"]
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,25 @@
1
+
2
+ module Battlerite
3
+
4
+ class DuplicateRecordError < StandardError
5
+
6
+ attr_reader :id, :records
7
+
8
+ def initialize data, msg="Two or more records exist with the same ID."
9
+ @id = data[:id]
10
+ @records = data[:records]
11
+ end
12
+
13
+ end
14
+
15
+ class ApiError < StandardError
16
+
17
+ attr_reader :res
18
+
19
+ def initialize res, msg="Error received from Battlerite API"
20
+ @res = res
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,20 @@
1
+ module Battlerite
2
+
3
+ class Battlerite::Model
4
+ attr_reader :id
5
+
6
+ def to_s
7
+ "#{self.class.name.split("::").last}: #{id[0,5]}..."
8
+ end
9
+
10
+ def to_s_tree
11
+ str = "#{to_s}"
12
+ relationships.each { |k, v| v.each { |d| str += "\n├────#{d.to_s_tree.gsub("\n", "\n│ ")}" } } if respond_to? :relationships
13
+ str
14
+ end
15
+
16
+ end
17
+
18
+ Dir[File.join(File.dirname(__FILE__), "models", "*.rb")].each { |file| require file }
19
+
20
+ end
@@ -0,0 +1,23 @@
1
+ module Battlerite
2
+
3
+ class Asset < Battlerite::Model
4
+ attr_reader :created, :description, :name, :blob
5
+
6
+ def self.munch asset, relationships, blob
7
+ a = self.new(
8
+ id: asset["id"],
9
+ created: Time.parse(asset["attributes"]["createdAt"]),
10
+ description: asset["attributes"]["description"],
11
+ name: asset["attributes"]["name"],
12
+ blob: blob,
13
+ )
14
+ end
15
+
16
+ def initialize params
17
+ params.each { |k, v| self.instance_variable_set :"@#{k}", v }
18
+ end
19
+
20
+ end
21
+
22
+
23
+ end
@@ -0,0 +1,32 @@
1
+ module Battlerite
2
+
3
+ class Match < Battlerite::Model
4
+
5
+ attr_reader :started, :duration, :finished, :game_mode, :map, :match_type, :relationships, :blob
6
+
7
+ def self.munch match, relationships, blob
8
+ started = Time.parse match["attributes"]["createdAt"]
9
+ duration = match["attributes"]["duration"]
10
+ finished = started + duration
11
+ m = self.new(
12
+ id: match["id"],
13
+ started: started,
14
+ duration: duration,
15
+ finished: finished,
16
+ game_mode: match["attributes"]["gameMode"],
17
+ map: match["attributes"]["stats"]["mapID"],
18
+ match_type: match["attributes"]["stats"]["type"],
19
+ relationships: relationships,
20
+ blob: blob,
21
+ )
22
+ end
23
+
24
+
25
+ def initialize params
26
+ params.each { |k, v| self.instance_variable_set :"@#{k}", v }
27
+ end
28
+
29
+ end
30
+
31
+
32
+ end
@@ -0,0 +1,46 @@
1
+ module Battlerite
2
+
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
+
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
+ end
41
+
42
+ end
43
+
44
+
45
+
46
+ end
@@ -0,0 +1,31 @@
1
+ module Battlerite
2
+
3
+ class Player < Battlerite::Model
4
+
5
+ attr_reader :name, :stats, :relationships, :blob
6
+
7
+ def self.munch player, relationships, blob
8
+ p = self.new(
9
+ id: player["id"],
10
+ name: player["attributes"]["name"],
11
+ stats: player["attributes"]["stats"],
12
+ relationships: relationships,
13
+ blob: blob
14
+ )
15
+ end
16
+
17
+ def name
18
+ raise NotImplementedError, "Battlerite API 1.0 has not implemented player data yet. Check http://battlerite-docs.readthedocs.io for more information."
19
+ end
20
+
21
+ def stats
22
+ raise NotImplementedError, "Battlerite API 1.0 has not implemented player data yet. Check http://battlerite-docs.readthedocs.io for more information."
23
+ end
24
+
25
+ def initialize params
26
+ params.each { |k, v| self.instance_variable_set :"@#{k}", v }
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,25 @@
1
+ module Battlerite
2
+
3
+ class Roster < Battlerite::Model
4
+
5
+ attr_reader :score, :won, :relationships, :blob
6
+ alias_method :won?, :won
7
+
8
+ def self.munch roster, relationships, blob
9
+ r = self.new(
10
+ id: roster["id"],
11
+ score: roster["attributes"]["stats"]["score"],
12
+ won: roster["attributes"]["stats"]["won"] == "true",
13
+ relationships: relationships,
14
+ blob: blob,
15
+ )
16
+ end
17
+
18
+ def initialize params
19
+ params.each { |k, v| self.instance_variable_set :"@#{k}", v }
20
+ end
21
+
22
+ end
23
+
24
+
25
+ end
@@ -0,0 +1,23 @@
1
+ module Battlerite
2
+
3
+ class Round < Battlerite::Model
4
+ attr_reader :duration, :ordinal, :winning_team
5
+ alias_method :number, :ordinal
6
+
7
+ def self.munch round, relationships, blob
8
+ r = self.new(
9
+ id: round["id"],
10
+ duration: round["attributes"]["duration"],
11
+ ordinal: round["attributes"]["ordinal"],
12
+ winning_team: round["attributes"]["stats"]["winningTeam"],
13
+ )
14
+ end
15
+
16
+ def initialize params
17
+ params.each { |k, v| self.instance_variable_set :"@#{k}", v }
18
+ end
19
+
20
+ end
21
+
22
+
23
+ end
@@ -0,0 +1,32 @@
1
+ require_relative "model"
2
+
3
+ module Battlerite
4
+ class Muncher
5
+
6
+ def self.munch blob
7
+ data = blob["data"].respond_to?(:to_ary) ? blob["data"] : [blob["data"]]
8
+ data.each_with_object([]) do |datum, results|
9
+ #results << self.public_send(:"munch_#{datum["type"].downcase}", datum, blob)
10
+ results << self.munch_agnostic(datum, blob)
11
+ end
12
+ end
13
+
14
+ def self.munch_agnostic record, blob
15
+ relationships = Hash.new { |h, k| h[k] = [] }
16
+ record["relationships"].each do |rel, value|
17
+ data = value["data"].nil? ? [] : value["data"]
18
+ data = data.respond_to?(:to_ary) ? data : [data]
19
+ relationships[rel.to_sym] += data.map { |datum| munch_included(datum["id"], blob) }
20
+ end unless record["relationships"].nil?
21
+
22
+ Battlerite.const_get(record["type"].capitalize).munch record, relationships, blob
23
+ end
24
+
25
+ def self.munch_included id, blob
26
+ findings = blob["included"].find_all { |item| item["id"] == id }
27
+ raise DuplicateRecordError.new id: id, records: findings if findings.count > 1
28
+ self.munch_agnostic findings[0], blob
29
+ end
30
+
31
+ end
32
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: battlerite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.1
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-28 00:00:00.000000000 Z
11
+ date: 2017-11-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: API wrapper for Battlerite
14
14
  email: kokumeiko@gmail.com
@@ -17,6 +17,16 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - lib/battlerite.rb
20
+ - lib/battlerite/api.rb
21
+ - lib/battlerite/errors.rb
22
+ - lib/battlerite/model.rb
23
+ - lib/battlerite/models/asset.rb
24
+ - lib/battlerite/models/match.rb
25
+ - lib/battlerite/models/participant.rb
26
+ - lib/battlerite/models/player.rb
27
+ - lib/battlerite/models/roster.rb
28
+ - lib/battlerite/models/round.rb
29
+ - lib/battlerite/muncher.rb
20
30
  homepage: https://gitlab.com/eiko/battlerite-ruby
21
31
  licenses:
22
32
  - MIT