Skip to content

Instantly share code, notes, and snippets.

@ecavazos
Created July 27, 2012 21:07
Show Gist options
  • Save ecavazos/3190482 to your computer and use it in GitHub Desktop.
Save ecavazos/3190482 to your computer and use it in GitHub Desktop.
This is for Blaine
require 'open-uri'
require 'sinatra'
require 'nokogiri'
require 'yajl'
class MedalScraper
def initialize
@data = []
html = open('http://www.london2012.com/medals/')
doc = Nokogiri::HTML(html)
doc.css('.historicalMedals tbody tr').each do |tr|
@data << {
:country => cell(tr, 1),
:gold => cell(tr, 2),
:silver => cell(tr, 3),
:bronze => cell(tr, 4),
:total => cell(tr, 6) # the fifth cell is skipped because it is a hidden meta-cell
}
end
end
def json
Yajl.dump @data
end
private
def cell(tr, idx)
node = tr.css("td:nth-child(#{ idx })")
node.inner_html.gsub(/\s+/, '')
end
end
class App < Sinatra::Base
get '/' do
content_type :json
MedalScraper.new.json
end
end
require './app'
run App
source 'http://rubygems.org'
gem 'nokogiri'
gem 'shotgun'
gem 'sinatra'
gem 'yajl-ruby'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment