scoreoid 0.1.0 → 1.1.1.alpha
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +2 -0
- data/.yardopts +4 -0
- data/CHANGES.md +15 -0
- data/Gemfile +6 -2
- data/README.md +57 -12
- data/Rakefile +5 -0
- data/lib/scoreoid/api.rb +99 -0
- data/lib/scoreoid/player.rb +154 -10
- data/lib/scoreoid/version.rb +2 -1
- data/lib/scoreoid.rb +20 -12
- data/scoreoid.gemspec +4 -0
- data/spec/scoreoid/api_spec.rb +79 -0
- data/spec/scoreoid/player_spec.rb +73 -0
- data/spec/scoreoid/version_spec.rb +9 -0
- data/spec/scoreoid_spec.rb +6 -8
- data/spec/spec_helper.rb +8 -8
- metadata +39 -29
- data/lib/scoreoid/api_client.rb +0 -61
- data/spec/api_client_spec.rb +0 -80
- data/spec/player_spec.rb +0 -10
- data/spec/version_spec.rb +0 -7
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 053fe26edc9947c41476ce2e2539a66d6e928e5dc09ef8cf9883e3d606af0c06
|
4
|
+
data.tar.gz: 7d03f92f8fb9d44337f494a60ccd715e0c61e226e5c0ee84e7e740c4d9e58865
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7bebbfe865785aa65cec817db5b8032c2aa896c0ce2fb9b45331d2256d12b75d52601c2befe262900818eef835b7e87c7065f1bbd4fa8211480fd7821d89515a
|
7
|
+
data.tar.gz: 80b430ad30cfe62d0f010141a2a9c55e7393118e72daeb73662f0ac67d2563e0e8203e4dfea22b82c1078191649d6dc1da7dba19ed9c452a167e95794dc0f825
|
data/.travis.yml
ADDED
data/.yardopts
ADDED
data/CHANGES.md
CHANGED
@@ -1,6 +1,21 @@
|
|
1
1
|
Scoreoid Ruby Change Log
|
2
2
|
========================
|
3
3
|
|
4
|
+
2012-11-25 *v1.1.1*
|
5
|
+
-------------------
|
6
|
+
|
7
|
+
- Fix a issue that occured when parsing API responses where the root node is an array.
|
8
|
+
|
9
|
+
2012-11-25 *v1.1.0*
|
10
|
+
-------------------
|
11
|
+
|
12
|
+
- Allow passing date parameters (`:start_date` or `:end_date`) in natural language, such as "1 year ago". You can still pass a Date or Time object instead.
|
13
|
+
|
14
|
+
2012-11-25 *v1.0.0*
|
15
|
+
-------------------
|
16
|
+
|
17
|
+
First stable release.
|
18
|
+
|
4
19
|
2012-11-24 *v0.1.0*
|
5
20
|
-------------------
|
6
21
|
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,41 +1,79 @@
|
|
1
|
+
## NOTICE: Project is unmaintained because the Scoroid API no longer exists.
|
2
|
+
|
3
|
+
This repository is left for historical reasons.
|
4
|
+
|
1
5
|
Scoreoid Ruby
|
2
6
|
=============
|
3
7
|
|
4
|
-
|
5
|
-
|
8
|
+
[![Build Status](https://travis-ci.org/xtagon/scoreoid-gem.png)](https://travis-ci.org/xtagon/scoreoid-gem)
|
9
|
+
|
10
|
+
Summary
|
11
|
+
-------
|
12
|
+
|
13
|
+
Scoreoid Ruby is a wrapper for the [Scoreoid][1] API.
|
6
14
|
|
7
15
|
Installation
|
8
16
|
------------
|
9
17
|
|
10
18
|
Add this line to your application's Gemfile:
|
11
19
|
|
12
|
-
|
20
|
+
``` ruby
|
21
|
+
gem 'scoreoid'
|
22
|
+
```
|
13
23
|
|
14
24
|
And then execute:
|
15
25
|
|
16
|
-
|
26
|
+
``` shell
|
27
|
+
$ bundle
|
28
|
+
```
|
17
29
|
|
18
30
|
Or install it yourself as:
|
19
31
|
|
20
|
-
|
32
|
+
``` shell
|
33
|
+
$ gem install scoreoid
|
34
|
+
```
|
35
|
+
|
36
|
+
Usage
|
37
|
+
-----
|
38
|
+
|
39
|
+
Full documentation is [available online][2].
|
40
|
+
|
41
|
+
To get started, configure Scoreoid Ruby with your API key and game ID:
|
42
|
+
|
43
|
+
``` ruby
|
44
|
+
require 'scoreoid'
|
45
|
+
|
46
|
+
Scoreoid.configure(api_key: 'YOUR_API_KEY', game_id: 'YOUR_GAME_ID')
|
47
|
+
```
|
48
|
+
|
49
|
+
Then you can start querying Scoreoid API methods:
|
50
|
+
|
51
|
+
``` ruby
|
52
|
+
new_players_count = Scoreoid::API.query('countPlayers', start_date: '2009-08-04')
|
53
|
+
new_players_count['players'] # => 34
|
54
|
+
```
|
55
|
+
|
56
|
+
Any Scoreoid API method may be called in this manner. See the [Scoreoid Wiki][3] for information on available API methods.
|
57
|
+
|
58
|
+
Future versions of Scoreoid Ruby will provide a more object-oriented manner of querying data. Then it will look more like this:
|
59
|
+
|
60
|
+
``` ruby
|
61
|
+
Scoreoid::Player.count # => 34
|
62
|
+
```
|
21
63
|
|
22
64
|
Contributing
|
23
65
|
------------
|
24
66
|
|
25
67
|
Contributions are most welcome!
|
26
68
|
|
27
|
-
|
28
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
-
5. Create new Pull Request
|
69
|
+
You can fork the source code on [GitHub][4] or [BitBucket][5].
|
32
70
|
|
33
|
-
Please use the [issue tracker]
|
71
|
+
Please use the [issue tracker][6] if you encounter a bug or have a feature request.
|
34
72
|
|
35
73
|
License
|
36
74
|
-------
|
37
75
|
|
38
|
-
Copyright © 2012 [Justin Workman](mailto:xtagon@gmail.com)
|
76
|
+
Copyright © 2012-2013 [Justin Workman](mailto:xtagon@gmail.com)
|
39
77
|
|
40
78
|
MIT License
|
41
79
|
|
@@ -57,3 +95,10 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
57
95
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
58
96
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
59
97
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
98
|
+
|
99
|
+
[1]: https://rubygems.org/gems/scoreoid
|
100
|
+
[2]: http://rubydoc.info/gems/scoreoid/frames
|
101
|
+
[3]: http://wiki.scoreoid.net/category/api/
|
102
|
+
[4]: https://github.com/xtagon/scoreoid-gem
|
103
|
+
[5]: https://bitbucket.org/xtagon/scoreoid-gem
|
104
|
+
[6]: https://github.com/xtagon/scoreoid-gem/issues
|
data/Rakefile
CHANGED
data/lib/scoreoid/api.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'chronic'
|
2
|
+
require 'multi_json'
|
3
|
+
require 'rest_client'
|
4
|
+
|
5
|
+
module Scoreoid
|
6
|
+
# This exception is raised when the Scoreoid API returns an error response.
|
7
|
+
class APIError < StandardError; end
|
8
|
+
|
9
|
+
# A singleton class with methods for querying the Scoreoid API.
|
10
|
+
class API
|
11
|
+
class << self
|
12
|
+
# Default API request parameters used by {.query}
|
13
|
+
#
|
14
|
+
# This would normally be set with {Scoreoid.configure}
|
15
|
+
attr_accessor :default_params
|
16
|
+
|
17
|
+
# Query a given Scoreoid API method and return the repsonse as a string.
|
18
|
+
#
|
19
|
+
# Supplied parameters will be prepared with {.prepare_params} before sending.
|
20
|
+
# This is so that you can, for example, supply a Date object for :start_date
|
21
|
+
# even though the Scoreoid API expects it to be a string formatted as "YYYY-MM-DD".
|
22
|
+
#
|
23
|
+
# @param [String] api_method The Scoreoid API method to query
|
24
|
+
# @param [Hash] params Parameters to include in the API request
|
25
|
+
#
|
26
|
+
# @return [String] The Scoreoid API response
|
27
|
+
#
|
28
|
+
# @see .query_and_parse
|
29
|
+
def query(api_method, params={})
|
30
|
+
params = self.prepare_params(params)
|
31
|
+
params.merge!(self.default_params ||= {})
|
32
|
+
RestClient.post("https://www.scoreoid.com/api/#{api_method}", params)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Query a given Scoreoid API method and parse the JSON response.
|
36
|
+
#
|
37
|
+
# The response type is set to 'json' for you automatically.
|
38
|
+
#
|
39
|
+
# @param [String] api_method The Scoreoid API method to query
|
40
|
+
# @param [Hash] params Parameters to include in the API request.
|
41
|
+
#
|
42
|
+
# @raise [Scoreoid::APIError] if the Scoreoid API returns an error response.
|
43
|
+
# @raise [MultiJson::DecodeError] if the Scoreoid API response can't be parsed
|
44
|
+
# (report a bug if this happens!)
|
45
|
+
#
|
46
|
+
# @return [Hash] The Scoreoid API response parsed into a Hash.
|
47
|
+
#
|
48
|
+
# @see .query
|
49
|
+
def query_and_parse(api_method, params={})
|
50
|
+
# We're gonna parse JSON, so ask for JSON
|
51
|
+
params = params.merge(response: 'json')
|
52
|
+
|
53
|
+
# Query Scoreoid
|
54
|
+
api_response = self.query(api_method, params)
|
55
|
+
|
56
|
+
# Fix for API responses that return arrays (they can't be parsed by MultiJson)
|
57
|
+
if api_response =~ /\A\[/ and api_response =~ /\]\Z/
|
58
|
+
api_response.sub!(/\A\[/, '') # Remove leading bracket
|
59
|
+
api_response.sub!(/\]\Z/, '') # Remove trailing bracket
|
60
|
+
end
|
61
|
+
|
62
|
+
# Parse the response
|
63
|
+
parsed_result = MultiJson.load(api_response)
|
64
|
+
|
65
|
+
# Raise an error if the response JSON contained one
|
66
|
+
raise APIError, parsed_result['error'] if parsed_result.key? 'error'
|
67
|
+
|
68
|
+
# Return the parsed result
|
69
|
+
parsed_result
|
70
|
+
end
|
71
|
+
|
72
|
+
# Attempt to coerce parameters into formats that the Scoreoid API expects.
|
73
|
+
#
|
74
|
+
# Date parameters will be parsed with Chronic, so you can supply dates in
|
75
|
+
# natural language such as "may 5th 2012" or "1 year ago".
|
76
|
+
#
|
77
|
+
# @param [Hash] params A hash of any parameters you wish to format.
|
78
|
+
#
|
79
|
+
# @option params [#to_s, #strftime] :start_date
|
80
|
+
# @option params [#to_s, #strftime] :end_date
|
81
|
+
#
|
82
|
+
# @return [Hash] The formatted parameters, ready to use in an API query.
|
83
|
+
def prepare_params(params)
|
84
|
+
params.each do |key, _|
|
85
|
+
if [:start_date, :end_date].include?(key)
|
86
|
+
if params[key].respond_to? :to_s
|
87
|
+
params[key] = Chronic.parse(params[key].to_s, context: :past)
|
88
|
+
end
|
89
|
+
|
90
|
+
if params[key].respond_to? :strftime
|
91
|
+
params[key] = params[key].strftime '%Y-%m-%d'
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
params
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
data/lib/scoreoid/player.rb
CHANGED
@@ -1,15 +1,159 @@
|
|
1
|
-
require 'scoreoid/api_client'
|
2
|
-
|
3
1
|
module Scoreoid
|
2
|
+
# Represents the game players.
|
4
3
|
class Player
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# @example
|
8
|
-
# Scoreoid.
|
9
|
-
#
|
10
|
-
#
|
11
|
-
|
12
|
-
|
4
|
+
# Get the number of players for a game.
|
5
|
+
#
|
6
|
+
# @example Count the total number of players
|
7
|
+
# puts "This game has #{Scoreoid::Player.count} total players."
|
8
|
+
#
|
9
|
+
# @example Count new players
|
10
|
+
# count = Scoreoid::Player.count(start_date: '2012-11-01')
|
11
|
+
# puts "There are #{count} new players since 2012-11-01."
|
12
|
+
#
|
13
|
+
# @param [Hash] params Parameters to include in the API request.
|
14
|
+
# Default parameters set with {Scoreoid.configure} will be included for you.
|
15
|
+
#
|
16
|
+
# @option params [String] :api_key Your Scoreoid API key
|
17
|
+
# @option params [String] :game_id The game ID
|
18
|
+
# @option params [Date, Time, String] :start_date optional
|
19
|
+
# @option params [Date, Time, String] :end_date optional
|
20
|
+
#
|
21
|
+
# @return [Integer] The number of players.
|
22
|
+
def self.count(params={})
|
23
|
+
Scoreoid::API.query_and_parse('countPlayers', params)['players']
|
24
|
+
end
|
25
|
+
|
26
|
+
# Create a new player for a game.
|
27
|
+
#
|
28
|
+
# @example Create a player named Bob
|
29
|
+
# Scoreoid::Player.create(username: 'bob', first_name: 'Bob', last_name: 'Ross')
|
30
|
+
#
|
31
|
+
# @param [Hash] params Parameters to include in the API request.
|
32
|
+
# Default parameters set with {Scoreoid.configure} will be included for you.
|
33
|
+
#
|
34
|
+
# @option params [String] :api_key Your Scoreoid API key
|
35
|
+
# @option params [String] :game_id The game ID
|
36
|
+
# @option params [String] :username The player's username (required)
|
37
|
+
# @option params [String] :password Optional
|
38
|
+
# @option params [String] :score optional
|
39
|
+
# @option params [String] :difficulty optional
|
40
|
+
# @option params [String] :unique_id optional
|
41
|
+
# @option params [String] :first_name optional
|
42
|
+
# @option params [String] :last_name optional
|
43
|
+
# @option params [String] :email optional
|
44
|
+
# @option params [String] :created optional
|
45
|
+
# @option params [String] :updated optional
|
46
|
+
# @option params [String] :bonus optional
|
47
|
+
# @option params [String] :achievements optional
|
48
|
+
# @option params [String] :best_score optional
|
49
|
+
# @option params [String] :gold optional
|
50
|
+
# @option params [String] :money optional
|
51
|
+
# @option params [String] :kills optional
|
52
|
+
# @option params [String] :lives optional
|
53
|
+
# @option params [String] :time_played optional
|
54
|
+
# @option params [String] :unlocked_levels optional
|
55
|
+
# @option params [String] :unlocked_items optional
|
56
|
+
# @option params [String] :inventory optional
|
57
|
+
# @option params [String] :last_level optional
|
58
|
+
# @option params [String] :current_level optional
|
59
|
+
# @option params [String] :current_time optional
|
60
|
+
# @option params [String] :current_bonus optional
|
61
|
+
# @option params [String] :current_kills optional
|
62
|
+
# @option params [String] :current_achievements optional
|
63
|
+
# @option params [String] :current_gold optional
|
64
|
+
# @option params [String] :current_unlocked_levels optional
|
65
|
+
# @option params [String] :current_unlocked_items optional
|
66
|
+
# @option params [String] :current_lives optional
|
67
|
+
# @option params [String] :xp optional
|
68
|
+
# @option params [String] :energy optional
|
69
|
+
# @option params [String] :boost optional
|
70
|
+
# @option params [String] :latitude optional
|
71
|
+
# @option params [String] :longitude optional
|
72
|
+
# @option params [String] :game_state optional
|
73
|
+
# @option params [String] :platform optional
|
74
|
+
#
|
75
|
+
# @raise [Scoreoid::APIError] if the player could not be created
|
76
|
+
#
|
77
|
+
# @return [Hash] The API response from Scoreoid (should contain a success message)
|
78
|
+
def self.create(params={})
|
79
|
+
Scoreoid::API.query_and_parse('createPlayer', params)
|
80
|
+
end
|
81
|
+
|
82
|
+
# Edit player information.
|
83
|
+
#
|
84
|
+
# @example Update John's first and last name
|
85
|
+
# Scoreoid::Player.edit(username: 'john', first_name: 'John', last_name: 'Dough')
|
86
|
+
#
|
87
|
+
# @param [Hash] params Parameters to include in the API request.
|
88
|
+
# Default parameters set with {Scoreoid.configure} will be included for you.
|
89
|
+
#
|
90
|
+
# @option params [String] :api_key Your Scoreoid API key
|
91
|
+
# @option params [String] :game_id The game ID
|
92
|
+
# @option params [String] :username The player's username (required)
|
93
|
+
# @option params [String] :password Optional
|
94
|
+
# @option params [String] :score optional
|
95
|
+
# @option params [String] :difficulty optional
|
96
|
+
# @option params [String] :unique_id optional
|
97
|
+
# @option params [String] :first_name optional
|
98
|
+
# @option params [String] :last_name optional
|
99
|
+
# @option params [String] :email optional
|
100
|
+
# @option params [String] :created optional
|
101
|
+
# @option params [String] :updated optional
|
102
|
+
# @option params [String] :bonus optional
|
103
|
+
# @option params [String] :achievements optional
|
104
|
+
# @option params [String] :best_score optional
|
105
|
+
# @option params [String] :gold optional
|
106
|
+
# @option params [String] :money optional
|
107
|
+
# @option params [String] :kills optional
|
108
|
+
# @option params [String] :lives optional
|
109
|
+
# @option params [String] :time_played optional
|
110
|
+
# @option params [String] :unlocked_levels optional
|
111
|
+
# @option params [String] :unlocked_items optional
|
112
|
+
# @option params [String] :inventory optional
|
113
|
+
# @option params [String] :last_level optional
|
114
|
+
# @option params [String] :current_level optional
|
115
|
+
# @option params [String] :current_time optional
|
116
|
+
# @option params [String] :current_bonus optional
|
117
|
+
# @option params [String] :current_kills optional
|
118
|
+
# @option params [String] :current_achievements optional
|
119
|
+
# @option params [String] :current_gold optional
|
120
|
+
# @option params [String] :current_unlocked_levels optional
|
121
|
+
# @option params [String] :current_unlocked_items optional
|
122
|
+
# @option params [String] :current_lives optional
|
123
|
+
# @option params [String] :xp optional
|
124
|
+
# @option params [String] :energy optional
|
125
|
+
# @option params [String] :boost optional
|
126
|
+
# @option params [String] :latitude optional
|
127
|
+
# @option params [String] :longitude optional
|
128
|
+
# @option params [String] :game_state optional
|
129
|
+
# @option params [String] :platform optional
|
130
|
+
#
|
131
|
+
# @raise [Scoreoid::APIError] if the player could not be updated
|
132
|
+
#
|
133
|
+
# @return [Hash] The API response from Scoreoid (should contain a success message)
|
134
|
+
def self.edit(params={})
|
135
|
+
Scoreoid::API.query_and_parse('editPlayer', params)
|
136
|
+
end
|
137
|
+
|
138
|
+
# Updates a players field.
|
139
|
+
#
|
140
|
+
# @example Update John's e-mail address
|
141
|
+
# Scoreoid::Player.update_field(username: 'john', field: 'email', value: 'john@example.com')
|
142
|
+
#
|
143
|
+
# @param [Hash] params Parameters to include in the API request.
|
144
|
+
# Default parameters set with {Scoreoid.configure} will be included for you.
|
145
|
+
#
|
146
|
+
# @option params [String] :api_key Your Scoreoid API key
|
147
|
+
# @option params [String] :game_id The game ID
|
148
|
+
# @option params [String] :username The player's username (required)
|
149
|
+
# @option params [String] :field The field name to update (see {.edit})
|
150
|
+
# @option params [String] :value The value to set
|
151
|
+
#
|
152
|
+
# @raise [Scoreoid::APIError] if the field could not be updated
|
153
|
+
#
|
154
|
+
# @return [Hash] The API response from Scoreoid (should contain a success message)
|
155
|
+
def self.update_field(params={})
|
156
|
+
Scoreoid::API.query_and_parse('updatePlayerField', params)
|
13
157
|
end
|
14
158
|
end
|
15
159
|
end
|
data/lib/scoreoid/version.rb
CHANGED
data/lib/scoreoid.rb
CHANGED
@@ -1,20 +1,28 @@
|
|
1
1
|
require 'scoreoid/version'
|
2
|
-
require 'scoreoid/
|
2
|
+
require 'scoreoid/api'
|
3
3
|
require 'scoreoid/player'
|
4
4
|
|
5
|
+
# The main Scoreoid Ruby namespace.
|
6
|
+
#
|
7
|
+
# To get started, set your Scoreoid API key and game ID with {Scoreoid.configure}.
|
8
|
+
# Then you can use {Scoreoid::API.query_and_parse} to query any Scoreoid API method.
|
9
|
+
# See the {http://wiki.scoreoid.net/category/api/ Scoreoid Wiki} for information
|
10
|
+
# on available API methods.
|
5
11
|
module Scoreoid
|
6
|
-
#
|
7
|
-
#
|
8
|
-
|
9
|
-
|
10
|
-
#
|
11
|
-
#
|
12
|
+
# Configure Scoreoid Ruby by setting default API request parameters.
|
13
|
+
# You can set any parameters here, but it is only recommended to set
|
14
|
+
# :api_key and :game_id as they are common to all API methods.
|
15
|
+
#
|
16
|
+
# @example Setting your API key and game ID from environment variables:
|
17
|
+
# Scoreoid.configure(api_key: ENV['SCOREOID_API_KEY'], game_id: ENV['SCOREOID_GAME_ID'])
|
18
|
+
#
|
19
|
+
# @param [Hash] params A hash of default parameters to set
|
12
20
|
#
|
13
|
-
# @
|
14
|
-
#
|
21
|
+
# @option params [String] :api_key Your Scoreoid API key
|
22
|
+
# @option params [String] :game_id Your Scoreoid game ID
|
15
23
|
#
|
16
|
-
# @return [Hash]
|
17
|
-
def self.configure(
|
18
|
-
Scoreoid::
|
24
|
+
# @return [Hash] The parameters you just set
|
25
|
+
def self.configure(params)
|
26
|
+
Scoreoid::API.default_params = params
|
19
27
|
end
|
20
28
|
end
|
data/scoreoid.gemspec
CHANGED
@@ -9,8 +9,12 @@ Gem::Specification.new do |gem|
|
|
9
9
|
gem.author = 'Justin Workman'
|
10
10
|
gem.email = 'xtagon@gmail.com'
|
11
11
|
gem.summary = 'Scoreoid Ruby is a wrapper for the Scoreoid API.'
|
12
|
+
gem.license = 'MIT'
|
13
|
+
|
14
|
+
gem.metadata = { 'rubygems_mfa_required' => 'true' }
|
12
15
|
|
13
16
|
gem.required_ruby_version = '1.9.2'
|
17
|
+
gem.add_runtime_dependency 'chronic', '~> 0.8.0'
|
14
18
|
gem.add_runtime_dependency 'multi_json', '~> 1.3'
|
15
19
|
gem.add_runtime_dependency 'rest-client', '~> 1.6'
|
16
20
|
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Scoreoid::API do
|
4
|
+
before :each do
|
5
|
+
# Tests should be run independantly, so I don't understand why this is neccessary :/
|
6
|
+
Scoreoid.configure({})
|
7
|
+
end
|
8
|
+
|
9
|
+
describe '.query' do
|
10
|
+
it 'should query a Scoreoid API method and return the response as a string' do
|
11
|
+
example_response = %q({"players": 7})
|
12
|
+
given_params = {response: 'json', api_key: 'API_KEY', game_id: 'GAME_ID'}
|
13
|
+
|
14
|
+
RestClient.stub(:post).and_return(example_response)
|
15
|
+
RestClient.should_receive(:post).with('https://www.scoreoid.com/api/playerCount', given_params)
|
16
|
+
|
17
|
+
api_response = Scoreoid::API.query('playerCount', given_params)
|
18
|
+
api_response.should be_instance_of String
|
19
|
+
api_response.should == example_response
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should use default params if they are set' do
|
23
|
+
example_response = %q({"players": 7})
|
24
|
+
default_params = {api_key: 'API_KEY', game_id: 'GAME_ID'}
|
25
|
+
given_params = {start_date: '2009-01-01'}
|
26
|
+
expected_params = default_params.merge(given_params)
|
27
|
+
|
28
|
+
RestClient.stub(:post).and_return(example_response)
|
29
|
+
RestClient.should_receive(:post).with('https://www.scoreoid.com/api/playerCount', expected_params)
|
30
|
+
|
31
|
+
Scoreoid.configure(default_params)
|
32
|
+
Scoreoid::API.query('playerCount', given_params)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should format the parameters before sending' do
|
36
|
+
example_response = %q({"players": 7})
|
37
|
+
given_params = {start_date: Date.new(2010, 1, 1), end_date: 'december 3rd 2011'}
|
38
|
+
formatted_params = {start_date: '2010-01-01', end_date: '2011-12-03'}
|
39
|
+
|
40
|
+
RestClient.stub(:post).and_return(example_response)
|
41
|
+
RestClient.should_receive(:post).with('https://www.scoreoid.com/api/playerCount', formatted_params)
|
42
|
+
|
43
|
+
Scoreoid::API.query('playerCount', given_params)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '.query_and_parse' do
|
48
|
+
it 'should query a Scoreoid API method and parse the JSON response' do
|
49
|
+
given_params = {api_key: 'API_KEY', game_id: 'GAME_ID'}
|
50
|
+
params_plus_response_param = given_params.merge(response: 'json')
|
51
|
+
|
52
|
+
Scoreoid::API.stub(:query).and_return(%q({"players": 7}))
|
53
|
+
Scoreoid::API.should_receive(:query).with('playerCount', params_plus_response_param)
|
54
|
+
|
55
|
+
parsed_response = Scoreoid::API.query_and_parse('playerCount', given_params)
|
56
|
+
parsed_response.should be_instance_of Hash
|
57
|
+
parsed_response.should == {'players' => 7}
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'should raise an error if the Scoreoid API returned one in the JSON' do
|
61
|
+
response_with_error = %q({"error": "The API key is broken or the game is not active"})
|
62
|
+
|
63
|
+
Scoreoid::API.stub(:query).and_return(response_with_error)
|
64
|
+
|
65
|
+
expect do
|
66
|
+
Scoreoid::API.query_and_parse('getScores')
|
67
|
+
end.to raise_error(Scoreoid::APIError, 'The API key is broken or the game is not active')
|
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
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Scoreoid::Player do
|
4
|
+
describe '.create' do
|
5
|
+
it 'should return true on successfully creating a player' do
|
6
|
+
success_response = %q({"success":["The player has been created"]})
|
7
|
+
params = {username: 'AzureDiamond', password: 'hunter2'}
|
8
|
+
|
9
|
+
Scoreoid::API.stub(:query_and_parse).and_return(success_response)
|
10
|
+
Scoreoid::API.should_receive(:query_and_parse).with('createPlayer', params)
|
11
|
+
|
12
|
+
expect do
|
13
|
+
Scoreoid::Player.create(params).should be_true
|
14
|
+
end.to_not raise_error
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '.count' do
|
19
|
+
it 'should count players with no query parameters' do
|
20
|
+
Scoreoid::API.stub(:query_and_parse).and_return({'players' => 7})
|
21
|
+
Scoreoid::API.should_receive(:query_and_parse).with('countPlayers', {})
|
22
|
+
count = Scoreoid::Player.count
|
23
|
+
count.should == 7
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should count players with query parameters' do
|
27
|
+
params = {start_date: '2011-11-01', end_date: Time.now}
|
28
|
+
Scoreoid::API.stub(:query_and_parse).and_return({'players' => 7})
|
29
|
+
Scoreoid::API.should_receive(:query_and_parse).with('countPlayers', params)
|
30
|
+
count = Scoreoid::Player.count(params)
|
31
|
+
count.should == 7
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '.create' do
|
36
|
+
it 'should create the player and return the parsed API response' do
|
37
|
+
success_response = {'success' => ['The player has been created']}
|
38
|
+
params = {username: 'AzureDiamond', password: 'hunter2'}
|
39
|
+
|
40
|
+
Scoreoid::API.stub(:query_and_parse).and_return(success_response)
|
41
|
+
Scoreoid::API.should_receive(:query_and_parse).with('createPlayer', params)
|
42
|
+
|
43
|
+
response = Scoreoid::Player.create(params)
|
44
|
+
response.should == success_response
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '.edit' do
|
49
|
+
it 'should edit the player and return the parsed API response' do
|
50
|
+
success_response = {'success' => ['The player has been updated']}
|
51
|
+
params = {username: 'AzureDiamond', password: '*******'}
|
52
|
+
|
53
|
+
Scoreoid::API.stub(:query_and_parse).and_return(success_response)
|
54
|
+
Scoreoid::API.should_receive(:query_and_parse).with('editPlayer', params)
|
55
|
+
|
56
|
+
response = Scoreoid::Player.edit(params)
|
57
|
+
response.should == success_response
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe '.update_field' do
|
62
|
+
it 'should update the player field and return the parsed API response' do
|
63
|
+
success_response = {'success' => ['The player field last_name has been updated']}
|
64
|
+
params = {username: 'john', field: 'last_name', value: 'Doe'}
|
65
|
+
|
66
|
+
Scoreoid::API.stub(:query_and_parse).and_return(success_response)
|
67
|
+
Scoreoid::API.should_receive(:query_and_parse).with('updatePlayerField', params)
|
68
|
+
|
69
|
+
response = Scoreoid::Player.update_field(params)
|
70
|
+
response.should == success_response
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Scoreoid do
|
4
|
+
it 'has a valid version constant' do
|
5
|
+
# This regex is far from perfect, but it should keep me from accidentally
|
6
|
+
# doing something stupid to the version.
|
7
|
+
Scoreoid::VERSION.should =~ /\A\d+\.\d+\.\d+(\.\w+)?\Z/
|
8
|
+
end
|
9
|
+
end
|
data/spec/scoreoid_spec.rb
CHANGED
@@ -1,14 +1,12 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Scoreoid do
|
4
|
-
describe '
|
5
|
-
it 'should set
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
default_params[:api_key].should == 'API_KEY'
|
11
|
-
default_params[:game_id].should == 'GAME_ID'
|
4
|
+
describe '.configure' do
|
5
|
+
it 'should set default API parameters' do
|
6
|
+
default_params = {api_key: 'API_KEY', game_id: 'GAME_ID'}
|
7
|
+
Scoreoid::API.should_receive(:default_params=).and_return(default_params)
|
8
|
+
returned_default_params = Scoreoid.configure(default_params)
|
9
|
+
returned_default_params.should == default_params
|
12
10
|
end
|
13
11
|
end
|
14
12
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,13 +2,13 @@ require 'scoreoid'
|
|
2
2
|
|
3
3
|
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
4
4
|
RSpec.configure do |config|
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
6
|
+
config.run_all_when_everything_filtered = true
|
7
|
+
config.filter_run :focus
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
# Run specs in random order to surface order dependencies. If you find an
|
10
|
+
# order dependency and want to debug it, you can fix the order by providing
|
11
|
+
# the seed, which is printed after each run.
|
12
|
+
# --seed 1234
|
13
|
+
config.order = 'random'
|
14
14
|
end
|
metadata
CHANGED
@@ -1,46 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scoreoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
5
|
-
prerelease:
|
4
|
+
version: 1.1.1.alpha
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Justin Workman
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2022-07-15 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: chronic
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.8.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.8.0
|
14
27
|
- !ruby/object:Gem::Dependency
|
15
28
|
name: multi_json
|
16
29
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
30
|
requirements:
|
19
|
-
- - ~>
|
31
|
+
- - "~>"
|
20
32
|
- !ruby/object:Gem::Version
|
21
33
|
version: '1.3'
|
22
34
|
type: :runtime
|
23
35
|
prerelease: false
|
24
36
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
37
|
requirements:
|
27
|
-
- - ~>
|
38
|
+
- - "~>"
|
28
39
|
- !ruby/object:Gem::Version
|
29
40
|
version: '1.3'
|
30
41
|
- !ruby/object:Gem::Dependency
|
31
42
|
name: rest-client
|
32
43
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
44
|
requirements:
|
35
|
-
- - ~>
|
45
|
+
- - "~>"
|
36
46
|
- !ruby/object:Gem::Version
|
37
47
|
version: '1.6'
|
38
48
|
type: :runtime
|
39
49
|
prerelease: false
|
40
50
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
51
|
requirements:
|
43
|
-
- - ~>
|
52
|
+
- - "~>"
|
44
53
|
- !ruby/object:Gem::Version
|
45
54
|
version: '1.6'
|
46
55
|
description:
|
@@ -49,51 +58,52 @@ executables: []
|
|
49
58
|
extensions: []
|
50
59
|
extra_rdoc_files: []
|
51
60
|
files:
|
52
|
-
- .gitignore
|
53
|
-
- .rspec
|
61
|
+
- ".gitignore"
|
62
|
+
- ".rspec"
|
63
|
+
- ".travis.yml"
|
64
|
+
- ".yardopts"
|
54
65
|
- CHANGES.md
|
55
66
|
- Gemfile
|
56
67
|
- LICENSE.txt
|
57
68
|
- README.md
|
58
69
|
- Rakefile
|
59
70
|
- lib/scoreoid.rb
|
60
|
-
- lib/scoreoid/
|
71
|
+
- lib/scoreoid/api.rb
|
61
72
|
- lib/scoreoid/player.rb
|
62
73
|
- lib/scoreoid/version.rb
|
63
74
|
- scoreoid.gemspec
|
64
|
-
- spec/
|
65
|
-
- spec/player_spec.rb
|
75
|
+
- spec/scoreoid/api_spec.rb
|
76
|
+
- spec/scoreoid/player_spec.rb
|
77
|
+
- spec/scoreoid/version_spec.rb
|
66
78
|
- spec/scoreoid_spec.rb
|
67
79
|
- spec/spec_helper.rb
|
68
|
-
- spec/version_spec.rb
|
69
80
|
homepage:
|
70
|
-
licenses:
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata:
|
84
|
+
rubygems_mfa_required: 'true'
|
71
85
|
post_install_message:
|
72
86
|
rdoc_options: []
|
73
87
|
require_paths:
|
74
88
|
- lib
|
75
89
|
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
-
none: false
|
77
90
|
requirements:
|
78
91
|
- - '='
|
79
92
|
- !ruby/object:Gem::Version
|
80
93
|
version: 1.9.2
|
81
94
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
-
none: false
|
83
95
|
requirements:
|
84
|
-
- -
|
96
|
+
- - ">"
|
85
97
|
- !ruby/object:Gem::Version
|
86
|
-
version:
|
98
|
+
version: 1.3.1
|
87
99
|
requirements: []
|
88
|
-
|
89
|
-
rubygems_version: 1.8.24
|
100
|
+
rubygems_version: 3.3.18
|
90
101
|
signing_key:
|
91
|
-
specification_version:
|
102
|
+
specification_version: 4
|
92
103
|
summary: Scoreoid Ruby is a wrapper for the Scoreoid API.
|
93
104
|
test_files:
|
94
|
-
- spec/
|
95
|
-
- spec/player_spec.rb
|
105
|
+
- spec/scoreoid/api_spec.rb
|
106
|
+
- spec/scoreoid/player_spec.rb
|
107
|
+
- spec/scoreoid/version_spec.rb
|
96
108
|
- spec/scoreoid_spec.rb
|
97
109
|
- spec/spec_helper.rb
|
98
|
-
- spec/version_spec.rb
|
99
|
-
has_rdoc:
|
data/lib/scoreoid/api_client.rb
DELETED
@@ -1,61 +0,0 @@
|
|
1
|
-
require 'multi_json'
|
2
|
-
require 'rest_client'
|
3
|
-
|
4
|
-
module Scoreoid
|
5
|
-
# This exception is raised when the Scoreoid API returns an error response.
|
6
|
-
class APIError < StandardError; end
|
7
|
-
|
8
|
-
class APIClient
|
9
|
-
REQUIRED_PARAMETERS = [:api_key, :game_id]
|
10
|
-
|
11
|
-
@@default_params = Hash.new
|
12
|
-
|
13
|
-
# Set the default request parameters.
|
14
|
-
# Normally you would not call this directly, but instead use {Scoreoid.configure}.
|
15
|
-
#
|
16
|
-
# @param [Hash] params A hash of the default parameters to set.
|
17
|
-
#
|
18
|
-
# @return [Hash] Default request parameters.
|
19
|
-
def self.default_params= params
|
20
|
-
@@default_params = params
|
21
|
-
end
|
22
|
-
|
23
|
-
# Get the default request parameters.
|
24
|
-
#
|
25
|
-
# @raise [Scoreoid::NotConfiguredError] if required parameters are not set.
|
26
|
-
#
|
27
|
-
# @return Default request paremeters.
|
28
|
-
def self.default_params
|
29
|
-
REQUIRED_PARAMETERS.each do |param_key|
|
30
|
-
unless @@default_params.key?(param_key)
|
31
|
-
raise Scoreoid::NotConfiguredError, "Scoreoid Ruby has not been configured correctly. Please set the API key and game ID with Scoreoid.configure(api_key, game_id) before using this library."
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
@@default_params
|
36
|
-
end
|
37
|
-
|
38
|
-
# Query the Scoreoid API and return the un-parsed response.
|
39
|
-
# This is used internally and you should only call it if you really know what you're doing.
|
40
|
-
def self.api_call(api_method)
|
41
|
-
# Add :response => 'json' to the post parameters because the entire library
|
42
|
-
# epects JSON responses.
|
43
|
-
post_params = self.default_params.merge({response: 'json'})
|
44
|
-
RestClient.post("https://www.scoreoid.com/api/#{api_method}", post_params)
|
45
|
-
end
|
46
|
-
|
47
|
-
# Query the Scoreoid API method "countPlayers()" and parse the response.
|
48
|
-
#
|
49
|
-
# @see Scoreoid::Player.count
|
50
|
-
#
|
51
|
-
# @raise [Scoreoid::APIError] if the Scoreoid API returns an error response.
|
52
|
-
#
|
53
|
-
# @return [Hash] The Scoreoid API response parsed into a Hash.
|
54
|
-
def self.countPlayers
|
55
|
-
api_response = self.api_call('countPlayers')
|
56
|
-
json = MultiJson.load(api_response)
|
57
|
-
raise APIError, json['error'] if json.key? 'error'
|
58
|
-
json
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
data/spec/api_client_spec.rb
DELETED
@@ -1,80 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Scoreoid::APIClient do
|
4
|
-
describe 'self.default_params' do
|
5
|
-
it 'should be set to empty hash by default' do
|
6
|
-
Scoreoid::APIClient.class_variable_get(:@@default_params).should be_instance_of Hash
|
7
|
-
end
|
8
|
-
|
9
|
-
context 'setting default parameters' do
|
10
|
-
it 'should set default parameters when required parameters are provided' do
|
11
|
-
example_params = {api_key: 'API_KEY', game_id: 'GAME_ID'}
|
12
|
-
Scoreoid::APIClient.default_params = example_params
|
13
|
-
Scoreoid::APIClient.class_variable_get(:@@default_params).should == example_params
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
context 'getting default parameters' do
|
18
|
-
it 'returns exactly the parameters which were set' do
|
19
|
-
example_params = {api_key: 'API_KEY', game_id: 'GAME_ID'}
|
20
|
-
Scoreoid::APIClient.default_params = example_params
|
21
|
-
Scoreoid::APIClient.default_params.should == example_params
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'raises an error if required parameters are absent' do
|
25
|
-
Scoreoid::APIClient.default_params = {api_key: 'API_KEY'} # missing :game_id
|
26
|
-
expect do
|
27
|
-
Scoreoid::APIClient.default_params
|
28
|
-
end.to raise_error Scoreoid::NotConfiguredError
|
29
|
-
|
30
|
-
Scoreoid::APIClient.default_params = {game_key: 'GAME_ID'} # missing :api_key
|
31
|
-
expect do
|
32
|
-
Scoreoid::APIClient.default_params
|
33
|
-
end.to raise_error Scoreoid::NotConfiguredError
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
describe 'self.api_call' do
|
39
|
-
it 'should call Scoreoid API methods with default parameters' do
|
40
|
-
# We're testing that :response => 'json' is added to the parameters
|
41
|
-
# because the entire library expects JSON responses.
|
42
|
-
|
43
|
-
example_response = %q({"players": 7})
|
44
|
-
default_params = {api_key: 'API_KEY', game_id: 'GAME_ID'}
|
45
|
-
post_params = default_params.merge({response: 'json'})
|
46
|
-
|
47
|
-
Scoreoid::APIClient.stub(:default_params).and_return(default_params)
|
48
|
-
RestClient.stub(:post).and_return(example_response)
|
49
|
-
RestClient.should_receive(:post).with('https://www.scoreoid.com/api/playerCount', post_params)
|
50
|
-
|
51
|
-
api_response = Scoreoid::APIClient.api_call('playerCount')
|
52
|
-
api_response.should be_instance_of String
|
53
|
-
api_response.should == example_response
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
describe 'self.countPlayers' do
|
58
|
-
it 'should call the countPlayers() Scoreoid API method' do
|
59
|
-
Scoreoid::APIClient.stub(:api_call).and_return(%q({"players":7}))
|
60
|
-
Scoreoid::APIClient.should_receive(:api_call).with('countPlayers')
|
61
|
-
Scoreoid::APIClient.countPlayers
|
62
|
-
end
|
63
|
-
|
64
|
-
it 'should parse the JSON result into a Hash' do
|
65
|
-
Scoreoid::APIClient.stub(:api_call).and_return(%q({"players":7}))
|
66
|
-
result = Scoreoid::APIClient.countPlayers
|
67
|
-
result.should be_instance_of Hash
|
68
|
-
result.should == {'players' => 7}
|
69
|
-
end
|
70
|
-
|
71
|
-
it 'should raise an error if the Scoreoid API returns an error' do
|
72
|
-
example_response = %q({"error": "The API key is broken or the game is not active"})
|
73
|
-
Scoreoid::APIClient.stub(:api_call).and_return(example_response)
|
74
|
-
|
75
|
-
expect do
|
76
|
-
Scoreoid::APIClient.countPlayers
|
77
|
-
end.to raise_error(Scoreoid::APIError, 'The API key is broken or the game is not active')
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
data/spec/player_spec.rb
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Scoreoid::Player do
|
4
|
-
it 'should count players' do
|
5
|
-
Scoreoid::APIClient.should_receive(:countPlayers).and_return({'players' => 7})
|
6
|
-
how_many_players = Scoreoid::Player.count
|
7
|
-
how_many_players.should be_kind_of Integer
|
8
|
-
how_many_players.should >= 0
|
9
|
-
end
|
10
|
-
end
|