Class: SteamApi::Client

Inherits:
Object
  • Object
show all
Includes:
IPlayerService, ISteamUser, ISteamUserStats
Defined in:
lib/steam_api/client.rb

Overview

This Client class is to be used as the entry point to the rest of the program. A client object should be initialized and with that object comes the ability to use any of the methods which call the Steam API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from IPlayerService

#get_badges, #get_community_badge_progress, #get_owned_games, #get_recently_played_games, #get_steam_level

Methods included from ISteamUserStats

#get_global_achievement_percentages_for_app, #get_number_of_current_players, #get_player_achievements, #get_schema_for_game, #get_user_stats_for_game

Methods included from ISteamUser

#get_friend_list, #get_player_bans, #get_player_summaries, #get_user_group_list, #resolve_vanity_url

Constructor Details

#initialize(api_key) ⇒ Client

This method is the entry point to use the API. It is initialized with an API key. Only one instance of this class should exist to interface with the API

Parameters:


13
14
15
# File 'lib/steam_api/client.rb', line 13

def initialize(api_key)
  @api_key = api_key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key


9
10
11
# File 'lib/steam_api/client.rb', line 9

def api_key
  @api_key
end

Instance Method Details

#get(url, key_needed: true) ⇒ Hash

This method is a helper method which many other methods rely on to call the Steam API

Parameters:

  • url (String)

    This should be the base url for the desired API call

  • key_needed (Boolean)

    This is a boolean value which is used to decide whether a specific API call needs to have a key interpolated into the URL

Returns:

  • (Hash)

    The return value is a hash, but its contents depends on the success or lack thereof, of the api call. If it was successful, it returns the JSON as a hash. Otherwise, it returns a hash with an error message.


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/steam_api/client.rb', line 24

def get(url, key_needed: true)
  url = add_key url if key_needed
  begin
    uri = URI(url)
    response = Net::HTTP.get_response(uri)
    parsed = JSON.parse(response.body)
    return parsed
  rescue JSON::ParserError
    return {
      "error" => "There request was OK but there was a problem parsing the response."
    }
  rescue URI::InvalidURIError
    return {
      "error" => "bad URI(is not URI?)"
    }
  end
end