steam_api_client 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. checksums.yaml +7 -0
  2. data/.dockerignore +21 -0
  3. data/.pronto.yml +12 -0
  4. data/.reek.yml +25 -0
  5. data/.rspec +4 -0
  6. data/.ruby-version +1 -0
  7. data/.standard.yml +3 -0
  8. data/.tool-versions +1 -0
  9. data/CHANGELOG.md +9 -0
  10. data/CLAUDE.md +52 -0
  11. data/CODE_OF_CONDUCT.md +84 -0
  12. data/Gemfile +34 -0
  13. data/Gemfile.lock +220 -0
  14. data/LICENSE +21 -0
  15. data/README.md +158 -0
  16. data/lib/steam_api_client/client.rb +112 -0
  17. data/lib/steam_api_client/errors/api_error.rb +48 -0
  18. data/lib/steam_api_client/errors/response_parse_error.rb +19 -0
  19. data/lib/steam_api_client/errors/url_error.rb +5 -0
  20. data/lib/steam_api_client/failure_handler.rb +46 -0
  21. data/lib/steam_api_client/request.rb +86 -0
  22. data/lib/steam_api_client/requestors/app_details.rb +27 -0
  23. data/lib/steam_api_client/requestors/app_global_achievements.rb +19 -0
  24. data/lib/steam_api_client/requestors/app_news.rb +23 -0
  25. data/lib/steam_api_client/requestors/base.rb +32 -0
  26. data/lib/steam_api_client/requestors/game_current_players.rb +19 -0
  27. data/lib/steam_api_client/requestors/player_achievements.rb +21 -0
  28. data/lib/steam_api_client/requestors/player_friends.rb +21 -0
  29. data/lib/steam_api_client/requestors/player_game_stats.rb +21 -0
  30. data/lib/steam_api_client/requestors/player_level.rb +18 -0
  31. data/lib/steam_api_client/requestors/player_owned_games.rb +23 -0
  32. data/lib/steam_api_client/requestors/player_recent_games.rb +21 -0
  33. data/lib/steam_api_client/requestors/player_steamid.rb +19 -0
  34. data/lib/steam_api_client/requestors/player_summaries.rb +19 -0
  35. data/lib/steam_api_client/response.rb +64 -0
  36. data/lib/steam_api_client/structs/app_details/achievement.rb +12 -0
  37. data/lib/steam_api_client/structs/app_details/category.rb +12 -0
  38. data/lib/steam_api_client/structs/app_details/details.rb +82 -0
  39. data/lib/steam_api_client/structs/app_details/genre.rb +12 -0
  40. data/lib/steam_api_client/structs/app_details/movie.rb +25 -0
  41. data/lib/steam_api_client/structs/app_details/package_group.rb +18 -0
  42. data/lib/steam_api_client/structs/app_details/requirements.rb +12 -0
  43. data/lib/steam_api_client/structs/app_details/screenshot.rb +13 -0
  44. data/lib/steam_api_client/structs/app_details/sub.rb +18 -0
  45. data/lib/steam_api_client/structs/app_global_achievements.rb +18 -0
  46. data/lib/steam_api_client/structs/app_new.rb +20 -0
  47. data/lib/steam_api_client/structs/app_news.rb +13 -0
  48. data/lib/steam_api_client/structs/base.rb +15 -0
  49. data/lib/steam_api_client/structs/game_current_players.rb +12 -0
  50. data/lib/steam_api_client/structs/player_achievements.rb +23 -0
  51. data/lib/steam_api_client/structs/player_friends.rb +17 -0
  52. data/lib/steam_api_client/structs/player_game_stats.rb +20 -0
  53. data/lib/steam_api_client/structs/player_level.rb +11 -0
  54. data/lib/steam_api_client/structs/player_owned_games.rb +25 -0
  55. data/lib/steam_api_client/structs/player_recent_games.rb +22 -0
  56. data/lib/steam_api_client/structs/player_steamid.rb +16 -0
  57. data/lib/steam_api_client/structs/player_summaries.rb +11 -0
  58. data/lib/steam_api_client/structs/player_summary.rb +34 -0
  59. data/lib/steam_api_client/structs/requests/abstract.rb +59 -0
  60. data/lib/steam_api_client/structs/requests/get_app_details.rb +54 -0
  61. data/lib/steam_api_client/structs/requests/get_app_global_achievements.rb +54 -0
  62. data/lib/steam_api_client/structs/requests/get_app_news.rb +62 -0
  63. data/lib/steam_api_client/structs/requests/get_game_current_players.rb +54 -0
  64. data/lib/steam_api_client/structs/requests/get_player_achievements.rb +54 -0
  65. data/lib/steam_api_client/structs/requests/get_player_friends.rb +57 -0
  66. data/lib/steam_api_client/structs/requests/get_player_game_stats.rb +54 -0
  67. data/lib/steam_api_client/structs/requests/get_player_level.rb +48 -0
  68. data/lib/steam_api_client/structs/requests/get_player_owned_games.rb +56 -0
  69. data/lib/steam_api_client/structs/requests/get_player_recent_games.rb +54 -0
  70. data/lib/steam_api_client/structs/requests/get_player_steamid.rb +48 -0
  71. data/lib/steam_api_client/structs/requests/get_player_summaries.rb +52 -0
  72. data/lib/steam_api_client/structs/requests/storefront_abstract.rb +58 -0
  73. data/lib/steam_api_client/structs/types.rb +14 -0
  74. data/lib/steam_api_client/version.rb +5 -0
  75. data/lib/steam_api_client.rb +75 -0
  76. metadata +161 -0
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SteamApiClient
4
+ # Main class to interact with the Steam Api.
5
+
6
+ # Before using this client, you need to configure the
7
+ # credentials through the 'SteamApiClient::Client.configure' method:
8
+ #
9
+ # ```ruby
10
+ # SteamApiClient::Client.configure do |config|
11
+ # config.api_token = 'token-value'
12
+ # end
13
+ # ```
14
+ #
15
+ # All API calls take the form:
16
+ # http://api.steampowered.com/<interface name>/<method name>/<version>/?key=<api key>&format=<format>.
17
+ # Format can be any of:
18
+ # json - The output will be returned in the JSON format
19
+ # xml - Output is returned as an XML document
20
+ # vdf - Output is returned as a VDF file.
21
+ # If you do not specify a format, your results will be returns in the JSON format.
22
+ #
23
+ # As we dive deeper into the implementation and find more endpoints to include in this
24
+ # gem, some changes were needed to support the storefront API which is what Steam's
25
+ # Big Picture Mode uses.
26
+ # All API calls take the form:
27
+ # https://store.steampowered.com/api/<method>?appids=1,2,3&filters=basic,recommendations
28
+ # For more information, vist: https://wiki.teamfortress.com/wiki/User:RJackson/StorefrontAPI
29
+ class Client
30
+ extend ::Dry::Configurable
31
+
32
+ BASE_ENDPOINT = "https://api.steampowered.com"
33
+ STOREFRONT_API_ENDPOINT = "https://store.steampowered.com/api/"
34
+ DEFAULT_TIMEOUT = 10
35
+ DEFAULT_CONNECT_TIMEOUT = 5
36
+
37
+ setting :api_token
38
+ # Maximum time, in seconds, for the whole request
39
+ setting :timeout, default: DEFAULT_TIMEOUT
40
+ # Maximum time, in seconds, to establish the connection
41
+ setting :connect_timeout, default: DEFAULT_CONNECT_TIMEOUT
42
+
43
+ attr_writer :api_token, :timeout, :connect_timeout
44
+
45
+ attr_reader \
46
+ :app_details,
47
+ :app_global_achievements,
48
+ :app_news,
49
+ :game_current_players,
50
+ :player_achievements,
51
+ :player_friends,
52
+ :player_game_stats,
53
+ :player_level,
54
+ :player_owned_games,
55
+ :player_recent_games,
56
+ :player_steamid,
57
+ :player_summaries
58
+
59
+ # Initialize the client
60
+ # @param [String] api_token - An existing api token.
61
+ # @param [Numeric] timeout - Request timeout in seconds. Defaults to the configured value.
62
+ # @param [Numeric] connect_timeout - Connection timeout in seconds. Defaults to the configured value.
63
+ # @return [SteamApiClient::Client] - A new class instance.
64
+ def initialize(api_token: nil, timeout: nil, connect_timeout: nil)
65
+ @api_token = api_token
66
+ @timeout = timeout
67
+ @connect_timeout = connect_timeout
68
+
69
+ @app_details ||= SteamApiClient::Requestors::AppDetails.new(self)
70
+ @app_global_achievements ||= SteamApiClient::Requestors::AppGlobalAchievements.new(self)
71
+ @app_news ||= SteamApiClient::Requestors::AppNews.new(self)
72
+ @game_current_players ||= SteamApiClient::Requestors::GameCurrentPlayers.new(self)
73
+ @player_achievements ||= SteamApiClient::Requestors::PlayerAchievements.new(self)
74
+ @player_friends ||= SteamApiClient::Requestors::PlayerFriends.new(self)
75
+ @player_game_stats ||= SteamApiClient::Requestors::PlayerGameStats.new(self)
76
+ @player_level ||= SteamApiClient::Requestors::PlayerLevel.new(self)
77
+ @player_owned_games ||= SteamApiClient::Requestors::PlayerOwnedGames.new(self)
78
+ @player_recent_games ||= SteamApiClient::Requestors::PlayerRecentGames.new(self)
79
+ @player_steamid ||= SteamApiClient::Requestors::PlayerSteamId.new(self)
80
+ @player_summaries ||= SteamApiClient::Requestors::PlayerSummaries.new(self)
81
+ end
82
+
83
+ # The token sent on authorized requests. Falls back to the value set
84
+ # through 'SteamApiClient::Client.configure' when none was given to the instance.
85
+ # @return [String, nil]
86
+ def api_token
87
+ @api_token || config.api_token
88
+ end
89
+
90
+ # @return [Numeric] the request timeout in seconds
91
+ def timeout
92
+ @timeout || config.timeout
93
+ end
94
+
95
+ # @return [Numeric] the connection timeout in seconds
96
+ def connect_timeout
97
+ @connect_timeout || config.connect_timeout
98
+ end
99
+
100
+ def config
101
+ self.class.config
102
+ end
103
+
104
+ def base_endpoint
105
+ BASE_ENDPOINT
106
+ end
107
+
108
+ def storefront_endpoint
109
+ STOREFRONT_API_ENDPOINT
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,48 @@
1
+ module SteamApiClient
2
+ module Errors
3
+ # Error class that wraps an Api request failed
4
+ #
5
+ class ApiError < ::StandardError
6
+ # Initializes a request API custom error
7
+ # @param [SteamApiClient::Response] Http response wrapper
8
+ # @return A new instance of ApiError
9
+ #
10
+ def initialize(response)
11
+ @http_status_code = response.status_code
12
+ @response = response
13
+ @request = response.request
14
+
15
+ super(build_description_message)
16
+ end
17
+
18
+ private
19
+
20
+ attr_accessor :http_status_code, :response
21
+
22
+ def build_description_message
23
+ <<~MESSAGE.split.join(" ")
24
+ A request failed with the following status code: #{http_status_code}
25
+ and body: #{response.body}
26
+ MESSAGE
27
+ end
28
+ end
29
+
30
+ # status_code: 400
31
+ class HttpBadRequest < ApiError; end
32
+
33
+ # status_code: 401
34
+ class HttpUnauthorized < ApiError; end
35
+
36
+ # status_code: 403
37
+ class HttpForbidden < ApiError; end
38
+
39
+ # status_code: 404
40
+ class HttpNotFound < ApiError; end
41
+
42
+ # status_code: 408
43
+ class HttpTimeout < ApiError; end
44
+
45
+ # status_code: 500
46
+ class HttpInternalServerError < ApiError; end
47
+ end
48
+ end
@@ -0,0 +1,19 @@
1
+ module SteamApiClient
2
+ module Errors
3
+ # Raised when an API response can't be built into its response struct,
4
+ # e.g. the body isn't a JSON object or doesn't match the struct's schema.
5
+ # The underlying JSON or Dry::Struct error is available through #cause.
6
+ class ResponseParseError < ::StandardError
7
+ attr_reader :response
8
+
9
+ # @param [SteamApiClient::Response] response Http response wrapper
10
+ # @param [Class] response_struct The struct the response was built into
11
+ # @return A new instance of ResponseParseError
12
+ def initialize(response, response_struct)
13
+ @response = response
14
+
15
+ super("Unable to build #{response_struct} from the API response (status code: #{response.status_code})")
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ module SteamApiClient
2
+ module Errors
3
+ class EndpointKindNotSupported < ::StandardError; end
4
+ end
5
+ end
@@ -0,0 +1,46 @@
1
+ module SteamApiClient
2
+ # Class that checks an Api response and raise the correct exception
3
+ class FailureHandler
4
+ include Singleton
5
+
6
+ # Perform the check
7
+ # @param [SteamApiClient::Response] the response received from the Api
8
+ # @return [NilClass] if response was successful
9
+ # @raise [SteamApiClient::Errors::ApiError] if response was a failure.
10
+ def call(response)
11
+ return unless response.fail?
12
+
13
+ raise_timedout(response) if response.timed_out?
14
+
15
+ raise_response_4xx(response) if (400..499).cover?(response.status_code)
16
+ raise_response_5xx(response) if (500..599).cover?(response.status_code)
17
+
18
+ raise_api_error(response)
19
+ end
20
+
21
+ private
22
+
23
+ def raise_timedout(response)
24
+ raise SteamApiClient::Errors::HttpTimeout, response
25
+ end
26
+
27
+ def raise_response_4xx(response)
28
+ case response.status_code
29
+ when 400 then raise SteamApiClient::Errors::HttpBadRequest, response
30
+ when 401 then raise SteamApiClient::Errors::HttpUnauthorized, response
31
+ when 403 then raise SteamApiClient::Errors::HttpForbidden, response
32
+ when 404 then raise SteamApiClient::Errors::HttpNotFound, response
33
+ end
34
+ end
35
+
36
+ def raise_response_5xx(response)
37
+ case response.status_code
38
+ when 500 then raise SteamApiClient::Errors::HttpInternalServerError, response
39
+ end
40
+ end
41
+
42
+ def raise_api_error(response)
43
+ raise SteamApiClient::Errors::ApiError, response
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,86 @@
1
+ module SteamApiClient
2
+ class Request
3
+ def initialize(request_struct:, response_struct:, client:)
4
+ @failure_handler = SteamApiClient::FailureHandler.instance
5
+ @request_struct = request_struct
6
+ @response_struct = response_struct
7
+ @client = client
8
+
9
+ build_request
10
+ end
11
+
12
+ def run
13
+ freeze
14
+
15
+ raw_response = @raw_request.run
16
+
17
+ Response.new(raw_response: raw_response, response_struct: response_struct, request: self).tap do |response|
18
+ failure_handler.call(response)
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ attr_accessor :request_struct, :response_struct, :client, :raw_request, :failure_handler
25
+
26
+ def build_request
27
+ url = nil
28
+
29
+ case request_struct.endpoint_kind
30
+ when :base_endpoint then url = build_base_url
31
+ when :storefront_endpoint then url = build_storefront_url
32
+ else raise SteamApiClient::Errors::EndpointKindNotSupported
33
+ end
34
+
35
+ @raw_request = ::Typhoeus::Request.new(url.to_s, build_options)
36
+ end
37
+
38
+ def build_base_url
39
+ url = URI.join(
40
+ client.base_endpoint,
41
+ request_struct.interface,
42
+ request_struct.path,
43
+ request_struct.version
44
+ )
45
+ url.query = query_params
46
+
47
+ url
48
+ end
49
+
50
+ def build_storefront_url
51
+ url = URI.join(
52
+ client.storefront_endpoint,
53
+ request_struct.path
54
+ )
55
+ url.query = query_params
56
+
57
+ url
58
+ end
59
+
60
+ def query_params
61
+ all_params = {}.tap do |params|
62
+ params[:key] = client.api_token if request_struct.needs_authorization?
63
+ params.merge!(request_struct.query_params)
64
+ end
65
+
66
+ URI.encode_www_form(all_params)
67
+ end
68
+
69
+ def build_options
70
+ {
71
+ method: request_struct.method,
72
+ headers: default_headers,
73
+ body: request_struct.body.to_json,
74
+ followlocation: true,
75
+ timeout_ms: (client.timeout * 1000).to_i,
76
+ connecttimeout_ms: (client.connect_timeout * 1000).to_i,
77
+ # Required for sub-second timeouts and safe use of timeouts across threads
78
+ nosignal: true
79
+ }
80
+ end
81
+
82
+ def default_headers
83
+ {"Content-Type" => "application/json"}
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,27 @@
1
+ module SteamApiClient
2
+ module Requestors
3
+ # Class that expose the necessary method(s) to get the details of an
4
+ # app in the Steam Storefront Api.
5
+ class AppDetails < Base
6
+ # Get on the Api the details of an app.
7
+ # @param [Integer] appid The Steam app id
8
+ # @param [Array<Integer>] filters The Steam to filter field values
9
+ # @param [String] country_code Country code for method to return appropriate currency values.
10
+ # Defaults to 'US'.
11
+ # @param [String] locale Localized ISO 639-1 string.
12
+ # Defaults to 'en'.
13
+ # @return [SteamApiClient::Structs::AppDetails::Details] instance with the app details.
14
+ # When the app is unavailable, `response.success` is false and `response.data` is nil.
15
+ def retrieve(appid:, filters: [], country_code: "US", locale: "en")
16
+ build_request_and_run(
17
+ SteamApiClient::Structs::Requests::GetAppDetails,
18
+ SteamApiClient::Structs::AppDetails::Details,
19
+ appid: appid,
20
+ filters: filters,
21
+ cc: country_code,
22
+ l: locale
23
+ )
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,19 @@
1
+ module SteamApiClient
2
+ module Requestors
3
+ # Class that expose the necessary method(s) to get the global achievements
4
+ # of an app in the Steam Web Api.
5
+ class AppGlobalAchievements < Base
6
+ # Get on the Api all global achievements info of an App in the Steam.
7
+ # @param [Integer] gameid The Steam app id
8
+ # @return [SteamApiClient::Structs::AppGlobalAchievements] instance with the
9
+ # player summary details.
10
+ def retrieve(gameid)
11
+ build_request_and_run(
12
+ SteamApiClient::Structs::Requests::GetAppGlobalAchievements,
13
+ SteamApiClient::Structs::AppGlobalAchievements,
14
+ gameid: gameid
15
+ )
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ module SteamApiClient
2
+ module Requestors
3
+ # Class that expose the necessary method(s) to get the news of an
4
+ # app in the Steam Web Api.
5
+ class AppNews < Base
6
+ # Get on the Api one or more news of an App in the Steam.
7
+ # @param [Integer] appid The Steam app id
8
+ # @param [Integer] count The number of wanted news
9
+ # @param [Integer] maxlength The max length of the news content
10
+ # @return [SteamApiClient::Structs::AppNews] instance with the
11
+ # player summary details.
12
+ def retrieve(appid, count, maxlength = 300)
13
+ build_request_and_run(
14
+ SteamApiClient::Structs::Requests::GetAppNews,
15
+ SteamApiClient::Structs::AppNews,
16
+ appid: appid,
17
+ count: count,
18
+ maxlength: maxlength
19
+ )
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,32 @@
1
+ module SteamApiClient
2
+ module Requestors
3
+ # Class responsible to expose the methods necessary to perform a request
4
+ # on the Steam Web Api.
5
+ # @abstract class for the Requestors
6
+ class Base
7
+ # Initialize the class
8
+ # @param [SteamApiClient::Client] the client object.
9
+ def initialize(client)
10
+ @client = client
11
+ end
12
+
13
+ protected
14
+
15
+ # Build an Request and executes it
16
+ def build_request_and_run(request_struct_class, response_struct_class, **params)
17
+ request_struct = request_struct_class.build_from_params(params)
18
+
19
+ response = build_request(request_struct, response_struct_class).run
20
+ response.result
21
+ end
22
+
23
+ attr_accessor :client
24
+
25
+ private
26
+
27
+ def build_request(request_struct, response_struct_class)
28
+ Request.new(request_struct: request_struct, response_struct: response_struct_class, client: client)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,19 @@
1
+ module SteamApiClient
2
+ module Requestors
3
+ # Class that expose the necessary method(s) to get number of current players
4
+ # for a game in the Steam Web Api.
5
+ class GameCurrentPlayers < Base
6
+ # Get the number of current players for a game.
7
+ # @param [Integer] appid The Steam app id
8
+ # @return [SteamApiClient::Structs::GameCurrentPlayers] instance with the
9
+ # game current players count.
10
+ def retrieve(appid)
11
+ build_request_and_run(
12
+ SteamApiClient::Structs::Requests::GetGameCurrentPlayers,
13
+ SteamApiClient::Structs::GameCurrentPlayers,
14
+ appid: appid
15
+ )
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ module SteamApiClient
2
+ module Requestors
3
+ # Class that exposes the necessary method(s) to get the achievements of a
4
+ # player for a given app.
5
+ class PlayerAchievements < Base
6
+ # Returns a list of achievements for a user by app id.
7
+ # @param [Integer] appid The Steam app id
8
+ # @param [Integer] steamid The Steam user id
9
+ # @return [SteamApiClient::Structs::PlayerAchievements] instance with the
10
+ # player summary details.
11
+ def retrieve(appid, steamid)
12
+ build_request_and_run(
13
+ SteamApiClient::Structs::Requests::GetPlayerAchievements,
14
+ SteamApiClient::Structs::PlayerAchievements,
15
+ appid: appid,
16
+ steamid: steamid
17
+ )
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module SteamApiClient
2
+ module Requestors
3
+ # Class that expose the necessary method(s) to get an User
4
+ # friend list in the Steam Web Api.
5
+ class PlayerFriends < Base
6
+ # Get an User complete friend list, if public.
7
+ # @param [Integer] steamid The Steam user id
8
+ # @param [String] relationship Relationship filter. Possibles values: 'all', 'friend'.
9
+ # @return [SteamApiClient::Structs::PlayerFriends] instance with the
10
+ # player friends list.
11
+ def retrieve(steamid, relationship = "all")
12
+ build_request_and_run(
13
+ SteamApiClient::Structs::Requests::GetPlayerFriends,
14
+ SteamApiClient::Structs::PlayerFriends,
15
+ steamid: steamid,
16
+ relationship: relationship
17
+ )
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module SteamApiClient
2
+ module Requestors
3
+ # Class that exposes the necessary method(s) to get the stats of a
4
+ # player for a given game/appid.
5
+ class PlayerGameStats < Base
6
+ # Returns a list of stats for a user for a given game/appid.
7
+ # @param [Integer] appid The Steam app id
8
+ # @param [Integer] steamid The Steam user id
9
+ # @return [SteamApiClient::Structs::PlayerGameStats] instance with the
10
+ # player summary details.
11
+ def retrieve(appid, steamid)
12
+ build_request_and_run(
13
+ SteamApiClient::Structs::Requests::GetPlayerGameStats,
14
+ SteamApiClient::Structs::PlayerGameStats,
15
+ appid: appid,
16
+ steamid: steamid
17
+ )
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ module SteamApiClient
2
+ module Requestors
3
+ # Class that exposes the necessary method(s) to get level of an user.
4
+ class PlayerLevel < Base
5
+ # Returns the user current level on Steam.
6
+ # @param [Integer] steamid The Steam user id
7
+ # @return [SteamApiClient::Structs::PlayerLevel] instance with the
8
+ # player level.
9
+ def retrieve(steamid)
10
+ build_request_and_run(
11
+ SteamApiClient::Structs::Requests::GetPlayerLevel,
12
+ SteamApiClient::Structs::PlayerLevel,
13
+ steamid: steamid
14
+ )
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ module SteamApiClient
2
+ module Requestors
3
+ # Class that exposes the necessary method(s) to get a list of
4
+ # the player owned games, if public.
5
+ class PlayerOwnedGames < Base
6
+ # Returns a list of all games owned by the user.
7
+ # @param [Integer] steamid The Steam user id
8
+ # @param [Boolean] include_appinfo Flag to include all game related information
9
+ # @param [Boolean] include_played_free_games Flag to include free games
10
+ # @return [SteamApiClient::Structs::PlayerOwnedGames] instance with the
11
+ # player owned games details.
12
+ def retrieve(steamid, include_appinfo: false, include_played_free_games: false)
13
+ build_request_and_run(
14
+ SteamApiClient::Structs::Requests::GetPlayerOwnedGames,
15
+ SteamApiClient::Structs::PlayerOwnedGames,
16
+ steamid: steamid,
17
+ include_appinfo: include_appinfo,
18
+ include_played_free_games: include_played_free_games
19
+ )
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ module SteamApiClient
2
+ module Requestors
3
+ # Class that exposes the necessary method(s) to get a list of
4
+ # recently played games for a user.
5
+ class PlayerRecentGames < Base
6
+ # Returns a list of recently played games for a user.
7
+ # @param [Integer] steamid - The Steam user id
8
+ # @param [Integer] count - The number of games that should return
9
+ # @return [SteamApiClient::Structs::PlayerRecentGames] instance with the
10
+ # player summary details.
11
+ def retrieve(steamid, count = 5)
12
+ build_request_and_run(
13
+ SteamApiClient::Structs::Requests::GetPlayerRecentGames,
14
+ SteamApiClient::Structs::PlayerRecentGames,
15
+ steamid: steamid,
16
+ count: count
17
+ )
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ module SteamApiClient
2
+ module Requestors
3
+ # Class that exposes the necessary method(s) to get The 64 bit Steam ID
4
+ # the vanity URL resolves to.
5
+ class PlayerSteamId < Base
6
+ # Returns the 64 bit Steam ID the vanity URL resolves to.
7
+ # @param [String] vanityurl - The Steam user short url
8
+ # @return [SteamApiClient::Structs::PlayerSteamId] instance with the
9
+ # player SteamId.
10
+ def retrieve(vanityurl)
11
+ build_request_and_run(
12
+ SteamApiClient::Structs::Requests::GetPlayerSteamId,
13
+ SteamApiClient::Structs::PlayerSteamId,
14
+ vanityurl: vanityurl
15
+ )
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module SteamApiClient
2
+ module Requestors
3
+ # Class that expose the necessary method(s) to get the summary of a
4
+ # player profile in the Steam Web Api.
5
+ class PlayerSummaries < Base
6
+ # Get on the Api the summary of a player profile.
7
+ # @param [Array<Integer>] steamids - The Steam order's id
8
+ # @return [SteamApiClient::Structs::PlayerSummaries] instance with the
9
+ # player summary details.
10
+ def retrieve(steamids)
11
+ build_request_and_run(
12
+ SteamApiClient::Structs::Requests::GetPlayerSummaries,
13
+ SteamApiClient::Structs::PlayerSummaries,
14
+ steamids: steamids
15
+ )
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,64 @@
1
+ require "forwardable"
2
+
3
+ module SteamApiClient
4
+ # Wraps the raw response received from the Api
5
+ class Response
6
+ extend Forwardable
7
+
8
+ def_delegators :@raw_response, :timed_out?, :success?, :body
9
+ attr_accessor :request
10
+
11
+ # Initialize a new instance
12
+ # @param [Typhoeus::Response] raw_response
13
+ # @param [SteamApiClient::Structs::Base] response_struct The struct necessary to
14
+ # build the response received from Api
15
+ # @param [SteamApiClient::Request] request The wrapper Request class
16
+ def initialize(raw_response:, response_struct:, request:)
17
+ @raw_response = raw_response
18
+ @response_struct = response_struct
19
+ @request = request
20
+ end
21
+
22
+ # Check if the request was successful or not
23
+ # @return [Boolean]
24
+ def fail?
25
+ !success?
26
+ end
27
+
28
+ # Returns the HTTP status code
29
+ # @return [Integer]
30
+ def status_code
31
+ raw_response.code
32
+ end
33
+
34
+ # Parses the response's json body as a Hash. If the response body isn't a json
35
+ # returns an empty hash
36
+ # @return [Hash]
37
+ def to_h
38
+ parse_body
39
+ rescue JSON::ParserError
40
+ {}
41
+ end
42
+
43
+ # Parses the response's json body and build the expected struct
44
+ # @return [SteamApiClient::Structs::Base]
45
+ # @raise [SteamApiClient::Errors::ResponseParseError] if the body isn't a JSON object
46
+ # or doesn't match the response struct
47
+ def result
48
+ json_response = parse_body
49
+ raise TypeError, "expected a JSON object, got #{json_response.class}" unless json_response.is_a?(Hash)
50
+
51
+ response_struct.build_from_response(json_response)
52
+ rescue JSON::ParserError, TypeError, Dry::Struct::Error
53
+ raise Errors::ResponseParseError.new(self, response_struct)
54
+ end
55
+
56
+ private
57
+
58
+ attr_accessor :raw_response, :response_struct
59
+
60
+ def parse_body
61
+ ::JSON.parse(raw_response.body.to_s, symbolize_names: true)
62
+ end
63
+ end
64
+ end