diff --git a/.travis.yml b/.travis.yml index 17f10df..5ede63f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,10 +2,13 @@ language: ruby cache: bundler sudo: false rvm: - - 2.0 - 2.1 - 2.2 - - 2.3.1 + - 2.3 + - 2.6 + - 2.7 + - 3.0 + - - jruby-head - ruby-head matrix: diff --git a/README.md b/README.md index 1a6ba20..51a07fc 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,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: diff --git a/lib/steam-condenser/community/game_leaderboard.rb b/lib/steam-condenser/community/game_leaderboard.rb index b3b60fd..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' @@ -197,7 +198,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/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 8d3b8ea..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' @@ -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 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' diff --git a/test/steam-condenser/community/test_steam_group.rb b/test/steam-condenser/community/test_steam_group.rb index a6b3cac..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' - Community::SteamGroup.any_instance.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' - Community::SteamGroup.any_instance.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' - Community::SteamGroup.any_instance.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 35ddf67..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' - Community::SteamId.any_instance.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' - Community::SteamId.any_instance.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 34b3dc7..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' - Community::WebApi.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' - Community::WebApi.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 - Community::WebApi.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 - Community::WebApi.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' - Community::WebApi.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