Created
September 29, 2008 19:59
-
-
Save btakita/13661 to your computer and use it in GitHub Desktop.
This file contains 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
*.iml | |
*.ipr | |
*.iws |
This file contains 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 "rubygems" | |
require "thor" | |
module Gems | |
class Downloads < Thor | |
desc "list", "Print list of Gem downloads from rubyforge.org" | |
def list | |
require "rubygems" | |
require "open-uri" | |
require "hpricot" | |
print_gems_list | |
end | |
protected | |
def print_gems_list | |
rows = parse_rubyforge_gems_page | |
max_lengths = calculate_maximum_row_lengths(rows) | |
hr max_lengths | |
row rows[0], max_lengths | |
hr max_lengths | |
rows[1..-1].each do |row| | |
row(row, max_lengths) | |
end | |
hr max_lengths | |
end | |
def parse_rubyforge_gems_page | |
content = "" | |
open("http://gems.rubyforge.org/stats.html").each do |line| | |
content << line | |
end | |
doc = Hpricot(content) | |
rows = [] | |
doc.search("table/tr").each do |row| | |
cells = row.search("/") | |
rows << [cells[0].at("/").inner_html.to_s, cells[1].inner_html.to_s] | |
end | |
rows | |
end | |
def calculate_maximum_row_lengths(rows) | |
max_lengths = [0, 0] | |
rows.inject(max_lengths) do |max_lengths, row| | |
max_lengths[0] = row[0].length if row[0].length > max_lengths[0] | |
max_lengths[1] = row[1].length if row[1].length > max_lengths[1] | |
max_lengths | |
end | |
max_lengths | |
end | |
def hr(max_lengths) | |
out.puts '-' * (max_lengths[0] + max_lengths[1] + 7) | |
end | |
def row(row, max_lengths) | |
out.printf("| %#{max_lengths[0]}s | %#{max_lengths[1]}s |", row[0], row[1]) | |
out.puts | |
end | |
def out | |
$stdout | |
end | |
end | |
end |
This file contains 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 File.expand_path("#{File.dirname(__FILE__)}/gems_downloads_spec_helper") | |
module Gems | |
describe Downloads do | |
describe "#list" do | |
attr_reader :downloads, :out | |
before do | |
@downloads = Downloads.new | |
@out = out = StringIO.new("") | |
stub(downloads).out {out} | |
end | |
it "reformats the gem stat list from http://gems.rubyforge.org/stats.html into a console-friendly view" do | |
html = (<<-HTML).strip.gsub(/^ /, "") | |
<html><head><title>Gem download stats</title></head><body><h3>Generated Mon Sep 29 04:40:26 -0400 2008; 29618468 RubyGem downloads to date</h3><table><tr><th>Gem</th><th>Downloads</th></tr><tr><td><a name="rails">rails</a></td><td>2170030</td></tr> | |
<tr><td><a name="activerecord">activerecord</a></td><td>2139380</td></tr> | |
<tr><td><a name="activesupport">activesupport</a></td><td>2069193</td></tr> | |
<tr><td><a name="actionpack">actionpack</a></td><td>2045547</td></tr> | |
<tr><td><a name="actionmailer">actionmailer</a></td><td>1896488</td></tr> | |
<tr><td><a name="rake">rake</a></td><td>1597441</td></tr> | |
<tr><td><a name="actionwebservice">actionwebservice</a></td><td>1339385</td></tr> | |
<tr><td><a name="mysql">mysql</a></td><td>844519</td></tr> | |
<tr><td><a name="mongrel">mongrel</a></td><td>622147</td></tr> | |
<tr><td><a name="activeresource">activeresource</a></td><td>561454</td></tr> | |
<tr><td><a name="daemons">daemons</a></td><td>457133</td></tr> | |
</body></html> | |
HTML | |
mock(downloads).open("http://gems.rubyforge.org/stats.html") {html} | |
downloads.list | |
out.string.should == (<<-OUT).gsub(/^ /, "") | |
-------------------------------- | |
| | Downloads | | |
-------------------------------- | |
| rails | 2170030 | | |
| activerecord | 2139380 | | |
| activesupport | 2069193 | | |
| actionpack | 2045547 | | |
| actionmailer | 1896488 | | |
| rake | 1597441 | | |
| actionwebservice | 1339385 | | |
| mysql | 844519 | | |
| mongrel | 622147 | | |
| activeresource | 561454 | | |
| daemons | 457133 | | |
-------------------------------- | |
OUT | |
end | |
end | |
end | |
end |
This file contains 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 "rubygems" | |
require "spec" | |
require "rr" | |
dir = File.dirname(__FILE__) | |
$LOAD_PATH.unshift(File.expand_path("#{dir}/../lib")) | |
load File.expand_path("#{dir}/../lib/gems.downloads.thor") | |
Spec::Runner.configure do |config| | |
config.mock_with RR::Adapters::Rspec | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment