blizzard_api_rb_rb 3.4.2
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 +7 -0
- data/.github/workflows/ruby.yml +22 -0
- data/.gitignore +12 -0
- data/.rubocop.yml +18 -0
- data/.rubocop_todo.yml +7 -0
- data/CHANGELOG.md +269 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +50 -0
- data/LICENSE.txt +21 -0
- data/README.md +200 -0
- data/Rakefile +12 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/blizzard_api_rb.gemspec +44 -0
- data/lib/blizzard_api_rb/api_response.rb +27 -0
- data/lib/blizzard_api_rb/api_standards.rb +55 -0
- data/lib/blizzard_api_rb/configuration.rb +93 -0
- data/lib/blizzard_api_rb/diablo/community/act.rb +35 -0
- data/lib/blizzard_api_rb/diablo/community/artisan.rb +38 -0
- data/lib/blizzard_api_rb/diablo/community/character_class.rb +38 -0
- data/lib/blizzard_api_rb/diablo/community/follower.rb +25 -0
- data/lib/blizzard_api_rb/diablo/community/item.rb +25 -0
- data/lib/blizzard_api_rb/diablo/community/item_type.rb +35 -0
- data/lib/blizzard_api_rb/diablo/community/profile.rb +83 -0
- data/lib/blizzard_api_rb/diablo/game_data/era.rb +21 -0
- data/lib/blizzard_api_rb/diablo/game_data/generic_data_endpoint.rb +58 -0
- data/lib/blizzard_api_rb/diablo/game_data/season.rb +21 -0
- data/lib/blizzard_api_rb/diablo/request.rb +15 -0
- data/lib/blizzard_api_rb/diablo.rb +91 -0
- data/lib/blizzard_api_rb/exception.rb +14 -0
- data/lib/blizzard_api_rb/hearthstone/game_data/back.rb +59 -0
- data/lib/blizzard_api_rb/hearthstone/game_data/card.rb +111 -0
- data/lib/blizzard_api_rb/hearthstone/game_data/deck.rb +30 -0
- data/lib/blizzard_api_rb/hearthstone/game_data/generic_data_endpoint.rb +49 -0
- data/lib/blizzard_api_rb/hearthstone/game_data/metadata.rb +22 -0
- data/lib/blizzard_api_rb/hearthstone/request.rb +15 -0
- data/lib/blizzard_api_rb/hearthstone.rb +49 -0
- data/lib/blizzard_api_rb/overwatch_league/community/matches.rb +22 -0
- data/lib/blizzard_api_rb/overwatch_league/community/players.rb +22 -0
- data/lib/blizzard_api_rb/overwatch_league/community/segments.rb +22 -0
- data/lib/blizzard_api_rb/overwatch_league/community/summary.rb +22 -0
- data/lib/blizzard_api_rb/overwatch_league/community/teams.rb +22 -0
- data/lib/blizzard_api_rb/overwatch_league/request.rb +15 -0
- data/lib/blizzard_api_rb/overwatch_league.rb +50 -0
- data/lib/blizzard_api_rb/request.rb +189 -0
- data/lib/blizzard_api_rb/starcraft/community/account.rb +23 -0
- data/lib/blizzard_api_rb/starcraft/community/ladder.rb +34 -0
- data/lib/blizzard_api_rb/starcraft/community/legacy.rb +87 -0
- data/lib/blizzard_api_rb/starcraft/community/profile.rb +76 -0
- data/lib/blizzard_api_rb/starcraft/game_data/league.rb +27 -0
- data/lib/blizzard_api_rb/starcraft/request.rb +36 -0
- data/lib/blizzard_api_rb/starcraft.rb +57 -0
- data/lib/blizzard_api_rb/token_manager.rb +49 -0
- data/lib/blizzard_api_rb/version.rb +6 -0
- data/lib/blizzard_api_rb/wow/game_data/achievement.rb +46 -0
- data/lib/blizzard_api_rb/wow/game_data/auction.rb +50 -0
- data/lib/blizzard_api_rb/wow/game_data/azerite_essence.rb +30 -0
- data/lib/blizzard_api_rb/wow/game_data/connected_realm.rb +18 -0
- data/lib/blizzard_api_rb/wow/game_data/covenant.rb +72 -0
- data/lib/blizzard_api_rb/wow/game_data/creature.rb +102 -0
- data/lib/blizzard_api_rb/wow/game_data/generic_data_endpoint.rb +50 -0
- data/lib/blizzard_api_rb/wow/game_data/guild_crest.rb +48 -0
- data/lib/blizzard_api_rb/wow/game_data/heirloom.rb +16 -0
- data/lib/blizzard_api_rb/wow/game_data/item.rb +102 -0
- data/lib/blizzard_api_rb/wow/game_data/journal.rb +110 -0
- data/lib/blizzard_api_rb/wow/game_data/media.rb +26 -0
- data/lib/blizzard_api_rb/wow/game_data/modified_crafting.rb +60 -0
- data/lib/blizzard_api_rb/wow/game_data/mount.rb +18 -0
- data/lib/blizzard_api_rb/wow/game_data/mythic_keystone.rb +86 -0
- data/lib/blizzard_api_rb/wow/game_data/mythic_keystone_affix.rb +28 -0
- data/lib/blizzard_api_rb/wow/game_data/mythic_keystone_leaderboard.rb +48 -0
- data/lib/blizzard_api_rb/wow/game_data/mythic_raid_leaderboard.rb +28 -0
- data/lib/blizzard_api_rb/wow/game_data/pet.rb +62 -0
- data/lib/blizzard_api_rb/wow/game_data/playable_class.rb +39 -0
- data/lib/blizzard_api_rb/wow/game_data/playable_race.rb +16 -0
- data/lib/blizzard_api_rb/wow/game_data/playable_specialization.rb +65 -0
- data/lib/blizzard_api_rb/wow/game_data/power_type.rb +16 -0
- data/lib/blizzard_api_rb/wow/game_data/profession.rb +65 -0
- data/lib/blizzard_api_rb/wow/game_data/pvp_region.rb +82 -0
- data/lib/blizzard_api_rb/wow/game_data/pvp_season.rb +53 -0
- data/lib/blizzard_api_rb/wow/game_data/pvp_tier.rb +26 -0
- data/lib/blizzard_api_rb/wow/game_data/quest.rb +79 -0
- data/lib/blizzard_api_rb/wow/game_data/realm.rb +18 -0
- data/lib/blizzard_api_rb/wow/game_data/region.rb +16 -0
- data/lib/blizzard_api_rb/wow/game_data/reputation.rb +74 -0
- data/lib/blizzard_api_rb/wow/game_data/spell.rb +38 -0
- data/lib/blizzard_api_rb/wow/game_data/talent.rb +75 -0
- data/lib/blizzard_api_rb/wow/game_data/tech_talent.rb +50 -0
- data/lib/blizzard_api_rb/wow/game_data/title.rb +16 -0
- data/lib/blizzard_api_rb/wow/game_data/toy.rb +16 -0
- data/lib/blizzard_api_rb/wow/game_data/wow_token.rb +20 -0
- data/lib/blizzard_api_rb/wow/profile/account_profile.rb +97 -0
- data/lib/blizzard_api_rb/wow/profile/character_profile.rb +415 -0
- data/lib/blizzard_api_rb/wow/profile/guild.rb +75 -0
- data/lib/blizzard_api_rb/wow/request.rb +16 -0
- data/lib/blizzard_api_rb/wow/search/search_composer.rb +97 -0
- data/lib/blizzard_api_rb/wow/search/search_request.rb +24 -0
- data/lib/blizzard_api_rb/wow/slug.rb +12 -0
- data/lib/blizzard_api_rb/wow.rb +335 -0
- data/lib/blizzard_api_rb.rb +26 -0
- metadata +237 -0
@@ -0,0 +1,189 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
##
|
4
|
+
# @!macro [new] init_options
|
5
|
+
# @param [Hash] options
|
6
|
+
# @option options [String] :region API Region
|
7
|
+
# @option options [Symbol] :mode API Mode
|
8
|
+
|
9
|
+
##
|
10
|
+
# @!macro [new] request_options
|
11
|
+
# @param {Hash} options You can specify some options
|
12
|
+
# @option options [String] :locale Overrides the default locale for a single call
|
13
|
+
# @option options [String] :namespace Overrides the default namespace for a single call
|
14
|
+
# @option options [String] :access_token Overrides the access_token for a single call
|
15
|
+
# @option options [Boolean] :ignore_cache If set to true the request will not use the cache
|
16
|
+
# @option options [Integer] :ttl Override the default time (in seconds) a request should be cached
|
17
|
+
# @option options [DateTime] :since Adds the If-modified-since headers. Will always ignore cache when set.
|
18
|
+
|
19
|
+
##
|
20
|
+
# @!macro [new] response
|
21
|
+
# @return [Hash] API Response. The actual type of the returned object depends on the *format* option
|
22
|
+
# in the configuration module
|
23
|
+
|
24
|
+
module BlizzardApi
|
25
|
+
##
|
26
|
+
# Simplifies the requests to Blizzard APIS
|
27
|
+
class Request
|
28
|
+
include ApiStandards
|
29
|
+
|
30
|
+
# One hour cache
|
31
|
+
CACHE_HOUR = 3600
|
32
|
+
# One day cache
|
33
|
+
CACHE_DAY = 24 * CACHE_HOUR
|
34
|
+
# Three (commercial) months cache
|
35
|
+
CACHE_TRIMESTER = CACHE_DAY * 90
|
36
|
+
|
37
|
+
##
|
38
|
+
# @!attribute region
|
39
|
+
# @return [String] Api region
|
40
|
+
attr_accessor :region
|
41
|
+
|
42
|
+
##
|
43
|
+
# @!attribute mode
|
44
|
+
# @return [:regular, :extended]
|
45
|
+
attr_accessor :mode
|
46
|
+
|
47
|
+
##
|
48
|
+
# @!macro init_options
|
49
|
+
def initialize(**options)
|
50
|
+
self.region = options[:region] || BlizzardApi.region
|
51
|
+
# Use the shared access_token, or create one if it doesn't exists. This avoids unnecessary calls to create tokens.
|
52
|
+
create_access_token if BlizzardApi.access_token_expired?
|
53
|
+
|
54
|
+
# Mode
|
55
|
+
@mode = options[:mode] || BlizzardApi.mode
|
56
|
+
end
|
57
|
+
|
58
|
+
require 'net/http'
|
59
|
+
require 'uri'
|
60
|
+
require 'json'
|
61
|
+
require 'redis'
|
62
|
+
|
63
|
+
protected
|
64
|
+
|
65
|
+
def create_access_token
|
66
|
+
return if BlizzardApi.restore_access_token
|
67
|
+
|
68
|
+
uri = URI.parse("https://#{BlizzardApi.region}.battle.net/oauth/token")
|
69
|
+
|
70
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
71
|
+
http.use_ssl = true
|
72
|
+
|
73
|
+
request = Net::HTTP::Post.new(uri.path)
|
74
|
+
request.basic_auth(BlizzardApi.app_id, BlizzardApi.app_secret)
|
75
|
+
request['Content-Type'] = 'application/x-www-form-urlencoded'
|
76
|
+
request.set_form_data grant_type: 'client_credentials'
|
77
|
+
|
78
|
+
response = http.request(request)
|
79
|
+
BlizzardApi.save_access_token(JSON.parse(response.body))
|
80
|
+
end
|
81
|
+
|
82
|
+
def request(url, **options)
|
83
|
+
# Creates the whole url for request
|
84
|
+
parsed_url = URI.parse(url)
|
85
|
+
data = using_cache?(options) ? find_in_cache(parsed_url.to_s) : nil
|
86
|
+
|
87
|
+
# If data was found that means cache is enabled and valid
|
88
|
+
return prepare_response data if data
|
89
|
+
|
90
|
+
response = consume_api parsed_url, **options
|
91
|
+
|
92
|
+
handle_cache_on_response parsed_url, response, **options
|
93
|
+
end
|
94
|
+
|
95
|
+
def handle_cache_on_response(parsed_url, response, **options)
|
96
|
+
case response.code.to_i
|
97
|
+
when 304
|
98
|
+
response_data = nil
|
99
|
+
when 200
|
100
|
+
response_data = response.body
|
101
|
+
else
|
102
|
+
return nil, response
|
103
|
+
end
|
104
|
+
|
105
|
+
save_in_cache parsed_url.to_s, response.body, options[:ttl] || CACHE_DAY if using_cache? options
|
106
|
+
prepare_response response_data, response
|
107
|
+
end
|
108
|
+
|
109
|
+
def api_request(uri, **query_string)
|
110
|
+
# List of request options
|
111
|
+
options_key = %i[ignore_cache ttl format access_token namespace classic classic1x headers since]
|
112
|
+
|
113
|
+
# Separates request options from api fields and options. Any user-defined option will be treated as api field.
|
114
|
+
options = query_string.select { |k, _v| query_string.delete(k) || true if options_key.include? k }
|
115
|
+
|
116
|
+
# Namespace
|
117
|
+
query_string[:namespace] = endpoint_namespace(options) if options.include? :namespace
|
118
|
+
|
119
|
+
# In case uri already have query string parameters joins them with &
|
120
|
+
if query_string.size.positive?
|
121
|
+
query_string = URI.encode_www_form(query_string)
|
122
|
+
uri = uri.include?('?') ? "#{uri}&#{query_string}" : "#{uri}?#{query_string}"
|
123
|
+
end
|
124
|
+
|
125
|
+
request uri, **options
|
126
|
+
end
|
127
|
+
|
128
|
+
private
|
129
|
+
|
130
|
+
##
|
131
|
+
# Resolves the response based on the mode
|
132
|
+
def prepare_response(data, response = nil)
|
133
|
+
parsed_data = data.nil? ? data : JSON.parse(data, symbolize_names: true)
|
134
|
+
|
135
|
+
return parsed_data unless mode.eql? :extended
|
136
|
+
|
137
|
+
response ||= ApiResponse.new(data)
|
138
|
+
|
139
|
+
[response, parsed_data]
|
140
|
+
end
|
141
|
+
|
142
|
+
##
|
143
|
+
# @param options [Hash] Request options
|
144
|
+
def using_cache?(options)
|
145
|
+
return false if options.key?(:since)
|
146
|
+
|
147
|
+
!options.fetch(:ignore_cache, false)
|
148
|
+
end
|
149
|
+
|
150
|
+
def http_connection(url)
|
151
|
+
Net::HTTP.new(url.host, url.port).tap do |http|
|
152
|
+
http.use_ssl = true
|
153
|
+
http.keep_alive_timeout = 30
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def consume_api(url, **options)
|
158
|
+
# Creates a HTTP connection and request to ensure thread safety
|
159
|
+
request = Net::HTTP::Get.new(url)
|
160
|
+
|
161
|
+
add_headers request, options
|
162
|
+
|
163
|
+
# Executes the request
|
164
|
+
http_connection(url).request(request).tap do |response|
|
165
|
+
if mode.eql?(:regular) && ![200, 304].include?(response.code.to_i)
|
166
|
+
raise BlizzardApi::ApiException.new "Request failed with code '#{response.code}' details: #{response.to_hash}", response.code.to_i
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
def add_headers(request, options)
|
172
|
+
# Blizzard API documentation states the preferred way to send the access_token is using Bearer token on header
|
173
|
+
request['Authorization'] = "Bearer #{options.fetch(:access_token, BlizzardApi.access_token)}"
|
174
|
+
# Format If-modified-since option
|
175
|
+
request['If-Modified-Since'] = options[:since].httpdate if options.key? :since
|
176
|
+
options[:headers]&.each { |header, content| request[header] = content }
|
177
|
+
end
|
178
|
+
|
179
|
+
def save_in_cache(resource_url, data, ttl)
|
180
|
+
BlizzardApi.redis_connection.setex resource_url, ttl, data if BlizzardApi.use_cache
|
181
|
+
end
|
182
|
+
|
183
|
+
def find_in_cache(resource_url)
|
184
|
+
return false unless BlizzardApi.use_cache
|
185
|
+
|
186
|
+
BlizzardApi.redis_connection.get resource_url if BlizzardApi.redis_connection.exists? resource_url
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BlizzardApi
|
4
|
+
module Starcraft
|
5
|
+
##
|
6
|
+
# This class allows access to Starcraft II league data
|
7
|
+
#
|
8
|
+
# @see https://develop.battle.net/documentation/api-reference/starcraft-2-game-data-api
|
9
|
+
#
|
10
|
+
# You can get an instance of this class using the default region as follows:
|
11
|
+
# api_instance = BlizzardApi::Starcraft.account
|
12
|
+
class Account < Starcraft::Request
|
13
|
+
##
|
14
|
+
# Returns information about a player account
|
15
|
+
#
|
16
|
+
# @param [Integer] account_id Account ID
|
17
|
+
# @!macro request_options
|
18
|
+
def player(account_id, **options)
|
19
|
+
api_request "#{base_url(:community)}/player/#{account_id}", **{ ttl: CACHE_DAY }.merge(options)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BlizzardApi
|
4
|
+
module Starcraft
|
5
|
+
##
|
6
|
+
# This class allows access to Starcraft II ladder data
|
7
|
+
#
|
8
|
+
# @see https://develop.battle.net/documentation/api-reference/starcraft-2-game-data-api
|
9
|
+
#
|
10
|
+
# You can get an instance of this class using the default region as follows:
|
11
|
+
# api_instance = BlizzardApi::Starcraft.ladder
|
12
|
+
class Ladder < Starcraft::Request
|
13
|
+
##
|
14
|
+
# Grandmaster endpoint
|
15
|
+
#
|
16
|
+
# @!macro sc2_regions
|
17
|
+
# @!macro request_options
|
18
|
+
def grandmaster(region_id, **options)
|
19
|
+
reg = resolve_region(region_id)
|
20
|
+
api_request "#{base_url(:community)}/ladder/grandmaster/#{reg}", **{ ttl: CACHE_DAY }.merge(options)
|
21
|
+
end
|
22
|
+
|
23
|
+
##
|
24
|
+
# Season endpoint
|
25
|
+
#
|
26
|
+
# @!macro sc2_regions
|
27
|
+
# @!macro request_options
|
28
|
+
def season(region_id, **options)
|
29
|
+
reg = resolve_region(region_id)
|
30
|
+
api_request "#{base_url(:community)}/ladder/season/#{reg}", **{ ttl: CACHE_DAY }.merge(options)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BlizzardApi
|
4
|
+
module Starcraft
|
5
|
+
##
|
6
|
+
# This class allows access to Starcraft II legacy data
|
7
|
+
#
|
8
|
+
# @see https://develop.battle.net/documentation/api-reference/starcraft-2-game-data-api
|
9
|
+
#
|
10
|
+
# You can get an instance of this class using the default region as follows:
|
11
|
+
# api_instance = BlizzardApi::Starcraft.legacy
|
12
|
+
class Legacy < Starcraft::Request
|
13
|
+
##
|
14
|
+
# Profile data
|
15
|
+
#
|
16
|
+
# @!macro sc2_regions
|
17
|
+
# @param [Integer] realm_id Realm ID
|
18
|
+
# @param [Integer] profile_id Profile ID
|
19
|
+
# @!macro request_options
|
20
|
+
def profile(region_id, realm_id, profile_id, **options)
|
21
|
+
reg = resolve_region(region_id)
|
22
|
+
opts = { ttl: CACHE_DAY }.merge(options)
|
23
|
+
api_request "#{base_url(:community)}/legacy/profile/#{reg}/#{realm_id}/#{profile_id}", **opts
|
24
|
+
end
|
25
|
+
|
26
|
+
##
|
27
|
+
# Ladders
|
28
|
+
#
|
29
|
+
# @!macro sc2_regions
|
30
|
+
# @param [Integer] realm_id Realm ID
|
31
|
+
# @param [Integer] profile_id Profile ID
|
32
|
+
# @!macro request_options
|
33
|
+
def ladders(region_id, realm_id, profile_id, **options)
|
34
|
+
reg = resolve_region(region_id)
|
35
|
+
opts = { ttl: CACHE_DAY }.merge(options)
|
36
|
+
api_request "#{base_url(:community)}/legacy/profile/#{reg}/#{realm_id}/#{profile_id}/ladder ", **opts
|
37
|
+
end
|
38
|
+
|
39
|
+
##
|
40
|
+
# Match History
|
41
|
+
#
|
42
|
+
# @!macro sc2_regions
|
43
|
+
# @param [Integer] realm_id Realm ID
|
44
|
+
# @param [Integer] profile_id Profile ID
|
45
|
+
# @!macro request_options
|
46
|
+
def match(region_id, realm_id, profile_id, **options)
|
47
|
+
reg = resolve_region(region_id)
|
48
|
+
opts = { ttl: CACHE_DAY }.merge(options)
|
49
|
+
api_request "#{base_url(:community)}/legacy/profile/#{reg}/#{realm_id}/#{profile_id}/matches", **opts
|
50
|
+
end
|
51
|
+
|
52
|
+
##
|
53
|
+
# Ladder
|
54
|
+
#
|
55
|
+
# @!macro sc2_regions
|
56
|
+
# @param [Integer] ladder_id Ladder ID
|
57
|
+
# @!macro request_options
|
58
|
+
def ladder(region_id, ladder_id, **options)
|
59
|
+
reg = resolve_region(region_id)
|
60
|
+
opts = { ttl: CACHE_DAY }.merge(options)
|
61
|
+
api_request "#{base_url(:community)}/legacy/ladder/#{reg}/#{ladder_id}", **opts
|
62
|
+
end
|
63
|
+
|
64
|
+
##
|
65
|
+
# Achievement data
|
66
|
+
#
|
67
|
+
# @!macro sc2_regions
|
68
|
+
# @!macro request_options
|
69
|
+
def achievements(region_id, **options)
|
70
|
+
reg = resolve_region(region_id)
|
71
|
+
opts = { ttl: CACHE_DAY }.merge(options)
|
72
|
+
api_request "#{base_url(:community)}/legacy/data/achievements/#{reg}", **opts
|
73
|
+
end
|
74
|
+
|
75
|
+
##
|
76
|
+
# Rewards data
|
77
|
+
#
|
78
|
+
# @!macro sc2_regions
|
79
|
+
# @!macro request_options
|
80
|
+
def rewards(region_id, **options)
|
81
|
+
reg = resolve_region(region_id)
|
82
|
+
opts = { ttl: CACHE_DAY }.merge(options)
|
83
|
+
api_request "#{base_url(:community)}/legacy/data/rewards/#{reg}", **opts
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BlizzardApi
|
4
|
+
module Starcraft
|
5
|
+
##
|
6
|
+
# This class allows access to Starcraft II profile data
|
7
|
+
#
|
8
|
+
# @see https://develop.battle.net/documentation/api-reference/starcraft-2-game-data-api
|
9
|
+
#
|
10
|
+
# You can get an instance of this class using the default region as follows:
|
11
|
+
# api_instance = BlizzardApi::Starcraft.profile
|
12
|
+
class Profile < Starcraft::Request
|
13
|
+
##
|
14
|
+
# Static profile data
|
15
|
+
#
|
16
|
+
# @!macro sc2_regions
|
17
|
+
# @!macro request_options
|
18
|
+
def static(region_id, **options)
|
19
|
+
reg = resolve_region(region_id)
|
20
|
+
api_request "#{base_url(:community)}/static/profile/#{reg}", **{ ttl: CACHE_DAY }.merge(options)
|
21
|
+
end
|
22
|
+
|
23
|
+
##
|
24
|
+
# Metadata
|
25
|
+
#
|
26
|
+
# @!macro sc2_regions
|
27
|
+
# @param [Integer] realm_id Realm ID
|
28
|
+
# @param [Integer] profile_id Profile ID
|
29
|
+
# @!macro request_options
|
30
|
+
def metadata(region_id, realm_id, profile_id, **options)
|
31
|
+
reg = resolve_region(region_id)
|
32
|
+
opts = { ttl: CACHE_DAY }.merge(options)
|
33
|
+
api_request "#{base_url(:community)}/metadata/profile/#{reg}/#{realm_id}/#{profile_id}", **opts
|
34
|
+
end
|
35
|
+
|
36
|
+
##
|
37
|
+
# Profile data
|
38
|
+
#
|
39
|
+
# @!macro sc2_regions
|
40
|
+
# @param [Integer] realm_id Realm ID
|
41
|
+
# @param [Integer] profile_id Profile ID
|
42
|
+
# @!macro request_options
|
43
|
+
def profile(region_id, realm_id, profile_id, **options)
|
44
|
+
reg = resolve_region(region_id)
|
45
|
+
opts = { ttl: CACHE_DAY }.merge(options)
|
46
|
+
api_request "#{base_url(:community)}/profile/#{reg}/#{realm_id}/#{profile_id}", **opts
|
47
|
+
end
|
48
|
+
|
49
|
+
##
|
50
|
+
# Ladder summary
|
51
|
+
#
|
52
|
+
# @!macro sc2_regions
|
53
|
+
# @param [Integer] realm_id Realm ID
|
54
|
+
# @param [Integer] profile_id Profile ID
|
55
|
+
# @!macro request_options
|
56
|
+
def ladder_summary(region_id, realm_id, profile_id, **options)
|
57
|
+
reg = resolve_region(region_id)
|
58
|
+
opts = { ttl: CACHE_DAY }.merge(options)
|
59
|
+
api_request "#{base_url(:community)}profile/#{reg}/#{realm_id}/#{profile_id}/ladder/summary ", **opts
|
60
|
+
end
|
61
|
+
|
62
|
+
##
|
63
|
+
# Ladder data
|
64
|
+
#
|
65
|
+
# @!macro sc2_regions
|
66
|
+
# @param [Integer] realm_id Realm ID
|
67
|
+
# @param [Integer] profile_id Profile ID
|
68
|
+
# @!macro request_options
|
69
|
+
def ladder(region_id, realm_id, profile_id, ladder_id, **options)
|
70
|
+
reg = resolve_region(region_id)
|
71
|
+
opts = { ttl: CACHE_DAY }.merge(options)
|
72
|
+
api_request "#{base_url(:community)}/profile/#{reg}/#{realm_id}/#{profile_id}/ladder/#{ladder_id}", **opts
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BlizzardApi
|
4
|
+
module Starcraft
|
5
|
+
##
|
6
|
+
# This class allows access to Starcraft II league data
|
7
|
+
#
|
8
|
+
# @see https://develop.battle.net/documentation/api-reference/starcraft-2-game-data-api
|
9
|
+
#
|
10
|
+
# You can get an instance of this class using the default region as follows:
|
11
|
+
# api_instance = BlizzardApi::Starcraft.league
|
12
|
+
class League < Starcraft::Request
|
13
|
+
##
|
14
|
+
# Return information about league
|
15
|
+
#
|
16
|
+
# @param [Integer] season_id Season id
|
17
|
+
# @param [Integer] queue_id Queue id
|
18
|
+
# @param [Integer] team_type Team type
|
19
|
+
# @param [Integer] league_id League id
|
20
|
+
# @!macro request_options
|
21
|
+
def get(season_id, queue_id, team_type, league_id, **options)
|
22
|
+
opts = { ttl: CACHE_DAY }.merge(options)
|
23
|
+
api_request "#{base_url(:game_data)}/league/#{season_id}/#{queue_id}/#{team_type}/#{league_id}", **opts
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
##
|
4
|
+
# @!macro [new] sc2_regions
|
5
|
+
# @param {Symbol} region_id One of the valid Starcraft II regions *:US*, *:EU*, *:KO*, and *:TW*
|
6
|
+
# @note This gem do not support nor will support China endpoints
|
7
|
+
|
8
|
+
module BlizzardApi
|
9
|
+
module Starcraft
|
10
|
+
##
|
11
|
+
# Starcraft requests
|
12
|
+
class Request < BlizzardApi::Request
|
13
|
+
# Possible regions for the SC II API (Except China)
|
14
|
+
SC_REGION = { US: 1, EU: 2, KO: 3, TW: 3 }.freeze
|
15
|
+
|
16
|
+
##
|
17
|
+
# @!macro init_options
|
18
|
+
def initialize(**options)
|
19
|
+
super(**options)
|
20
|
+
@game = 'sc2'
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
##
|
26
|
+
# Helper method to find a region_id by its symbol
|
27
|
+
def resolve_region(region_arg)
|
28
|
+
return region_arg if region_arg.is_a? Integer
|
29
|
+
|
30
|
+
return SC_REGION[region_arg] if region_arg.is_a? Symbol
|
31
|
+
|
32
|
+
raise ArgumentError, 'Invalid region, please read the documentation'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
##
|
4
|
+
# @!macro [new] init_options
|
5
|
+
# @param options [Hash] Initialization options
|
6
|
+
# @option options [String] :region API region
|
7
|
+
# @option options [Symbol] :model API mode (:regular, :extended)
|
8
|
+
|
9
|
+
module BlizzardApi
|
10
|
+
# Starcraft II related classes
|
11
|
+
module Starcraft
|
12
|
+
require_relative 'starcraft/request'
|
13
|
+
|
14
|
+
# Starcraft II data api
|
15
|
+
require_relative 'starcraft/game_data/league'
|
16
|
+
|
17
|
+
##
|
18
|
+
# @!macro init_options
|
19
|
+
# @return {League}
|
20
|
+
def self.league(**options)
|
21
|
+
BlizzardApi::Starcraft::League.new(**options)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Starcraft community api
|
25
|
+
require_relative 'starcraft/community/profile'
|
26
|
+
require_relative 'starcraft/community/ladder'
|
27
|
+
require_relative 'starcraft/community/account'
|
28
|
+
require_relative 'starcraft/community/legacy'
|
29
|
+
|
30
|
+
##
|
31
|
+
# @!macro init_options
|
32
|
+
# @return {Profile}
|
33
|
+
def self.profile(**options)
|
34
|
+
BlizzardApi::Starcraft::Profile.new(**options)
|
35
|
+
end
|
36
|
+
|
37
|
+
##
|
38
|
+
# @!macro init_options
|
39
|
+
# @return {Ladder}
|
40
|
+
def self.ladder(**options)
|
41
|
+
BlizzardApi::Starcraft::Ladder.new(**options)
|
42
|
+
end
|
43
|
+
|
44
|
+
##
|
45
|
+
# @!macro init_options
|
46
|
+
# @return {Account}
|
47
|
+
def self.account(**options)
|
48
|
+
BlizzardApi::Starcraft::Account.new(**options)
|
49
|
+
end
|
50
|
+
|
51
|
+
##
|
52
|
+
# @return {Legacy}
|
53
|
+
def self.legacy(**options)
|
54
|
+
BlizzardApi::Starcraft::Legacy.new(**options)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BlizzardApi
|
4
|
+
##
|
5
|
+
# Manages the current token and expiration date
|
6
|
+
module TokenManager
|
7
|
+
REDIS_TOKEN_KEY = 'access_token'
|
8
|
+
|
9
|
+
##
|
10
|
+
# @!attribute access_token
|
11
|
+
# Current access token.
|
12
|
+
# @return [String] access_token
|
13
|
+
attr_accessor :access_token
|
14
|
+
|
15
|
+
##
|
16
|
+
# @!attribute expires_at
|
17
|
+
# Current access token expiration date.
|
18
|
+
# @return [String] expires_at
|
19
|
+
attr_accessor :expires_at
|
20
|
+
|
21
|
+
##
|
22
|
+
# Returns if the current token has expired
|
23
|
+
#
|
24
|
+
# @return [Boolean]
|
25
|
+
def access_token_expired?
|
26
|
+
return true if access_token.nil?
|
27
|
+
|
28
|
+
expires_at < Time.now
|
29
|
+
end
|
30
|
+
|
31
|
+
def restore_access_token
|
32
|
+
return false unless use_cache && cache_access_token
|
33
|
+
|
34
|
+
return false unless redis_connection.exists? REDIS_TOKEN_KEY
|
35
|
+
|
36
|
+
self.access_token = redis_connection.get REDIS_TOKEN_KEY
|
37
|
+
self.expires_at = Time.now + redis_connection.ttl(REDIS_TOKEN_KEY)
|
38
|
+
true
|
39
|
+
end
|
40
|
+
|
41
|
+
def save_access_token(token_data)
|
42
|
+
ttl = token_data['expires_in'].to_i
|
43
|
+
self.expires_at = Time.now + ttl
|
44
|
+
self.access_token = token_data['access_token']
|
45
|
+
|
46
|
+
redis_connection.setex REDIS_TOKEN_KEY, ttl, access_token if use_cache && cache_access_token
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BlizzardApi
|
4
|
+
module Wow
|
5
|
+
##
|
6
|
+
# This class allows access to World of Warcraft achievements
|
7
|
+
#
|
8
|
+
# @see https://develop.battle.net/documentation/api-reference/world-of-warcraft-game-data-api
|
9
|
+
#
|
10
|
+
# You can get an instance of this class using the default region as follows:
|
11
|
+
# api_instance = BlizzardApi::Wow.achievement
|
12
|
+
class Achievement < Wow::GenericDataEndpoint
|
13
|
+
setup 'achievement', :static, CACHE_TRIMESTER
|
14
|
+
|
15
|
+
##
|
16
|
+
# Return a list of possible achievement categories.
|
17
|
+
#
|
18
|
+
# @!macro request_options
|
19
|
+
#
|
20
|
+
# @!macro response
|
21
|
+
def categories(**options)
|
22
|
+
api_request "#{endpoint_uri('category')}/index", **default_options.merge(options)
|
23
|
+
end
|
24
|
+
|
25
|
+
##
|
26
|
+
# Return a achievement category.
|
27
|
+
#
|
28
|
+
# @!macro request_options
|
29
|
+
#
|
30
|
+
# @!macro response
|
31
|
+
def category(id, **options)
|
32
|
+
api_request "#{endpoint_uri('category')}/#{id}", **default_options.merge(options)
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Fetch media assets for the specified achievement
|
37
|
+
#
|
38
|
+
# @!macro request_options
|
39
|
+
#
|
40
|
+
# @!macro response
|
41
|
+
def media(id, **options)
|
42
|
+
api_request "#{base_url(:media)}/achievement/#{id}", **default_options.merge(options)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BlizzardApi
|
4
|
+
module Wow
|
5
|
+
##
|
6
|
+
# This class allows access to World of Warcraft auctions
|
7
|
+
#
|
8
|
+
# @see https://develop.battle.net/documentation/world-of-warcraft/game-data-apis
|
9
|
+
#
|
10
|
+
# You can get an instance of this class using the default region as follows:
|
11
|
+
# api_instance = BlizzardApi::Wow.auction
|
12
|
+
class Auction < Wow::Request
|
13
|
+
##
|
14
|
+
# Return all auction houses for the specified connected realm. Classic only.
|
15
|
+
#
|
16
|
+
# @param connected_realm_id [Integer] A valid connected realm id
|
17
|
+
# @!macro request_options
|
18
|
+
#
|
19
|
+
# @!macro response
|
20
|
+
def index(connected_realm_id, **options)
|
21
|
+
opts = { ttl: CACHE_HOUR, namespace: :dynamic }.merge(options)
|
22
|
+
api_request "#{base_url(:game_data)}/connected-realm/#{connected_realm_id}/auctions/index", **opts
|
23
|
+
end
|
24
|
+
|
25
|
+
##
|
26
|
+
# Return all active auctions for the specified connected realm
|
27
|
+
#
|
28
|
+
# @param connected_realm_id [Integer] A valid connected realm id
|
29
|
+
# @!macro request_options
|
30
|
+
# @option options [Boolean] :classic1x If set to true, this method will call the classic era version
|
31
|
+
#
|
32
|
+
# @!macro response
|
33
|
+
def get(connected_realm_id, auction_house_id = nil, **options)
|
34
|
+
opts = { ttl: CACHE_HOUR, namespace: :dynamic }.merge(options)
|
35
|
+
|
36
|
+
unless auction_house_id.nil?
|
37
|
+
return api_request "#{base_url(:game_data)}/connected-realm/#{connected_realm_id}/auctions/#{auction_house_id}", **opts
|
38
|
+
end
|
39
|
+
|
40
|
+
api_request "#{base_url(:game_data)}/connected-realm/#{connected_realm_id}/auctions", **opts
|
41
|
+
end
|
42
|
+
|
43
|
+
def commodities(**options)
|
44
|
+
opts = { ttl: CACHE_HOUR, namespace: :dynamic }.merge(options)
|
45
|
+
|
46
|
+
api_request "#{base_url(:game_data)}/auctions/commodities", **opts
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|