Skip to content

Instantly share code, notes, and snippets.

@dalibor
Created December 28, 2011 19:06
Show Gist options
  • Save dalibor/1529203 to your computer and use it in GitHub Desktop.
Save dalibor/1529203 to your computer and use it in GitHub Desktop.
Rubygems Redis
# rubygems.org / config / initializers / redis.rb
if Rails.env.test? || Rails.env.cucumber?
$redis = Redis.new(:db => 1)
else
$redis = Redis.connect(:url => ENV['REDISTOGO_URL'])
end
# rubygems.org / app / models / download.rb
class Download
def self.incr(name)
$redis.incr(rubygem_key(name))
end
def self.rubygem_key(name)
"downloads:rubygem:#{name}"
end
def self.for_rubygem(name)
$redis.get(rubygem_key(name)).to_i
end
end
# rubygems.org / config / application.rb
module Gemcutter
class Application < Rails::Application
config.middleware.use "Hostess"
end
end
# rubygems.org / app / middleware / hostess.rb
class Hostess < Sinatra::Base
get "/gems/*.gem" do
name = Version.rubygem_name_for(full_name)
Download.incr(name, full_name)
end
def full_name
@full_name ||= params[:splat].join.chomp('.gem')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment