brawlhalla-api 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3b0672936f9392289c88a4b6cf86774a9b7af81c8aa30afcac9f2cd99dc0f78d
4
- data.tar.gz: bc7f5ccb7eb3c40c94d591bb6a97e0a804b17b2fc6eb553d3cfc781373d8825a
3
+ metadata.gz: 825dbd3cb6b62b94a63c9af8423dc8b036a618c5948b900080ccc6a1080195fe
4
+ data.tar.gz: 6cb7df461f6a2f96f73211fc1fd1dde428055f697319d20527baa60b1174850c
5
5
  SHA512:
6
- metadata.gz: 0b7bb22aaddcee75c0fa889094c9245764c4ec4a7e76632e651ed7720dd9f7e301c785f09ca959483239cfd2ed4d9a50c9c6672bd401dde3c5e666e2fe3b25a7
7
- data.tar.gz: 3f016cdae9736d6f5939f1321ef416f7cd734ca653c0d845a4101aaa4cf02817140ba8382e4d79898219788396a315f28ebf4304cfaf1da29d3bdf667c262e28
6
+ metadata.gz: 8c55b1c15581b13fe99836672a41e4e6ad52838f41aecf64da05d6f4b9ea54d50c8880750916da0d6352ae6e2e0c83f22b8e4007b93b3683886dd42e8e903499
7
+ data.tar.gz: 85d551b32404f87360bbf0e88962f59b18c63168839ff53e3ca051af4f2f578996aea4324f7eea4b8df7f2ff5d5a7fd47f1ff965f0ba013d079ef88accc6c891
data/README.md CHANGED
@@ -20,40 +20,96 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
+ Before using Brawlhalla::API you need to set it up with a valid API key.
24
+
25
+ ```ruby
26
+ Brawlhalla::API.configure do |config|
27
+ config.api_key = 'YOUR_API_KEY_HERE'
28
+ config.debug = false # set to true for debugging
29
+ end
30
+ ```
31
+
23
32
  ### Search By Steam ID
24
33
 
25
34
  A Player can be looked up by steam ID. This returns the player’s name and brawlhalla_id.
26
35
 
27
- _(A player’s steamid in format steamID64 (ex 76561198025185087).)_
36
+ _(A player’s steamid in format steamID64 (ex 76561198048321884).)_
28
37
 
29
38
  ```ruby
30
- bh = Brawlhalla::API::Client.new('YOUR_API_KEY_HERE')
31
- bh.search('PLAYER_STEAM_ID')
39
+ Brawlhalla::API.search('76561198048321884')
40
+ # => #<Brawlhalla::API::PlayerSearch:0x00007fa9db937a10 @brawlhalla_id=8817417, @name="Rikas">
32
41
  ```
33
42
 
34
43
  ### Players
35
44
 
36
45
  #### Stats
37
46
 
38
- This endpoint retrieves all stats about a player
47
+ This endpoint retrieves all stats about a player, given a `brawlhalla_id`. It will return a new
48
+ `Brawlhalla::API::Player` instance with all the stats as attributes.
39
49
 
40
50
  ```ruby
41
- bh = Brawlhalla::API::Client.new('YOUR_API_KEY_HERE')
42
- bh.stats('PLAYER_BRAWLHALLA_ID')
51
+ player = Brawlhalla::API::Player.find('8817417')
52
+ # => #<Brawlhalla::API::Player:0x00007f9642b71350 ...>
53
+
54
+ player.games # => 1839
55
+ player.brawlhalla_id # => 8817417
56
+ player.damagebomb # => 5516
57
+ player.kobomb # => 47
58
+ ```
59
+
60
+ You also get individual stats (`Brawlhalla::API::LegendStat` instance) for each legend.
61
+
62
+ ```ruby
63
+ player.legend_stats.size # => 42
64
+
65
+ scarlet_stats = player.legend_stats.first
66
+
67
+ scarlet_stats.damagedealt # => 4481
68
+ scarlet_stats.damagegadgets # => 53
69
+ scarlet_stats.games # => 45
43
70
  ```
44
71
 
45
72
  #### Ranked
46
73
 
47
- This endpoint retrieves ranked data about a player.
74
+ This endpoint retrieves ranked data about a player. It receives one argument - the brawlhalla_id of
75
+ the player.
76
+
77
+ ```ruby
78
+ ranked = Ranking.find('8817417')
79
+ ranked.wins # => 172
80
+ ranked.region # => "EU"
81
+ ranked.rating # => 1394
82
+ ranked.peak_rating # => 1415
83
+ ```
84
+
85
+ **Note**: You can also get the ranked data if you already have a `Brawlhalla::API::Player` instance
86
+ by calling the `#ranking` method. Keep in mind that this will also trigger an API call:
87
+
88
+ ```ruby
89
+ player = Brawlhalla::API::Player.find('8817417')
90
+ player.ranking #=> All the ranked data like the one you get with `Brawlhalla::API::Ranked.find`
91
+ ```
92
+
93
+ You can check each legend's rankings by calling the `#legend_rankings` method. This will return an
94
+ array of `Brawlhalla::API::LegendRanking` instances:
48
95
 
49
96
  ```ruby
50
- bh = Brawlhalla::API::Client.new('YOUR_API_KEY_HERE')
51
- bh.ranked('PLAYER_BRAWLHALLA_ID')
97
+ ranked = Ranking.find('8817417')
98
+ legend_rankings = ranked.legend_rankings # Array of LegendRanking instances
99
+ legend_rankings.size # => 10
100
+
101
+ bodvar_ranking = legend_rankings.first
102
+ bodvar_ranking.games # => 31
103
+ bodvar_ranking.peak_rating # => 1200
104
+ bodvar.rating # => 1199
105
+ bodvar_ranking.tier # => "Silver 2"
52
106
  ```
53
107
 
54
108
  ## Development
55
109
 
56
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
110
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run
111
+ the tests. You can also run `bin/console` for an interactive prompt that will allow you to
112
+ experiment.
57
113
 
58
114
  ## Contributing
59
115
 
data/bin/console CHANGED
@@ -8,8 +8,13 @@ require 'brawlhalla/api'
8
8
  require 'dotenv'
9
9
  Dotenv.load
10
10
 
11
- # Uncomment this line after adding your API key to .env file to use the bh client in the console.
12
- bh = Brawlhalla::API::Client.new(ENV['API_KEY'])
11
+ Brawlhalla::API.configure do |config|
12
+ config.api_key = ENV['API_KEY']
13
+ config.debug = false
14
+ end
15
+
16
+ # So we can use Player, Clan, Client, etc. without namespacing every time.
17
+ self.send(:include, Brawlhalla::API)
13
18
 
14
19
  require 'pry'
15
20
  Pry.start
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Brawlhalla
4
+ module API
5
+ class Clan
6
+ ATTRIBUTES = %i[
7
+ clan_name clan_id clan_xp personal_xp
8
+ ]
9
+
10
+ attr_accessor *ATTRIBUTES
11
+ alias name clan_name
12
+ alias id clan_id
13
+ alias xp clan_xp
14
+
15
+ def initialize(json)
16
+ ATTRIBUTES.each do |attr|
17
+ send("#{attr}=", json[attr])
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Brawlhalla
4
+ module API
5
+ class Configuration
6
+ attr_accessor :api_key, :debug
7
+
8
+ def initialize
9
+ @api_key = ''
10
+ @debug = false
11
+ end
12
+
13
+ def debug?
14
+ @debug || false
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Brawlhalla
4
+ module API
5
+ class DoublesRanking
6
+ ATTRIBUTES = %i[
7
+ brawlhalla_id_one brawlhalla_id_two rating peak_rating tier wins games teamname region
8
+ global_rank
9
+ ]
10
+
11
+ attr_accessor *ATTRIBUTES
12
+
13
+ def initialize(json)
14
+ ATTRIBUTES.each do |attr|
15
+ send("#{attr}=", json[attr])
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Brawlhalla
4
+ module API
5
+ class LegendRanking
6
+ ATTRIBUTES = %i[legend_id legend_name_key rating peak_rating tier wins games]
7
+
8
+ attr_accessor *ATTRIBUTES
9
+
10
+ def initialize(json)
11
+ ATTRIBUTES.each do |attr|
12
+ send("#{attr}=", json[attr])
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Brawlhalla
4
+ module API
5
+ class LegendStat
6
+ ATTRIBUTES = %i[
7
+ legend_id legend_name_key damagedealt damagetaken kos falls suicides teamkos matchtime games
8
+ wins damageunarmed damagethrownitem damageweaponone damageweapontwo damagegadgets kounarmed
9
+ kothrownitem koweaponone koweapontwo kogadgets timeheldweaponone timeheldweapontwo xp level
10
+ xp_percentage
11
+ ]
12
+
13
+ attr_accessor *ATTRIBUTES
14
+
15
+ def initialize(json)
16
+ ATTRIBUTES.each do |attr|
17
+ value = json[attr]
18
+ value = value.to_i if attr.to_s.start_with?('damage')
19
+
20
+ send("#{attr}=", value)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'brawlhalla/api/legend_stat'
4
+ require 'brawlhalla/api/clan'
5
+ require 'brawlhalla/api/ranking'
6
+
7
+ module Brawlhalla
8
+ module API
9
+ class Player
10
+ ATTRIBUTES = %i[
11
+ brawlhalla_id name xp level xp_percentage games wins damagebomb damagespikeball
12
+ damagesidekick hitsnowball kobomb komine kospikeball kosnowball
13
+ ]
14
+
15
+ attr_accessor *ATTRIBUTES
16
+ attr_reader :legend_stats, :clan
17
+
18
+ def self.find(brawlhalla_id)
19
+ stats_json = API.client.stats(brawlhalla_id)
20
+
21
+ new(stats_json)
22
+ end
23
+
24
+ def initialize(json)
25
+ ATTRIBUTES.each do |attr|
26
+ value = json[attr]
27
+ value = value.to_i if attr.to_s.start_with?('damage')
28
+
29
+ send("#{attr}=", value)
30
+ end
31
+
32
+ initialize_clan(json)
33
+ initialize_legends(json)
34
+ end
35
+
36
+ def ranking
37
+ @ranking ||= Ranking.find(brawlhalla_id)
38
+ end
39
+
40
+ private
41
+
42
+ # For some odd reason the damage values come as strings instead of integers.
43
+ def sanitalize_damage_values
44
+ @damagebomb = @damagebomb.to_i
45
+ @damagesidekick = @damagesidekick.to_i
46
+ @damagespikeball = @damagespikeball.to_i
47
+ end
48
+
49
+ def initialize_clan(json)
50
+ return unless json[:clan]
51
+
52
+ @clan = Clan.new(json[:clan])
53
+ end
54
+
55
+ def initialize_legends(json)
56
+ return unless json[:legends]
57
+
58
+ @legend_stats = json[:legends].map { |legend_json| LegendStat.new(legend_json) }
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Brawlhalla
4
+ module API
5
+ class PlayerSearch
6
+ attr_accessor :name, :brawlhalla_id
7
+
8
+ def initialize(json)
9
+ @name = json[:name]
10
+ @brawlhalla_id = json[:brawlhalla_id]
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'brawlhalla/api/legend_ranking'
4
+ require 'brawlhalla/api/doubles_ranking'
5
+
6
+ module Brawlhalla
7
+ module API
8
+ class Ranking
9
+ ATTRIBUTES = %i[
10
+ name brawlhalla_id rating peak_rating tier wins games region global_rank region_rank
11
+ ]
12
+
13
+ attr_accessor *ATTRIBUTES
14
+ attr_reader :legend_rankings, :doubles_rankings
15
+
16
+ def self.find(brawlhalla_id)
17
+ ranking_json = API.client.ranked(brawlhalla_id)
18
+ new(ranking_json)
19
+ end
20
+
21
+ def initialize(json)
22
+ ATTRIBUTES.each do |attr|
23
+ send("#{attr}=", json[attr])
24
+ end
25
+
26
+ @legend_rankings = json[:legends].map { |legend_json| LegendRanking.new(legend_json) }
27
+ @doubles_rankings = json[:"2v2"].map { |doubles_json| DoublesRanking.new(doubles_json) }
28
+ end
29
+ end
30
+ end
31
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Brawlhalla
4
4
  module API
5
- VERSION = '0.2.0'
5
+ VERSION = '0.3.0'
6
6
  end
7
7
  end
@@ -1,12 +1,35 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'brawlhalla/api/version'
4
+ require 'brawlhalla/api/configuration'
4
5
  require 'brawlhalla/api/client'
6
+ require 'brawlhalla/api/player'
7
+ require 'brawlhalla/api/player_search'
8
+
5
9
  require 'faraday'
6
10
  require 'faraday_middleware'
7
11
  require 'addressable'
8
12
 
9
13
  module Brawlhalla
10
14
  module API
15
+ module_function
16
+
17
+ def configuration
18
+ @configuration ||= Configuration.new
19
+ end
20
+
21
+ def client
22
+ @client = Client.new(configuration.api_key, debug: configuration.debug?)
23
+ end
24
+
25
+ def configure
26
+ yield(configuration)
27
+ end
28
+
29
+ def search(steam_id)
30
+ search_json = client.search(steam_id)
31
+
32
+ PlayerSearch.new(search_json)
33
+ end
11
34
  end
12
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brawlhalla-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - rikas
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-29 00:00:00.000000000 Z
11
+ date: 2018-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -198,7 +198,15 @@ files:
198
198
  - bin/setup
199
199
  - brawlhalla-api.gemspec
200
200
  - lib/brawlhalla/api.rb
201
+ - lib/brawlhalla/api/clan.rb
201
202
  - lib/brawlhalla/api/client.rb
203
+ - lib/brawlhalla/api/configuration.rb
204
+ - lib/brawlhalla/api/doubles_ranking.rb
205
+ - lib/brawlhalla/api/legend_ranking.rb
206
+ - lib/brawlhalla/api/legend_stat.rb
207
+ - lib/brawlhalla/api/player.rb
208
+ - lib/brawlhalla/api/player_search.rb
209
+ - lib/brawlhalla/api/ranking.rb
202
210
  - lib/brawlhalla/api/version.rb
203
211
  homepage: https://github.com/rikas/brawlhalla-api
204
212
  licenses: