Last active
December 10, 2015 23:45
-
-
Save dthyresson/64afd4d742fa0c281597 to your computer and use it in GitHub Desktop.
Example of fetching Cam New Football TD Feats from the Stattleship API in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/ruby | |
require 'awesome_print' | |
require 'csv' | |
require 'dotenv' | |
require 'httparty' | |
require 'hashie' | |
require 'highline' | |
require 'link_header' | |
Dotenv.load | |
cli = HighLine.new | |
sport = cli.ask("What sport? ") { |q| q.default = 'football' } | |
league_abbreviation = cli.ask("What league? ") { |q| q.default = 'nfl' } | |
feat = cli.ask("What feat? ") { |q| q.default = 'passes_touchdowns' } | |
player_id = cli.ask("What player? ") { |q| q.default = 'nfl-cam-newton' } | |
endpoint = 'feats' | |
url = "https://www.stattleship.com/#{sport.downcase}/#{league_abbreviation.downcase}/#{endpoint.downcase}" | |
headers = { | |
'Accept' => 'application/vnd.stattleship.com; version=1', | |
'Content-Type' => 'application/json', | |
'Authorization' => "Token token=#{ENV.fetch('STATTLESHIP_ACCESS_TOKEN')}", | |
'User-Agent' => "Stattleship Ruby/1.0 (#{RUBY_PLATFORM})" | |
} | |
query = { | |
player_id: player_id, | |
feat: feat, | |
} | |
response = HTTParty.get(url, | |
query: query, | |
headers: headers) | |
models = [] | |
while(response.code == 200 && url) do | |
url = nil | |
result = Hashie::Mash.new(response) | |
players = result.players || [] | |
games = result.games || [] | |
seasons = result.seasons || [] | |
leagues = result.leagues || [] | |
teams = result.teams || [] | |
home_teams = result.home_teams || [] | |
away_teams = result.away_teams || [] | |
items = result.send(endpoint) | |
items.each do |item| | |
player = players.detect {|p| p.id == (item.player_id || item.subject_id)} || Hashie::Mash.new | |
team = teams.detect {|t| t.id == player.team_id} || Hashie::Mash.new | |
player['team'] = team || Hashie::Mash.new | |
item['player'] = player | |
game = games.detect {|g| g.id == item.game_id} | |
item['game'] = game || Hashie::Mash.new | |
season = seasons.detect {|s| s.id == game.season_id} | |
league = leagues.detect {|l| l.id == season.league_id} | |
item['league'] = league || Hashie::Mash.new | |
item['season'] = season || Hashie::Mash.new | |
models << item | |
end | |
LinkHeader.parse(response.headers['Link']).links.map do |link| | |
if link.attrs['rel'] == 'next' | |
url = link.href | |
end | |
end | |
if url | |
response = HTTParty.get(url, headers: headers) | |
unless response.code == 200 | |
url = nil | |
end | |
end | |
end | |
models.each do |model| | |
puts "#{model.player.last_name} #{model.accomplishment} #{model.vs}" | |
end | |
csv_string = CSV.generate do |csv| | |
models.each do |model| | |
csv << [model.player.last_name, | |
model.actual, | |
model.name, | |
model.level, | |
model.accomplishment, | |
model.subject_name, | |
model.vs] | |
end | |
end | |
puts csv_string |
Added same example, but with CSV output (some extra fields).
Newton,1.0,passes_touchdowns,common,threw 1 touchdown,Cam Newton,vs. the Eagles
Newton,1.0,passes_touchdowns,common,threw 1 touchdown,Cam Newton,vs. the Patriots
Newton,1.0,passes_touchdowns,common,threw 1 touchdown,Cam Newton,vs. the Bills
Newton,1.0,passes_touchdowns,common,threw 1 touchdown,Cam Newton,vs. the Jaguars
Newton,2.0,passes_touchdowns,common,threw 2 touchdowns,Cam Newton,vs. the Colts
Newton,2.0,passes_touchdowns,common,threw 2 touchdowns,Cam Newton,vs. the Texans
Newton,2.0,passes_touchdowns,common,threw 2 touchdowns,Cam Newton,vs. the Saints
Newton,2.0,passes_touchdowns,common,threw 2 touchdowns,Cam Newton,vs. the Buccaneers
Newton,1.0,passes_touchdowns,common,threw 1 touchdown,Cam Newton,vs. the Seahawks
Newton,1.0,passes_touchdowns,common,threw 1 touchdown,Cam Newton,vs. the Titans
Newton,3.0,passes_touchdowns,often,threw 3 touchdowns,Cam Newton,vs. the Packers
Newton,5.0,passes_touchdowns,rare,threw 5 touchdowns,Cam Newton,vs. the Saints
Newton,5.0,passes_touchdowns,rare,threw 5 touchdowns,Cam Newton,vs. the Redskins
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Returns