Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
parnic committed Apr 6, 2018
0 parents commit 384ffd5
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
*.gem
*.rbc
*.sassc
.bundle
.config
.yardoc
.rvmrc
.rspec
.sass-cache
.DS_Store
Gemfile.lock
InstalledFiles
_yardoc
doc/
coverage
lib/bundler/man
capybara-*.html
pkg
rdoc
spec/reports
spec/tmp
test/tmp
test/version_tmp
tmp
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source :rubygems

gemspec
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# ShiftStats gem
This gem pulls data from Digital Shift sites such as HockeyShift, SoccerShift, LacrosseShift, FootballShift, BasketballShift, and BaseballShift.
[![Gem Version](https://badge.fury.io/rb/shift_stats.svg)](http://badge.fury.io/rb/shift_stats)

## Features
* Get details about any team for any sport
* Get a specific team's game schedule
* Retrieve the list of players on a specific team
* Find all games for a specific division
* Find all divisions in a specific season

## Install
You can install shift_stats via rubygems: `gem install shift_stats`

## Usage

```ruby
require 'shift_stats'
```

Optionally configure your API key (note: by default uses the same API key as the Android HockeyShift app)

```ruby
ShiftStats.configure do |config|
config.api_key 'my_api_key'
end
```

Create a new instance of ShiftSats with `s = new ShiftStats`. This connects to the ShiftStats server, logs in, and stores the ticket hash. This ticket is only valid for a limited amount of time.

Creating an instance of the class can raise an error if there are network issues or the API key is invalid.

### Available methods

* `team_search(sport_name, team_name)` - Search for the given team in the given sport for all active seasons.
* `team_schedule(team_id)` - Retrieve the game schedule for the supplied team.
* `team_players_list(team_id)` - Get the list of players on a specific team.
* `division_games_list(division_id)` - Returns all games for a specified division.
* `season_divisions_list(season_id)` - Lists all divisions for the specified season.

## Bug reports, feature requests, contributions

Please create an issue or pull request on github. Assistance is most welcome.

There are many more endpoints available on Shift sites, but none are documented. Running a mobile app inside an emulator and watching wireshark, fiddler, etc. is how the current endpoints were discovered.
5 changes: 5 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task default: :spec
87 changes: 87 additions & 0 deletions lib/shift_stats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
require 'httpclient'
require 'json'
require 'shift_stats/configuration'

class ShiftStats
class << self
attr_accessor :configuration
end

def self.configuration
@configuration ||= Configuration.new
end

def self.reset
@configuration = Configuration.new
end

def self.configure
yield(configuration)
end

def initialize
@url_base = 'http://api.shiftstats.com/'
@ticket_hash = nil

@client = HTTPClient.new
@client.transparent_gzip_decompression = true

response = @client.get(url('login'), query: login_query, header: basic_headers)
response_json = JSON.parse(response.body) if response
if response_json && response_json['ticket'] && response_json['ticket']['hash']
@ticket_hash = response_json['ticket']['hash']
else
if response && response.body
raise response.body
else
raise "Unknown error. Make sure you have an internet connection and your API key is correct."
end
end
end

def team_search(sport, name)
JSON.parse(@client.get(url('teams'), query: {name: name, not_ended: true, sport: sport.downcase}, header: headers).body)
end

def team_schedule(team_id)
JSON.parse(@client.get(url("team/#{team_id}/games"), query: {future: true, today: true, past: true}, header: headers).body)
end

def team_players_list(team_id)
JSON.parse(@client.get(url("team/#{team_id}/players"), query: {status: 'active'}, header: headers).body)
end

def division_games_list(division_id)
JSON.parse(@client.get(url("division/#{division_id}/games"), header: headers).body)
end

def season_divisions_list(season_id)
JSON.parse(@client.get(url("season/#{season_id}/divisions"), header: headers).body)
end

private
def url(sub)
return "#{@url_base}#{sub}"
end

def login_query
{key: self.class.configuration.api_key}
end

def basic_headers
{
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'X-Requested-With' => 'com.digitalshift.hockeyshift',
'Accept-Language' => 'en-US',
'Accept-Encoding' => 'gzip,deflate',
'Connection' => 'keep-alive',
}
end

def headers
basic_headers.merge({
'Authorization' => "StatsAuth ticket=\"#{@ticket_hash}\""
})
end
end
9 changes: 9 additions & 0 deletions lib/shift_stats/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ShiftStats
class Configuration
attr_accessor :api_key

def initialize
@api_key = 'YXBpLnNoaWZ0c3RhdHMuY29tLDE5YjhhZGIwNDVjZjAxMzJhM2E5N2VmZDQ1YTRj'
end
end
end
15 changes: 15 additions & 0 deletions shift_stats.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Gem::Specification.new do |s|
s.name = 'shift_stats'
s.version = '0.0.1'
s.date = '2018-04-05'
s.summary = 'Shift stats API'
s.description = 'Interfaces with Digital Shift APIs (HockeyShift, BasketballShift, etc.)'
s.authors = ['Chris Pickett']
s.email = '[email protected]'
s.files = ['lib/shift_stats.rb', 'lib/shift_stats/configuration.rb']
s.homepage = 'https://github.com/parnic/shift_stats'
s.license = 'MIT'
s.add_runtime_dependency 'httpclient', '~> 2'
s.add_development_dependency 'rake', '~> 0'
s.add_development_dependency 'rspec', '~> 0'
end
62 changes: 62 additions & 0 deletions spec/shift_stats_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'spec_helper'

describe ShiftStats do
describe 'bad api key' do
it 'throws an error' do
ShiftStats.configure {|config| config.api_key = 'a'}
expect {ShiftStats.new}.to raise_error RuntimeError
end
end

describe 'with valid api key' do
s = ShiftStats.new
team_search = s.team_search('hockey', 'bears')

context 'team_search' do
it 'returns something' do
expect(team_search).to be_truthy
end

it 'returns some teams' do
expect(team_search).to include 'teams'
end

it 'finds the right team' do
bears = team_search['teams'].select{|team| team['id'] == 18827}.first
expect(bears).to be_truthy
end
end

context 'team_schedule' do
team_schedule = s.team_schedule(18827)

it 'returns something' do
expect(team_schedule).to be_truthy
end
end

context 'team_players_list' do
team_players = s.team_players_list(18827)

it 'returns something' do
expect(team_players).to be_truthy
end
end

context 'division_games_list' do
division_games = s.division_games_list(3057)

it 'returns something' do
expect(division_games).to be_truthy
end
end

context 'season_divisions_list' do
season_divisions = s.season_divisions_list(741)

it 'returns something' do
expect(season_divisions).to be_truthy
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'shift_stats'

0 comments on commit 384ffd5

Please sign in to comment.