Class: SteamClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/steam-client/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil) ⇒ Client

Returns a new instance of Client.



19
20
21
# File 'lib/steam-client/client.rb', line 19

def initialize(api_key = nil)
  @api_key = api_key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



17
18
19
# File 'lib/steam-client/client.rb', line 17

def api_key
  @api_key
end

Instance Method Details

#find_profile_by_id(id) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/steam-client/client.rb', line 37

def find_profile_by_id(id)
  url = ""
  if not @api_key.nil?
    url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=#{@api_key}&steamids=#{id}"
    data = get_with_retries url
    return Profile.from_json(data)
  else
	  url = "http://steamcommunity.com/profiles/#{id}?xml=1"
	  xml = get_with_retries url
	  return Profile.from_xml(xml)
  end
end

#find_profile_by_name(name) ⇒ Object



32
33
34
35
# File 'lib/steam-client/client.rb', line 32

def find_profile_by_name(name)
  id = find_steam_id_by_name(name)
	return find_profile_by_id(id)
end

#find_steam_id_by_name(name) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/steam-client/client.rb', line 23

def find_steam_id_by_name(name)
  if not @api_key.nil?
    url = "http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=#{@api_key}&vanityurl=#{name}"
    data = get_with_retries url
    j = JSON.parse(data)
    return j['response']['steamid']
  end
end

#get_friends_from_id(id) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/steam-client/client.rb', line 56

def get_friends_from_id(id)
  url = ""
  if not @api_key.nil?
    url = "http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key=#{@api_key}&steamid=#{id}&relationship=friend"
    data = get_with_retries url
    jFriends = JSON.parse(data)
    friends = []
    jFriends['friendslist']['friends'].each do |friend|
      friends << Profile.new(friend['steamid'])
    end
    return friends
  else
    url = "http://steamcommunity.com/profiles/#{id}/friends?xml=1"
    xml = get_with_retries url
    hFriends = Crack::XML.parse xml
    friends = []
    hFriends['friendsList']['friends']['friend'].each do |friendID|
        friends << Profile.new(friendID)
    end
    return friends
  end
end

#get_friends_from_profile(profile) ⇒ Object



50
51
52
53
54
# File 'lib/steam-client/client.rb', line 50

def get_friends_from_profile(profile)
  friends = get_friends_from_id(profile.steamID64)
  profile.friends = friends
  return friends
end

#get_games_from_id(id) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/steam-client/client.rb', line 85

def get_games_from_id(id)
  url = "http://steamcommunity.com/profiles/#{id}/games\?tab=all&xml\=1"
  xml = get_with_retries url
  hGames = Crack::XML.parse xml
  
  if hGames['gamesList'].has_key?("error") and hGames['gamesList']['error'] =~ /This profile is private/
    raise SteamClient::Error::PrivateProfileError
  end
  
  games = []
  hGames['gamesList']['games']['game'].each do |game|
      games << Game.new(game)
  end
  return games
end

#get_games_from_profile(profile) ⇒ Object



79
80
81
82
83
# File 'lib/steam-client/client.rb', line 79

def get_games_from_profile(profile)
  games = get_games_from_id(profile.steamID64)
  profile.games = games
  return games
end

#get_with_retries(url, retries = 10) ⇒ Object

Raises:

  • (RestClient::ServiceUnavailable)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/steam-client/client.rb', line 101

def get_with_retries(url, retries = 10)
    attempt = 0
    while attempt < retries
        begin
            resp = RestClient.get url
            return resp
        rescue RestClient::ServiceUnavailable
            attempt += 1
            sleep 0.5
        end
    end

    raise RestClient::ServiceUnavailable
end