scoreoid 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES.md +5 -0
- data/lib/scoreoid/api.rb +11 -0
- data/lib/scoreoid/version.rb +1 -1
- data/spec/api_spec.rb +9 -0
- metadata +1 -1
data/CHANGES.md
CHANGED
data/lib/scoreoid/api.rb
CHANGED
@@ -35,14 +35,25 @@ module Scoreoid
|
|
35
35
|
# @param [Hash] params Parameters to include in the API request.
|
36
36
|
#
|
37
37
|
# @raise [Scoreoid::APIError] if the Scoreoid API returns an error response.
|
38
|
+
# @raise [MultiJson::DecodeError] if the Scoreoid API response can't be parsed
|
39
|
+
# (report a bug if this happens!)
|
38
40
|
#
|
39
41
|
# @return [Hash] The Scoreoid API response parsed into a Hash.
|
40
42
|
#
|
41
43
|
# @see .query
|
42
44
|
def query_and_parse(api_method, params={})
|
45
|
+
# We're gonna parse JSON, so ask for JSON
|
43
46
|
params = params.merge(response: 'json')
|
44
47
|
|
48
|
+
# Query Scoreoid
|
45
49
|
api_response = self.query(api_method, params)
|
50
|
+
|
51
|
+
# Fix for API responses that return arrays (they can't be parsed by MultiJson)
|
52
|
+
if api_response =~ /\A\[/ and api_response =~ /\]\Z/
|
53
|
+
api_response.sub!(/\A\[/, '') # Remove leading bracket
|
54
|
+
api_response.sub!(/\]\Z/, '') # Remove trailing bracket
|
55
|
+
end
|
56
|
+
|
46
57
|
parsed_result = MultiJson.load(api_response)
|
47
58
|
|
48
59
|
raise APIError, parsed_result['error'] if parsed_result.key? 'error'
|
data/lib/scoreoid/version.rb
CHANGED
data/spec/api_spec.rb
CHANGED
@@ -66,5 +66,14 @@ describe Scoreoid::API do
|
|
66
66
|
Scoreoid::API.query_and_parse('getScores')
|
67
67
|
end.to raise_error(Scoreoid::APIError, 'The API key is broken or the game is not active')
|
68
68
|
end
|
69
|
+
|
70
|
+
it 'should not raise an error if the the response is an array' do
|
71
|
+
example_response = %q([{"Player": {"username": "pwner"}}])
|
72
|
+
Scoreoid::API.stub(:query).and_return(example_response)
|
73
|
+
|
74
|
+
expect do
|
75
|
+
Scoreoid::API.query_and_parse('getPlayer', username: 'someuser')
|
76
|
+
end.to_not raise_error
|
77
|
+
end
|
69
78
|
end
|
70
79
|
end
|