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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
source "http://gems.simplesideias.com.br"
source "http://rubygems.org"
gem "rack", ">= 1.0.0"
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GEM
remote: http://gems.simplesideias.com.br/
remote: http://rubygems.org/
specs:
rack (1.2.2)
rack (1.4.1)

PLATFORMS
ruby
Expand Down
24 changes: 23 additions & 1 deletion rubygems_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ def run
when "/"
[200, {"Content-Type" => "text/html"}, [erb(:index)]]
else
[200, {"Content-Type" => "application/octet-stream"}, [contents]]
if env["QUERY_STRING"].empty?
[200, {"Content-Type" => "application/octet-stream"}, [contents]]
else
# add for query
[200, {"Content-Type" => "application/octet-stream"}, [query]]
end
end
rescue Exception
[200, {"Content-Type" => "text/html"}, [erb(404)]]
Expand Down Expand Up @@ -99,6 +104,23 @@ def contents
open(filepath).read
end

# add for query
def query
logger.info "Query from interwebz: #{api_url}"
open(api_url).read
rescue Exception => error
# Just try to load from file if something goes wrong.
# This includes HTTP timeout, or something.
# If it fails again, we won't have any files anyway!
logger.error "Error: #{error.class} => #{error.message}"
open(filepath).read
end

# the query url
def api_url
File.join("http://rubygems.org", env["PATH_INFO"] + '?' + env["QUERY_STRING"])
end

def save(contents)
FileUtils.mkdir_p File.dirname(filepath)
File.open(filepath, "wb") {|handler| handler << contents}
Expand Down