Class: SteamGroup

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

Overview

The SteamGroup class represents a group in the Steam Community

Author:

  • Sebastian Staudt

Instance Attribute Summary collapse

Attributes included from Cacheable

#fetch_time

Instance Method Summary collapse

Methods included from Cacheable

#cache, #fetched?, included

Constructor Details

#initialize(id, fetch = true) ⇒ SteamGroup

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

Parameters:

  • id (String, Fixnum)

    The custom URL of the group specified by the group admin or the 64bit group ID

  • fetch (Boolean) (defaults to: true)

    if ‘true` the groups’s data is loaded into the object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/steam/community/steam_group.rb', line 40

def initialize(id, fetch = true)
  begin
    if id.is_a? Numeric
      @group_id64 = id
    else
      @custom_url = id.downcase
    end

    super(fetch)
  rescue REXML::ParseException
    raise SteamCondenserError, 'Group could not be loaded.'
  end
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.

Returns:

  • (String)

    The custom URL of this group



27
28
29
# File 'lib/steam/community/steam_group.rb', line 27

def custom_url
  @custom_url
end

#group_id64Fixnum (readonly)

Returns this group’s 64bit SteamID

Returns:

  • (Fixnum)

    This group’s 64bit SteamID



32
33
34
# File 'lib/steam/community/steam_group.rb', line 32

def group_id64
  @group_id64
end

Instance Method Details

#base_urlString

Returns the base URL for this group’s page

This URL is different for groups having a custom URL.

Returns:

  • (String)

    The base URL for this group



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

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

#fetchObject

Loads the members of this group

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:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/steam/community/steam_group.rb', line 73

def fetch
  @members = []
  page = 0

  begin
    page += 1
    url = open("#{base_url}/memberslistxml?p=#{page}", {:proxy => true})
    member_data = REXML::Document.new(url.read).root

    begin
      @group_id64 = member_data.elements['groupID64'].text.to_i if page == 1
      total_pages = member_data.elements['totalPages'].text.to_i

      member_data.elements['members'].elements.each do |member|
        @members << SteamId.new(member.text.to_i, false)
      end
    rescue
      raise SteamCondenserError, 'XML data could not be parsed.'
    end
  end while page < total_pages

  super
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 group size is separately fetched without needing multiple requests for big groups.

Returns:

  • (Fixnum)

    The number of this group’s members



104
105
106
107
108
109
110
111
# File 'lib/steam/community/steam_group.rb', line 104

def member_count
  if @members.nil?
    url = open("#{base_url}/memberslistxml", {:proxy => true})
    REXML::Document.new(url.read).root.elements['memberCount'].text.to_i
  else
    @members.size
  end
end

#membersArray<SteamId>

Returns the members of this group

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

Returns:

  • (Array<SteamId>)

    The Steam ID’s of the members of this group

See Also:



119
120
121
122
# File 'lib/steam/community/steam_group.rb', line 119

def members
  fetch if @members.nil? || @members[0].nil?
  @members
end