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

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 SteamGroup object with the given group ID



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/steam/community/steam_group.rb', line 21

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 SteamCondenserException.new('Group could not be loaded.')
  end
end

Instance Attribute Details

#custom_urlObject (readonly)

Returns the value of attribute custom_url.



18
19
20
# File 'lib/steam/community/steam_group.rb', line 18

def custom_url
  @custom_url
end

#group_id64Object (readonly)

Returns the value of attribute group_id64.



18
19
20
# File 'lib/steam/community/steam_group.rb', line 18

def group_id64
  @group_id64
end

Instance Method Details

#base_urlObject

Returns the URL to the group’s Steam Community page



36
37
38
39
40
41
42
# File 'lib/steam/community/steam_group.rb', line 36

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

#fetchObject

Parses the data about this groups members



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/steam/community/steam_group.rb', line 45

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

    @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
  end while page < total_pages

  super
end

#member_countObject

Returns the number of members this group has. If the members have already been fetched with fetch_members the size of the member array is returned. Otherwise the group size is separately fetched.



69
70
71
72
73
74
75
76
# File 'lib/steam/community/steam_group.rb', line 69

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

#membersObject

Returns the members of this group Calls fetch if the members haven’t been fetched already.



80
81
82
83
# File 'lib/steam/community/steam_group.rb', line 80

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