sports_database 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +69 -0
- data/Rakefile +8 -0
- data/lib/sports_database.rb +12 -0
- data/lib/sports_database/api.rb +5 -0
- data/lib/sports_database/api/query.rb +30 -0
- data/lib/sports_database/client.rb +14 -0
- data/lib/sports_database/helpers.rb +9 -0
- data/lib/sports_database/helpers/connection.rb +30 -0
- data/lib/sports_database/helpers/data_set.rb +46 -0
- data/lib/sports_database/helpers/parse_jsonp.rb +13 -0
- data/lib/sports_database/helpers/result.rb +73 -0
- data/lib/sports_database/version.rb +3 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/sports_database/api/query_spec.rb +28 -0
- data/spec/sports_database/client_spec.rb +106 -0
- data/spec/sports_database/helpers/data_set_spec.rb +71 -0
- data/spec/sports_database/helpers/result_spec.rb +78 -0
- data/sports_database.gemspec +31 -0
- metadata +184 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bcd4a246afbe87fa84b671b23b6125cb5bb6b86b
|
4
|
+
data.tar.gz: 58cb89873bae55d5aa16ed07f6cf0a80db70b5f2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2dafea3cf4bb24d49bcf7297fb331dcb3e845d095be8545ff9835ad2d7c74b40e99ec7600ed60cb0acbcda5dec89fedd673b07662eacd9f2dd814fe2cf5cf44f
|
7
|
+
data.tar.gz: 68e882a1b283e6d9dc54dbac4f32f89f25b8fd9f6a3394b72c260b5b8ea0c796818f78dbc334a5240150e6348304faaed9960cba283675f415d4e2c839f1f5da
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 dtkocher
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# SportsDatabase
|
2
|
+
|
3
|
+
A ruby client to help with interacting to http://www.sportsdatabase.com/api. Currently tested and working on Ruby 2.2.1.
|
4
|
+
|
5
|
+
JSONP is returned from the API and this gem will handle the calls made to the API as well as parsing the JSONP returned.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'sports_database'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install sports_database
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Example call:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'sports_database'
|
29
|
+
|
30
|
+
# By default if no params are passed api_key will be set to quest and sport to ncaabb
|
31
|
+
client = SportsDatabase::Client.new({ api_key: "guest", sport: "ncaabb" })
|
32
|
+
|
33
|
+
# the following will perform the same query
|
34
|
+
client.query("full name, school name, team @ season=2014 and team=PURD")
|
35
|
+
client.query("full name, school name, team", "season=2014 and team=PURD")
|
36
|
+
|
37
|
+
# Result class example
|
38
|
+
result = client.query("date,points,o:points@team=Bears and season=2011 and site")
|
39
|
+
result.success? # => true
|
40
|
+
result.fail? # => false
|
41
|
+
result.status_code # => 200
|
42
|
+
result.data # => returns the array of hashes
|
43
|
+
# [{:group=>"team = Bears and season = 2011 and site = home",
|
44
|
+
# :data_set=>[{:date=>20110911, :points=>30, :o_points=>12},
|
45
|
+
# {:date=>20110925, :points=>17, :o_points=>27},
|
46
|
+
# {:date=>20111002, :points=>34, :o_points=>29},
|
47
|
+
# {:date=>20111016, :points=>39, :o_points=>10},
|
48
|
+
# {:date=>20111113, :points=>37, :o_points=>13},
|
49
|
+
# {:date=>20111120, :points=>31, :o_points=>20},
|
50
|
+
# {:date=>20111204, :points=>3, :o_points=>10},
|
51
|
+
# {:date=>20111218, :points=>14, :o_points=>38}]},
|
52
|
+
# {:group=>"team = Bears and season = 2011 and site = away",
|
53
|
+
# :data_set=>[{:date=>20110918, :points=>13, :o_points=>30},
|
54
|
+
# {:date=>20111010, :points=>13, :o_points=>24},
|
55
|
+
# {:date=>20111023, :points=>24, :o_points=>18},
|
56
|
+
# {:date=>20111107, :points=>30, :o_points=>24},
|
57
|
+
# {:date=>20111127, :points=>20, :o_points=>25},
|
58
|
+
# {:date=>20111211, :points=>10, :o_points=>13},
|
59
|
+
# {:date=>20111225, :points=>21, :o_points=>35},
|
60
|
+
# {:date=>20120101, :points=>17, :o_points=>13}]}]
|
61
|
+
```
|
62
|
+
|
63
|
+
## Contributing
|
64
|
+
|
65
|
+
1. Fork it ( https://github.com/[my-github-username]/sdb_api_client/fork )
|
66
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
67
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
68
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
69
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'faraday_middleware'
|
2
|
+
require 'faraday_middleware/response_middleware'
|
3
|
+
require 'typhoeus/adapters/faraday'
|
4
|
+
require 'typhoeus'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
module SportsDatabase
|
8
|
+
autoload :Client, 'sports_database/client'
|
9
|
+
autoload :Api, 'sports_database/api'
|
10
|
+
autoload :Helpers, 'sports_database/helpers'
|
11
|
+
autoload :VERSION, 'sports_database/version'
|
12
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module SportsDatabase
|
2
|
+
module Api
|
3
|
+
module Query
|
4
|
+
|
5
|
+
# Performs the desired query agains the Sports Database api
|
6
|
+
#
|
7
|
+
# @example:
|
8
|
+
# client.query("full name,team@season=2014 and team=PURD")
|
9
|
+
# or
|
10
|
+
# client.query("full name,team", "season=2014 and team=PURD")
|
11
|
+
#
|
12
|
+
# @param [String], the sdb query
|
13
|
+
# or
|
14
|
+
# @param [String], the select portion
|
15
|
+
# @param [String], the table and condition portion
|
16
|
+
#
|
17
|
+
# @return [Result]
|
18
|
+
def query(*query)
|
19
|
+
get(formatted_query(query))
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def formatted_query(query)
|
25
|
+
raise ArgumentError, "Must have 1 or 2 arguments" if query.nil? || query.size > 2
|
26
|
+
query.join("@")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module SportsDatabase
|
2
|
+
class Client
|
3
|
+
include Api::Query
|
4
|
+
include Helpers::Connection
|
5
|
+
|
6
|
+
attr_accessor :api_key, :sport
|
7
|
+
|
8
|
+
def initialize(opts={})
|
9
|
+
self.api_key = opts.fetch(:api_key, "guest")
|
10
|
+
self.sport = opts.fetch(:sport, "ncaabb")
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module SportsDatabase
|
2
|
+
module Helpers
|
3
|
+
autoload :ParseJsonp, 'sports_database/helpers/parse_jsonp'
|
4
|
+
autoload :Connection, 'sports_database/helpers/connection'
|
5
|
+
autoload :Result, 'sports_database/helpers/result'
|
6
|
+
autoload :DataSet, 'sports_database/helpers/data_set'
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module SportsDatabase
|
2
|
+
module Helpers
|
3
|
+
module Connection
|
4
|
+
|
5
|
+
attr_reader :result
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
def connection
|
10
|
+
@connection ||=
|
11
|
+
Faraday.new(url: "http://api.sportsdatabase.com/#{sport}/query.json") do |config|
|
12
|
+
config.request :url_encoded
|
13
|
+
config.adapter :typhoeus
|
14
|
+
config.use ParseJsonp
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def get(query)
|
19
|
+
response = connection.get do |request|
|
20
|
+
request.params[:sdql] = query
|
21
|
+
request.params[:output] = "json"
|
22
|
+
request.params[:api_key] = api_key
|
23
|
+
end
|
24
|
+
|
25
|
+
@result = Result.new response
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module SportsDatabase
|
2
|
+
module Helpers
|
3
|
+
class DataSet
|
4
|
+
|
5
|
+
class << self
|
6
|
+
attr_accessor :column_names
|
7
|
+
|
8
|
+
def column_names=(column_names)
|
9
|
+
raise ArgumentError, "Must pass in none empty array" if column_names.nil? || !column_names.is_a?(Array) || column_names.empty?
|
10
|
+
@column_names = column_names.map { |column_name| methodize(column_name) }
|
11
|
+
@column_names.each do |column_name|
|
12
|
+
self.send(:attr_accessor, column_name)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def create(column_names)
|
17
|
+
self.column_names = column_names
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
def methodize(string)
|
22
|
+
raise ArgumentError, "Must be a valid string" if string.nil? || string.empty?
|
23
|
+
string.strip.gsub(/[:, ]/,"_").to_sym
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def set(column_name, value)
|
28
|
+
send(DataSet.methodize("#{column_name}="), value)
|
29
|
+
end
|
30
|
+
|
31
|
+
def get(column_name)
|
32
|
+
send(DataSet.methodize(column_name))
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_hash
|
36
|
+
hash = Hash.new
|
37
|
+
self.class.column_names.each do |column_name|
|
38
|
+
hash[:"#{column_name}"] = send(column_name)
|
39
|
+
end
|
40
|
+
|
41
|
+
hash
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module SportsDatabase
|
2
|
+
module Helpers
|
3
|
+
class Result
|
4
|
+
|
5
|
+
attr_reader :raw_response, :data
|
6
|
+
|
7
|
+
def initialize(response)
|
8
|
+
@raw_response = response
|
9
|
+
@data = []
|
10
|
+
generate_data(response.body)
|
11
|
+
end
|
12
|
+
|
13
|
+
def status_code
|
14
|
+
raw_response.status
|
15
|
+
end
|
16
|
+
|
17
|
+
def success?
|
18
|
+
raw_response.success?
|
19
|
+
end
|
20
|
+
|
21
|
+
def fail?
|
22
|
+
!success?
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
# Parases through the response body and creates nicer ruby loved hash
|
28
|
+
#
|
29
|
+
# @param [Hash], the sdb query
|
30
|
+
# {"headers"=>["team", "o:team"],
|
31
|
+
# "groups"=>[{"sdql"=>"season = 2014 and team = PURD",
|
32
|
+
# "columns"=>[["PURD", "PURD", "PURD", "PURD"],
|
33
|
+
# ["SAM", "INDPU", "GRAMB", "KANST"]]}]}
|
34
|
+
# or
|
35
|
+
# {"headers"=>['date', 'points', 'o:points'],
|
36
|
+
# "groups"=>[{ "sdql"=>"team = Bears and season = 2011 and site = home" ,
|
37
|
+
# "columns"=>[ [20110911,20110925,20111002,20111016,20111113,20111120,20111204,20111218],
|
38
|
+
# [30,17,34,39,37,31,3,14], [12,27,29,10,13,20,10,38] ]},
|
39
|
+
# { "sdql"=>"team = Bears and season = 2011 and site = away" ,
|
40
|
+
# "columns"=>[ [20110918,20111010,20111023,20111107,20111127,20111211,20111225,20120101],
|
41
|
+
# [13,13,24,30,20,10,21,17],
|
42
|
+
# [30,24,18,24,25,13,35,13] ]} ] }
|
43
|
+
#
|
44
|
+
# @return []
|
45
|
+
def generate_data(body)
|
46
|
+
return true unless success? && !body.nil? && !body.empty?
|
47
|
+
|
48
|
+
klass = DataSet.create(body["headers"])
|
49
|
+
body["groups"].each do |group|
|
50
|
+
@data << { group: group["sdql"], data_set: format_data_set(klass, group["columns"], body["headers"]) }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def format_data_set(klass, columns, column_names)
|
55
|
+
data_sets = []
|
56
|
+
|
57
|
+
# loop through the first column to get row num
|
58
|
+
# then loop through each column num
|
59
|
+
# then grab that columns row and set it in the data set object
|
60
|
+
# and create the data set
|
61
|
+
columns[0].each_with_index do |row, row_num|
|
62
|
+
data_set = klass.new
|
63
|
+
(0..(column_names.size-1)).each do |column_num|
|
64
|
+
data_set.set(column_names[column_num], columns[column_num][row_num])
|
65
|
+
end
|
66
|
+
data_sets << data_set.to_hash
|
67
|
+
end
|
68
|
+
|
69
|
+
data_sets
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SportsDatabase::Api::Query do
|
4
|
+
subject { Class.new { include SportsDatabase::Api::Query }.new }
|
5
|
+
|
6
|
+
describe "#formatted_query(query)" do
|
7
|
+
context "query = nil" do
|
8
|
+
let(:query) { nil }
|
9
|
+
it { expect{subject.send(:formatted_query, query)}.to raise_error(ArgumentError, "Must have 1 or 2 arguments") }
|
10
|
+
end
|
11
|
+
|
12
|
+
context "query = 'one', two, 'three' " do
|
13
|
+
let(:query) { ['one', 'two', 'three'] }
|
14
|
+
it { expect{subject.send(:formatted_query, query)}.to raise_error(ArgumentError, "Must have 1 or 2 arguments") }
|
15
|
+
end
|
16
|
+
|
17
|
+
context "query = 'team, full name @ team = PURD'" do
|
18
|
+
let(:query) { ['team, full name @ team = PURD'] }
|
19
|
+
it { expect(subject.send(:formatted_query, query)).to eq(query.first) }
|
20
|
+
end
|
21
|
+
|
22
|
+
context "query = 'team, full name', 'team = PURD'" do
|
23
|
+
let(:query) { ['team, full name', 'team = PURD'] }
|
24
|
+
it { expect(subject.send(:formatted_query, query)).to eq(query.join("@")) }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SportsDatabase do
|
4
|
+
it 'has a version number' do
|
5
|
+
expect(SportsDatabase::VERSION).not_to be nil
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "integration test" do
|
9
|
+
subject { SportsDatabase::Client.new(parameters) }
|
10
|
+
let(:parameters) { {} }
|
11
|
+
|
12
|
+
context "Client class" do
|
13
|
+
context "defaults" do
|
14
|
+
it { expect(subject.sport).to eq("ncaabb") }
|
15
|
+
it { expect(subject.api_key).to eq("guest") }
|
16
|
+
end
|
17
|
+
|
18
|
+
context "with params" do
|
19
|
+
let(:parameters) { {sport: "ncaafb", api_key: "test"} }
|
20
|
+
it { expect(subject.sport).to eq("ncaafb") }
|
21
|
+
it { expect(subject.api_key).to eq("test") }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "Query module" do
|
26
|
+
context "mock" do
|
27
|
+
let(:response) { Faraday::Response.new(status: code, body: body, response_headers: {'Content-Type' => 'application/json'}) }
|
28
|
+
let(:body) { {} }
|
29
|
+
let(:code) { 200 }
|
30
|
+
let(:result) { subject.query("team, o:team@season = 2014 and team = PURD") }
|
31
|
+
|
32
|
+
before { subject.stub(:get) { SportsDatabase::Helpers::Result.new response } }
|
33
|
+
|
34
|
+
it { expect(result.class).to eq(SportsDatabase::Helpers::Result) }
|
35
|
+
|
36
|
+
context "invalid return" do
|
37
|
+
let(:code) { 401 }
|
38
|
+
|
39
|
+
it { expect(result.success?).to be_falsey }
|
40
|
+
it { expect(result.fail?).to be_truthy }
|
41
|
+
it { expect(result.status_code).to eq(401) }
|
42
|
+
end
|
43
|
+
|
44
|
+
context "valid return" do
|
45
|
+
let(:code) { 200 }
|
46
|
+
|
47
|
+
context "no data" do
|
48
|
+
it { expect(result.success?).to be_truthy }
|
49
|
+
it { expect(result.status_code).to eq(200) }
|
50
|
+
it { expect(result.data).to eq([]) }
|
51
|
+
it { expect(result.raw_response).to eq(response) }
|
52
|
+
end
|
53
|
+
|
54
|
+
context "data" do
|
55
|
+
let(:body) do
|
56
|
+
{"headers"=>["team", "o:team"],
|
57
|
+
"groups"=>[{"sdql"=>"season = 2014 and team = PURD",
|
58
|
+
"columns"=>[["PURD", "PURD", "PURD", "PURD"],["SAM", "INDPU", "GRAMB", "KANST"]]}]}
|
59
|
+
end
|
60
|
+
|
61
|
+
it { expect(result.success?).to be_truthy }
|
62
|
+
it { expect(result.status_code).to eq(200) }
|
63
|
+
it { expect(result.raw_response).to eq(response) }
|
64
|
+
it do
|
65
|
+
expected = [{group: "season = 2014 and team = PURD",
|
66
|
+
data_set: [{:team=>"PURD", :o_team=>"SAM"},
|
67
|
+
{:team=>"PURD", :o_team=>"INDPU"},
|
68
|
+
{:team=>"PURD", :o_team=>"GRAMB"},
|
69
|
+
{:team=>"PURD", :o_team=>"KANST"}]}]
|
70
|
+
expect(result.data).to eq(expected)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
context "real" do
|
76
|
+
let(:parameters) { {sport: "nfl"} }
|
77
|
+
let(:result) { subject.query("date,points,o:points@team=Bears and season=2011 and site") }
|
78
|
+
|
79
|
+
it do
|
80
|
+
expect(result.status_code).to eq(200)
|
81
|
+
expect(result.success?).to be_truthy
|
82
|
+
|
83
|
+
expected = [{:group=>"team = Bears and season = 2011 and site = home",
|
84
|
+
:data_set=>[{:date=>20110911, :points=>30, :o_points=>12},
|
85
|
+
{:date=>20110925, :points=>17, :o_points=>27},
|
86
|
+
{:date=>20111002, :points=>34, :o_points=>29},
|
87
|
+
{:date=>20111016, :points=>39, :o_points=>10},
|
88
|
+
{:date=>20111113, :points=>37, :o_points=>13},
|
89
|
+
{:date=>20111120, :points=>31, :o_points=>20},
|
90
|
+
{:date=>20111204, :points=>3, :o_points=>10},
|
91
|
+
{:date=>20111218, :points=>14, :o_points=>38}]},
|
92
|
+
{:group=>"team = Bears and season = 2011 and site = away",
|
93
|
+
:data_set=>[{:date=>20110918, :points=>13, :o_points=>30},
|
94
|
+
{:date=>20111010, :points=>13, :o_points=>24},
|
95
|
+
{:date=>20111023, :points=>24, :o_points=>18},
|
96
|
+
{:date=>20111107, :points=>30, :o_points=>24},
|
97
|
+
{:date=>20111127, :points=>20, :o_points=>25},
|
98
|
+
{:date=>20111211, :points=>10, :o_points=>13},
|
99
|
+
{:date=>20111225, :points=>21, :o_points=>35},
|
100
|
+
{:date=>20120101, :points=>17, :o_points=>13}]}]
|
101
|
+
expect(result.data).to eq(expected)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SportsDatabase::Helpers::DataSet do
|
4
|
+
subject do
|
5
|
+
DataSet = SportsDatabase::Helpers::DataSet.create(column_names)
|
6
|
+
DataSet.new
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#create" do
|
10
|
+
subject { SportsDatabase::Helpers::DataSet.create(column_names) }
|
11
|
+
|
12
|
+
context "invalid" do
|
13
|
+
context "pass in nil" do
|
14
|
+
let(:column_names) { nil }
|
15
|
+
it { expect{subject}.to raise_error(ArgumentError, "Must pass in none empty array") }
|
16
|
+
end
|
17
|
+
|
18
|
+
context "pass in none array" do
|
19
|
+
let(:column_names) { "string" }
|
20
|
+
it { expect{subject}.to raise_error(ArgumentError, "Must pass in none empty array") }
|
21
|
+
end
|
22
|
+
|
23
|
+
context "pass in empty array" do
|
24
|
+
let(:column_names) { [] }
|
25
|
+
it { expect{subject}.to raise_error(ArgumentError, "Must pass in none empty array") }
|
26
|
+
end
|
27
|
+
|
28
|
+
context "pass in nil in the array" do
|
29
|
+
let(:column_names) { ["first", nil, "third"] }
|
30
|
+
it { expect{subject}.to raise_error(ArgumentError, "Must be a valid string") }
|
31
|
+
end
|
32
|
+
|
33
|
+
context "pass in empty string in the array" do
|
34
|
+
let(:column_names) { ["first", "", "third"] }
|
35
|
+
it { expect{subject}.to raise_error(ArgumentError, "Must be a valid string") }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "valid" do
|
40
|
+
let(:column_names) { ["first", "second", "third"] }
|
41
|
+
it { expect(subject.class).to eq(Class) }
|
42
|
+
it { expect(subject.column_names).to eq([:first, :second, :third]) }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "#get(column_name)" do
|
47
|
+
let(:column_names) { ["first", "second", "third"] }
|
48
|
+
before { subject.first = "1" }
|
49
|
+
|
50
|
+
it { expect(subject.first).to eq("1") }
|
51
|
+
it { expect(subject.get("first")).to eq(subject.first) }
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "#set(column_name, value)" do
|
55
|
+
let(:column_names) { ["first", "second", "third"] }
|
56
|
+
before { subject.set("first", 123) }
|
57
|
+
|
58
|
+
it { expect(subject.get("first")).to eq(123) }
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "#to_hash" do
|
62
|
+
let(:column_names) { ["first", "second", "third"] }
|
63
|
+
before do
|
64
|
+
subject.set("first", 123)
|
65
|
+
subject.set("second", 1122)
|
66
|
+
subject.set("third", 3)
|
67
|
+
end
|
68
|
+
|
69
|
+
it { expect(subject.to_hash).to eq({first: 123, second: 1122, third: 3}) }
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SportsDatabase::Helpers::Result do
|
4
|
+
|
5
|
+
describe "#intialize" do
|
6
|
+
subject { SportsDatabase::Helpers::Result.new(response) }
|
7
|
+
let(:response) { Faraday::Response.new(status: code, body: body, response_headers: {'Content-Type' => 'application/json'}) }
|
8
|
+
let(:body) { {} }
|
9
|
+
|
10
|
+
context "unsuccessful response" do
|
11
|
+
let(:code) { 401 }
|
12
|
+
|
13
|
+
it { expect(subject.success?).to be_falsey }
|
14
|
+
it { expect(subject.fail?).to be_truthy }
|
15
|
+
it { expect(subject.status_code).to eq(401) }
|
16
|
+
end
|
17
|
+
|
18
|
+
context "successful response" do
|
19
|
+
let(:code) { 200 }
|
20
|
+
it { expect(subject.success?).to be_truthy }
|
21
|
+
it { expect(subject.status_code).to eq(200) }
|
22
|
+
it { expect(subject.data).to eq([]) }
|
23
|
+
|
24
|
+
|
25
|
+
context "response body contains one group" do
|
26
|
+
let(:body) do
|
27
|
+
{"headers"=>["team", "o:team"],
|
28
|
+
"groups"=>[{"sdql"=>"season = 2014 and team = PURD",
|
29
|
+
"columns"=>[["PURD", "PURD", "PURD", "PURD"],["SAM", "INDPU", "GRAMB", "KANST"]]}]}
|
30
|
+
end
|
31
|
+
|
32
|
+
it do
|
33
|
+
expected = [{group: "season = 2014 and team = PURD",
|
34
|
+
data_set: [{:team=>"PURD", :o_team=>"SAM"},
|
35
|
+
{:team=>"PURD", :o_team=>"INDPU"},
|
36
|
+
{:team=>"PURD", :o_team=>"GRAMB"},
|
37
|
+
{:team=>"PURD", :o_team=>"KANST"}]}]
|
38
|
+
expect(subject.data).to eq(expected)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "response body conatins two groups" do
|
43
|
+
let(:body) do
|
44
|
+
{"headers"=>['date', 'points', 'o:points'],
|
45
|
+
"groups"=>[{ "sdql"=>"team = Bears and season = 2011 and site = home" ,
|
46
|
+
"columns"=>[ [20110911,20110925,20111002,20111016,20111113,20111120,20111204,20111218],
|
47
|
+
[30,17,34,39,37,31,3,14], [12,27,29,10,13,20,10,38] ]},
|
48
|
+
{ "sdql"=>"team = Bears and season = 2011 and site = away" ,
|
49
|
+
"columns"=>[ [20110918,20111010,20111023,20111107,20111127,20111211,20111225,20120101],
|
50
|
+
[13,13,24,30,20,10,21,17],
|
51
|
+
[30,24,18,24,25,13,35,13] ]} ] }
|
52
|
+
end
|
53
|
+
|
54
|
+
it do
|
55
|
+
expected = [{:group=>"team = Bears and season = 2011 and site = home",
|
56
|
+
:data_set=>[{:date=>20110911, :points=>30, :o_points=>12},
|
57
|
+
{:date=>20110925, :points=>17, :o_points=>27},
|
58
|
+
{:date=>20111002, :points=>34, :o_points=>29},
|
59
|
+
{:date=>20111016, :points=>39, :o_points=>10},
|
60
|
+
{:date=>20111113, :points=>37, :o_points=>13},
|
61
|
+
{:date=>20111120, :points=>31, :o_points=>20},
|
62
|
+
{:date=>20111204, :points=>3, :o_points=>10},
|
63
|
+
{:date=>20111218, :points=>14, :o_points=>38}]},
|
64
|
+
{:group=>"team = Bears and season = 2011 and site = away",
|
65
|
+
:data_set=>[{:date=>20110918, :points=>13, :o_points=>30},
|
66
|
+
{:date=>20111010, :points=>13, :o_points=>24},
|
67
|
+
{:date=>20111023, :points=>24, :o_points=>18},
|
68
|
+
{:date=>20111107, :points=>30, :o_points=>24},
|
69
|
+
{:date=>20111127, :points=>20, :o_points=>25},
|
70
|
+
{:date=>20111211, :points=>10, :o_points=>13},
|
71
|
+
{:date=>20111225, :points=>21, :o_points=>35},
|
72
|
+
{:date=>20120101, :points=>17, :o_points=>13}]}]
|
73
|
+
expect(subject.data).to eq(expected)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sports_database/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sports_database"
|
8
|
+
spec.version = SportsDatabase::VERSION
|
9
|
+
spec.authors = ["dtkocher"]
|
10
|
+
spec.email = ["dtkocher@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A ruby client to help with interacting to http://www.sportsdatabase.com/api.}
|
13
|
+
spec.description = %q{A ruby client to help with interacting to http://www.sportsdatabase.com/api.}
|
14
|
+
spec.homepage = "https://github.com/dtkocher/sports_database"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.8"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "byebug", "~> 3.5"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.2"
|
26
|
+
spec.add_development_dependency "simplecov", "~> 0.9"
|
27
|
+
spec.add_dependency "faraday", "~> 0.9.1"
|
28
|
+
spec.add_dependency "faraday_middleware", "~> 0.9.1"
|
29
|
+
spec.add_dependency "typhoeus", "~> 0.7.1"
|
30
|
+
end
|
31
|
+
|
metadata
ADDED
@@ -0,0 +1,184 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sports_database
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- dtkocher
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: byebug
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.5'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.2'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.2'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.9'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.9'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: faraday
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.9.1
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.9.1
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: faraday_middleware
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 0.9.1
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 0.9.1
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: typhoeus
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.7.1
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 0.7.1
|
125
|
+
description: A ruby client to help with interacting to http://www.sportsdatabase.com/api.
|
126
|
+
email:
|
127
|
+
- dtkocher@gmail.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- ".travis.yml"
|
135
|
+
- Gemfile
|
136
|
+
- LICENSE.txt
|
137
|
+
- README.md
|
138
|
+
- Rakefile
|
139
|
+
- lib/sports_database.rb
|
140
|
+
- lib/sports_database/api.rb
|
141
|
+
- lib/sports_database/api/query.rb
|
142
|
+
- lib/sports_database/client.rb
|
143
|
+
- lib/sports_database/helpers.rb
|
144
|
+
- lib/sports_database/helpers/connection.rb
|
145
|
+
- lib/sports_database/helpers/data_set.rb
|
146
|
+
- lib/sports_database/helpers/parse_jsonp.rb
|
147
|
+
- lib/sports_database/helpers/result.rb
|
148
|
+
- lib/sports_database/version.rb
|
149
|
+
- spec/spec_helper.rb
|
150
|
+
- spec/sports_database/api/query_spec.rb
|
151
|
+
- spec/sports_database/client_spec.rb
|
152
|
+
- spec/sports_database/helpers/data_set_spec.rb
|
153
|
+
- spec/sports_database/helpers/result_spec.rb
|
154
|
+
- sports_database.gemspec
|
155
|
+
homepage: https://github.com/dtkocher/sports_database
|
156
|
+
licenses:
|
157
|
+
- MIT
|
158
|
+
metadata: {}
|
159
|
+
post_install_message:
|
160
|
+
rdoc_options: []
|
161
|
+
require_paths:
|
162
|
+
- lib
|
163
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
164
|
+
requirements:
|
165
|
+
- - ">="
|
166
|
+
- !ruby/object:Gem::Version
|
167
|
+
version: '0'
|
168
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
169
|
+
requirements:
|
170
|
+
- - ">="
|
171
|
+
- !ruby/object:Gem::Version
|
172
|
+
version: '0'
|
173
|
+
requirements: []
|
174
|
+
rubyforge_project:
|
175
|
+
rubygems_version: 2.4.5
|
176
|
+
signing_key:
|
177
|
+
specification_version: 4
|
178
|
+
summary: A ruby client to help with interacting to http://www.sportsdatabase.com/api.
|
179
|
+
test_files:
|
180
|
+
- spec/spec_helper.rb
|
181
|
+
- spec/sports_database/api/query_spec.rb
|
182
|
+
- spec/sports_database/client_spec.rb
|
183
|
+
- spec/sports_database/helpers/data_set_spec.rb
|
184
|
+
- spec/sports_database/helpers/result_spec.rb
|