Skip to content

Instantly share code, notes, and snippets.

@JoePym
Created April 5, 2011 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoePym/904800 to your computer and use it in GitHub Desktop.
Save JoePym/904800 to your computer and use it in GitHub Desktop.
Using the FUMBBL API
require 'nokogiri'
require 'open-uri'
#First, we define what we want to do with the API. This path here grabs the list of all matches for a team.
# This is returned in lists of 26 results per page. To make this work for all teams, there's a little bit of complexity looping through pages
# It loops making page requests and appending items to the array until it finds a page which has no games
# If you want to use your own team, set the @team_id to the id you want to play with.
@team_id = 641993
base_path = "http://fumbbl.com/xml:matches?id=#{@team_id}"
page = 1
#Nokogiri is ruby's XML parsing library. "open" retrieves a url and outputs the document as a string
doc = Nokogiri::XML(open(base_path + "&p=#{page}"))
wins = []
draws = []
losses = []
#Nokogiri uses XPath as a language to step through the nodes. This is very simple to use and learn.
while doc.xpath('//match').size > 0
doc.xpath('//match').each do |match|
rating = match.xpath("home/rating/text()").to_s
my_tds = match.xpath("home/touchdowns/text()").to_s.to_i
their_tds = match.xpath("away/touchdowns/text()").to_s.to_i
wins << rating if my_tds > their_tds
draws << rating if my_tds == their_tds
losses << rating if my_tds < their_tds
end
page += 1
doc = Nokogiri::XML(open(base_path + "&p=#{page}"))
end
#Tada! A list of TV's in wins, draws and losses. It says "rating", but this appears to actually use the TV in this element for LRB 6 teams.
#Now I'm just going to do a quick bit of presenting to make it readable and useful...
# If you want to alter which brackets are used, change it here.
@tv_brackets = [120, 140, 160,180,200,220,240]
previous_bracket = 0
@tv_brackets.each do |tv|
total_wins = wins.select{|w| w.to_i < tv && w.to_i > previous_bracket}.count
total_draws = draws.select{|w| w.to_i < tv && w.to_i > previous_bracket}.count
total_losses = losses.select{|w| w.to_i < tv && w.to_i > previous_bracket}.count
total_games = total_wins + total_draws + total_losses
puts "---------------------"
puts "Between #{previous_bracket} and #{tv} TV"
if total_games == 0
puts "There were no games in this bracket"
next
end
puts "There were #{total_wins} wins out of #{total_games}"
puts "There were #{total_draws} draws out of #{total_games}"
puts "There were #{total_losses} losses out of #{total_games}"
puts "There was a #{'%.2f' % (total_wins*100.0/total_games)}% win rate at this TV"
previous_bracket = tv
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment