scoreoid 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.yardopts ADDED
@@ -0,0 +1,4 @@
1
+ -
2
+ CHANGES.md
3
+ LICENSE.txt
4
+ README.md
data/CHANGES.md CHANGED
@@ -1,6 +1,11 @@
1
1
  Scoreoid Ruby Change Log
2
2
  ========================
3
3
 
4
+ 2012-11-25 *v1.0.0*
5
+ -------------------
6
+
7
+ First stable release.
8
+
4
9
  2012-11-24 *v0.1.0*
5
10
  -------------------
6
11
 
data/README.md CHANGED
@@ -19,13 +19,30 @@ Or install it yourself as:
19
19
 
20
20
  $ gem install scoreoid
21
21
 
22
+ Usage
23
+ -----
24
+
25
+ To get started, configure Scoreoid Ruby with your API key and game ID:
26
+
27
+ require 'scoreoid'
28
+
29
+ Scoreoid.configure(api_key: 'YOUR_API_KEY', game_id: 'YOUR_GAME_ID')
30
+
31
+ Then you can start querying Scoreoid API methods:
32
+
33
+ new_players_count = Scoreoid::API.query('countPlayers', start_date: '2009-08-04')
34
+
35
+ Any Scoreoid API method may be called in this mannor. See the [Scoreoid Wiki](http://wiki.scoreoid.net/category/api/) for information on available API methods.
36
+
37
+ Future versions of Scoreoid Ruby will provide a more object-oriented mannor of querying data. See the `Scoreoid::Player` class for a basic example of this.
38
+
22
39
  Contributing
23
40
  ------------
24
41
 
25
42
  Contributions are most welcome!
26
43
 
27
44
  1. [Fork it on Bitbucket](https://bitbucket.org/xtagon/scoreoid-ruby-gem/fork)
28
- 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 2. Create your feature branch (`git checkout -b feature/my-new-feature`)
29
46
  3. Commit your changes (`git commit -am 'Add some feature'`)
30
47
  4. Push to the branch (`git push origin my-new-feature`)
31
48
  5. Create new Pull Request
@@ -0,0 +1,74 @@
1
+ require 'date'
2
+
3
+ require 'multi_json'
4
+ require 'rest_client'
5
+
6
+ module Scoreoid
7
+ # This exception is raised when the Scoreoid API returns an error response.
8
+ class APIError < StandardError; end
9
+
10
+ # A singleton class with methods for querying the Scoreoid API.
11
+ class API
12
+ class << self
13
+ # Query a given Scoreoid API method and return the repsonse as a string.
14
+ #
15
+ # @param [String] api_method The Scoreoid API method to query
16
+ # @param [Hash] params Parameters to include in the API request
17
+ #
18
+ # @return [String] The Scoreoid API response
19
+ #
20
+ # @see .query_and_parse
21
+ def query(api_method, params={})
22
+ params = self.prepare_params(params)
23
+ params.merge!(self.default_params ||= {})
24
+ RestClient.post("https://www.scoreoid.com/api/#{api_method}", params)
25
+ end
26
+
27
+ # Query a given Scoreoid API method and parse the JSON response.
28
+ #
29
+ # The response type is set to 'json' for you automatically.
30
+ #
31
+ # @param [String] api_method The Scoreoid API method to query
32
+ # @param [Hash] params Parameters to include in the API request.
33
+ #
34
+ # @raise [Scoreoid::APIError] if the Scoreoid API returns an error response.
35
+ #
36
+ # @return [Hash] The Scoreoid API response parsed into a Hash.
37
+ #
38
+ # @see .query
39
+ # @see .prepare_params
40
+ def query_and_parse(api_method, params={})
41
+ params = params.merge(response: 'json')
42
+
43
+ api_response = self.query(api_method, params)
44
+ parsed_result = MultiJson.load(api_response)
45
+
46
+ raise APIError, parsed_result['error'] if parsed_result.key? 'error'
47
+
48
+ parsed_result
49
+ end
50
+
51
+ # Attempt to coerce parameters into formats that the Scoreoid API expects.
52
+ #
53
+ # @param [Hash] params A hash of any parameters you wish to format.
54
+ #
55
+ # @option params [Date, Time, String] :start_date
56
+ # @option params [Date, Time, String] :end_date
57
+ #
58
+ # @return [Hash] The formatted parameters, ready to use in an API query.
59
+ def prepare_params(params)
60
+ params.each do |key, value|
61
+ if [:start_date, :end_date].include?(key) and value.respond_to?(:strftime)
62
+ params[key] = value.strftime '%Y-%m-%d'
63
+ end
64
+ end
65
+ params
66
+ end
67
+
68
+ # Default API request parameters used by {.query}
69
+ #
70
+ # This would normally be set with {Scoreoid.configure}
71
+ attr_accessor :default_params
72
+ end
73
+ end
74
+ end
@@ -1,15 +1,22 @@
1
- require 'scoreoid/api_client'
2
-
3
1
  module Scoreoid
2
+ # Represents the game players.
4
3
  class Player
5
- # @return [Integer] The total number of players for the game.
6
- #
7
- # @example
8
- # Scoreoid.configure 'YOUR_API_KEY', 'YOUR_GAME_ID'
9
- #
10
- # puts "This game has #{Scoreoid::Player.count} players!"
11
- def self.count
12
- Scoreoid::APIClient.countPlayers['players']
4
+ # Get the number of players for the 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 Optional criteria for counting players.
14
+ # @option params [Date, Time, String] :start_date Start date ("YYYY-MM-DD" if passed as String)
15
+ # @option params [Date, Time, String] :end_date End date ("YYYY-MM-DD" if passed as String)
16
+ #
17
+ # @return [Integer] The number of players.
18
+ def self.count(params={})
19
+ Scoreoid::API.query_and_parse('countPlayers', params)['players']
13
20
  end
14
21
  end
15
22
  end
@@ -1,3 +1,4 @@
1
1
  module Scoreoid
2
- VERSION = '0.1.0'
2
+ # The currently loaded Scoreoid Ruby version.
3
+ VERSION = '1.0.0'
3
4
  end
data/lib/scoreoid.rb CHANGED
@@ -1,20 +1,28 @@
1
1
  require 'scoreoid/version'
2
- require 'scoreoid/api_client'
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
- # This exception is raised when Scoreoid Ruby is used without first being configured.
7
- # To configure Scoreoid Ruby, see {Scoreoid.configure}.
8
- class NotConfiguredError < RuntimeError; end
9
-
10
- # Configure Scoreoid Ruby with your Scoreoid API key and game ID.
11
- # This must be done once before calling any Scoreoid API methods.
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
- # @example
14
- # Scoreoid.configure(ENV['SCOREOID_API_KEY'], ENV['SCOREOID_GAME_ID'])
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] A hash of the parameters you just configured.
17
- def self.configure(api_key, game_id)
18
- Scoreoid::APIClient.default_params = {api_key: api_key, game_id: game_id}
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,6 +9,7 @@ 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'
12
13
 
13
14
  gem.required_ruby_version = '1.9.2'
14
15
  gem.add_runtime_dependency 'multi_json', '~> 1.3'
data/spec/api_spec.rb ADDED
@@ -0,0 +1,70 @@
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: Date.new(2012, 2, 3).to_time}
38
+ formatted_params = {start_date: '2010-01-01', end_date: '2012-02-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
+ end
70
+ end
data/spec/player_spec.rb CHANGED
@@ -1,10 +1,20 @@
1
1
  require 'spec_helper'
2
2
 
3
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
4
+ describe '.count' do
5
+ it 'should count players with no query parameters' do
6
+ Scoreoid::API.stub(:query_and_parse).and_return({'players' => 7})
7
+ Scoreoid::API.should_receive(:query_and_parse).with('countPlayers', {})
8
+ count = Scoreoid::Player.count
9
+ count.should == 7
10
+ end
11
+
12
+ it 'should count players with query parameters' do
13
+ params = {start_date: '2011-11-01', end_date: Time.now}
14
+ Scoreoid::API.stub(:query_and_parse).and_return({'players' => 7})
15
+ Scoreoid::API.should_receive(:query_and_parse).with('countPlayers', params)
16
+ count = Scoreoid::Player.count(params)
17
+ count.should == 7
18
+ end
9
19
  end
10
20
  end
@@ -1,14 +1,12 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Scoreoid do
4
- describe 'self.configure' do
5
- it 'should set Scoreoid API key and game ID parameters' do
6
- Scoreoid.configure('API_KEY', 'GAME_ID')
7
- default_params = Scoreoid::APIClient.class_variable_get(:@@default_params)
8
- default_params.key?(:api_key).should be_true
9
- default_params.key?(:game_id).should be_true
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
- config.treat_symbols_as_metadata_keys_with_true_values = true
6
- config.run_all_when_everything_filtered = true
7
- config.filter_run :focus
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
- # 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'
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,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scoreoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -51,23 +51,25 @@ extra_rdoc_files: []
51
51
  files:
52
52
  - .gitignore
53
53
  - .rspec
54
+ - .yardopts
54
55
  - CHANGES.md
55
56
  - Gemfile
56
57
  - LICENSE.txt
57
58
  - README.md
58
59
  - Rakefile
59
60
  - lib/scoreoid.rb
60
- - lib/scoreoid/api_client.rb
61
+ - lib/scoreoid/api.rb
61
62
  - lib/scoreoid/player.rb
62
63
  - lib/scoreoid/version.rb
63
64
  - scoreoid.gemspec
64
- - spec/api_client_spec.rb
65
+ - spec/api_spec.rb
65
66
  - spec/player_spec.rb
66
67
  - spec/scoreoid_spec.rb
67
68
  - spec/spec_helper.rb
68
69
  - spec/version_spec.rb
69
70
  homepage:
70
- licenses: []
71
+ licenses:
72
+ - MIT
71
73
  post_install_message:
72
74
  rdoc_options: []
73
75
  require_paths:
@@ -91,7 +93,7 @@ signing_key:
91
93
  specification_version: 3
92
94
  summary: Scoreoid Ruby is a wrapper for the Scoreoid API.
93
95
  test_files:
94
- - spec/api_client_spec.rb
96
+ - spec/api_spec.rb
95
97
  - spec/player_spec.rb
96
98
  - spec/scoreoid_spec.rb
97
99
  - spec/spec_helper.rb
@@ -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
@@ -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