diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..bd59ac6 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true +max_line_length = 100 diff --git a/.gitignore b/.gitignore index c28d848..0561908 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ spec/secret.rb test/tmp test/version_tmp tmp +spec/examples.txt diff --git a/.rspec b/.rspec index 6515722..677bc22 100644 --- a/.rspec +++ b/.rspec @@ -1 +1,4 @@ --f d -c --warning +--require spec_helper +--format doc +--color +--warning diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..ddcf4cd --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,21 @@ +require: + - rubocop-rake + - rubocop-rspec + +AllCops: + NewCops: enable + TargetRubyVersion: 2.4 + +Layout/LineLength: + Max: 100 + +Metrics/BlockLength: + Exclude: + - spec/**/*.rb + +Naming/FileName: + Exclude: + - lib/steam-api.rb +Naming/AccessorMethodName: + Exclude: + - lib/steam-api/steam/**/* diff --git a/Gemfile b/Gemfile index 7ace430..16c3deb 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + source 'https://rubygems.org' # Specify your gem's dependencies in simple-steam.gemspec diff --git a/Rakefile b/Rakefile index 2922906..4050718 100644 --- a/Rakefile +++ b/Rakefile @@ -1,7 +1,11 @@ +# frozen_string_literal: true + require 'bundler/gem_tasks' require 'rspec/core/rake_task' RSpec::Core::RakeTask.new task default: :spec + +desc 'Alias for spec task' task test: :spec diff --git a/lib/steam-api.rb b/lib/steam-api.rb index f69ed80..47e035d 100644 --- a/lib/steam-api.rb +++ b/lib/steam-api.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'faraday' require 'json' @@ -6,7 +8,6 @@ require_relative 'steam-api/client' require_relative 'steam-api/exceptions' require_relative 'steam-api/response' -require_relative 'steam-api/helpers' require_relative 'steam-api/steam/apps' require_relative 'steam-api/steam/economy' require_relative 'steam-api/steam/news' @@ -15,5 +16,3 @@ require_relative 'steam-api/steam/store' require_relative 'steam-api/steam/user' require_relative 'steam-api/steam/user_stats' - -include Steam::Helpers diff --git a/lib/steam-api/client.rb b/lib/steam-api/client.rb index c7316dd..922a957 100644 --- a/lib/steam-api/client.rb +++ b/lib/steam-api/client.rb @@ -1,7 +1,13 @@ +# frozen_string_literal: true + module Steam # Client object used to communicate with the steam webapi class Client - def initialize(url) + # @param [String] api The endpoint of the API + # @param [String] base_url the root uri for steam's API + # @return [Steam::Client] The Client + def initialize(api, base_url: 'https://api.steampowered.com') + url = File.join(base_url, api) @conn = Faraday.new(url: url) end @@ -12,8 +18,8 @@ def initialize(url) def get(resource, params: {}, key: Steam.apikey) params[:key] = key response = @conn.get resource, params - JSON.parse(response.body) - # response + response = JSON.parse(response.body) + response = Response.new response rescue JSON::ParserError puts response.body # If the steam web api returns an error it's virtually never in json, so @@ -21,7 +27,7 @@ def get(resource, params: {}, key: Steam.apikey) # for errors. raise Steam::UnavailableError if response.status == '503' - { error: '500 Internal Server Error' } + Response.new error: '500 Internal Server Error' end end end diff --git a/lib/steam-api/exceptions.rb b/lib/steam-api/exceptions.rb index 84352c2..7fadaa9 100644 --- a/lib/steam-api/exceptions.rb +++ b/lib/steam-api/exceptions.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Steam # Error returning the requested object from the Steam API class SteamError < StandardError diff --git a/lib/steam-api/helpers.rb b/lib/steam-api/helpers.rb deleted file mode 100644 index 5ec9b34..0000000 --- a/lib/steam-api/helpers.rb +++ /dev/null @@ -1,15 +0,0 @@ -module Steam - # Helper Module - module Helpers - # Conveniance method to build clients - # @param [String] api The endpoint of the API - # @param [String] base_url the root uri for steam's API - # @return [Steam::Client] The Client - def build_client(api, base_url: 'https://api.steampowered.com') - Steam::Client.new([base_url, api].join('/')) - end - - - STORE_API_BASE_URL = 'https://store.steampowered.com/api' - end -end diff --git a/lib/steam-api/response.rb b/lib/steam-api/response.rb index 23167f0..4048875 100644 --- a/lib/steam-api/response.rb +++ b/lib/steam-api/response.rb @@ -1,33 +1,39 @@ +# frozen_string_literal: true + module Steam # Since the steam responses are so randomly inconsistant we're making a new # class to manage the responses. - # FIXME: move all hash extensions here once the gem is finished and make class Response < Hash - # def parse_key(key) - # fail Steam::JSONError unless self.key?(key) - # self[key] - # end - end -end + def initialize(raw_response) + super() -class Hash - # Simple method to access a nested field, since Valve seems to like - # nesting their json a few levels on every request. - # @param [String] key The key to extract from the hash - def parse_key(key) - raise Steam::JSONError unless key?(key) + raw_response.each do |key, value| + self[key] = + case value + when Hash then self.class.new(value) + else value + end + end + end - self[key] - end + # Simple method to access a nested field, since Valve seems to like + # nesting their json a few levels on every request. + # @param [String] key The key to extract from the hash + def parse_key(key) + raise Steam::JSONError unless key?(key) + + self[key] + end - # Many responses from the apis (but not all) include a success - # field, so this allows us to check it wiht minimal fuss. - # @param [String] success_condition what the success condition should be - # @return [Boolean] Returns true or raises an exception. - def check_success(success_condition: true) - success = parse_key('success') - raise Steam::SteamError unless success == success_condition + # Many responses from the apis (but not all) include a success + # field, so this allows us to check it wiht minimal fuss. + # @param [String] success_condition what the success condition should be + # @return [Boolean] Returns true or raises an exception. + def check_success(success_condition: true) + success = parse_key('success') + raise Steam::SteamError unless success == success_condition - true + true + end end end diff --git a/lib/steam-api/ruby/hash.rb b/lib/steam-api/ruby/hash.rb index 6f94470..5a54226 100644 --- a/lib/steam-api/ruby/hash.rb +++ b/lib/steam-api/ruby/hash.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Takes a hash and converts it into a URL encoded parameter string. # NOTE: this does not do any uri escaping at this point, since all args # should be numeric. diff --git a/lib/steam-api/steam.rb b/lib/steam-api/steam.rb index 95f2650..a89c357 100644 --- a/lib/steam-api/steam.rb +++ b/lib/steam-api/steam.rb @@ -1,19 +1,16 @@ -# Base class def +# frozen_string_literal: true + +# Base class definition module Steam - @apikey = ENV['STEAM_API_KEY'] + class << self + attr_writer :apikey - def self.apikey - if @apikey.nil? - unless ENV.key?('STEAM_API_KEY') - raise ArgumentError, 'Please set your Steam API key.' - end + def apikey + return @apikey if @apikey - @apikey = ENV['STEAM_API_KEY'] - end - @apikey - end + raise ArgumentError, 'Please set your Steam API key.' unless ENV.key?('STEAM_API_KEY') - def self.apikey=(key) - @apikey = key + @apikey = ENV.fetch('STEAM_API_KEY') + end end end diff --git a/lib/steam-api/steam/apps.rb b/lib/steam-api/steam/apps.rb index 522ee7b..f452d94 100644 --- a/lib/steam-api/steam/apps.rb +++ b/lib/steam-api/steam/apps.rb @@ -1,47 +1,50 @@ +# frozen_string_literal: true + module Steam # A Ruby DSL for communicating with the Apps portion of the Steam Web API. # @see https://developer.valvesoftware.com/wiki/Steam_Web_API # @since 1.0.0 module Apps - # Get Steam Applications - # @return [Hash] A list of objects containing the title and app ID of - # each program available in the store. - # @see http://wiki.teamfortress.com/wiki/WebAPI/GetAppList - def self.get_all - response = client.get('GetApplist/v2') - .parse_key('applist') - .parse_key('apps') - response - end + class << self + # Get Steam Applications + # @return [Hash] A list of objects containing the title and app ID of + # each program available in the store. + # @see http://wiki.teamfortress.com/wiki/WebAPI/GetAppList + def get_all + client.get('GetApplist/v2').parse_key('applist').parse_key('apps') + end - # Get Servers at Address - # @param [String] addr IP or IP:queryport to list - # @return [Hash] A hash containing the API response - # @see http://wiki.teamfortress.com/wiki/WebAPI/GetServersAtAddress - def self.get_servers(addr: nil, api_version: 'v1') - response = client.get "GetServersAtAddress/#{api_version}", - params: { addr: ERB::Util.url_encode(addr) } - response = response.parse_key('response') - response.check_success - response.parse_key('servers') - end + # Get Servers at Address + # @param [String] addr IP or IP:queryport to list + # @return [Hash] A hash containing the API response + # @see http://wiki.teamfortress.com/wiki/WebAPI/GetServersAtAddress + def get_servers(addr: nil, api_version: 'v1') + response = client.get( + "GetServersAtAddress/#{api_version}", params: { addr: ERB::Util.url_encode(addr) } + ) + response = response.parse_key('response') + response.check_success + response.parse_key('servers') + end - # Check if a given version of an App is current - # @param [Fixnum] appid AppID of game - # @param [Fixnum] version The installed version of the game - # @return [Hash] A hash containing the API response - # @see http://wiki.teamfortress.com/wiki/WebAPI/UpToDateCheck - def self.up_to_date(appid: nil, version: 'v1', api_version: 'v1') - response = client.get "UpToDateCheck/#{api_version}", - params: { appid: appid, version: version } - response = response.parse_key('response') - response.check_success - response.delete('success') - response - end + # Check if a given version of an App is current + # @param [Fixnum] appid AppID of game + # @param [Fixnum] version The installed version of the game + # @return [Hash] A hash containing the API response + # @see http://wiki.teamfortress.com/wiki/WebAPI/UpToDateCheck + def up_to_date(appid: nil, version: 'v1', api_version: 'v1') + response = client.get( + "UpToDateCheck/#{api_version}", params: { appid: appid, version: version } + ) + response = response.parse_key('response') + response.check_success + response.delete('success') + response + end - def self.client - build_client 'ISteamApps' + def client + Steam::Client.new 'ISteamApps' + end end end end diff --git a/lib/steam-api/steam/economy.rb b/lib/steam-api/steam/economy.rb index 616dad9..084e921 100644 --- a/lib/steam-api/steam/economy.rb +++ b/lib/steam-api/steam/economy.rb @@ -1,60 +1,63 @@ +# frozen_string_literal: true + module Steam # A Ruby DSL for communicating with the Steam Web API. # @see https://developer.valvesoftware.com/wiki/Steam_Web_API # @since 1.0.0 module Economy - # Get Asset Class Info - # @param [String] appid The application ID for the Steam Game. - # @param [Hash] params Parameters to pass to the API - # @option params [Fixnum] :class_count The number of classids passed to - # the request. - # @option params [Fixnum] :classidN Where N can be a series of sequential - # numbers to form a list of class IDs. [1] [2] - # @option params [Fixnum] :instanceidN Instance ID of the nth class. - # @option params [String] :language The ISO639-1 language code for the - # language all localized strings should be returned in. Not all strings - # have been translated to every language. - # If a language does not have a string, the English string will be - # returned instead. If this parameter is omitted the string token will - # be returned for the strings. - # @return [Hash] A hash containing the API response - # @see http://wiki.teamfortress.com/wiki/WebAPI/UpToDateCheck - def self.asset_info(appid, params: {}) - params[:appid] = appid - response = client.get 'GetAssetClassInfo/v1', params: params - parse_response(response) - end + class << self + # Get Asset Class Info + # @param [String] appid The application ID for the Steam Game. + # @param [Hash] params Parameters to pass to the API + # @option params [Fixnum] :class_count The number of classids passed to + # the request. + # @option params [Fixnum] :classidN Where N can be a series of sequential + # numbers to form a list of class IDs. [1] [2] + # @option params [Fixnum] :instanceidN Instance ID of the nth class. + # @option params [String] :language The ISO639-1 language code for the + # language all localized strings should be returned in. Not all strings + # have been translated to every language. + # If a language does not have a string, the English string will be + # returned instead. If this parameter is omitted the string token will + # be returned for the strings. + # @return [Hash] A hash containing the API response + # @see http://wiki.teamfortress.com/wiki/WebAPI/UpToDateCheck + def asset_info(appid, params: {}) + params[:appid] = appid + response = client.get 'GetAssetClassInfo/v1', params: params + parse_response(response) + end - # Get Asset Prices - # @param [String] appid The application ID for the Steam Game. - # @param [String] language The ISO639-1 language code for the language - # all localized strings should be returned in. Not all strings have been - # translated to every language. - # If a language does not have a string, the English string will be - # returned instead. If this parameter is omitted the string token will - # be returned for the strings. (Optional) - # @param [String] currency The ISO 4217 code for currency specific - # filtering. (Optional) - # @return [Hash] A hash containing the API response - # @see http://wiki.teamfortress.com/wiki/WebAPI/GetAssetPrices - def self.asset_prices(appid, language: nil, currency: nil) - params = { appid: appid } - params[:language] = language unless language.nil? - params[:currency] = currency unless currency.nil? - response = client.get 'GetAssetPrices/v1', - params: params - parse_response(response) - end + # Get Asset Prices + # @param [String] appid The application ID for the Steam Game. + # @param [String] language The ISO639-1 language code for the language + # all localized strings should be returned in. Not all strings have been + # translated to every language. + # If a language does not have a string, the English string will be + # returned instead. If this parameter is omitted the string token will + # be returned for the strings. (Optional) + # @param [String] currency The ISO 4217 code for currency specific + # filtering. (Optional) + # @return [Hash] A hash containing the API response + # @see http://wiki.teamfortress.com/wiki/WebAPI/GetAssetPrices + def asset_prices(appid, language: nil, currency: nil) + params = { appid: appid } + params[:language] = language unless language.nil? + params[:currency] = currency unless currency.nil? + response = client.get 'GetAssetPrices/v1', params: params + parse_response(response) + end - def self.client - build_client 'ISteamEconomy' - end + def client + Steam::Client.new 'ISteamEconomy' + end - def self.parse_response(response) - response = response.parse_key('result') - response.check_success - response.delete('success') - response + def parse_response(response) + response = response.parse_key('result') + response.check_success + response.delete('success') + response + end end end end diff --git a/lib/steam-api/steam/news.rb b/lib/steam-api/steam/news.rb index abd0ff9..de9efd6 100644 --- a/lib/steam-api/steam/news.rb +++ b/lib/steam-api/steam/news.rb @@ -1,32 +1,34 @@ +# frozen_string_literal: true + module Steam # A Ruby DSL for communicating with the Steam Web API. # @see https://developer.valvesoftware.com/wiki/Steam_Web_API # @since 1.0.0 module News - # Get News for App - # @param [Hash] params Parameters to pass to the API - # @option params [String] :appid The application ID for the Steam Game. - # @option params [String] :key Steam Api Key - # @option params [Fixnum] :count How many news enties you want to get - # returned. (Optional) - # @option params [Fixnum] :maxlength Maximum length of each news - # entry. (Optional) - # @option params [Fixnum] :enddate Unix timestamp, returns posts before - # this date. (Optional) - # @option params [String] :feeds Commma-seperated list of feed names to - # return news for. (Optional) - # @return [Hash] A hash object of the latest news items for a game - # specified by its appID. - # @see http://wiki.teamfortress.com/wiki/WebAPI/GetNewsForApp - def self.get(appid, params: {}) - params[:appid] = appid - client.get('GetNewsForApp/v2', params: params) - .parse_key('appnews') - .parse_key('newsitems') - end + class << self + # Get News for App + # @param [Hash] params Parameters to pass to the API + # @option params [String] :appid The application ID for the Steam Game. + # @option params [String] :key Steam Api Key + # @option params [Fixnum] :count How many news enties you want to get + # returned. (Optional) + # @option params [Fixnum] :maxlength Maximum length of each news + # entry. (Optional) + # @option params [Fixnum] :enddate Unix timestamp, returns posts before + # this date. (Optional) + # @option params [String] :feeds Commma-seperated list of feed names to + # return news for. (Optional) + # @return [Hash] A hash object of the latest news items for a game + # specified by its appID. + # @see http://wiki.teamfortress.com/wiki/WebAPI/GetNewsForApp + def get(appid, params: {}) + params[:appid] = appid + client.get('GetNewsForApp/v2', params: params).parse_key('appnews').parse_key('newsitems') + end - def self.client - build_client 'ISteamNews' + def client + Steam::Client.new 'ISteamNews' + end end end end diff --git a/lib/steam-api/steam/player.rb b/lib/steam-api/steam/player.rb index 42ea892..c6d518e 100644 --- a/lib/steam-api/steam/player.rb +++ b/lib/steam-api/steam/player.rb @@ -1,73 +1,71 @@ +# frozen_string_literal: true + module Steam # A Ruby DSL for communicating with the Steam Web API. # @see https://developer.valvesoftware.com/wiki/Steam_Web_API # @since 1.0.0 module Player - # Get Owned Games - # @param [Hash] params Parameters to pass to the API - # @option params [Fixnum] :steamid The 64 bit ID of the player. (Optional) - # @option params [Integer] :include_appinfo (0) Whether or not to include - # additional details of apps - name and images. - # @option params [Boolean] :include_played_free_games (false) Whether or - # not to list free-to-play games in the results. - # @option params [Array] :appids_filter You can optionally filter the list - # to a set of appids. - # Note that these cannot be passed as a URL parameter, instead you must - # use the JSON format described in - # Steam_Web_API#Calling_Service_interfaces. The expected input is an - # array of integers (in JSON: "appids_filter: [ 440, 500, 550 ]" ) - # @see http://wiki.teamfortress.com/wiki/WebAPI/GetOwnedGames - def self.owned_games(steamid, params: {}) - params[:steamid] = steamid - response = client.get 'GetOwnedGames/v1', params: params - response.parse_key('response') - end + class << self + # Get Owned Games + # @param [Hash] params Parameters to pass to the API + # @option params [Fixnum] :steamid The 64 bit ID of the player. (Optional) + # @option params [Integer] :include_appinfo (0) Whether or not to include + # additional details of apps - name and images. + # @option params [Boolean] :include_played_free_games (false) Whether or + # not to list free-to-play games in the results. + # @option params [Array] :appids_filter You can optionally filter the list + # to a set of appids. + # Note that these cannot be passed as a URL parameter, instead you must + # use the JSON format described in + # Steam_Web_API#Calling_Service_interfaces. The expected input is an + # array of integers (in JSON: "appids_filter: [ 440, 500, 550 ]" ) + # @see http://wiki.teamfortress.com/wiki/WebAPI/GetOwnedGames + def owned_games(steamid, params: {}) + params[:steamid] = steamid + response = client.get 'GetOwnedGames/v1', params: params + response.parse_key('response') + end - # Get Recently Played Games - # @param [Hash] params Parameters to pass to the API - # @option params [String] :steamid The SteamID of the account. - # @option params [String] :count Optionally limit to a certain number of - # games (the number of games a person has played in the last 2 weeks is - # typically very small) - # @see http://wiki.teamfortress.com/wiki/WebAPI/GetRecentlyPlayedGames - def self.recently_played_games(steamid, params: {}) - params[:steamid] = steamid - response = client.get 'GetRecentlyPlayedGames/v1', - params: params - response.parse_key('response') - end + # Get Recently Played Games + # @param [Hash] params Parameters to pass to the API + # @option params [String] :steamid The SteamID of the account. + # @option params [String] :count Optionally limit to a certain number of + # games (the number of games a person has played in the last 2 weeks is + # typically very small) + # @see http://wiki.teamfortress.com/wiki/WebAPI/GetRecentlyPlayedGames + def recently_played_games(steamid, params: {}) + params[:steamid] = steamid + response = client.get 'GetRecentlyPlayedGames/v1', params: params + response.parse_key('response') + end - # Get a player's Steam Level - # @param [Fixnum] steamid The SteamID of the account. - # @see http://wiki.teamfortress.com/wiki/WebAPI/GetSteamLevel - def self.steam_level(steamid) - response = client.get 'GetSteamLevel/v1', - params: { steamid: steamid } - response.parse_key('response') - .parse_key('player_level') - end + # Get a player's Steam Level + # @param [Fixnum] steamid The SteamID of the account. + # @see http://wiki.teamfortress.com/wiki/WebAPI/GetSteamLevel + def steam_level(steamid) + response = client.get 'GetSteamLevel/v1', params: { steamid: steamid } + response.parse_key('response').parse_key('player_level') + end - # Get a player's Steam badges - # @param [Fixnum] steamid The SteamID of the account. - # @see http://wiki.teamfortress.com/wiki/WebAPI/GetBadges - def self.badges(steamid) - response = client.get 'GetBadges/v1', - params: { steamid: steamid } - response.parse_key('response') - end + # Get a player's Steam badges + # @param [Fixnum] steamid The SteamID of the account. + # @see http://wiki.teamfortress.com/wiki/WebAPI/GetBadges + def badges(steamid) + response = client.get 'GetBadges/v1', params: { steamid: steamid } + response.parse_key('response') + end - # Get a player's Steam Level - # @param [Fixnum] steamid The SteamID of the account. - # @see http://wiki.teamfortress.com/wiki/WebAPI/GetCommunityBadgeProgress - def self.community_badge_progress(steamid) - response = client.get 'GetCommunityBadgeProgress/v1', - params: { steamid: steamid } - response.parse_key('response') - .parse_key('quests') - end + # Get a player's Steam Level + # @param [Fixnum] steamid The SteamID of the account. + # @see http://wiki.teamfortress.com/wiki/WebAPI/GetCommunityBadgeProgress + def community_badge_progress(steamid) + response = client.get 'GetCommunityBadgeProgress/v1', params: { steamid: steamid } + response.parse_key('response').parse_key('quests') + end - def self.client - build_client 'IPlayerService' + def client + Steam::Client.new 'IPlayerService' + end end end end diff --git a/lib/steam-api/steam/remote_storage.rb b/lib/steam-api/steam/remote_storage.rb index 2cedc63..d8987b3 100644 --- a/lib/steam-api/steam/remote_storage.rb +++ b/lib/steam-api/steam/remote_storage.rb @@ -1,35 +1,37 @@ +# frozen_string_literal: true + module Steam # A Ruby DSL for communicating with the Steam Web API. # @see https://developer.valvesoftware.com/wiki/Steam_Web_API # @since 1.0.0 class RemoteStorage - # Get Published File Details - # @param [Hash] params Parameters to pass to the API - # @option params [Fixnum] :itemcount Number of items being requested - # @option params [Fixnum] :publishedfileids Published file id to look up - # @return [Hash] A hash containing the API response - # @see http://wiki.teamfortress.com/wiki/WebAPI/GetPublishedFileDetails - def self.published_file(params: {}) - response = client.get 'GetPublishedFileDetails/v1', params: params - response - end + class << self + # Get Published File Details + # @param [Hash] params Parameters to pass to the API + # @option params [Fixnum] :itemcount Number of items being requested + # @option params [Fixnum] :publishedfileids Published file id to look up + # @return [Hash] A hash containing the API response + # @see http://wiki.teamfortress.com/wiki/WebAPI/GetPublishedFileDetails + def published_file(params: {}) + client.get 'GetPublishedFileDetails/v1', params: params + end - # Get UGC File Details - # @param [Hash] params Parameters to pass to the API - # @option params [String] :key Steam Api Key - # @option params [Fixnum] :steamid If specified, only returns details - # if the file is owned by the SteamID specified - # @option params [Fixnum] :ugcid ID of UGC file to get info for - # @option params [Fixnum] :appid appID of product - # @return [Hash] A hash containing the API response - # @see http://wiki.teamfortress.com/wiki/WebAPI/GetUGCFileDetails - def self.ugc_file(params: {}) - response = client.get 'GetUGCFileDetails/v1', params: params - response - end + # Get UGC File Details + # @param [Hash] params Parameters to pass to the API + # @option params [String] :key Steam Api Key + # @option params [Fixnum] :steamid If specified, only returns details + # if the file is owned by the SteamID specified + # @option params [Fixnum] :ugcid ID of UGC file to get info for + # @option params [Fixnum] :appid appID of product + # @return [Hash] A hash containing the API response + # @see http://wiki.teamfortress.com/wiki/WebAPI/GetUGCFileDetails + def ugc_file(params: {}) + client.get 'GetUGCFileDetails/v1', params: params + end - def self.client - build_client 'ISteamRemoteStorage' + def client + Steam::Client.new 'ISteamRemoteStorage' + end end end end diff --git a/lib/steam-api/steam/store.rb b/lib/steam-api/steam/store.rb index f2f8196..998a2e3 100644 --- a/lib/steam-api/steam/store.rb +++ b/lib/steam-api/steam/store.rb @@ -1,20 +1,23 @@ +# frozen_string_literal: true + module Steam # A Ruby DSL for communicating with the Steam Web API. # @see https://developer.valvesoftware.com/wiki/Steam_Web_API # @since 1.0.0 module Store - # Get App Details - # @param String appid The UUID of the Steam Application - # @see https://wiki.teamfortress.com/wiki/User:RJackson/StorefrontAPI#appdetails - def self.app_details(appid) - response = client.get 'appdetails', params: { appids: appid } - response - end + class << self + # Get App Details + # @param String appid The UUID of the Steam Application + # @see https://wiki.teamfortress.com/wiki/User:RJackson/StorefrontAPI#appdetails + def app_details(appid) + client.get 'appdetails', params: { appids: appid } + end - private + private - def self.client - build_client '', base_url: Steam::Helpers::STORE_API_BASE_URL + def client + Steam::Client.new '', base_url: 'https://store.steampowered.com/api' + end end end end diff --git a/lib/steam-api/steam/user.rb b/lib/steam-api/steam/user.rb index 7eab724..695cfcc 100644 --- a/lib/steam-api/steam/user.rb +++ b/lib/steam-api/steam/user.rb @@ -1,88 +1,84 @@ -# -*- encoding: utf-8 -*- +# frozen_string_literal: true + module Steam # A Ruby DSL for communicating with the Steam::User Web API. # @see https://developer.valvesoftware.com/wiki/Steam_Web_API # @since 1.0.0 module User - # Get User's Friend List - # @param [String] steamid - # @param [String] relationship Relationship filter. - # Possibles values: all, friend. - # @return [Hash] A hash object resulting from the API call; should - # returns the friend list of any Steam user, provided their Steam - # Community profile visibility is set to "Public". - # @see http://wiki.teamfortress.com/wiki/WebAPI/GetFriendList - def self.friends(steamid, relationship: :all) - response = client.get 'GetFriendList/v1/', - params: { steamid: steamid, - relationship: relationship } - response = response.parse_key('friendslist') - .parse_key('friends') - response - end + class << self + # Get User's Friend List + # @param [String] steamid + # @param [String] relationship Relationship filter. + # Possibles values: all, friend. + # @return [Hash] A hash object resulting from the API call; should + # returns the friend list of any Steam user, provided their Steam + # Community profile visibility is set to "Public". + # @see http://wiki.teamfortress.com/wiki/WebAPI/GetFriendList + def friends(steamid, relationship: :all) + response = client.get( + 'GetFriendList/v1/', params: { steamid: steamid, relationship: relationship } + ) + response.parse_key('friendslist').parse_key('friends') + end - # Get Multiple Player Bans - # @param [Array] steamids Array of SteamIDs - # @return [Hash] A hash containing the API response - # @see http://wiki.teamfortress.com/wiki/WebAPI/GetPlayerBans - def self.bans(steamids) - steamids = [steamids] unless steamids.is_a?(Array) - response = client.get 'GetPlayerBans/v1/', - params: { steamids: steamids.join(',') } - response - end + # Get Multiple Player Bans + # @param [Array] steamids Array of SteamIDs + # @return [Hash] A hash containing the API response + # @see http://wiki.teamfortress.com/wiki/WebAPI/GetPlayerBans + def bans(steamids) + steamids = [steamids] unless steamids.is_a?(Array) + client.get 'GetPlayerBans/v1/', params: { steamids: steamids.join(',') } + end - # Get Player Summaries - # @option params [Array] :steamids List of player's steamids - # @return [Hash] The hash object resulting from the API call. Some data - # associated with a Steam account may be hidden if the user has their - # profile visibility set to "Friends Only" or "Private". In that case, - # only public data will be returned. - # @see http://wiki.teamfortress.com/wiki/WebAPI/GetPlayerSummaries - def self.summary(steamid) - summaries([steamid]).first - end + # Get Player Summaries + # @option params [Array] :steamids List of player's steamids + # @return [Hash] The hash object resulting from the API call. Some data + # associated with a Steam account may be hidden if the user has their + # profile visibility set to "Friends Only" or "Private". In that case, + # only public data will be returned. + # @see http://wiki.teamfortress.com/wiki/WebAPI/GetPlayerSummaries + def summary(steamid) + summaries([steamid]).first + end - # Get Player Summaries - # @param [Array] steamids List of player's steamids - # @return [Hash] The hash object resulting from the API call. Some data - # associated with a Steam account may be hidden if the user has their - # profile visibility set to "Friends Only" or "Private". In that case, - # only public data will be returned. - # @see http://wiki.teamfortress.com/wiki/WebAPI/GetPlayerSummaries - def self.summaries(steamids) - response = client.get 'GetPlayerSummaries/v2/', - params: { steamids: steamids.join(',') } - response.parse_key('response') - .parse_key('players') - end + # Get Player Summaries + # @param [Array] steamids List of player's steamids + # @return [Hash] The hash object resulting from the API call. Some data + # associated with a Steam account may be hidden if the user has their + # profile visibility set to "Friends Only" or "Private". In that case, + # only public data will be returned. + # @see http://wiki.teamfortress.com/wiki/WebAPI/GetPlayerSummaries + def summaries(steamids) + response = client.get 'GetPlayerSummaries/v2/', params: { steamids: steamids.join(',') } + response.parse_key('response').parse_key('players') + end - # Get User Groups - # @param [Fixnum] steamid 64bit Steam ID to return friend list. - # @return [Hash] A hash containing the API response - # @see http://wiki.teamfortress.com/wiki/WebAPI/GetUserGroupList - def self.groups(steamid) - response = client.get 'GetUserGroupList/v1', params: { steamid: steamid } - response = response.parse_key('response') - response.check_success - response.parse_key('groups') - end + # Get User Groups + # @param [Fixnum] steamid 64bit Steam ID to return friend list. + # @return [Hash] A hash containing the API response + # @see http://wiki.teamfortress.com/wiki/WebAPI/GetUserGroupList + def groups(steamid) + response = client.get 'GetUserGroupList/v1', params: { steamid: steamid } + response = response.parse_key('response') + response.check_success + response.parse_key('groups') + end - # Resolve Vanity URL - # @param [String] vanityurl The vanity URL part of a user's Steam - # profile URL. This is the basename of http://steamcommunity.com/id/ URLs - # @return [Hash] A hash containing the API response - # @see http://wiki.teamfortress.com/wiki/WebAPI/ResolveVanityURL - def self.vanity_to_steamid(vanityurl) - response = client.get 'ResolveVanityURL/v1', - params: { vanityurl: vanityurl } - response = response.parse_key('response') - response.check_success(success_condition: 1) - response.parse_key('steamid') - end + # Resolve Vanity URL + # @param [String] vanityurl The vanity URL part of a user's Steam + # profile URL. This is the basename of http://steamcommunity.com/id/ URLs + # @return [Hash] A hash containing the API response + # @see http://wiki.teamfortress.com/wiki/WebAPI/ResolveVanityURL + def vanity_to_steamid(vanityurl) + response = client.get 'ResolveVanityURL/v1', params: { vanityurl: vanityurl } + response = response.parse_key('response') + response.check_success(success_condition: 1) + response.parse_key('steamid') + end - def self.client - build_client 'ISteamUser' + def client + Steam::Client.new 'ISteamUser' + end end end end diff --git a/lib/steam-api/steam/user_stats.rb b/lib/steam-api/steam/user_stats.rb index 5e2981f..5c8c7b4 100644 --- a/lib/steam-api/steam/user_stats.rb +++ b/lib/steam-api/steam/user_stats.rb @@ -1,94 +1,92 @@ +# frozen_string_literal: true + module Steam # A Ruby DSL for communicating with the Steam Web API. # @see https://developer.valvesoftware.com/wiki/Steam_Web_API # @since 1.0.0 module UserStats - # Get Global Achievement Percentages for App - # @param [Fixnum] appid The ID of the game or application - # @return [Hash] The hash object of information on the global achievements - # overview of a specific game in percentages. - # @see http://wiki.teamfortress.com/wiki/WebAPI/GetGlobalAchievementPercentagesForApp - def self.achievement_percentages(appid) - response = client.get 'GetGlobalAchievementPercentagesForApp/v2', - params: { gameid: appid } - response = response.parse_key('achievementpercentages') - .parse_key('achievements') - response - end + class << self + # Get Global Achievement Percentages for App + # @param [Fixnum] appid The ID of the game or application + # @return [Hash] The hash object of information on the global achievements + # overview of a specific game in percentages. + # @see http://wiki.teamfortress.com/wiki/WebAPI/GetGlobalAchievementPercentagesForApp + def achievement_percentages(appid) + response = client.get 'GetGlobalAchievementPercentagesForApp/v2', params: { gameid: appid } + response.parse_key('achievementpercentages').parse_key('achievements') + end - # Get Global Stats for Game - # @param [Fixnum] appid The ID of the game or application - # @param [Hash] params Parameters to pass to the API - # @option params [Fixnum] :count Number of stats to get data for. - # @option params [String] :name[0] Names of the stats to get. For more than - # one value, use a parameter for each request. (name[0], name[1], ...) - # Not all stats are globally aggregated. The developer of the game must - # mark the stat as globally aggregated. - # @option params [String] :startdate Start date for daily totals - # (unix epoch timestamp). (Optional) - # @option params [String] :enddate End date for daily totals (unix epoch - # timestamp). (Optional) - # @return [Hash] A hash containing the API response - # @see http://wiki.teamfortress.com/wiki/WebAPI/GetGlobalStatsForGame - def self.global_for_game(appid, params: {}) - params[:appid] = appid - response = client.get 'GetGlobalStatsForGame/v1', params: params - response.parse_key('response') - end + # Get Global Stats for Game + # @param [Fixnum] appid The ID of the game or application + # @param [Hash] params Parameters to pass to the API + # @option params [Fixnum] :count Number of stats to get data for. + # @option params [String] :name[0] Names of the stats to get. For more than + # one value, use a parameter for each request. (name[0], name[1], ...) + # Not all stats are globally aggregated. The developer of the game must + # mark the stat as globally aggregated. + # @option params [String] :startdate Start date for daily totals + # (unix epoch timestamp). (Optional) + # @option params [String] :enddate End date for daily totals (unix epoch + # timestamp). (Optional) + # @return [Hash] A hash containing the API response + # @see http://wiki.teamfortress.com/wiki/WebAPI/GetGlobalStatsForGame + def global_for_game(appid, params: {}) + params[:appid] = appid + response = client.get 'GetGlobalStatsForGame/v1', params: params + response.parse_key('response') + end - # Get stat schema - # @param [Fixnum] appid The application ID for the Steam Game. - # @param [String] language (Optional) Language - # @return [Hash] A hash containing the API response - # @see http://wiki.teamfortress.com/wiki/WebAPI/GetSchemaForGame - def self.game_schema(appid, language: nil) - params = { appid: appid } - params[:l] = language unless language.nil? - response = client.get 'GetSchemaForGame/v2', params: params - response.parse_key('game') - end + # Get stat schema + # @param [Fixnum] appid The application ID for the Steam Game. + # @param [String] language (Optional) Language + # @return [Hash] A hash containing the API response + # @see http://wiki.teamfortress.com/wiki/WebAPI/GetSchemaForGame + def game_schema(appid, language: nil) + params = { appid: appid } + params[:l] = language unless language.nil? + response = client.get 'GetSchemaForGame/v2', params: params + response.parse_key('game') + end - # Get Number of Current Players - # @param [Fixnum] appid to pass to the API - # @return [Hash] A hash containing the API response - # @see http://wiki.teamfortress.com/wiki/WebAPI/GetNumberOfCurrentPlayers - def self.player_count(appid) - response = client.get 'GetNumberOfCurrentPlayers/v1', - params: { appid: appid } - response.parse_key('response') - .parse_key('player_count') - end + # Get Number of Current Players + # @param [Fixnum] appid to pass to the API + # @return [Hash] A hash containing the API response + # @see http://wiki.teamfortress.com/wiki/WebAPI/GetNumberOfCurrentPlayers + def player_count(appid) + response = client.get 'GetNumberOfCurrentPlayers/v1', params: { appid: appid } + response.parse_key('response').parse_key('player_count') + end - # Get Player Achievements - # @param [Fixnum] steamid 64 bit Steam ID to return Achievements list for. - # @param [Fixnum] appid AppID to get achievements for - # @param [String] language Language. If specified, it will return language - # data for the requested language. (Optional) - # @return [Hash] A hash containing the API response - # @see http://wiki.teamfortress.com/wiki/WebAPI/GetPlayerAchievements - def self.player_achievements(appid, steamid, language: nil) - params = { appid: appid, steamid: steamid } - params[:l] = language unless language.nil? - response = client.get 'GetPlayerAchievements/v1', params: params - response = response.parse_key('playerstats') - response.check_success - response.delete('success') - response - end + # Get Player Achievements + # @param [Fixnum] steamid 64 bit Steam ID to return Achievements list for. + # @param [Fixnum] appid AppID to get achievements for + # @param [String] language Language. If specified, it will return language + # data for the requested language. (Optional) + # @return [Hash] A hash containing the API response + # @see http://wiki.teamfortress.com/wiki/WebAPI/GetPlayerAchievements + def player_achievements(appid, steamid, language: nil) + params = { appid: appid, steamid: steamid } + params[:l] = language unless language.nil? + response = client.get 'GetPlayerAchievements/v1', params: params + response = response.parse_key('playerstats') + response.check_success + response.delete('success') + response + end - # Get User Stats for Game - # @param [Fixnum] appid AppID to get stats for. - # @param [Fixnum] steamid 64 bit Steam ID to return stats for. - # @return [Hash] A hash containing the API response. - # @see https://developer.valvesoftware.com/wiki/Steam_Web_API#GetUserStatsForGame_.28v0002.29 - def self.player_stats(appid, steamid) - params = { appid: appid, steamid: steamid } - response = client.get 'GetUserStatsForGame/v2', params: params - response.parse_key('playerstats') - end + # Get User Stats for Game + # @param [Fixnum] appid AppID to get stats for. + # @param [Fixnum] steamid 64 bit Steam ID to return stats for. + # @return [Hash] A hash containing the API response. + # @see https://developer.valvesoftware.com/wiki/Steam_Web_API#GetUserStatsForGame_.28v0002.29 + def player_stats(appid, steamid) + response = client.get 'GetUserStatsForGame/v2', params: { appid: appid, steamid: steamid } + response.parse_key('playerstats') + end - def self.client - build_client('ISteamUserStats') + def client + Steam::Client.new 'ISteamUserStats' + end end end end diff --git a/lib/steam-api/version.rb b/lib/steam-api/version.rb index 64ac7cf..1fc8192 100644 --- a/lib/steam-api/version.rb +++ b/lib/steam-api/version.rb @@ -1,4 +1,6 @@ +# frozen_string_literal: true + # Versioning Info module Steam - VERSION = '1.2.0'.freeze + VERSION = '1.2.0' end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6e3284f..960d27e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,8 +1,12 @@ +# frozen_string_literal: true + require 'simplecov' SimpleCov.start -if File::exist? File.join(File.dirname(__FILE__), "secret.rb") - require 'secret' -end +require_relative 'secret' if File.exist? File.join(__dir__, 'secret.rb') -require 'steam-api' +require_relative '../lib/steam-api' + +RSpec.configure do |config| + config.example_status_persistence_file_path = 'spec/examples.txt' +end diff --git a/spec/steam/apps_spec.rb b/spec/steam/apps_spec.rb index e1f213e..4490890 100644 --- a/spec/steam/apps_spec.rb +++ b/spec/steam/apps_spec.rb @@ -1,11 +1,11 @@ -require 'spec_helper' +# frozen_string_literal: true describe Steam::Apps do describe '.get_all' do - let(:result) { Steam::Apps.get_all } + let(:result) { described_class.get_all } it 'returns a list of apps' do - expect(result).to_not be_nil + expect(result).not_to be_nil end it 'returns an appid for each game' do @@ -18,10 +18,10 @@ end describe '.get_servers' do - let(:result) { Steam::Apps.get_servers(addr: '192.168.1.1') } + let(:result) { described_class.get_servers(addr: '192.168.1.1') } it 'returns a valid response' do - expect(result).to_not be_nil + expect(result).not_to be_nil end it 'returns an empty array for an ip with no servers' do @@ -31,10 +31,10 @@ describe '.up_to_date' do context 'when looking up out of date version info' do - let(:result) { Steam::Apps.up_to_date(appid: 440, version: 10) } + let(:result) { described_class.up_to_date(appid: 440, version: 10) } it 'does not return a nil response' do - expect(result).to_not be_nil + expect(result).not_to be_nil end it "returns a false value for 'up_to_date'" do @@ -55,8 +55,8 @@ end context 'when looking up current version info' do - let(:current) { Steam::Apps.up_to_date(appid: 440, version: 10)['required_version'] } - let(:check) { Steam::Apps.up_to_date(appid: 440, version: current) } + let(:current) { described_class.up_to_date(appid: 440, version: 10)['required_version'] } + let(:check) { described_class.up_to_date(appid: 440, version: current) } it 'returns a positive up to date value' do expect(check['up_to_date']).to be_truthy @@ -67,8 +67,9 @@ end end - it 'should capture json errors' do - expect { Steam::Apps.up_to_date(appid: nil, version: 'foo') }.to raise_error(Steam::JSONError) + it 'captures json errors' do + expect { described_class.up_to_date(appid: nil, version: 'foo') } + .to raise_error(Steam::JSONError) end end end diff --git a/spec/steam/economy_spec.rb b/spec/steam/economy_spec.rb index 2717ec0..bbfa59d 100644 --- a/spec/steam/economy_spec.rb +++ b/spec/steam/economy_spec.rb @@ -1,31 +1,31 @@ -require 'spec_helper' +# frozen_string_literal: true describe Steam::Economy do describe '.asset_info' do - let(:result) { Steam::Economy.asset_info(440, - params: { class_count: 2, - classid0: 195151, - classid1: 16891096 })} + let(:result) do + described_class.asset_info( + 440, params: { class_count: 2, classid0: '195151', classid1: '16891096' } + ) + end it 'returns data' do - expect(result).to_not be_nil + expect(result).not_to be_nil end it 'requires class params' do - expect { Steam::Economy.asset_info(440) }.to raise_error(Steam::JSONError) + expect { described_class.asset_info(440) }.to raise_error(Steam::JSONError) end it 'allows users to query asset info' do - expect(result).to have_key('195151') - expect(result).to have_key('16891096') + expect(result).to match '195151' => a_kind_of(Hash), '16891096' => a_kind_of(Hash) end end describe '.asset_prices' do - let(:result) { Steam::Economy.asset_prices(440) } + let(:result) { described_class.asset_prices(440) } it 'allows users to look up a list asset prices' do - expect(result).to_not be_nil + expect(result).not_to be_nil end it 'returns a list of assets' do diff --git a/spec/steam/news_spec.rb b/spec/steam/news_spec.rb index a3f9afe..57c7cec 100644 --- a/spec/steam/news_spec.rb +++ b/spec/steam/news_spec.rb @@ -1,11 +1,11 @@ -require 'spec_helper' +# frozen_string_literal: true describe Steam::News do describe '.get' do - let(:result) { Steam::News.get(440) } + let(:result) { described_class.get(440) } it 'allow users to look up a news list' do - expect(result).to_not be_nil + expect(result).not_to be_nil end it 'returns the first 20 articles' do diff --git a/spec/steam/player_spec.rb b/spec/steam/player_spec.rb index b54df6b..cea5fe7 100644 --- a/spec/steam/player_spec.rb +++ b/spec/steam/player_spec.rb @@ -1,13 +1,13 @@ -require 'spec_helper' +# frozen_string_literal: true describe Steam::Player do - let(:playerid) { 76561197993276293 } + let(:playerid) { '76561198039590772' } describe '.owned_games' do - let(:result) { Steam::Player.owned_games(playerid) } + let(:result) { described_class.owned_games(playerid) } it 'allows users to retrieve a list of games' do - expect(result).to_not be_nil + expect(result).not_to be_nil end it 'returns a game_count' do @@ -20,10 +20,10 @@ end describe '.recently_played_games' do - let(:result) { Steam::Player.recently_played_games(playerid) } + let(:result) { described_class.recently_played_games(playerid) } it 'allows users to list a players recent games' do - expect(result).to_not be_nil + expect(result).not_to be_nil end it 'returns total_count' do @@ -44,10 +44,10 @@ end describe '.steam_level' do - let(:result) { Steam::Player.steam_level(playerid) } + let(:result) { described_class.steam_level(playerid) } it 'allows users to retrieve a users steam level' do - expect(result).to_not be_nil + expect(result).not_to be_nil end it 'returns the level number' do @@ -56,10 +56,10 @@ end describe '.badges' do - let(:result) { Steam::Player.badges(playerid) } + let(:result) { described_class.badges(playerid) } it 'allows a user to retrieve badges for a player' do - expect(result).to_not be_nil + expect(result).not_to be_nil end it 'returns a list of badges' do @@ -88,10 +88,10 @@ end describe '.community_badge_progress' do - let(:result) { Steam::Player.community_badge_progress(playerid) } + let(:result) { described_class.community_badge_progress(playerid) } it 'allows a user to retrieve community badge info for a player' do - expect(result).to_not be_nil + expect(result).not_to be_nil end it 'returns a list of quests' do @@ -99,8 +99,7 @@ end it 'returns a list of quests with ids and completion status' do - expect(result.first).to have_key('questid') - expect(result.first).to have_key('completed') + expect(result.first).to match 'questid' => a_kind_of(Integer), 'completed' => boolean end end end diff --git a/spec/steam/remote-storage_spec.rb b/spec/steam/remote-storage_spec.rb deleted file mode 100644 index d5f95de..0000000 --- a/spec/steam/remote-storage_spec.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'spec_helper' - -describe Steam::RemoteStorage do - describe '.published_file' do - end - - describe '.ugc_file' do - end -end diff --git a/spec/steam/remote_storage_spec.rb b/spec/steam/remote_storage_spec.rb new file mode 100644 index 0000000..5b6a14d --- /dev/null +++ b/spec/steam/remote_storage_spec.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +describe Steam::RemoteStorage do + pending '.published_file' + + pending '.ugc_file' +end diff --git a/spec/steam/store_spec.rb b/spec/steam/store_spec.rb index 8f97e5c..1df7092 100644 --- a/spec/steam/store_spec.rb +++ b/spec/steam/store_spec.rb @@ -1,18 +1,17 @@ -require 'spec_helper' - +# frozen_string_literal: true describe Steam::Store do - let(:appid) { 976730 } + let(:appid) { '976730' } describe '.app_details' do - let (:result) { Steam::Store.app_details(appid) } - + let(:result) { described_class.app_details(appid) } + it 'returns the details of an application' do - expect(result).to_not be_nil + expect(result).not_to be_nil end it 'returns a JSON structure containing details of the returned app' do - expect(result).to include("976730") + expect(result).to include('976730') end end end diff --git a/spec/steam/users-stats_spec.rb b/spec/steam/user_stats_spec.rb similarity index 62% rename from spec/steam/users-stats_spec.rb rename to spec/steam/user_stats_spec.rb index da5fd59..9baf5ca 100644 --- a/spec/steam/users-stats_spec.rb +++ b/spec/steam/user_stats_spec.rb @@ -1,24 +1,23 @@ -require 'spec_helper' +# frozen_string_literal: true describe Steam::UserStats do describe '.achievement_percentages' do - let(:result) { Steam::UserStats.achievement_percentages(440) } + let(:result) { described_class.achievement_percentages(440) } it 'returns achievement information' do - expect(result).to_not be_nil + expect(result).not_to be_nil end it 'returns all achievements for a game' do - expect(result.first).to have_key('name') - expect(result.first).to have_key('percent') + expect(result.first).to match 'name' => a_kind_of(String), 'percent' => a_kind_of(Numeric) end end describe '.game_schema' do - let(:result) { Steam::UserStats.game_schema(440) } + let(:result) { described_class.game_schema(440) } it 'returns global game stats' do - expect(result).to_not be_nil + expect(result).not_to be_nil end it 'returns global game stats with a gameName' do @@ -35,19 +34,35 @@ end describe '.global_for_game' do - it 'returns global game stats' do - Steam::UserStats.global_for_game(201830, params: { 'name[0]' => 'totalDeaths', count: 10 }) + subject do + ## https://stackoverflow.com/a/35650119/2630849 + ## https://stackoverflow.com/q/57750281/2630849 + ## Sadly, it seems that Steam closed (by default?) all the aggregated stats + described_class.global_for_game( + '779340', params: { 'name[0]' => 'TOTAL_PLAY_TIME', count: 1 } + ) + end + + let(:expected_result) do + { + 'error' => "Stat 'TOTAL_PLAY_TIME' is not an aggregated stat", + 'result' => 8 + } end + + it { is_expected.to eq expected_result } end describe '.player_count' do it 'returns a player count' do - expect(Steam::UserStats.player_count(440)).to be_a(Integer) + expect(described_class.player_count(440)).to be_a(Integer) end end describe '.get_player_achievements' do - let(:achs) { Steam::UserStats.player_achievements(440, 76561197993276293) } + let(:achs) do + described_class.player_achievements(440, '76561197993276293') + end it 'returns a list of player achievements' do expect(achs).to have_key('achievements') @@ -71,7 +86,7 @@ end describe '.player_stats' do - let(:stats) { Steam::UserStats.player_stats(440, 76561197993276293) } + let(:stats) { described_class.player_stats(440, '76561197993276293') } it 'returns player stats' do expect(stats).to have_key('stats') diff --git a/spec/steam/users_spec.rb b/spec/steam/users_spec.rb index 6455b57..504b6f0 100644 --- a/spec/steam/users_spec.rb +++ b/spec/steam/users_spec.rb @@ -1,14 +1,14 @@ -require 'spec_helper' +# frozen_string_literal: true describe Steam::User do - let(:playerid) { 76561197993276293 } - let(:playerid2) { 76561197969622382 } + let(:playerid) { '76561197993276293' } + let(:playerid2) { '76561197969622382' } describe '.friends' do - let(:result) { Steam::User.friends(playerid) } + let(:result) { described_class.friends(playerid) } it 'allows users to check a friends for a user' do - expect(result).to_not be_nil + expect(result).not_to be_nil end it 'returns a list of friends of a user' do @@ -16,49 +16,50 @@ end it 'raises an error on a bad friend id' do - expect { Steam::User.friends('765611') }.to raise_error(Steam::JSONError) + expect { described_class.friends('765611') }.to raise_error(Steam::JSONError) end it 'returns the same content for :friends and :all' do - friends = Steam::User.friends(playerid2, relationship: :friend) - all_friends = Steam::User.friends(playerid2, relationship: :all) + friends = described_class.friends(playerid2, relationship: :friend) + all_friends = described_class.friends(playerid2, relationship: :all) expect(friends).to eq(all_friends) end it 'returns an error on a bad friend relationship' do - expect { Steam::User.friends(playerid2, relationship: :sadsad) }.to raise_error(Steam::JSONError) + expect { described_class.friends(playerid2, relationship: :sadsad) } + .to raise_error(Steam::JSONError) end end describe '.bans' do it 'allows users to check bans for a user' do - expect(Steam::User.bans(playerid)).to_not be_nil + expect(described_class.bans(playerid)).not_to be_nil end it 'returns a blank list for bad ids' do - expect(Steam::User.bans(7993276293)).to eq({ 'players' => [] }) + expect(described_class.bans('7993276293')).to eq({ 'players' => [] }) end it 'allow users to check bans for multiple steamids' do - expect(Steam::User.bans([playerid, playerid2])).to_not be_nil + expect(described_class.bans([playerid, playerid2])).not_to be_nil end end describe '.summary' do it 'allows users to get a summary for a user' do - expect(Steam::User.summary(playerid)).to_not be_nil + expect(described_class.summary(playerid)).not_to be_nil end it 'allows users to check summaries for multiple accounts' do - expect(Steam::User.summaries([playerid, playerid2])).to_not be_nil + expect(described_class.summaries([playerid, playerid2])).not_to be_nil end end describe '.vanity_to_steamid' do - let(:result) { Steam::User.vanity_to_steamid('theasmer') } + let(:result) { described_class.vanity_to_steamid('theasmer') } it 'return values when they look up vanity urls' do - expect(result).to_not be_nil + expect(result).not_to be_nil end it 'returns the correct id when users look up vanity urls' do @@ -67,10 +68,10 @@ end describe '.groups' do - let(:result) { Steam::User.groups(playerid) } + let(:result) { described_class.groups(playerid) } it 'allow users to look up a users groups' do - expect(result).to_not be_nil + expect(result).not_to be_nil end it 'returns an accurate list of groups a player is a member of' do diff --git a/spec/steam_spec.rb b/spec/steam_spec.rb index b9f9e79..3a8f4dc 100644 --- a/spec/steam_spec.rb +++ b/spec/steam_spec.rb @@ -1,38 +1,43 @@ -require 'spec_helper' +# frozen_string_literal: true describe Steam do describe '.apikey' do - before(:all) do - @apikey = Steam.apikey - end + subject(:apikey) { described_class.apikey } - after(:each) do - Steam.apikey = @apikey - end + it { is_expected.not_to be_nil } - it 'returns a Steam API key if one is defined' do - expect(Steam.apikey).to_not be_nil - end + context 'when `nil`' do + before do + described_class.apikey = nil + ENV['STEAM_API_KEY'] = nil + end - it 'returns an error if the Steam Key is missing' do - Steam.apikey = nil - ENV['STEAM_API_KEY'] = nil - expect { Steam.apikey }.to raise_error(ArgumentError, /Please set your Steam API key/) + specify do + expect { apikey }.to raise_error(ArgumentError, /Please set your Steam API key/) + end end - it 'returns a new value if set to a different API key' do - old = Steam.apikey - Steam.apikey = 'blah' - expect(Steam.apikey).to_not eq(old) - expect(Steam.apikey).to eq('blah') + context 'when set using ENV' do + let(:new_apikey) { 'blah' } + + before do + described_class.apikey = nil + ENV['STEAM_API_KEY'] = new_apikey + end + + it { is_expected.to eq new_apikey } end + end + + describe '.apikey=' do + subject(:apikey) { described_class.apikey } - it 'allows users to set the apikey post init using ENV' do - Steam.apikey = nil - ENV['STEAM_API_KEY'] = nil - expect { Steam.apikey }.to raise_error(ArgumentError, /Please set your Steam API key/) - ENV['STEAM_API_KEY'] = @apikey - expect(Steam.apikey).to eq(@apikey) + let(:new_apikey) { 'blah' } + + before do + described_class.apikey = new_apikey end + + it { is_expected.to eq new_apikey } end end diff --git a/steam-api.gemspec b/steam-api.gemspec index 2a24351..a392e5b 100644 --- a/steam-api.gemspec +++ b/steam-api.gemspec @@ -1,6 +1,6 @@ -lib = File.expand_path('lib', __dir__) -$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'steam-api/version' +# frozen_string_literal: true + +require_relative 'lib/steam-api/version' Gem::Specification.new do |gem| gem.name = 'steam-api' @@ -12,14 +12,23 @@ Gem::Specification.new do |gem| gem.homepage = 'https://github.com/bhaberer/steam-api' gem.license = 'MIT' + gem.metadata = { + 'rubygems_mfa_required' => 'true' + } + gem.files = `git ls-files`.split($RS) gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) } - gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) gem.require_paths = ['lib'] + gem.required_ruby_version = '>= 2.4', '< 4' + + gem.add_dependency 'faraday', '~> 2.0' + gem.add_development_dependency 'codeclimate-test-reporter', '~> 1.0' gem.add_development_dependency 'rake', '~> 13.0' gem.add_development_dependency 'rspec', '~> 3.9' - gem.add_dependency 'faraday', '~> 1.0' + gem.add_development_dependency 'rubocop', '~> 1.37.1' + gem.add_development_dependency 'rubocop-rake', '~> 0.6.0' + gem.add_development_dependency 'rubocop-rspec', '~> 2.14.2' end