class ShiftStats
Attributes
configuration[RW]
Public Class Methods
configure() { |configuration| ... }
click to toggle source
# File lib/shift_stats.rb, line 18 def self.configure yield(configuration) end
new()
click to toggle source
# File lib/shift_stats.rb, line 22 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
reset()
click to toggle source
# File lib/shift_stats.rb, line 14 def self.reset @configuration = Configuration.new end
Public Instance Methods
division_games_list(division_id)
click to toggle source
# File lib/shift_stats.rb, line 122 def division_games_list(division_id) JSON.parse(@client.get(url("division/#{division_id}/games"), header: headers).body) end
division_leaders(division_id, type: 'Regular Season', limit: 20, metrics: [:points, :goals, :assists, :goals_against_average, :save_percentage, :wins, :shutouts, :number_first_stars, :number_stars])
click to toggle source
type is 'Regular Season', 'Playoffs', or 'Exhibition', required limit, required metrics, required
# File lib/shift_stats.rb, line 138 def division_leaders(division_id, type: 'Regular Season', limit: 20, metrics: [:points, :goals, :assists, :goals_against_average, :save_percentage, :wins, :shutouts, :number_first_stars, :number_stars]) JSON.parse(@client.get(url("division/#{division_id}/leaders"), query: {limit: limit, metrics: metrics.join(','), type: type}, header: headers).body) end
division_standings(division_id, type: 'Regular Season')
click to toggle source
type is 'Regular Season', 'Playoffs', or 'Exhibition', required
# File lib/shift_stats.rb, line 127 def division_standings(division_id, type: 'Regular Season') JSON.parse(@client.get(url("division/#{division_id}/standings"), query: {type: type}, header: headers).body) end
division_suspensions(division_id, only_active: true)
click to toggle source
# File lib/shift_stats.rb, line 142 def division_suspensions(division_id, only_active: true) query = {} if only_active query[:status] = 'active' end JSON.parse(@client.get(url("division/#{division_id}/suspensions"), query: query, header: headers).body) end
division_teams(division_id)
click to toggle source
# File lib/shift_stats.rb, line 131 def division_teams(division_id) JSON.parse(@client.get(url("division/#{division_id}/teams"), header: headers).body) end
game(game_id)
click to toggle source
# File lib/shift_stats.rb, line 98 def game(game_id) JSON.parse(@client.get(url("game/#{game_id}"), header: headers).body) end
game_goalies(game_id, only: nil)
click to toggle source
only is :home or :away, optional
# File lib/shift_stats.rb, line 108 def game_goalies(game_id, only: nil) game_subpath(game_id, 'goalies', only) end
game_goals(game_id, only: nil)
click to toggle source
only is :home or :away, optional
# File lib/shift_stats.rb, line 103 def game_goals(game_id, only: nil) game_subpath(game_id, 'goals', only) end
game_penalties(game_id, only: nil)
click to toggle source
only is :home or :away, optional
# File lib/shift_stats.rb, line 113 def game_penalties(game_id, only: nil) game_subpath(game_id, 'penalties', only) end
game_roster(game_id, only: nil)
click to toggle source
only is :home or :away, optional
# File lib/shift_stats.rb, line 118 def game_roster(game_id, only: nil) game_subpath(game_id, 'roster', only) end
league(league_id)
click to toggle source
# File lib/shift_stats.rb, line 46 def league(league_id) JSON.parse(@client.get(url("league/#{league_id}"), header: headers).body) end
league_seasons(league_id)
click to toggle source
# File lib/shift_stats.rb, line 50 def league_seasons(league_id) JSON.parse(@client.get(url("league/#{league_id}/seasons"), header: headers).body) end
league_suspensions(league_id, only_active: true)
click to toggle source
# File lib/shift_stats.rb, line 54 def league_suspensions(league_id, only_active: true) query = {} if only_active query[:status] = 'active' end JSON.parse(@client.get(url("league/#{league_id}/suspensions"), query: query, header: headers).body) end
leagues()
click to toggle source
# File lib/shift_stats.rb, line 42 def leagues() JSON.parse(@client.get(url('leagues'), header: headers).body) end
season(season_id)
click to toggle source
# File lib/shift_stats.rb, line 150 def season(season_id) JSON.parse(@client.get(url("season/#{season_id}"), header: headers).body) end
season_divisions_list(season_id)
click to toggle source
# File lib/shift_stats.rb, line 154 def season_divisions_list(season_id) JSON.parse(@client.get(url("season/#{season_id}/divisions"), header: headers).body) end
season_suspensions(season_id, only_active: true)
click to toggle source
# File lib/shift_stats.rb, line 158 def season_suspensions(season_id, only_active: true) query = {} if only_active query[:status] = 'active' end JSON.parse(@client.get(url("season/#{season_id}/suspensions"), query: query, header: headers).body) end
team_games(team_id, include_future: true, include_today: true)
click to toggle source
# File lib/shift_stats.rb, line 78 def team_games(team_id, include_future: true, include_today: true) JSON.parse(@client.get(url("team/#{team_id}/games"), query: {future: include_future, today: include_today}, header: headers).body) end
team_games_for_status(team_id, status: 'Final,In Progress,Forfeit')
click to toggle source
# File lib/shift_stats.rb, line 82 def team_games_for_status(team_id, status: 'Final,In Progress,Forfeit') JSON.parse(@client.get(url("team/#{team_id}/games"), query: {status: status}, header: headers).body) end
team_players_list(team_id)
click to toggle source
# File lib/shift_stats.rb, line 70 def team_players_list(team_id) JSON.parse(@client.get(url("team/#{team_id}/players"), query: {status: 'active'}, header: headers).body) end
team_practices(team_id, include_future: true, include_today: true)
click to toggle source
# File lib/shift_stats.rb, line 86 def team_practices(team_id, include_future: true, include_today: true) JSON.parse(@client.get(url("team/#{team_id}/practices"), query: {future: include_future, today: include_today}, header: headers).body) end
team_schedule(team_id)
click to toggle source
# File lib/shift_stats.rb, line 66 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
team_search(sport, name)
click to toggle source
# File lib/shift_stats.rb, line 62 def team_search(sport, name) JSON.parse(@client.get(url('teams'), query: {name: name, not_ended: true, sport: sport.downcase}, header: headers).body) end
team_suspensions(team_id, only_active: true)
click to toggle source
# File lib/shift_stats.rb, line 90 def team_suspensions(team_id, only_active: true) query = {} if only_active query[:status] = 'active' end JSON.parse(@client.get(url("team/#{team_id}/suspensions"), query: query, header: headers).body) end
teams_in_division(division_name, league_id, current_season: true)
click to toggle source
# File lib/shift_stats.rb, line 74 def teams_in_division(division_name, league_id, current_season: true) JSON.parse(@client.get(url('teams'), query: {division: division_name, league_id: league_id, not_ended: !!current_season}, header: headers).body) end
Private Instance Methods
basic_headers()
click to toggle source
# File lib/shift_stats.rb, line 181 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
game_subpath(game_id, path, only)
click to toggle source
# File lib/shift_stats.rb, line 171 def game_subpath(game_id, path, only) path = "home_#{path}" if only == :home path = "away_#{path}" if only == :away JSON.parse(@client.get(url("game/#{game_id}/#{path}"), header: headers).body) end
headers()
click to toggle source
# File lib/shift_stats.rb, line 192 def headers basic_headers.merge({ 'Authorization' => "StatsAuth ticket=\"#{@ticket_hash}\"" }) end
login_query()
click to toggle source
# File lib/shift_stats.rb, line 177 def login_query {key: self.class.configuration.api_key} end
url(sub)
click to toggle source
# File lib/shift_stats.rb, line 167 def url(sub) return "#{@url_base}#{sub}" end