Module: RBattlenet

Defined in:
lib/rbattlenet/version.rb,
lib/rbattlenet/exceptions.rb,
lib/rbattlenet/rbattlenet.rb,
lib/rbattlenet/endpoints/base.rb,
lib/rbattlenet/endpoints/d3/act.rb,
lib/rbattlenet/endpoints/d3/era.rb,
lib/rbattlenet/endpoints/d3/hero.rb,
lib/rbattlenet/endpoints/d3/item.rb,
lib/rbattlenet/endpoints/wow/pet.rb,
lib/rbattlenet/result_collection.rb,
lib/rbattlenet/endpoints/d3/skill.rb,
lib/rbattlenet/endpoints/wow/item.rb,
lib/rbattlenet/endpoints/d3/recipe.rb,
lib/rbattlenet/endpoints/d3/season.rb,
lib/rbattlenet/endpoints/wow/guild.rb,
lib/rbattlenet/endpoints/wow/mount.rb,
lib/rbattlenet/endpoints/wow/realm.rb,
lib/rbattlenet/endpoints/wow/title.rb,
lib/rbattlenet/endpoints/wow/token.rb,
lib/rbattlenet/endpoints/d3/account.rb,
lib/rbattlenet/endpoints/d3/artisan.rb,
lib/rbattlenet/endpoints/sc2/league.rb,
lib/rbattlenet/endpoints/sc2/season.rb,
lib/rbattlenet/endpoints/sc2/static.rb,
lib/rbattlenet/endpoints/wow/region.rb,
lib/rbattlenet/endpoints/d3/follower.rb,
lib/rbattlenet/endpoints/sc2/account.rb,
lib/rbattlenet/endpoints/sc2/profile.rb,
lib/rbattlenet/endpoints/wow/profile.rb,
lib/rbattlenet/endpoints/d3/item_type.rb,
lib/rbattlenet/endpoints/wow/creature.rb,
lib/rbattlenet/endpoints/wow/pvp_tier.rb,
lib/rbattlenet/endpoints/d3/hero/items.rb,
lib/rbattlenet/endpoints/wow/character.rb,
lib/rbattlenet/endpoints/wow/item_class.rb,
lib/rbattlenet/endpoints/wow/item_media.rb,
lib/rbattlenet/endpoints/wow/power_type.rb,
lib/rbattlenet/endpoints/wow/pvp_season.rb,
lib/rbattlenet/endpoints/sc2/leaderboard.rb,
lib/rbattlenet/endpoints/wow/achievement.rb,
lib/rbattlenet/endpoints/hearthstone/card.rb,
lib/rbattlenet/endpoints/hearthstone/deck.rb,
lib/rbattlenet/endpoints/wow/classic/item.rb,
lib/rbattlenet/endpoints/wow/guild/roster.rb,
lib/rbattlenet/endpoints/wow/profile/user.rb,
lib/rbattlenet/endpoints/sc2/legacy/ladder.rb,
lib/rbattlenet/endpoints/wow/creature_type.rb,
lib/rbattlenet/endpoints/wow/item_subclass.rb,
lib/rbattlenet/endpoints/wow/playable_race.rb,
lib/rbattlenet/endpoints/d3/character_class.rb,
lib/rbattlenet/endpoints/d3/era_leaderboard.rb,
lib/rbattlenet/endpoints/sc2/legacy/profile.rb,
lib/rbattlenet/endpoints/sc2/legacy/rewards.rb,
lib/rbattlenet/endpoints/sc2/profile_ladder.rb,
lib/rbattlenet/endpoints/wow/character/pets.rb,
lib/rbattlenet/endpoints/wow/creature_media.rb,
lib/rbattlenet/endpoints/wow/playable_class.rb,
lib/rbattlenet/endpoints/wow/pvp_tier_media.rb,
lib/rbattlenet/endpoints/wow/azerite_essence.rb,
lib/rbattlenet/endpoints/wow/character/media.rb,
lib/rbattlenet/endpoints/wow/connected_realm.rb,
lib/rbattlenet/endpoints/wow/creature_family.rb,
lib/rbattlenet/endpoints/wow/pvp_leaderboard.rb,
lib/rbattlenet/endpoints/hearthstone/metadata.rb,
lib/rbattlenet/endpoints/sc2/profile_metadata.rb,
lib/rbattlenet/endpoints/wow/character/legacy.rb,
lib/rbattlenet/endpoints/wow/character/mounts.rb,
lib/rbattlenet/endpoints/wow/character/status.rb,
lib/rbattlenet/endpoints/wow/character/titles.rb,
lib/rbattlenet/endpoints/wow/classic/creature.rb,
lib/rbattlenet/endpoints/wow/reputation_tiers.rb,
lib/rbattlenet/endpoints/d3/season_leaderboard.rb,
lib/rbattlenet/endpoints/wow/achievement_media.rb,
lib/rbattlenet/endpoints/wow/guild_crest_media.rb,
lib/rbattlenet/endpoints/d3/hero/follower_items.rb,
lib/rbattlenet/endpoints/wow/classic/item_class.rb,
lib/rbattlenet/endpoints/wow/classic/item_media.rb,
lib/rbattlenet/endpoints/wow/classic/power_type.rb,
lib/rbattlenet/endpoints/wow/guild/achievements.rb,
lib/rbattlenet/endpoints/wow/pvp_season/rewards.rb,
lib/rbattlenet/endpoints/wow/reputation_faction.rb,
lib/rbattlenet/endpoints/sc2/legacy/achievements.rb,
lib/rbattlenet/endpoints/wow/character/equipment.rb,
lib/rbattlenet/endpoints/wow/character/keystones.rb,
lib/rbattlenet/endpoints/wow/achievement_category.rb,
lib/rbattlenet/endpoints/wow/character/appearance.rb,
lib/rbattlenet/endpoints/wow/character/statistics.rb,
lib/rbattlenet/endpoints/wow/playable_class_media.rb,
lib/rbattlenet/endpoints/wow/azerite_essence_media.rb,
lib/rbattlenet/endpoints/wow/character/hunter_pets.rb,
lib/rbattlenet/endpoints/wow/character/professions.rb,
lib/rbattlenet/endpoints/wow/character/pvp_summary.rb,
lib/rbattlenet/endpoints/wow/character/reputations.rb,
lib/rbattlenet/endpoints/wow/classic/creature_type.rb,
lib/rbattlenet/endpoints/wow/classic/item_subclass.rb,
lib/rbattlenet/endpoints/wow/classic/playable_race.rb,
lib/rbattlenet/endpoints/wow/creature_family_media.rb,
lib/rbattlenet/endpoints/wow/mythic_keystone_affix.rb,
lib/rbattlenet/endpoints/sc2/legacy/profile_ladders.rb,
lib/rbattlenet/endpoints/sc2/profile_ladder_summary.rb,
lib/rbattlenet/endpoints/wow/character/achievements.rb,
lib/rbattlenet/endpoints/wow/classic/creature_media.rb,
lib/rbattlenet/endpoints/wow/classic/playable_class.rb,
lib/rbattlenet/endpoints/wow/mythic_keystone_period.rb,
lib/rbattlenet/endpoints/wow/mythic_keystone_season.rb,
lib/rbattlenet/endpoints/wow/character/active_quests.rb,
lib/rbattlenet/endpoints/wow/classic/creature_family.rb,
lib/rbattlenet/endpoints/wow/mythic_keystone_dungeon.rb,
lib/rbattlenet/endpoints/wow/mythic_raid_leaderboard.rb,
lib/rbattlenet/endpoints/wow/playable_specialization.rb,
lib/rbattlenet/endpoints/wow/profile/pets_collection.rb,
lib/rbattlenet/endpoints/wow/guild_crest_border_media.rb,
lib/rbattlenet/endpoints/wow/guild_crest_emblem_media.rb,
lib/rbattlenet/endpoints/wow/character/completed_raids.rb,
lib/rbattlenet/endpoints/wow/character/pvp_bracket_2v2.rb,
lib/rbattlenet/endpoints/wow/character/pvp_bracket_3v3.rb,
lib/rbattlenet/endpoints/wow/character/pvp_bracket_rbg.rb,
lib/rbattlenet/endpoints/wow/character/specializations.rb,
lib/rbattlenet/endpoints/wow/classic/guild_crest_media.rb,
lib/rbattlenet/endpoints/wow/profile/mounts_collection.rb,
lib/rbattlenet/endpoints/wow/profile/protected_summary.rb,
lib/rbattlenet/endpoints/wow/character/completed_quests.rb,
lib/rbattlenet/endpoints/wow/character/season_keystones.rb,
lib/rbattlenet/endpoints/wow/mythic_keystone_leaderboard.rb,
lib/rbattlenet/endpoints/sc2/legacy/profile_match_history.rb,
lib/rbattlenet/endpoints/wow/character/completed_dungeons.rb,
lib/rbattlenet/endpoints/wow/classic/playable_class_media.rb,
lib/rbattlenet/endpoints/wow/classic/creature_family_media.rb,
lib/rbattlenet/endpoints/wow/playable_class/pvp_talent_slot.rb,
lib/rbattlenet/endpoints/wow/character/achievement_statistics.rb,
lib/rbattlenet/endpoints/wow/classic/guild_crest_border_media.rb,
lib/rbattlenet/endpoints/wow/classic/guild_crest_emblem_media.rb

Defined Under Namespace

Modules: D3, Endpoints, Errors, Hearthstone, Sc2, Wow Classes: EmptyHashResult, EmptyResult, HashResult, Result, ResultCollection

Constant Summary collapse

VERSION =
"2.2.0"
@@region =
"eu"
@@locale =
"en_gb"
@@response_type =
:struct
@@concurrency =
20
@@timeout =
120

Class Method Summary collapse

Class Method Details

.authenticate(client_id:, client_secret:) ⇒ Object

Set Access Token for requests. Required



9
10
11
12
13
14
15
16
17
18
# File 'lib/rbattlenet/rbattlenet.rb', line 9

def self.authenticate(client_id:, client_secret:)
  oauth_region = ["kr", "tw"].include?(@@region.downcase) ? "apac" : @@region
  response = Typhoeus.post("https://#{oauth_region}.battle.net/oauth/token",
    body: { grant_type: :client_credentials },
    userpwd: "#{client_id}:#{client_secret}",
  )
  raise RBattlenet::Errors::Unauthorized.new if response.code == 401
  @@token = JSON.parse(response.body)['access_token']
  true
end

.fake_authenticateObject

Set a fake token for RSpec tests



21
22
23
# File 'lib/rbattlenet/rbattlenet.rb', line 21

def self.fake_authenticate
  @@token = "abcdefghijklmnopqrstuvwxyz01234567"
end

.get(subjects) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rbattlenet/rbattlenet.rb', line 33

def get(subjects)
  store = @@response_type == :raw ? [] : RBattlenet::ResultCollection.new(@@response_type)

  # Limit concurrency to prevent hitting the API request per-second cap.
  hydra = Typhoeus::Hydra.new(max_concurrency: @@concurrency)
  subjects.each do |uris, subject|
    uris.each do |field, uri|
      headers = {}
      headers['Authorization'] = "Bearer #{@@token}" if @@token && !uri.include?('access_token')
      request = Typhoeus::Request.new(URI::DEFAULT_PARSER.escape(uri), headers: headers, timeout: @@timeout)

      request.on_complete do |response|
        if @@response_type == :raw
          store << response
        else
          store.add(subject, field, response)
          if data = store.complete(subject, uris.size)
            yield subject, data
          end
        end
      end

      hydra.queue request
    end
  end

  hydra.run
  store.size == 1 ? store.first : store
end

.set_options(region: @@region, locale: @@locale, response_type: @@response_type, concurrency: @@concurrency, timeout: @@timeout) ⇒ Object



25
26
27
28
# File 'lib/rbattlenet/rbattlenet.rb', line 25

def self.set_options(region: @@region, locale: @@locale, response_type: @@response_type, concurrency: @@concurrency, timeout: @@timeout)
  @@region, @@locale, @@response_type, @@concurrency, @@timeout = region, locale, response_type, concurrency, timeout
  true
end

.uri(path) ⇒ Object



63
64
65
# File 'lib/rbattlenet/rbattlenet.rb', line 63

def uri(path)
  "https://#{@@region}.api.blizzard.com/#{path}#{@@region}&locale=#{@@locale}"
end