Class: SteamCondenser::Community::SteamGroup

Inherits:
Object
  • Object
show all
Includes:
Cacheable, XMLData
Defined in:
lib/steam-condenser/community/steam_group.rb

Overview

The SteamGroup class represents a group in the Steam Community

Author:

  • Sebastian Staudt

Constant Summary collapse

AVATAR_URL =
'http://media.steampowered.com/steamcommunity/public/images/avatars/%s/%s%s.jpg'

Instance Attribute Summary collapse

Attributes included from Cacheable

#fetch_time

Instance Method Summary collapse

Methods included from XMLData

#parse

Methods included from Cacheable

#cache, #cache_id_value, #cache_ids, #cached_instance, #fetched?, included

Constructor Details

#initialize(id, fetch = true, bypass_cache = false) ⇒ SteamGroup

Creates a new SteamGroup instance for the group with the given ID


58
59
60
61
62
63
64
65
# File 'lib/steam-condenser/community/steam_group.rb', line 58

def initialize(id)
  if id.is_a? Numeric
    @group_id64 = id
  else
    @custom_url = id.downcase
  end
  @members = []
end

Instance Attribute Details

#custom_urlString (readonly)

Returns the custom URL of this group

The custom URL is a admin specified unique string that can be used instead of the 64bit SteamID as an identifier for a group.


31
32
33
# File 'lib/steam-condenser/community/steam_group.rb', line 31

def custom_url
  @custom_url
end

#group_id64Fixnum (readonly)

Returns this group's 64bit SteamID


36
37
38
# File 'lib/steam-condenser/community/steam_group.rb', line 36

def group_id64
  @group_id64
end

#headlineString (readonly)

Returns this group's headline text


41
42
43
# File 'lib/steam-condenser/community/steam_group.rb', line 41

def headline
  @headline
end

#nameString (readonly)

Returns this group's name


46
47
48
# File 'lib/steam-condenser/community/steam_group.rb', line 46

def name
  @name
end

#summaryString (readonly)

Returns this group's summary text


51
52
53
# File 'lib/steam-condenser/community/steam_group.rb', line 51

def summary
  @summary
end

Instance Method Details

#avatar_full_urlString

Returns the URL to this group's full avatar


70
71
72
# File 'lib/steam-condenser/community/steam_group.rb', line 70

def avatar_full_url
  AVATAR_URL % [ @avatar_hash[0..1], @avatar_hash, '_full' ]
end

#avatar_icon_urlString

Returns the URL to this group's icon avatar


77
78
79
# File 'lib/steam-condenser/community/steam_group.rb', line 77

def avatar_icon_url
  AVATAR_URL % [ @avatar_hash[0..1], @avatar_hash, '' ]
end

#avatar_medium_urlString

Returns the URL to this group's medium avatar


84
85
86
# File 'lib/steam-condenser/community/steam_group.rb', line 84

def avatar_medium_url
  AVATAR_URL % [ @avatar_hash[0..1], @avatar_hash, '_medium' ]
end

#base_urlString

Returns the base URL for this group's page

This URL is different for groups having a custom URL.


93
94
95
96
97
98
99
# File 'lib/steam-condenser/community/steam_group.rb', line 93

def base_url
  if @custom_url.nil?
    "http://steamcommunity.com/gid/#@group_id64"
  else
    "http://steamcommunity.com/groups/#@custom_url"
  end
end

#fetchObject

Loads information about and members of this group

This includes the ID, name, headline, summary of the group as well as avatar and custom URLs.

This might take several HTTP requests as the Steam Community splits this data over several XML documents if the group has lots of members.

See Also:


110
111
112
113
114
115
116
117
118
119
120
# File 'lib/steam-condenser/community/steam_group.rb', line 110

def fetch
  if @member_count.nil? || @member_count == @members.size
    page = 0
  else
    page = 1
  end

  begin
    total_pages = fetch_page(page += 1)
  end while page < total_pages
end

#fetch_page(page) ⇒ Fixnum (private)

Fetches a specific page of the member listing of this group


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/steam-condenser/community/steam_group.rb', line 155

def fetch_page(page)
  member_data = parse "#{base_url}/memberslistxml?p=#{page}"

  @member_count = member_data['memberCount'].to_i
  total_pages   = member_data['totalPages'].to_i

  if page == 1
    member_data['groupDetails']['avatarIcon'] =~ /\/([0-9a-f]+)\.jpg$/
    @avatar_hash = $1
    @custom_url  = member_data['groupDetails']['groupURL']
    @group_id64  = member_data['groupID64'].to_i
    @headline    = member_data['groupDetails']['headline']
    @name        = member_data['groupDetails']['groupName']
    @summary     = member_data['groupDetails']['summary']
  end

  member_data['members']['steamID64'].each do |member|
    @members << SteamId.new(member.to_i, false)
  end

  total_pages
rescue
  raise $! if $!.is_a? SteamCondenser::Error
  raise SteamCondenser::Error, 'XML data could not be parsed.'
end

#member_countFixnum

Returns the number of members this group has

If the members have already been fetched the size of the member array is returned. Otherwise the the first page of the member listing is fetched and the member count and the first batch of members is stored.


129
130
131
132
133
134
135
136
# File 'lib/steam-condenser/community/steam_group.rb', line 129

def member_count
  if @member_count.nil?
    total_pages = fetch_page(1)
    @fetch_time = Time.now if total_pages == 1
  end

  @member_count
end

#membersArray<SteamId>

Returns the members of this group

If the members haven't been fetched yet, this is done now.

See Also:


144
145
146
147
# File 'lib/steam-condenser/community/steam_group.rb', line 144

def members
  fetch if @members.size != @member_count
  @members
end