Class: Bnet::Starcraft2::Profile

Inherits:
BnetResource show all
Defined in:
lib/bnet/starcraft2/profile.rb

Overview

TODO: Associations for career, current_season

Constant Summary collapse

PARAMS_MAPPING =
{
  "id" => :profile_id,
  "realm" =>  :realm,
  "displayName" => :name,
  "clanName" => :clan_name,
  "clanTag" => :clan_tag
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Profile

Returns a new instance of Profile.



17
18
19
20
21
# 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

Instance Attribute Details

#achievement_pointsObject

Returns the value of attribute achievement_points.



4
5
6
# File 'lib/bnet/starcraft2/profile.rb', line 4

def achievement_points
  @achievement_points
end

#acievement_pointsObject

Returns the value of attribute acievement_points.



4
5
6
# File 'lib/bnet/starcraft2/profile.rb', line 4

def acievement_points
  @acievement_points
end

#careerObject

Returns the value of attribute career.



4
5
6
# File 'lib/bnet/starcraft2/profile.rb', line 4

def career
  @career
end

#clan_nameObject

Returns the value of attribute clan_name.



4
5
6
# File 'lib/bnet/starcraft2/profile.rb', line 4

def clan_name
  @clan_name
end

#clan_tagObject

Returns the value of attribute clan_tag.



4
5
6
# File 'lib/bnet/starcraft2/profile.rb', line 4

def clan_tag
  @clan_tag
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/bnet/starcraft2/profile.rb', line 4

def name
  @name
end

#profile_idObject

Returns the value of attribute profile_id.



4
5
6
# File 'lib/bnet/starcraft2/profile.rb', line 4

def profile_id
  @profile_id
end

#protoss_levelObject

Returns the value of attribute protoss_level.



4
5
6
# File 'lib/bnet/starcraft2/profile.rb', line 4

def protoss_level
  @protoss_level
end

#raw_attributesObject

Returns the value of attribute raw_attributes.



4
5
6
# File 'lib/bnet/starcraft2/profile.rb', line 4

def raw_attributes
  @raw_attributes
end

#realmObject

Returns the value of attribute realm.



4
5
6
# File 'lib/bnet/starcraft2/profile.rb', line 4

def realm
  @realm
end

#regionObject

Returns the value of attribute region.



4
5
6
# File 'lib/bnet/starcraft2/profile.rb', line 4

def region
  @region
end

#swarm_levelObject

Returns the value of attribute swarm_level.



4
5
6
# File 'lib/bnet/starcraft2/profile.rb', line 4

def swarm_level
  @swarm_level
end

#terran_levelObject

Returns the value of attribute terran_level.



4
5
6
# File 'lib/bnet/starcraft2/profile.rb', line 4

def terran_level
  @terran_level
end

#zerg_levelObject

Returns the value of attribute zerg_level.



4
5
6
# File 'lib/bnet/starcraft2/profile.rb', line 4

def zerg_level
  @zerg_level
end

Class Method Details

.find(args) ⇒ Object

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


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# 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

Instance Method Details

#current_ladder_seasonObject



86
87
88
# File 'lib/bnet/starcraft2/profile.rb', line 86

def current_ladder_season
  @ladders ||= Bnet::Starcraft2::Ladder.find_current(self)
end

#matchesObject



82
83
84
# File 'lib/bnet/starcraft2/profile.rb', line 82

def matches
  @matches ||= Bnet::Starcraft2::Match.all(self)
end

#previous_ladder_seasonObject



90
91
92
# File 'lib/bnet/starcraft2/profile.rb', line 90

def previous_ladder_season
  @ladders ||= Bnet::Starcraft2::Ladder.find_previous(self)
end