From 81c5eb4d3fba6309adbe34d9019e3fec45fb1e1a Mon Sep 17 00:00:00 2001 From: Arie Date: Sun, 16 May 2021 19:48:33 +0200 Subject: [PATCH 1/6] Use URI.open for ruby 3 compatibility --- lib/steam-condenser/community/game_leaderboard.rb | 2 +- lib/steam-condenser/community/web_api.rb | 2 +- lib/steam-condenser/community/xml_data.rb | 7 ++----- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/steam-condenser/community/game_leaderboard.rb b/lib/steam-condenser/community/game_leaderboard.rb index b3b60fd..e290439 100644 --- a/lib/steam-condenser/community/game_leaderboard.rb +++ b/lib/steam-condenser/community/game_leaderboard.rb @@ -197,7 +197,7 @@ def parse_entries def self.load_leaderboards(app_id) begin url = "http://steamcommunity.com/stats/#{app_id}/leaderboards/?xml=1" - boards_data = MultiXml.parse(open(url, proxy: true)).values.first + boards_data = MultiXml.parse(URI.open(url, proxy: true)).values.first rescue raise SteamCondenser::Error.new 'XML data could not be parsed.', $! end diff --git a/lib/steam-condenser/community/web_api.rb b/lib/steam-condenser/community/web_api.rb index 8d3b8ea..77c8ab5 100644 --- a/lib/steam-condenser/community/web_api.rb +++ b/lib/steam-condenser/community/web_api.rb @@ -132,7 +132,7 @@ def self.get(format, interface, method, version = 1, params = {}) debug_url = @@api_key.nil? ? url : url.gsub(@@api_key, 'SECRET') log.debug "Querying Steam Web API: #{debug_url}" end - open(url, { 'Content-Type' => 'application/x-www-form-urlencoded' , proxy: true }).read + URI.open(url, { 'Content-Type' => 'application/x-www-form-urlencoded' , proxy: true }).read rescue OpenURI::HTTPError status = $!.io.status[0] status = [status, ''] unless status.is_a? Array diff --git a/lib/steam-condenser/community/xml_data.rb b/lib/steam-condenser/community/xml_data.rb index 4c30bc5..2fa36d9 100644 --- a/lib/steam-condenser/community/xml_data.rb +++ b/lib/steam-condenser/community/xml_data.rb @@ -8,23 +8,20 @@ require 'multi_xml' module SteamCondenser::Community - # This class provides basic functionality to parse XML data # # @author Sebastian Staudt module XMLData - # Parse the given URL as XML data using `multi_xml` # # @param [String] url The URL to parse # @return [Hash] The data parsed from the XML document # @raise [Error] if an error occurs while parsing the XML data def parse(url) - data = open url, proxy: true + data = URI.open url, proxy: true @xml_data = MultiXml.parse(data).values.first - rescue + rescue StandardError raise SteamCondenser::Error.new "XML data could not be parsed: #{$!.message}", $! end - end end From ae3909725d1ef88e75c78ade38174f4c186ac15d Mon Sep 17 00:00:00 2001 From: Arie Date: Mon, 17 May 2021 20:19:14 +0200 Subject: [PATCH 2/6] Change the mocks so they use URI.open for Ruby 3.0 compatibility --- test/steam-condenser/community/test_steam_group.rb | 6 +++--- test/steam-condenser/community/test_steam_id.rb | 4 ++-- test/steam-condenser/community/test_web_api.rb | 10 +++++----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/test/steam-condenser/community/test_steam_group.rb b/test/steam-condenser/community/test_steam_group.rb index a6b3cac..f050d12 100644 --- a/test/steam-condenser/community/test_steam_group.rb +++ b/test/steam-condenser/community/test_steam_group.rb @@ -31,7 +31,7 @@ class TestSteamGroup < Test::Unit::TestCase should 'be able to fetch its members and properties' do url = fixture_io 'valve-members.xml' - Community::SteamGroup.any_instance.expects(:open).with('http://steamcommunity.com/gid/103582791429521412/memberslistxml?p=1', proxy: true).returns url + URI.expects(:open).with('http://steamcommunity.com/gid/103582791429521412/memberslistxml?p=1', proxy: true).returns url group = Community::SteamGroup.new 103582791429521412 members = group.members @@ -69,7 +69,7 @@ class TestSteamGroup < Test::Unit::TestCase should 'raise an exception when parsing invalid XML' do error = assert_raises Error do url = fixture_io 'invalid.xml' - Community::SteamGroup.any_instance.expects(:open).with('http://steamcommunity.com/groups/valve/memberslistxml?p=1', proxy: true).returns url + URI.expects(:open).with('http://steamcommunity.com/groups/valve/memberslistxml?p=1', proxy: true).returns url Community::SteamGroup.new 'valve' end @@ -78,7 +78,7 @@ class TestSteamGroup < Test::Unit::TestCase should 'be able to parse just the member count' do url = fixture_io 'valve-members.xml' - Community::SteamGroup.any_instance.expects(:open).with('http://steamcommunity.com/groups/valve/memberslistxml?p=1', proxy: true).returns url + URI.expects(:open).with('http://steamcommunity.com/groups/valve/memberslistxml?p=1', proxy: true).returns url group = Community::SteamGroup.new 'valve', false assert_equal 239, group.member_count diff --git a/test/steam-condenser/community/test_steam_id.rb b/test/steam-condenser/community/test_steam_id.rb index 35ddf67..c20c61c 100644 --- a/test/steam-condenser/community/test_steam_id.rb +++ b/test/steam-condenser/community/test_steam_id.rb @@ -87,7 +87,7 @@ class TestSteamId < Test::Unit::TestCase should 'be able to fetch its data' do url = fixture_io 'sonofthor.xml' - Community::SteamId.any_instance.expects(:open).with('http://steamcommunity.com/id/son_of_thor?xml=1', proxy: true).returns url + URI.expects(:open).with('http://steamcommunity.com/id/son_of_thor?xml=1', proxy: true).returns url steam_id = Community::SteamId.new 'Son_of_Thor' @@ -157,7 +157,7 @@ class TestSteamId < Test::Unit::TestCase should 'raise an exception when parsing invalid XML' do error = assert_raises Error do url = fixture_io 'invalid.xml' - Community::SteamId.any_instance.expects(:open).with('http://steamcommunity.com/id/son_of_thor?xml=1', proxy: true).returns url + URI.expects(:open).with('http://steamcommunity.com/id/son_of_thor?xml=1', proxy: true).returns url Community::SteamId.new 'Son_of_Thor' end diff --git a/test/steam-condenser/community/test_web_api.rb b/test/steam-condenser/community/test_web_api.rb index 34b3dc7..4c0bde8 100644 --- a/test/steam-condenser/community/test_web_api.rb +++ b/test/steam-condenser/community/test_web_api.rb @@ -59,7 +59,7 @@ class TestWebApi < Test::Unit::TestCase should 'load data from the Steam Community Web API' do data = mock read: 'data' - Community::WebApi.expects(:open).with do |url, options| + URI.expects(:open).with do |url, options| options == { proxy: true, 'Content-Type' => 'application/x-www-form-urlencoded' } && url.start_with?('https://api.steampowered.com/interface/method/v2/?') && (url.split('?').last.split('&') & %w{test=param format=json key=0123456789ABCDEF0123456789ABCDEF}).size == 3 @@ -72,7 +72,7 @@ class TestWebApi < Test::Unit::TestCase Community::WebApi.api_key = nil data = mock read: 'data' - Community::WebApi.expects(:open).with do |url, options| + URI.expects(:open).with do |url, options| options == { proxy: true, 'Content-Type' => 'application/x-www-form-urlencoded' } && url.start_with?('https://api.steampowered.com/interface/method/v2/?') && (url.split('?').last.split('&') & %w{test=param format=json}).size == 2 @@ -84,7 +84,7 @@ class TestWebApi < Test::Unit::TestCase should 'handle unauthorized access error when loading data' do io = mock status: [401] http_error = OpenURI::HTTPError.new '', io - Community::WebApi.expects(:open).raises http_error + URI.expects(:open).raises http_error error = assert_raises Error::WebApi do Community::WebApi.get :json, 'interface', 'method', 2, test: 'param' @@ -95,7 +95,7 @@ class TestWebApi < Test::Unit::TestCase should 'handle generic HTTP errors when loading data' do io = mock status: [[404, 'Not found']] http_error = OpenURI::HTTPError.new '', io - Community::WebApi.expects(:open).raises http_error + URI.expects(:open).raises http_error error = assert_raises Error::WebApi do Community::WebApi.get :json, 'interface', 'method', 2, test: 'param' @@ -107,7 +107,7 @@ class TestWebApi < Test::Unit::TestCase Community::WebApi.secure = false data = mock read: 'data' - Community::WebApi.expects(:open).with do |url, options| + URI.expects(:open).with do |url, options| options == { proxy: true, 'Content-Type' => 'application/x-www-form-urlencoded' } && url.start_with?('http://api.steampowered.com/interface/method/v2/?') && (url.split('?').last.split('&') & %w{test=param format=json key=0123456789ABCDEF0123456789ABCDEF}).size == 3 From 649391a312b84c046a8232d1ecac4204d5d22914 Mon Sep 17 00:00:00 2001 From: Arie Date: Mon, 17 May 2021 20:26:30 +0200 Subject: [PATCH 3/6] Add rexml as a dependency because it is no longer part of ruby 3.0 and up --- steam-condenser.gemspec | 1 + 1 file changed, 1 insertion(+) diff --git a/steam-condenser.gemspec b/steam-condenser.gemspec index 89085c8..d1395a3 100644 --- a/steam-condenser.gemspec +++ b/steam-condenser.gemspec @@ -15,6 +15,7 @@ Gem::Specification.new do |s| s.add_dependency 'multi_json', '~> 1.6' s.add_dependency 'multi_xml', '~> 0.5' + s.add_dependency 'rexml', '~> 3.2' s.add_development_dependency 'mocha', '~> 1.1' s.add_development_dependency 'rake', '~> 10.4' From 8e40943051959d394fe1e6c145ca10e394524f40 Mon Sep 17 00:00:00 2001 From: Arie Date: Tue, 18 May 2021 22:27:00 +0200 Subject: [PATCH 4/6] Update ruby versions to build on --- .travis.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 17f10df..c766d20 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,11 @@ rvm: - 2.0 - 2.1 - 2.2 - - 2.3.1 + - 2.3 + - 2.6 + - 2.7 + - 3.0 + - - jruby-head - ruby-head matrix: From 692c1d8560ff5f3fd35c51fad393f21fea64e627 Mon Sep 17 00:00:00 2001 From: Arie Date: Tue, 18 May 2021 23:31:07 +0200 Subject: [PATCH 5/6] Use a custom URI module so we can switch between pre ruby 2.7 and later behavior --- lib/steam-condenser/community/game_leaderboard.rb | 1 + lib/steam-condenser/community/uri.rb | 15 +++++++++++++++ lib/steam-condenser/community/web_api.rb | 2 +- .../steam-condenser/community/test_steam_group.rb | 6 +++--- test/steam-condenser/community/test_steam_id.rb | 4 ++-- test/steam-condenser/community/test_web_api.rb | 10 +++++----- 6 files changed, 27 insertions(+), 11 deletions(-) create mode 100644 lib/steam-condenser/community/uri.rb diff --git a/lib/steam-condenser/community/game_leaderboard.rb b/lib/steam-condenser/community/game_leaderboard.rb index e290439..2ed9f3b 100644 --- a/lib/steam-condenser/community/game_leaderboard.rb +++ b/lib/steam-condenser/community/game_leaderboard.rb @@ -5,6 +5,7 @@ require 'multi_xml' +require 'steam-condenser/community/uri' require 'steam-condenser/community/game_leaderboard_entry' require 'steam-condenser/community/steam_id' require 'steam-condenser/community/xml_data' diff --git a/lib/steam-condenser/community/uri.rb b/lib/steam-condenser/community/uri.rb new file mode 100644 index 0000000..827031b --- /dev/null +++ b/lib/steam-condenser/community/uri.rb @@ -0,0 +1,15 @@ +require 'open-uri' + +module SteamCondenser::Community + module URI + if RUBY_VERSION >= '2.7.0' + def self.open(*args) + ::URI.open(*args) + end + else + def self.open(*args) + Kernel.open(*args) + end + end + end +end diff --git a/lib/steam-condenser/community/web_api.rb b/lib/steam-condenser/community/web_api.rb index 77c8ab5..891b280 100644 --- a/lib/steam-condenser/community/web_api.rb +++ b/lib/steam-condenser/community/web_api.rb @@ -4,7 +4,7 @@ # Copyright (c) 2010-2015, Sebastian Staudt require 'multi_json' -require 'open-uri' +require 'steam-condenser/community/uri' require 'steam-condenser/error/web_api' diff --git a/test/steam-condenser/community/test_steam_group.rb b/test/steam-condenser/community/test_steam_group.rb index f050d12..c974343 100644 --- a/test/steam-condenser/community/test_steam_group.rb +++ b/test/steam-condenser/community/test_steam_group.rb @@ -31,7 +31,7 @@ class TestSteamGroup < Test::Unit::TestCase should 'be able to fetch its members and properties' do url = fixture_io 'valve-members.xml' - URI.expects(:open).with('http://steamcommunity.com/gid/103582791429521412/memberslistxml?p=1', proxy: true).returns url + Community::URI.expects(:open).with('http://steamcommunity.com/gid/103582791429521412/memberslistxml?p=1', proxy: true).returns url group = Community::SteamGroup.new 103582791429521412 members = group.members @@ -69,7 +69,7 @@ class TestSteamGroup < Test::Unit::TestCase should 'raise an exception when parsing invalid XML' do error = assert_raises Error do url = fixture_io 'invalid.xml' - URI.expects(:open).with('http://steamcommunity.com/groups/valve/memberslistxml?p=1', proxy: true).returns url + Community::URI.expects(:open).with('http://steamcommunity.com/groups/valve/memberslistxml?p=1', proxy: true).returns url Community::SteamGroup.new 'valve' end @@ -78,7 +78,7 @@ class TestSteamGroup < Test::Unit::TestCase should 'be able to parse just the member count' do url = fixture_io 'valve-members.xml' - URI.expects(:open).with('http://steamcommunity.com/groups/valve/memberslistxml?p=1', proxy: true).returns url + Community::URI.expects(:open).with('http://steamcommunity.com/groups/valve/memberslistxml?p=1', proxy: true).returns url group = Community::SteamGroup.new 'valve', false assert_equal 239, group.member_count diff --git a/test/steam-condenser/community/test_steam_id.rb b/test/steam-condenser/community/test_steam_id.rb index c20c61c..72346be 100644 --- a/test/steam-condenser/community/test_steam_id.rb +++ b/test/steam-condenser/community/test_steam_id.rb @@ -87,7 +87,7 @@ class TestSteamId < Test::Unit::TestCase should 'be able to fetch its data' do url = fixture_io 'sonofthor.xml' - URI.expects(:open).with('http://steamcommunity.com/id/son_of_thor?xml=1', proxy: true).returns url + Community::URI.expects(:open).with('http://steamcommunity.com/id/son_of_thor?xml=1', proxy: true).returns url steam_id = Community::SteamId.new 'Son_of_Thor' @@ -157,7 +157,7 @@ class TestSteamId < Test::Unit::TestCase should 'raise an exception when parsing invalid XML' do error = assert_raises Error do url = fixture_io 'invalid.xml' - URI.expects(:open).with('http://steamcommunity.com/id/son_of_thor?xml=1', proxy: true).returns url + Community::URI.expects(:open).with('http://steamcommunity.com/id/son_of_thor?xml=1', proxy: true).returns url Community::SteamId.new 'Son_of_Thor' end diff --git a/test/steam-condenser/community/test_web_api.rb b/test/steam-condenser/community/test_web_api.rb index 4c0bde8..ef62b29 100644 --- a/test/steam-condenser/community/test_web_api.rb +++ b/test/steam-condenser/community/test_web_api.rb @@ -59,7 +59,7 @@ class TestWebApi < Test::Unit::TestCase should 'load data from the Steam Community Web API' do data = mock read: 'data' - URI.expects(:open).with do |url, options| + Community::URI.expects(:open).with do |url, options| options == { proxy: true, 'Content-Type' => 'application/x-www-form-urlencoded' } && url.start_with?('https://api.steampowered.com/interface/method/v2/?') && (url.split('?').last.split('&') & %w{test=param format=json key=0123456789ABCDEF0123456789ABCDEF}).size == 3 @@ -72,7 +72,7 @@ class TestWebApi < Test::Unit::TestCase Community::WebApi.api_key = nil data = mock read: 'data' - URI.expects(:open).with do |url, options| + Community::URI.expects(:open).with do |url, options| options == { proxy: true, 'Content-Type' => 'application/x-www-form-urlencoded' } && url.start_with?('https://api.steampowered.com/interface/method/v2/?') && (url.split('?').last.split('&') & %w{test=param format=json}).size == 2 @@ -84,7 +84,7 @@ class TestWebApi < Test::Unit::TestCase should 'handle unauthorized access error when loading data' do io = mock status: [401] http_error = OpenURI::HTTPError.new '', io - URI.expects(:open).raises http_error + Community::URI.expects(:open).raises http_error error = assert_raises Error::WebApi do Community::WebApi.get :json, 'interface', 'method', 2, test: 'param' @@ -95,7 +95,7 @@ class TestWebApi < Test::Unit::TestCase should 'handle generic HTTP errors when loading data' do io = mock status: [[404, 'Not found']] http_error = OpenURI::HTTPError.new '', io - URI.expects(:open).raises http_error + Community::URI.expects(:open).raises http_error error = assert_raises Error::WebApi do Community::WebApi.get :json, 'interface', 'method', 2, test: 'param' @@ -107,7 +107,7 @@ class TestWebApi < Test::Unit::TestCase Community::WebApi.secure = false data = mock read: 'data' - URI.expects(:open).with do |url, options| + Community::URI.expects(:open).with do |url, options| options == { proxy: true, 'Content-Type' => 'application/x-www-form-urlencoded' } && url.start_with?('http://api.steampowered.com/interface/method/v2/?') && (url.split('?').last.split('&') & %w{test=param format=json key=0123456789ABCDEF0123456789ABCDEF}).size == 3 From 5fa72ab5a171f768bd47b935b028c1b5c557852e Mon Sep 17 00:00:00 2001 From: Arie Date: Tue, 18 May 2021 23:39:44 +0200 Subject: [PATCH 6/6] Drop 2.0 support --- .travis.yml | 1 - README.md | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index c766d20..5ede63f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,6 @@ language: ruby cache: bundler sudo: false rvm: - - 2.0 - 2.1 - 2.2 - 2.3 diff --git a/README.md b/README.md index 32803f3..18d55c9 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Currently it is implemented in Java, PHP and Ruby. ## Requirements -* Ruby 2.0 or newer (and compatible Ruby VMs) +* Ruby 2.1 or newer (and compatible Ruby VMs) * Any operating system able to run such a VM The following gems are required: