class GemCheck::Checker
Constants
- COL_NAME
- COL_TDL
- COL_VDL
- COL_VER
- INFO_FILENAME
- REPORT_HEADINGS
- URL_GEMCHECK_INFO_JSON
Public Class Methods
new(args=[])
click to toggle source
# File lib/gem-check.rb, line 24 def initialize(args=[]) @new_downloads = 0 @update_version = nil @current_stats = nil @previous_stats = [] @previous_date = Time.now @f_previous_stats = File.join(Dir.home, INFO_FILENAME) @args = args end
option?(argv, opts)
click to toggle source
# File lib/gem-check/cli.rb, line 13 def self.option?(argv, opts) argv.any? { |option| opts.include?(option) } end
show_help()
click to toggle source
# File lib/gem-check/cli.rb, line 3 def self.show_help puts puts "GemCheck v#{GemCheck::VERSION} - Help" puts "\n Flags:\n" puts " -f Don't save the updated stats for this run." puts " -h Display this help screen." puts " -u Update gem-check." puts end
update()
click to toggle source
# File lib/gem-check/cli.rb, line 17 def self.update puts "\nThis will install the latest version of gem-check." print 'Continue? [Yn] ' response = STDIN.gets.chomp puts "\n" + `gem install gem-check` if response.downcase.include?('y') puts end
Public Instance Methods
build_table()
click to toggle source
# File lib/gem-check.rb, line 50 def build_table @current_stats = current_gem_info.sort!{|a, b| b[2].to_i <=> a[2].to_i} add_new_download_info(@current_stats) table = Terminal::Table.new(rows: @current_stats) table.title = header_string table.headings = REPORT_HEADINGS [1 ].each{ |col| table.align_column(col, :left) } [2,3].each{ |col| table.align_column(col, :center) } table end
check()
click to toggle source
# File lib/gem-check.rb, line 34 def check read_previous_info print_table(build_table) store_new_info unless @args.include?('-f') end
print_table(table)
click to toggle source
Display
# File lib/gem-check.rb, line 62 def print_table(table) puts "\n#{table}\n\n" end
read_previous_info()
click to toggle source
Read stored gem info
# File lib/gem-check.rb, line 41 def read_previous_info return unless File.exist? @f_previous_stats f = File.read(@f_previous_stats) @previous_date = Time.parse(f.lines.first.chomp) || nil @previous_stats = [] f.lines.drop(1).each { |line| @previous_stats << clean_stat_line(line)} @previous_stats end
store_new_info()
click to toggle source
Save newest gem info to file @f_previous_stats
# File lib/gem-check.rb, line 67 def store_new_info File.open(@f_previous_stats, 'w') do |file| file.write "#{current_date_string}\n" @current_stats.each do |row| row.each_with_index do |col, idx| row[idx] = decommafy(row[idx]).to_i if idx > 1 end file.write row.to_json + "\n" end end end
Private Instance Methods
add_new_download_info(latest_info)
click to toggle source
Features
# File lib/gem-check/helpers.rb, line 26 def add_new_download_info(latest_info) return latest_info if @previous_stats.empty? latest_info.each do |row| prev_info = search_by_column(@previous_stats, COL_NAME, row[COL_NAME]) prev_info = search_by_column(prev_info, COL_VER, row[COL_VER]) prev_info = prev_info.first || new_version_info(row) calc_new_downloads(COL_TDL, row, prev_info) calc_new_downloads(COL_VDL, row, prev_info) end end
calc_new_downloads(idx, row, info)
click to toggle source
# File lib/gem-check/helpers.rb, line 42 def calc_new_downloads(idx, row, info) new_dls = to_int(row[idx]) - to_int(info[idx]) @new_downloads += new_dls if idx.eql?(COL_TDL) row[idx] = new_dls > 0 ? "#{row[idx]} ( +#{commafy(new_dls)} )" : row[idx] end
clean_stat_line(line)
click to toggle source
File I/O
# File lib/gem-check/helpers.rb, line 6 def clean_stat_line(line) chars_to_filter = %w(\\\" [ ]) line.chomp! chars_to_filter.each { |c| line.delete!(c) } line.split(',') end
commafy(x)
click to toggle source
# File lib/gem-check/cli.rb, line 27 def commafy(x) i,d = x.to_s.split('.') i.gsub(/(\d)(?=\d{3}+$)/,'\\1,') + (d ? ('.'+d) : '') end
current_date_string()
click to toggle source
# File lib/gem-check/helpers.rb, line 13 def current_date_string Time.now.strftime("%b %d, %H:%M:%S") end
current_gem_info()
click to toggle source
Gems API
# File lib/gem-check/helpers.rb, line 18 def current_gem_info Gems.gems.map do |x| x = OpenStruct.new(x) [x.name, x.version, commafy(x.version_downloads), commafy(x.downloads)] end end
decommafy(x)
click to toggle source
# File lib/gem-check/cli.rb, line 32 def decommafy(x) return x unless x.class.eql?(String) x.delete(',') end
header_string()
click to toggle source
# File lib/gem-check/cli.rb, line 41 def header_string "GemCheck v#{GemCheck::VERSION}#{update_status_string}\n"+ "#{current_date_string}\n"+ "#{last_run}" end
last_run()
click to toggle source
adapted from stackoverflow.com/a/195894
# File lib/gem-check/cli.rb, line 48 def last_run time_diff = @previous_date ? (Time.now - @previous_date).to_i : nil time_words = case time_diff when nil then nil when 0 then 'just now' when 1 then 'a second ago' when 2..59 then time_diff.to_s+' seconds ago' when 60..119 then 'a minute ago' #120 = 2 minutes when 120..3540 then (time_diff/60).to_i.to_s+' minutes ago' when 3541..7100 then 'an hour ago' # 3600 = 1 hour when 7101..82800 then ((time_diff+99)/3600).to_i.to_s+' hours ago' when 82801..172000 then 'a day ago' # 86400 = 1 day when 172001..518400 then ((time_diff+800)/(60*60*24)).to_i.to_s+' days ago' when 518400..1036800 then 'a week ago' else ((time_diff+180000)/(60*60*24*7)).to_i.to_s+' weeks ago' end time_words ? "#{commafy(@new_downloads)} downloads since #{time_words}" : '' end
new_version_info(row)
click to toggle source
# File lib/gem-check/helpers.rb, line 37 def new_version_info(row) tdl = to_int(row[COL_TDL]) - to_int(row[COL_VDL]) [row[COL_NAME], row[COL_VER], 0 , tdl] end
search_by_column(table, column, search_s)
click to toggle source
Utils
# File lib/gem-check/helpers.rb, line 49 def search_by_column(table, column, search_s) return table if table.empty? table.select{ |r| r[column].eql?(search_s) } end
to_int(string)
click to toggle source
# File lib/gem-check/helpers.rb, line 54 def to_int(string) decommafy(string).to_i end
update_available?()
click to toggle source
Maintanence
# File lib/gem-check/helpers.rb, line 59 def update_available? versions = open(URL_GEMCHECK_INFO_JSON).read @update_version = JSON.parse(versions, object_class: OpenStruct).first.number @update_version > GemCheck::VERSION end
update_status_string()
click to toggle source
# File lib/gem-check/cli.rb, line 37 def update_status_string update_available? ? "\n [ Update available: v#{@update_version} ]" : nil end