Created
July 27, 2012 21:07
-
-
Save ecavazos/3190482 to your computer and use it in GitHub Desktop.
This is for Blaine
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
| 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 |
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
| require './app' | |
| run App |
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
| 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