Skip to content

Instantly share code, notes, and snippets.

@zmalltalker
Created October 23, 2008 13:14
Show Gist options
  • Save zmalltalker/19010 to your computer and use it in GitHub Desktop.
Save zmalltalker/19010 to your computer and use it in GitHub Desktop.
# A small script that collects details about matches in the Norwegian eliteserien and returns these as Ruby objects
# If you run this file itself, will return a list with details for the teams
require 'rubygems'
require 'open-uri'
require 'hpricot'
class Team
include Comparable
attr_accessor :team_name, :matches, :wins, :losses, :draws, :plus, :minus, :points
def initialize(team_name)
@team_name = team_name
end
def to_s
"#{team_name} has #{matches} matches, with #{wins} wins and #{points} points"
end
def <=>(another_team)
another_team.points.to_i <=> points.to_i
end
# Fetches the teams
def self.fetch_teams
data = open('http://live.dagbladet.no/liga.html?sex=m&sport=s&country=Norge&lid=17').read
fruit = Hpricot(data)
kamp_div = (fruit/"//div#gruppespill")[1]
teams = []
(kamp_div/"//tr").each do |tr|
lag_navn = (tr/"td.lag")[0]
if lag_navn
t = Team.new(lag_navn.inner_html)
(tr/"td").each_with_index do |td, i|
method_names = [:team_name=, :matches=, :wins=, :losses=, :draws=, :plus=, :minus=, :points=]
t.send(method_names[i], td.inner_html)
end
teams << t
end
end
return teams
end
end
if (__FILE__) == $0
Team.fetch_teams.sort.each {|t| puts t}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment