From e1bb3df908a640d20646cf9b1da3091d4e786dcf Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Fri, 28 Oct 2022 15:08:54 +0300 Subject: [PATCH 01/16] Enable RSpec `--only-fail` feature --- .gitignore | 1 + spec/spec_helper.rb | 4 ++++ 2 files changed, 5 insertions(+) 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/spec/spec_helper.rb b/spec/spec_helper.rb index 6e3284f..0097da8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -6,3 +6,7 @@ end require 'steam-api' + +RSpec.configure do |config| + config.example_status_persistence_file_path = 'spec/examples.txt' +end From d239be4ed2e27021d6311f15dd2396de32cfae4e Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Fri, 28 Oct 2022 15:09:33 +0300 Subject: [PATCH 02/16] Improve RSpec configuration --- .rspec | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 From 65e0c6754bd33709cfc3adaa27c1fecf0f35e420 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Fri, 28 Oct 2022 15:14:46 +0300 Subject: [PATCH 03/16] Add `frozen_string_literal` tech comment --- Gemfile | 2 ++ Rakefile | 2 ++ lib/steam-api.rb | 2 ++ lib/steam-api/client.rb | 2 ++ lib/steam-api/exceptions.rb | 2 ++ lib/steam-api/helpers.rb | 2 ++ lib/steam-api/response.rb | 2 ++ lib/steam-api/ruby/hash.rb | 2 ++ lib/steam-api/steam.rb | 4 +++- lib/steam-api/steam/apps.rb | 2 ++ lib/steam-api/steam/economy.rb | 2 ++ lib/steam-api/steam/news.rb | 2 ++ lib/steam-api/steam/player.rb | 2 ++ lib/steam-api/steam/remote_storage.rb | 2 ++ lib/steam-api/steam/store.rb | 2 ++ lib/steam-api/steam/user.rb | 3 ++- lib/steam-api/steam/user_stats.rb | 2 ++ lib/steam-api/version.rb | 4 +++- spec/spec_helper.rb | 2 ++ spec/steam/apps_spec.rb | 2 ++ spec/steam/economy_spec.rb | 2 ++ spec/steam/news_spec.rb | 2 ++ spec/steam/player_spec.rb | 2 ++ spec/steam/store_spec.rb | 3 ++- spec/steam/users_spec.rb | 2 ++ spec/steam_spec.rb | 2 ++ steam-api.gemspec | 2 ++ 27 files changed, 56 insertions(+), 4 deletions(-) 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..25864e6 100644 --- a/Rakefile +++ b/Rakefile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'bundler/gem_tasks' require 'rspec/core/rake_task' diff --git a/lib/steam-api.rb b/lib/steam-api.rb index f69ed80..60241c5 100644 --- a/lib/steam-api.rb +++ b/lib/steam-api.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'faraday' require 'json' diff --git a/lib/steam-api/client.rb b/lib/steam-api/client.rb index c7316dd..f031605 100644 --- a/lib/steam-api/client.rb +++ b/lib/steam-api/client.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Steam # Client object used to communicate with the steam webapi class Client 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 index 5ec9b34..4dfb867 100644 --- a/lib/steam-api/helpers.rb +++ b/lib/steam-api/helpers.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Steam # Helper Module module Helpers diff --git a/lib/steam-api/response.rb b/lib/steam-api/response.rb index 23167f0..1611bd2 100644 --- a/lib/steam-api/response.rb +++ b/lib/steam-api/response.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Steam # Since the steam responses are so randomly inconsistant we're making a new # class to manage the responses. 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..118eced 100644 --- a/lib/steam-api/steam.rb +++ b/lib/steam-api/steam.rb @@ -1,4 +1,6 @@ -# Base class def +# frozen_string_literal: true + +# Base class definition module Steam @apikey = ENV['STEAM_API_KEY'] diff --git a/lib/steam-api/steam/apps.rb b/lib/steam-api/steam/apps.rb index 522ee7b..1d05883 100644 --- a/lib/steam-api/steam/apps.rb +++ b/lib/steam-api/steam/apps.rb @@ -1,3 +1,5 @@ +# 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 diff --git a/lib/steam-api/steam/economy.rb b/lib/steam-api/steam/economy.rb index 616dad9..ec8aa24 100644 --- a/lib/steam-api/steam/economy.rb +++ b/lib/steam-api/steam/economy.rb @@ -1,3 +1,5 @@ +# 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 diff --git a/lib/steam-api/steam/news.rb b/lib/steam-api/steam/news.rb index abd0ff9..2f8efcf 100644 --- a/lib/steam-api/steam/news.rb +++ b/lib/steam-api/steam/news.rb @@ -1,3 +1,5 @@ +# 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 diff --git a/lib/steam-api/steam/player.rb b/lib/steam-api/steam/player.rb index 42ea892..fadbf06 100644 --- a/lib/steam-api/steam/player.rb +++ b/lib/steam-api/steam/player.rb @@ -1,3 +1,5 @@ +# 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 diff --git a/lib/steam-api/steam/remote_storage.rb b/lib/steam-api/steam/remote_storage.rb index 2cedc63..0db3089 100644 --- a/lib/steam-api/steam/remote_storage.rb +++ b/lib/steam-api/steam/remote_storage.rb @@ -1,3 +1,5 @@ +# 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 diff --git a/lib/steam-api/steam/store.rb b/lib/steam-api/steam/store.rb index f2f8196..a20e9ce 100644 --- a/lib/steam-api/steam/store.rb +++ b/lib/steam-api/steam/store.rb @@ -1,3 +1,5 @@ +# 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 diff --git a/lib/steam-api/steam/user.rb b/lib/steam-api/steam/user.rb index 7eab724..8a8f846 100644 --- a/lib/steam-api/steam/user.rb +++ b/lib/steam-api/steam/user.rb @@ -1,4 +1,5 @@ -# -*- 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 diff --git a/lib/steam-api/steam/user_stats.rb b/lib/steam-api/steam/user_stats.rb index 5e2981f..a6b1596 100644 --- a/lib/steam-api/steam/user_stats.rb +++ b/lib/steam-api/steam/user_stats.rb @@ -1,3 +1,5 @@ +# 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 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 0097da8..cc67be5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'simplecov' SimpleCov.start diff --git a/spec/steam/apps_spec.rb b/spec/steam/apps_spec.rb index e1f213e..1e1ec57 100644 --- a/spec/steam/apps_spec.rb +++ b/spec/steam/apps_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Steam::Apps do diff --git a/spec/steam/economy_spec.rb b/spec/steam/economy_spec.rb index 2717ec0..e403146 100644 --- a/spec/steam/economy_spec.rb +++ b/spec/steam/economy_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Steam::Economy do diff --git a/spec/steam/news_spec.rb b/spec/steam/news_spec.rb index a3f9afe..abd6da1 100644 --- a/spec/steam/news_spec.rb +++ b/spec/steam/news_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Steam::News do diff --git a/spec/steam/player_spec.rb b/spec/steam/player_spec.rb index b54df6b..9ef8a8d 100644 --- a/spec/steam/player_spec.rb +++ b/spec/steam/player_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Steam::Player do diff --git a/spec/steam/store_spec.rb b/spec/steam/store_spec.rb index 8f97e5c..c9dbb14 100644 --- a/spec/steam/store_spec.rb +++ b/spec/steam/store_spec.rb @@ -1,5 +1,6 @@ -require 'spec_helper' +# frozen_string_literal: true +require 'spec_helper' describe Steam::Store do let(:appid) { 976730 } diff --git a/spec/steam/users_spec.rb b/spec/steam/users_spec.rb index 6455b57..23c1d21 100644 --- a/spec/steam/users_spec.rb +++ b/spec/steam/users_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Steam::User do diff --git a/spec/steam_spec.rb b/spec/steam_spec.rb index b9f9e79..a49eb60 100644 --- a/spec/steam_spec.rb +++ b/spec/steam_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Steam do diff --git a/steam-api.gemspec b/steam-api.gemspec index 2a24351..4afc5fa 100644 --- a/steam-api.gemspec +++ b/steam-api.gemspec @@ -1,3 +1,5 @@ +# frozen_string_literal: true + lib = File.expand_path('lib', __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'steam-api/version' From 051c92efcbcb80e7b60a4e6e1339fadf90ed4803 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Fri, 28 Oct 2022 15:15:37 +0300 Subject: [PATCH 04/16] Rename some spec files --- .../{remote-storage_spec.rb => remote_storage_spec.rb} | 2 ++ .../steam/{users-stats_spec.rb => users_stats_spec.rb} | 10 +++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) rename spec/steam/{remote-storage_spec.rb => remote_storage_spec.rb} (80%) rename spec/steam/{users-stats_spec.rb => users_stats_spec.rb} (87%) diff --git a/spec/steam/remote-storage_spec.rb b/spec/steam/remote_storage_spec.rb similarity index 80% rename from spec/steam/remote-storage_spec.rb rename to spec/steam/remote_storage_spec.rb index d5f95de..4939675 100644 --- a/spec/steam/remote-storage_spec.rb +++ b/spec/steam/remote_storage_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Steam::RemoteStorage do diff --git a/spec/steam/users-stats_spec.rb b/spec/steam/users_stats_spec.rb similarity index 87% rename from spec/steam/users-stats_spec.rb rename to spec/steam/users_stats_spec.rb index da5fd59..9187d04 100644 --- a/spec/steam/users-stats_spec.rb +++ b/spec/steam/users_stats_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Steam::UserStats do @@ -36,7 +38,7 @@ describe '.global_for_game' do it 'returns global game stats' do - Steam::UserStats.global_for_game(201830, params: { 'name[0]' => 'totalDeaths', count: 10 }) + Steam::UserStats.global_for_game('201830', params: { 'name[0]' => 'totalDeaths', count: 10 }) end end @@ -47,7 +49,9 @@ end describe '.get_player_achievements' do - let(:achs) { Steam::UserStats.player_achievements(440, 76561197993276293) } + let(:achs) do + Steam::UserStats.player_achievements(440, '76561197993276293') + end it 'returns a list of player achievements' do expect(achs).to have_key('achievements') @@ -71,7 +75,7 @@ end describe '.player_stats' do - let(:stats) { Steam::UserStats.player_stats(440, 76561197993276293) } + let(:stats) { Steam::UserStats.player_stats(440, '76561197993276293') } it 'returns player stats' do expect(stats).to have_key('stats') From 3d8fcfc8ef0e1bf3acf45cc34fd40f2b7ff4552c Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Fri, 28 Oct 2022 15:17:29 +0300 Subject: [PATCH 05/16] Remove `require 'spec_helper'` since it's in the RSpec config --- spec/steam/apps_spec.rb | 2 -- spec/steam/economy_spec.rb | 2 -- spec/steam/news_spec.rb | 2 -- spec/steam/player_spec.rb | 2 -- spec/steam/remote_storage_spec.rb | 2 -- spec/steam/store_spec.rb | 2 -- spec/steam/users_spec.rb | 2 -- spec/steam/users_stats_spec.rb | 2 -- spec/steam_spec.rb | 2 -- 9 files changed, 18 deletions(-) diff --git a/spec/steam/apps_spec.rb b/spec/steam/apps_spec.rb index 1e1ec57..0eea0ba 100644 --- a/spec/steam/apps_spec.rb +++ b/spec/steam/apps_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'spec_helper' - describe Steam::Apps do describe '.get_all' do let(:result) { Steam::Apps.get_all } diff --git a/spec/steam/economy_spec.rb b/spec/steam/economy_spec.rb index e403146..2ffb88b 100644 --- a/spec/steam/economy_spec.rb +++ b/spec/steam/economy_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'spec_helper' - describe Steam::Economy do describe '.asset_info' do let(:result) { Steam::Economy.asset_info(440, diff --git a/spec/steam/news_spec.rb b/spec/steam/news_spec.rb index abd6da1..d12397c 100644 --- a/spec/steam/news_spec.rb +++ b/spec/steam/news_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'spec_helper' - describe Steam::News do describe '.get' do let(:result) { Steam::News.get(440) } diff --git a/spec/steam/player_spec.rb b/spec/steam/player_spec.rb index 9ef8a8d..8cc59cb 100644 --- a/spec/steam/player_spec.rb +++ b/spec/steam/player_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'spec_helper' - describe Steam::Player do let(:playerid) { 76561197993276293 } diff --git a/spec/steam/remote_storage_spec.rb b/spec/steam/remote_storage_spec.rb index 4939675..77fc464 100644 --- a/spec/steam/remote_storage_spec.rb +++ b/spec/steam/remote_storage_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'spec_helper' - describe Steam::RemoteStorage do describe '.published_file' do end diff --git a/spec/steam/store_spec.rb b/spec/steam/store_spec.rb index c9dbb14..8ddecbf 100644 --- a/spec/steam/store_spec.rb +++ b/spec/steam/store_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'spec_helper' - describe Steam::Store do let(:appid) { 976730 } diff --git a/spec/steam/users_spec.rb b/spec/steam/users_spec.rb index 23c1d21..44855da 100644 --- a/spec/steam/users_spec.rb +++ b/spec/steam/users_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'spec_helper' - describe Steam::User do let(:playerid) { 76561197993276293 } let(:playerid2) { 76561197969622382 } diff --git a/spec/steam/users_stats_spec.rb b/spec/steam/users_stats_spec.rb index 9187d04..ddd6f2a 100644 --- a/spec/steam/users_stats_spec.rb +++ b/spec/steam/users_stats_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'spec_helper' - describe Steam::UserStats do describe '.achievement_percentages' do let(:result) { Steam::UserStats.achievement_percentages(440) } diff --git a/spec/steam_spec.rb b/spec/steam_spec.rb index a49eb60..fd5c157 100644 --- a/spec/steam_spec.rb +++ b/spec/steam_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'spec_helper' - describe Steam do describe '.apikey' do before(:all) do From f8019ff3ce1d895c0a817a0069189068c00b8d18 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Fri, 28 Oct 2022 15:19:52 +0300 Subject: [PATCH 06/16] Improve code style And move blocks of class methods under `class << self`. --- lib/steam-api/helpers.rb | 3 +- lib/steam-api/steam.rb | 20 ++-- lib/steam-api/steam/apps.rb | 73 ++++++------ lib/steam-api/steam/economy.rb | 99 ++++++++-------- lib/steam-api/steam/news.rb | 46 ++++---- lib/steam-api/steam/player.rb | 116 +++++++++---------- lib/steam-api/steam/remote_storage.rb | 50 ++++----- lib/steam-api/steam/store.rb | 21 ++-- lib/steam-api/steam/user.rb | 141 +++++++++++------------ lib/steam-api/steam/user_stats.rb | 156 +++++++++++++------------- spec/spec_helper.rb | 4 +- spec/steam/economy_spec.rb | 9 +- spec/steam/player_spec.rb | 2 +- spec/steam/store_spec.rb | 8 +- spec/steam/users_spec.rb | 9 +- 15 files changed, 372 insertions(+), 385 deletions(-) diff --git a/lib/steam-api/helpers.rb b/lib/steam-api/helpers.rb index 4dfb867..fcff572 100644 --- a/lib/steam-api/helpers.rb +++ b/lib/steam-api/helpers.rb @@ -8,10 +8,9 @@ module Helpers # @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('/')) + Steam::Client.new File.join(base_url, api) end - STORE_API_BASE_URL = 'https://store.steampowered.com/api' end end diff --git a/lib/steam-api/steam.rb b/lib/steam-api/steam.rb index 118eced..6ca2d22 100644 --- a/lib/steam-api/steam.rb +++ b/lib/steam-api/steam.rb @@ -4,18 +4,16 @@ module Steam @apikey = ENV['STEAM_API_KEY'] - def self.apikey - if @apikey.nil? - unless ENV.key?('STEAM_API_KEY') - raise ArgumentError, 'Please set your Steam API key.' - end + class << self + attr_writer :apikey - @apikey = ENV['STEAM_API_KEY'] - end - @apikey - end + def apikey + if @apikey.nil? + raise ArgumentError, 'Please set your Steam API key.' unless ENV.key?('STEAM_API_KEY') - def self.apikey=(key) - @apikey = key + @apikey = ENV['STEAM_API_KEY'] + end + @apikey + end end end diff --git a/lib/steam-api/steam/apps.rb b/lib/steam-api/steam/apps.rb index 1d05883..8f5e224 100644 --- a/lib/steam-api/steam/apps.rb +++ b/lib/steam-api/steam/apps.rb @@ -5,45 +5,46 @@ module Steam # @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 + build_client 'ISteamApps' + end end end end diff --git a/lib/steam-api/steam/economy.rb b/lib/steam-api/steam/economy.rb index ec8aa24..a789b14 100644 --- a/lib/steam-api/steam/economy.rb +++ b/lib/steam-api/steam/economy.rb @@ -5,58 +5,59 @@ module Steam # @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 + build_client '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 2f8efcf..bc7499a 100644 --- a/lib/steam-api/steam/news.rb +++ b/lib/steam-api/steam/news.rb @@ -5,30 +5,30 @@ module Steam # @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 + build_client 'ISteamNews' + end end end end diff --git a/lib/steam-api/steam/player.rb b/lib/steam-api/steam/player.rb index fadbf06..c8b84c6 100644 --- a/lib/steam-api/steam/player.rb +++ b/lib/steam-api/steam/player.rb @@ -5,71 +5,67 @@ module Steam # @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 + build_client 'IPlayerService' + end end end end diff --git a/lib/steam-api/steam/remote_storage.rb b/lib/steam-api/steam/remote_storage.rb index 0db3089..0aa47fa 100644 --- a/lib/steam-api/steam/remote_storage.rb +++ b/lib/steam-api/steam/remote_storage.rb @@ -5,33 +5,33 @@ module Steam # @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 + build_client 'ISteamRemoteStorage' + end end end end diff --git a/lib/steam-api/steam/store.rb b/lib/steam-api/steam/store.rb index a20e9ce..51a9ea2 100644 --- a/lib/steam-api/steam/store.rb +++ b/lib/steam-api/steam/store.rb @@ -5,18 +5,19 @@ module Steam # @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 + build_client '', base_url: Steam::Helpers::STORE_API_BASE_URL + end end end end diff --git a/lib/steam-api/steam/user.rb b/lib/steam-api/steam/user.rb index 8a8f846..66208cc 100644 --- a/lib/steam-api/steam/user.rb +++ b/lib/steam-api/steam/user.rb @@ -5,85 +5,80 @@ module Steam # @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 + build_client 'ISteamUser' + end end end end diff --git a/lib/steam-api/steam/user_stats.rb b/lib/steam-api/steam/user_stats.rb index a6b1596..4857765 100644 --- a/lib/steam-api/steam/user_stats.rb +++ b/lib/steam-api/steam/user_stats.rb @@ -5,92 +5,88 @@ module Steam # @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 + build_client('ISteamUserStats') + end end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index cc67be5..b876028 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,9 +3,7 @@ require 'simplecov' SimpleCov.start -if File::exist? File.join(File.dirname(__FILE__), "secret.rb") - require 'secret' -end +require 'secret' if File.exist? File.join(__dir__, 'secret.rb') require 'steam-api' diff --git a/spec/steam/economy_spec.rb b/spec/steam/economy_spec.rb index 2ffb88b..9e1e28d 100644 --- a/spec/steam/economy_spec.rb +++ b/spec/steam/economy_spec.rb @@ -2,10 +2,11 @@ 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 + Steam::Economy.asset_info( + 440, params: { class_count: 2, classid0: '195151', classid1: '16891096' } + ) + end it 'returns data' do expect(result).to_not be_nil diff --git a/spec/steam/player_spec.rb b/spec/steam/player_spec.rb index 8cc59cb..82e6595 100644 --- a/spec/steam/player_spec.rb +++ b/spec/steam/player_spec.rb @@ -1,7 +1,7 @@ # 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) } diff --git a/spec/steam/store_spec.rb b/spec/steam/store_spec.rb index 8ddecbf..a670139 100644 --- a/spec/steam/store_spec.rb +++ b/spec/steam/store_spec.rb @@ -1,17 +1,17 @@ # 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) { Steam::Store.app_details(appid) } + it 'returns the details of an application' do expect(result).to_not 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_spec.rb b/spec/steam/users_spec.rb index 44855da..cdff6d3 100644 --- a/spec/steam/users_spec.rb +++ b/spec/steam/users_spec.rb @@ -1,8 +1,8 @@ # 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) } @@ -26,7 +26,8 @@ end it 'returns an error on a bad friend relationship' do - expect { Steam::User.friends(playerid2, relationship: :sadsad) }.to raise_error(Steam::JSONError) + expect { Steam::User.friends(playerid2, relationship: :sadsad) } + .to raise_error(Steam::JSONError) end end @@ -36,7 +37,7 @@ end it 'returns a blank list for bad ids' do - expect(Steam::User.bans(7993276293)).to eq({ 'players' => [] }) + expect(Steam::User.bans('7993276293')).to eq({ 'players' => [] }) end it 'allow users to check bans for multiple steamids' do From e41d47b9e0671a3ba591906ecc1d16bdf887379b Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Fri, 28 Oct 2022 15:20:17 +0300 Subject: [PATCH 07/16] Move `Hash` monkey-patching under our namespace --- lib/steam-api/client.rb | 6 ++--- lib/steam-api/response.rb | 50 +++++++++++++++++++++------------------ 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/lib/steam-api/client.rb b/lib/steam-api/client.rb index f031605..4857a6e 100644 --- a/lib/steam-api/client.rb +++ b/lib/steam-api/client.rb @@ -14,8 +14,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 @@ -23,7 +23,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/response.rb b/lib/steam-api/response.rb index 1611bd2..4048875 100644 --- a/lib/steam-api/response.rb +++ b/lib/steam-api/response.rb @@ -3,33 +3,37 @@ 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 From 3a98b07a3ee39afaac7a01b23214a9b7c0bf057f Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Fri, 28 Oct 2022 15:20:42 +0300 Subject: [PATCH 08/16] Require lib for specs relatively It can require installed version instead. --- spec/spec_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b876028..d8e663b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,7 +5,7 @@ require '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' From 4af77645536ad86a244e85f3e8fb7e0103509d2e Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Fri, 28 Oct 2022 15:20:59 +0300 Subject: [PATCH 09/16] Specify required Ruby version in gemspec --- steam-api.gemspec | 2 ++ 1 file changed, 2 insertions(+) diff --git a/steam-api.gemspec b/steam-api.gemspec index 4afc5fa..31adaaa 100644 --- a/steam-api.gemspec +++ b/steam-api.gemspec @@ -19,6 +19,8 @@ Gem::Specification.new do |gem| gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) gem.require_paths = ['lib'] + gem.required_ruby_version = '>= 2.4', '< 4' + gem.add_development_dependency 'codeclimate-test-reporter', '~> 1.0' gem.add_development_dependency 'rake', '~> 13.0' gem.add_development_dependency 'rspec', '~> 3.9' From fe78c734d7aa78fe6f7974c425eeb6a340ebf022 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Fri, 28 Oct 2022 15:22:27 +0300 Subject: [PATCH 10/16] Add EditorConfig file More info here: https://editorconfig.org/ --- .editorconfig | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .editorconfig 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 From 8ce67bc12d103a40d0343985bce4a9cfb6852962 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Fri, 28 Oct 2022 15:25:32 +0300 Subject: [PATCH 11/16] Use `require_relative` where necessary Don't change the global `$LOAD_PATHS`. --- spec/spec_helper.rb | 2 +- steam-api.gemspec | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d8e663b..960d27e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,7 +3,7 @@ require 'simplecov' SimpleCov.start -require 'secret' if File.exist? File.join(__dir__, 'secret.rb') +require_relative 'secret' if File.exist? File.join(__dir__, 'secret.rb') require_relative '../lib/steam-api' diff --git a/steam-api.gemspec b/steam-api.gemspec index 31adaaa..6fd81ad 100644 --- a/steam-api.gemspec +++ b/steam-api.gemspec @@ -1,8 +1,6 @@ # frozen_string_literal: true -lib = File.expand_path('lib', __dir__) -$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'steam-api/version' +require_relative 'lib/steam-api/version' Gem::Specification.new do |gem| gem.name = 'steam-api' From 505c28bbfcb15ded35049ff33de4a563310ba50b Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Fri, 28 Oct 2022 16:12:29 +0300 Subject: [PATCH 12/16] Get rid of `Helpers` in the `Kernel` --- lib/steam-api.rb | 3 --- lib/steam-api/client.rb | 6 +++++- lib/steam-api/helpers.rb | 16 ---------------- lib/steam-api/steam/apps.rb | 2 +- lib/steam-api/steam/economy.rb | 2 +- lib/steam-api/steam/news.rb | 2 +- lib/steam-api/steam/player.rb | 2 +- lib/steam-api/steam/remote_storage.rb | 2 +- lib/steam-api/steam/store.rb | 2 +- lib/steam-api/steam/user.rb | 2 +- lib/steam-api/steam/user_stats.rb | 2 +- 11 files changed, 13 insertions(+), 28 deletions(-) delete mode 100644 lib/steam-api/helpers.rb diff --git a/lib/steam-api.rb b/lib/steam-api.rb index 60241c5..47e035d 100644 --- a/lib/steam-api.rb +++ b/lib/steam-api.rb @@ -8,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' @@ -17,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 4857a6e..922a957 100644 --- a/lib/steam-api/client.rb +++ b/lib/steam-api/client.rb @@ -3,7 +3,11 @@ 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 diff --git a/lib/steam-api/helpers.rb b/lib/steam-api/helpers.rb deleted file mode 100644 index fcff572..0000000 --- a/lib/steam-api/helpers.rb +++ /dev/null @@ -1,16 +0,0 @@ -# frozen_string_literal: true - -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 File.join(base_url, api) - end - - STORE_API_BASE_URL = 'https://store.steampowered.com/api' - end -end diff --git a/lib/steam-api/steam/apps.rb b/lib/steam-api/steam/apps.rb index 8f5e224..f452d94 100644 --- a/lib/steam-api/steam/apps.rb +++ b/lib/steam-api/steam/apps.rb @@ -43,7 +43,7 @@ def up_to_date(appid: nil, version: 'v1', api_version: 'v1') end def client - build_client 'ISteamApps' + Steam::Client.new 'ISteamApps' end end end diff --git a/lib/steam-api/steam/economy.rb b/lib/steam-api/steam/economy.rb index a789b14..084e921 100644 --- a/lib/steam-api/steam/economy.rb +++ b/lib/steam-api/steam/economy.rb @@ -49,7 +49,7 @@ def asset_prices(appid, language: nil, currency: nil) end def client - build_client 'ISteamEconomy' + Steam::Client.new 'ISteamEconomy' end def parse_response(response) diff --git a/lib/steam-api/steam/news.rb b/lib/steam-api/steam/news.rb index bc7499a..de9efd6 100644 --- a/lib/steam-api/steam/news.rb +++ b/lib/steam-api/steam/news.rb @@ -27,7 +27,7 @@ def get(appid, params: {}) end def client - build_client 'ISteamNews' + Steam::Client.new 'ISteamNews' end end end diff --git a/lib/steam-api/steam/player.rb b/lib/steam-api/steam/player.rb index c8b84c6..c6d518e 100644 --- a/lib/steam-api/steam/player.rb +++ b/lib/steam-api/steam/player.rb @@ -64,7 +64,7 @@ def community_badge_progress(steamid) end def client - build_client 'IPlayerService' + Steam::Client.new 'IPlayerService' end end end diff --git a/lib/steam-api/steam/remote_storage.rb b/lib/steam-api/steam/remote_storage.rb index 0aa47fa..d8987b3 100644 --- a/lib/steam-api/steam/remote_storage.rb +++ b/lib/steam-api/steam/remote_storage.rb @@ -30,7 +30,7 @@ def ugc_file(params: {}) end def client - build_client 'ISteamRemoteStorage' + Steam::Client.new 'ISteamRemoteStorage' end end end diff --git a/lib/steam-api/steam/store.rb b/lib/steam-api/steam/store.rb index 51a9ea2..998a2e3 100644 --- a/lib/steam-api/steam/store.rb +++ b/lib/steam-api/steam/store.rb @@ -16,7 +16,7 @@ def app_details(appid) private def client - build_client '', base_url: Steam::Helpers::STORE_API_BASE_URL + Steam::Client.new '', base_url: 'https://store.steampowered.com/api' end end end diff --git a/lib/steam-api/steam/user.rb b/lib/steam-api/steam/user.rb index 66208cc..695cfcc 100644 --- a/lib/steam-api/steam/user.rb +++ b/lib/steam-api/steam/user.rb @@ -77,7 +77,7 @@ def vanity_to_steamid(vanityurl) end def client - build_client 'ISteamUser' + Steam::Client.new 'ISteamUser' end end end diff --git a/lib/steam-api/steam/user_stats.rb b/lib/steam-api/steam/user_stats.rb index 4857765..5c8c7b4 100644 --- a/lib/steam-api/steam/user_stats.rb +++ b/lib/steam-api/steam/user_stats.rb @@ -85,7 +85,7 @@ def player_stats(appid, steamid) end def client - build_client('ISteamUserStats') + Steam::Client.new 'ISteamUserStats' end end end From cbb31d1796f86d61c89a17a5356444eaa0d86a24 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Fri, 28 Oct 2022 16:17:35 +0300 Subject: [PATCH 13/16] Improve code for `Steam.apikey` --- lib/steam-api/steam.rb | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/steam-api/steam.rb b/lib/steam-api/steam.rb index 6ca2d22..a89c357 100644 --- a/lib/steam-api/steam.rb +++ b/lib/steam-api/steam.rb @@ -2,18 +2,15 @@ # Base class definition module Steam - @apikey = ENV['STEAM_API_KEY'] - class << self attr_writer :apikey def apikey - if @apikey.nil? - raise ArgumentError, 'Please set your Steam API key.' unless ENV.key?('STEAM_API_KEY') + return @apikey if @apikey + + raise ArgumentError, 'Please set your Steam API key.' unless ENV.key?('STEAM_API_KEY') - @apikey = ENV['STEAM_API_KEY'] - end - @apikey + @apikey = ENV.fetch('STEAM_API_KEY') end end end From c85288aff52bf8f28d28213b42ac6a290d6655b3 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Fri, 28 Oct 2022 16:37:03 +0300 Subject: [PATCH 14/16] Add RuboCop development dependencies and resolve offenses --- .rubocop.yml | 17 +++++++++++++++++ spec/steam/remote_storage_spec.rb | 6 ++---- steam-api.gemspec | 6 +++++- 3 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 .rubocop.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..df7890b --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,17 @@ +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/spec/steam/remote_storage_spec.rb b/spec/steam/remote_storage_spec.rb index 77fc464..5b6a14d 100644 --- a/spec/steam/remote_storage_spec.rb +++ b/spec/steam/remote_storage_spec.rb @@ -1,9 +1,7 @@ # frozen_string_literal: true describe Steam::RemoteStorage do - describe '.published_file' do - end + pending '.published_file' - describe '.ugc_file' do - end + pending '.ugc_file' end diff --git a/steam-api.gemspec b/steam-api.gemspec index 6fd81ad..0570081 100644 --- a/steam-api.gemspec +++ b/steam-api.gemspec @@ -12,9 +12,12 @@ 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' @@ -22,6 +25,7 @@ Gem::Specification.new do |gem| 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_development_dependency 'rubocop', '~> 1.37.1' gem.add_dependency 'faraday', '~> 1.0' end From f6a0cb778d00a7626663b647384aa8d4868ea842 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Fri, 28 Oct 2022 21:09:00 +0300 Subject: [PATCH 15/16] Add suggested RuboCop plugins And resolve their offenses. --- .rubocop.yml | 4 ++ Rakefile | 2 + spec/steam/apps_spec.rb | 21 ++++---- spec/steam/economy_spec.rb | 13 +++-- spec/steam/news_spec.rb | 4 +- spec/steam/player_spec.rb | 23 ++++---- spec/steam/store_spec.rb | 4 +- ...users_stats_spec.rb => user_stats_spec.rb} | 35 ++++++++---- spec/steam/users_spec.rb | 30 +++++------ spec/steam_spec.rb | 53 ++++++++++--------- steam-api.gemspec | 3 ++ 11 files changed, 109 insertions(+), 83 deletions(-) rename spec/steam/{users_stats_spec.rb => user_stats_spec.rb} (64%) diff --git a/.rubocop.yml b/.rubocop.yml index df7890b..ddcf4cd 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,3 +1,7 @@ +require: + - rubocop-rake + - rubocop-rspec + AllCops: NewCops: enable TargetRubyVersion: 2.4 diff --git a/Rakefile b/Rakefile index 25864e6..4050718 100644 --- a/Rakefile +++ b/Rakefile @@ -6,4 +6,6 @@ require 'rspec/core/rake_task' RSpec::Core::RakeTask.new task default: :spec + +desc 'Alias for spec task' task test: :spec diff --git a/spec/steam/apps_spec.rb b/spec/steam/apps_spec.rb index 0eea0ba..4490890 100644 --- a/spec/steam/apps_spec.rb +++ b/spec/steam/apps_spec.rb @@ -2,10 +2,10 @@ 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 9e1e28d..bbfa59d 100644 --- a/spec/steam/economy_spec.rb +++ b/spec/steam/economy_spec.rb @@ -3,30 +3,29 @@ describe Steam::Economy do describe '.asset_info' do let(:result) do - Steam::Economy.asset_info( + 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 d12397c..57c7cec 100644 --- a/spec/steam/news_spec.rb +++ b/spec/steam/news_spec.rb @@ -2,10 +2,10 @@ 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 82e6595..cea5fe7 100644 --- a/spec/steam/player_spec.rb +++ b/spec/steam/player_spec.rb @@ -4,10 +4,10 @@ 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/store_spec.rb b/spec/steam/store_spec.rb index a670139..1df7092 100644 --- a/spec/steam/store_spec.rb +++ b/spec/steam/store_spec.rb @@ -4,10 +4,10 @@ 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 diff --git a/spec/steam/users_stats_spec.rb b/spec/steam/user_stats_spec.rb similarity index 64% rename from spec/steam/users_stats_spec.rb rename to spec/steam/user_stats_spec.rb index ddd6f2a..9baf5ca 100644 --- a/spec/steam/users_stats_spec.rb +++ b/spec/steam/user_stats_spec.rb @@ -2,23 +2,22 @@ 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,20 +34,34 @@ 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) do - Steam::UserStats.player_achievements(440, '76561197993276293') + described_class.player_achievements(440, '76561197993276293') end it 'returns a list of player achievements' do @@ -73,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 cdff6d3..504b6f0 100644 --- a/spec/steam/users_spec.rb +++ b/spec/steam/users_spec.rb @@ -5,10 +5,10 @@ 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,50 +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) } + 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 @@ -68,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 fd5c157..3a8f4dc 100644 --- a/spec/steam_spec.rb +++ b/spec/steam_spec.rb @@ -2,37 +2,42 @@ 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 0570081..a11f784 100644 --- a/steam-api.gemspec +++ b/steam-api.gemspec @@ -25,7 +25,10 @@ Gem::Specification.new do |gem| 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_development_dependency 'rubocop', '~> 1.37.1' + gem.add_development_dependency 'rubocop-rake', '~> 0.6.0' + gem.add_development_dependency 'rubocop-rspec', '~> 2.14.2' gem.add_dependency 'faraday', '~> 1.0' end From f6f279a20a6bd47849ddeeffbdeb02e8a52d6ea3 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Fri, 28 Oct 2022 21:37:10 +0300 Subject: [PATCH 16/16] Update Faraday dependency to v2 --- steam-api.gemspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/steam-api.gemspec b/steam-api.gemspec index a11f784..a392e5b 100644 --- a/steam-api.gemspec +++ b/steam-api.gemspec @@ -22,6 +22,8 @@ Gem::Specification.new do |gem| 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' @@ -29,6 +31,4 @@ Gem::Specification.new do |gem| 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' - - gem.add_dependency 'faraday', '~> 1.0' end