Skip to content

Commit

Permalink
Merge pull request #28 from Arie/master
Browse files Browse the repository at this point in the history
Ruby 3.0 compatibility
  • Loading branch information
koraktor committed May 19, 2021
2 parents 368bf45 + 5fa72ab commit 5d0fd1f
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 21 deletions.
7 changes: 5 additions & 2 deletions .travis.yml
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion lib/steam-condenser/community/game_leaderboard.rb
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions 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
4 changes: 2 additions & 2 deletions lib/steam-condenser/community/web_api.rb
Expand Up @@ -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'

Expand Down Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions lib/steam-condenser/community/xml_data.rb
Expand Up @@ -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<String, Object>] 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
1 change: 1 addition & 0 deletions steam-condenser.gemspec
Expand Up @@ -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'
Expand Down
6 changes: 3 additions & 3 deletions test/steam-condenser/community/test_steam_group.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/steam-condenser/community/test_steam_id.rb
Expand Up @@ -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'

Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions test/steam-condenser/community/test_web_api.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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
Expand Down

0 comments on commit 5d0fd1f

Please sign in to comment.