Instantly share code, notes, and snippets.

Embed
What would you like to do?
Core of docker for RubyGems statistics
require 'rubygems'
# require 'bundler/setup'
require 'active_record'
require 'bestgems'
require 'tty-progressbar'
$client = Bestgems.client
# Prepare connection
project_root = File.dirname(File.absolute_path(__FILE__))
Dir.glob(project_root + '/app/models/*.rb').each{|f| require f}
connection_details = YAML::load(File.open('config/database.yml'))
ActiveRecord::Base.establish_connection(connection_details)
# Pase options
require 'optparse'
options = {gem: nil}
parser = OptionParser.new do|opts|
opts.banner = "Usage: starts.rb [options]"
opts.on('-g', '--gem gem', 'Gem') do |gem|
options[:gem] = gem
end
opts.on('-f', '--file file', 'File') do |file|
options[:file] = file
end
opts.on('-h', '--help', 'Displays Help') do
puts opts
exit
end
end
parser.parse!
def download_stats(gem)
puts "\n#{gem}"
puts '-' * gem.length
TTY::ProgressBar.new("daily stats [:bar] :percent")
.iterate($client.daily_downloads(gem)) do |date, downloads|
Stat.find_or_create_by(gem: gem, date: date).update(daily_downloads: downloads)
end
TTY::ProgressBar.new("total stats [:bar] :percent")
.iterate($client.total_downloads(gem)) do |date, downloads|
Stat.find_or_create_by(gem: gem, date: date).update(total_downloads: downloads)
end
end
if options.has_key?(:file)
File.readlines(options[:file]).each { |gem| download_stats(gem.strip) }
elsif options.has_key?(:gem)
download_stats(options[:gem])
else
put 'Nothing to do here'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment