Created
July 27, 2011 21:10
-
-
Save evanphx/1110378 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
require 'rubygems' | |
require 'net/http' | |
require 'sinatra' | |
require 'json' | |
def get_url(file) | |
response = nil | |
Net::HTTP.start('rubygems.org', 80) do |http| | |
response = http.head("/gems/#{file}") | |
end | |
response['location'] | |
end | |
urls = {} | |
counts = Hash.new(0) | |
get '/gems/:file' do | |
file = params[:file] | |
if u = urls[file] | |
counts[file] += 1 | |
else | |
u = get_url(file) | |
urls[file] = u | |
count = 0 | |
end | |
redirect u | |
end | |
get '/stats' do | |
counts.to_json | |
end | |
get '/refresh_stats' do | |
c = counts | |
counts = Hash.new(0) | |
c.to_json | |
end | |
get '/*' do | |
redirect "http://rubygems.org/#{params[:splat].first}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Evan,
Something I put together a while ago, the Fuse dependency is probably too heavy for many uses, but as a combined concept, could be somewhat interesting to build progressive mirrors:
https://gist.github.com/1113280
Enjoy!