class Bnet::Starcraft2::Profile

TODO: Associations for career, current_season

Constants

PARAMS_MAPPING

Attributes

achievement_points[RW]
acievement_points[RW]
career[RW]
clan_name[RW]
clan_tag[RW]
name[RW]
profile_id[RW]
protoss_level[RW]
raw_attributes[RW]
realm[RW]
region[RW]
swarm_level[RW]
terran_level[RW]
zerg_level[RW]

Public Class Methods

find(args) click to toggle source

Query Battlenet API for the SC2 profile recordand create an instance of an SC2 Profile.

Hash Params:

Required
  :realm         - (required but defaults to '1')
  :profile_id    - ID (Honestly i do not know why Blizzard still needs this if
                   localized Battletag is unique enough)
  :name  - Just the name string in the Battle tag.

Optional
  :locale        - (defaults to 'en_US')
  :api_key       - the api key

Example: If US account ‘Playerone#1309’ the profile can be accessible via web from ‘us.battle.net/sc2/en/profile/2143215/1/PlayerOne/’

find(region: 'us', profile_id: 2143215, name: 'PlayerOne')

Returns a Profile object with the following attributes

:profile_id, :realm, :name, :clan_name, :clan_tag,
:achievement_points, :swarm_level, :terran_level, :zerg_level,
:protoss_level, :acievement_points
# File lib/bnet/starcraft2/profile.rb, line 47
def self.find args
  region     = args[:region]
  profile_id = args[:profile_id]
  name       = args[:name]
  realm      = args[:realm] || '1'
  locale     = args[:locale] || 'en_US'
  api_key        = args[:api_key] || Bnet.configuration.api_key

  base_api = Bnet::Starcraft2.new(region: region)
  call_url = base_api.url + "profile/#{profile_id}/#{realm}/#{name}/?locale=#{locale}&apikey=#{api_key}"

  begin
    data = open(call_url)
    raw_response = JSON.parse(data.read)

    if Bnet::API.valid_call?(data.status, raw_response)
      bnet_object = from_api(raw_response)
      bnet_object.raw_attributes = raw_response
      bnet_object.region = region
    else
      bnet_object = nil
    end

  rescue OpenURI::HTTPError => e
    bnet_object = nil
  end

  return bnet_object

end
from_api(response) click to toggle source
Calls superclass method Bnet::BnetResource::from_api
# File lib/bnet/starcraft2/profile.rb, line 94
def self.from_api(response)
  bnet_resource = super(response)
  if bnet_resource && response["achievements"]
    bnet_resource.achievement_points = response["achievements"]["points"]["totalPoints"]
  end

  if bnet_resource && response["swarmLevels"]
    bnet_resource.swarm_level   = response["swarmLevels"]["level"]
    bnet_resource.terran_level  = response["swarmLevels"]["terran"]["level"]
    bnet_resource.protoss_level = response["swarmLevels"]["protoss"]["level"]
    bnet_resource.zerg_level    = response["swarmLevels"]["zerg"]["level"]
  end

  assign_career_from_raw_career(bnet_resource, response["career"]) if response["career"]

  bnet_resource
end
new(args) click to toggle source
# File lib/bnet/starcraft2/profile.rb, line 17
def initialize args
  args.each do |k,v|
    instance_variable_set("@#{k}", v) unless v.nil?
  end
end

Private Class Methods

assign_career_from_raw_career(profile, raw_career) click to toggle source
# File lib/bnet/starcraft2/profile.rb, line 114
def self.assign_career_from_raw_career(profile, raw_career)
  profile.career = Bnet::Starcraft2::Career.from_api(raw_career)
  return profile
end

Public Instance Methods

current_ladder_season() click to toggle source
# File lib/bnet/starcraft2/profile.rb, line 86
def current_ladder_season
  @ladders ||= Bnet::Starcraft2::Ladder.find_current(self)
end
matches() click to toggle source
# File lib/bnet/starcraft2/profile.rb, line 82
def matches
  @matches ||= Bnet::Starcraft2::Match.all(self)
end
previous_ladder_season() click to toggle source
# File lib/bnet/starcraft2/profile.rb, line 90
def previous_ladder_season
  @ladders ||= Bnet::Starcraft2::Ladder.find_previous(self)
end