Skip to content
This repository was archived by the owner on Sep 25, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,22 @@ And, finally, add this source to your ~/.gemrc.
- http://gems.simplesideias.com.br/
gem: --no-rdoc --no-ri

The <tt>specs.4.8.gz</tt> is cached for 24 hours. This file contains all gems/versions
The <tt>specs.4.8.gz</tt> is cached for 24 hours by default. This file contains all gems/versions
and is over 3MB. You can invalidate this cache by running
<tt>curl -X DELETE http://gems.simplesideias.com.br</tt>.
<tt>curl -X DELETE http://gems.simplesideias.com.br</tt>.

The expiration time and HTTP proxy settings can be specified in config.rb. Example:

Proxy.config do
# Proxy server
# If rubygems_proxy itself is behind a proxy server, add its configuration here.
# http_proxy_url "http://127.0.0.1:3128
# http_proxy_user "user"
# http_proxy_pass "password"

# Time until the download specs expire. Default is 24 hours
spec_expiry_time 84600
end

= Maintainer

Expand Down
13 changes: 13 additions & 0 deletions config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Idea from: http://speakmy.name/2011/05/29/simple-configuration-for-ruby-apps/
# Configuration parameters go here.

Proxy.config do
# Proxy server
# If rubygems_proxy itself is behind a proxy server, add its configuration here.
# http_proxy_url "http://127.0.0.1:3128
# http_proxy_user "user"
# http_proxy_pass "password"

# Time until the download specs expire. Default is 24 hours
spec_expiry_time 84600
end
55 changes: 53 additions & 2 deletions rubygems_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,46 @@
require "logger"
require "erb"

module Proxy
# we don't want to instantiate this class - it's a singleton,
# so just keep it as a self-extended module
extend self

# Appdata provides a basic single-method DSL with .parameter method
# being used to define a set of available settings.
# This method takes one or more symbols, with each one being
# a name of the configuration option.
def parameter(*names)
names.each do |name|
attr_accessor name

# For each given symbol we generate accessor method that sets option's
# value being called with an argument, or returns option's current value
# when called without arguments
define_method name do |*values|
value = values.first
value ? self.send("#{name}=", value) : instance_variable_get("@#{name}")
end
end
end

# And we define a wrapper for the configuration block, that we'll use to set up
# our set of options
def config(&block)
instance_eval &block
end

end

Proxy.config do
parameter :http_proxy_url
parameter :http_proxy_user
parameter :http_proxy_pass
parameter :spec_expiry_time
end

require File.expand_path("config.rb",File.dirname(__FILE__))

class RubygemsProxy
attr_reader :env

Expand Down Expand Up @@ -89,7 +129,18 @@ def contents
open(filepath).read
else
logger.info "Read from interwebz: #{url}"
open(url).read.tap {|content| save(content)}
proxy_args = { }
unless ::Proxy.http_proxy_url.nil?
logger.info "Using proxy:#{::Proxy.http_proxy_url}"
unless ::Proxy.http_proxy_user.nil?
logger.info "HTTP Proxy authentication enabled. Using user:#{::Proxy.http_proxy_user}"
proxy_args = { :proxy_http_basic_authentication => [::Proxy.http_proxy_url, ::Proxy.http_proxy_user, ::Proxy.http_proxy_pass]}
else
logger.info "Using proxy without authentication."
proxy_args = { :proxy => ::Proxy.http_proxy_url }
end
end
open(url, proxy_args).read.tap {|content| save(content)}
end
rescue Exception => error
# Just try to load from file if something goes wrong.
Expand All @@ -107,7 +158,7 @@ def save(contents)
def cached?
case File.basename(filepath)
when /^specs\./
File.exist?(filepath) && (Time.now - File.mtime(filepath)).to_i < 84600
File.exist?(filepath) && (Time.now - File.mtime(filepath)).to_i < ::Proxy.spec_expiry_time
when /\.gz$/
false
else
Expand Down