Skip to content

Instantly share code, notes, and snippets.

@dsample
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsample/9663838 to your computer and use it in GitHub Desktop.
Save dsample/9663838 to your computer and use it in GitHub Desktop.
Downloads the gem files for the given gem, along with all of it's dependencies
require 'net/http'
require 'json'
require 'fileutils'
HTTP_PROXY = URI 'http://proxy:80'
DOWNLOAD_DIRECTORY = 'gems'
def http_connection(host, port, use_ssl, &block)
proxy_connection = Net::HTTP::Proxy(HTTP_PROXY.host, HTTP_PROXY.port)
#Net::HTTP.start(host, port) do |http|
proxy_connection.start(host, port,
:verify_mode => OpenSSL::SSL::VERIFY_NONE,
:use_ssl => use_ssl) do |http|
block.call(http)
end
end
def download_file(source_url)
download_uri = URI source_url
filepath = File.join(DOWNLOAD_DIRECTORY, File.basename(download_uri.path))
if File.exists? filepath
puts "File #{filepath} already exists"
else
puts "Downloading #{source_url} to #{filepath}"
FileUtils.mkdir_p File.dirname File.expand_path(filepath)
http_connection download_uri.host, download_uri.port, download_uri.scheme=='https' do |http|
response = http.get(download_uri.path)
open(filepath, "wb") do |file|
file.write(response.body)
end
end
end
end
def download_gem(gem_name)
uri = URI "https://rubygems.org/api/v1/gems/#{gem_name}.json"
http_connection uri.host, uri.port, uri.scheme=='https' do |http|
request = Net::HTTP::Get.new uri.path
response = http.request(request)
json = JSON.parse response.body
puts json['gem_uri']
gem_uri = URI json['gem_uri']
download_file gem_uri.to_s
dependencies = json['dependencies']['runtime']
dependencies.each do |dependency|
puts "Downloading dependency: #{dependency['name']}"
download_gem dependency['name']
end
end
end
download_gem ARGV[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment