class RBT::Cookbooks::Highest

Constants

DISPLAY_N_PROGRAMS_BY_DEFAULT
#

DISPLAY_N_PROGRAMS_BY_DEFAULT

How many programs to display by default.

#
EXIT_ON_MISSING_ENTRY
#

EXIT_ON_MISSING_ENTRY

#

Public Class Methods

new( display_n = DISPLAY_N_PROGRAMS_BY_DEFAULT, run_already = true ) click to toggle source
#

initialize

To use more than the defaults, you can do this:

Cookbooks::Highest.new(150)
#
# File lib/rbt/cookbooks/highest.rb, line 48
def initialize(
    display_n   = DISPLAY_N_PROGRAMS_BY_DEFAULT,
    run_already = true
  )
  reset
  startup
  set_display_top_n(
    display_n
  ) # First set how many programs we will display.
  run if run_already
end

Public Instance Methods

beautify_file_size(i) click to toggle source
#

beautify_file_size

This will attempt to beautify the given input.

We require 3 trailing positions after the , position.

#
# File lib/rbt/cookbooks/highest.rb, line 206
def beautify_file_size(i)
  i  = i.to_s.strip
  x  = i.to_f
  kb = 1024
  mb = (1024 ** 2)
  gb = (1024 ** 3)
  # tb = (1024 ** 4)
  if x < 1024
    return "#{round(x.to_f)} bytes"
  elsif x > 1024 && x < mb
    return "#{round(x.to_f/kb)} kb"
  elsif x > mb && x < gb
    result = round(x.to_f/mb)
    result = '%.3f' % result
    return "#{result} MB"
  elsif x > gb
    return "#{round(x.to_f/gb)} GB"
  end
end
display_n?() click to toggle source
#

display_n?

#
# File lib/rbt/cookbooks/highest.rb, line 236
def display_n?
  @display_top_n
end
inform_the_user_what_we_will_do() click to toggle source
#

inform_the_user_what_we_will_do

#
# File lib/rbt/cookbooks/highest.rb, line 97
def inform_the_user_what_we_will_do
  # ======================================================================= #
  # Get the local URL.
  # ======================================================================= #
  opnn { :no_trailing }
  e ' Now fetching the top `'+salmon(@display_top_n.to_s)+
    '` largest local programs from the directory'
  e '  `'+sdir(source_directory?)+'`.'
  e 'This may take a while. Please be patient.'
end
menu( i = commandline_arguments_with_leading_hyphens? ) click to toggle source
#

menu (menu tag)

#
notify_the_user_that_we_have_finished_fetching_the_local_urls() click to toggle source
#

notify_the_user_that_we_have_finished_fetching_the_local_urls

#
# File lib/rbt/cookbooks/highest.rb, line 111
def notify_the_user_that_we_have_finished_fetching_the_local_urls
  e # Add a newline.
  e "Finished fetching the local URLs. We will display them now.#{N}"
  e
end
report()
Alias for: report_results
report_results() click to toggle source
#

report_results

#
# File lib/rbt/cookbooks/highest.rb, line 304
def report_results
  sort_results
  n_rjust = display_n?.to_s.size
  @results.each_with_index { |entry, index|
    index    += 1
    index     = index.to_s.rjust(n_rjust)
    # ===================================================================== #
    # For now, we shorten the url.
    # ===================================================================== #
    local_url = remove_file_suffix(
      File.basename(entry.first).strip
    ).strip
    local_url = '%-36s' % local_url # Then we pad it.
    _ = '%.3f' % entry[1].to_s
    filesize  = '%12s' % beautify_file_size(_)
    e '  ('+sfancy(index)+') '+
      steelblue(local_url)+' '+
      swarn(filesize)
  }
end
Also aliased as: report
reset() click to toggle source
#

reset

#
Calls superclass method RBT::Base#reset
# File lib/rbt/cookbooks/highest.rb, line 63
def reset
  super()
  infer_the_namespace
  # ======================================================================= #
  # === @use_this_dataset
  #
  # This variable must be a Symbol. The allowed symbols are:
  #
  #   :use_the_expanded_dataset
  #   :use_a_local_check
  #
  # The first one is much faster, but will not be as accurate, as it
  # does not do a full check on every available, local program.
  #
  # The user can override this on the commandline - see the menu()
  # interface in how to do this.
  # ======================================================================= #
  @use_this_dataset = :use_the_expanded_dataset # This is the faster variant.
  # ======================================================================= #
  # === @dataset
  # ======================================================================= #
  @dataset = nil
end
round(number, n_positions = 3) click to toggle source
#

round

#
# File lib/rbt/cookbooks/highest.rb, line 229
def round(number, n_positions = 3)
  ( number * 10 ** n_positions ).floor.to_f / (10 ** n_positions)
end
run() click to toggle source
#

run (run tag)

#
# File lib/rbt/cookbooks/highest.rb, line 354
def run
  menu
  inform_the_user_what_we_will_do
  sanitize_available_programs
  notify_the_user_that_we_have_finished_fetching_the_local_urls
  report_results # report results
end
sanitize_available_programs() click to toggle source
#

sanitize_available_programs

#
# File lib/rbt/cookbooks/highest.rb, line 250
def sanitize_available_programs
  use_the_expanded_dataset = use_the_expanded_dataset?
  array = [] # Dump the information into this array here.
  _ = @available_programs
  _.each {|i|
    # ===================================================================== #
    # Obtain the dataset for this program next.
    # ===================================================================== #
    if use_the_expanded_dataset
      this_file = "#{directory_expanded_cookbooks?}#{i}.yml"
      if File.exist? this_file
        dataset = YAML.load_file(this_file)
        url  = dataset[:url1]
        size = dataset[:archive_size] 
      else
        no_file_exists_at(this_file)
      end
      # dataset = this_file
    else
      @dataset = RBT::Cookbooks::SanitizeCookbook.new(i) { :be_fast_and_be_quiet }
      url = @dataset.program_path? # Keep the local URL here.
      if url.nil?
        e '`'+simp(i.to_s)+'` is nil, probably because it does not '\
          'exist locally.'
        e 'Please consider fixing this problem.'
        if EXIT_ON_MISSING_ENTRY
          e 'We will exit now as the constant EXIT_ON_MISSING_ENTRY '\
            'was set to true.'
          exit
        end
      end
      size = @dataset.size? # Keep the filesize here.
      # =================================================================== #
      # Handle the case when the filesize is 0. In this case we will
      # query the local file.
      # =================================================================== #
      if (size == 0) and (url and File.file?(url))
        size = File.size(url)
      end
    end
    if @debug
      e crimson('DEBUG:')+' Filesize of '+slateblue(i.to_s)+
        ' is '+orange(size)+'.'
    end
    if url
      array << [ url, size ] # Then append into the main Array containing this information.
    end
  }
  @available_programs = array # Store it here finally.
end
set_display_top_n( i = DISPLAY_N_PROGRAMS_BY_DEFAULT ) click to toggle source
#

set_display_top_n

#
# File lib/rbt/cookbooks/highest.rb, line 129
def set_display_top_n(
    i = DISPLAY_N_PROGRAMS_BY_DEFAULT
  )
  if i.is_a?(String) and i.empty?
    i = :default
  end 
  if i.is_a? Array # Handle arrays first.
    if i.empty?
      i = DISPLAY_N_PROGRAMS_BY_DEFAULT # Use default.
    else
      i = i.first
    end
  elsif i.is_a? Symbol
    case i # case tag
    # ===================================================================== #
    # === :display_ten_programs
    # ===================================================================== #
    when :display_ten_programs
      i = 10
    # ===================================================================== #
    # === :display_twenty_programs
    # ===================================================================== #
    when :display_twenty_programs
      i = 20
    # ===================================================================== #
    # === :display_fifty_programs
    # ===================================================================== #
    when :display_fifty_programs,
         :default
      i = 50
    end
  end
  case i # case tag
  # ======================================================================= #
  # === highest --help
  # ======================================================================= #
  when /-?-?help$/i
    show_help; exit
  end
  i = i.to_i unless i.is_a? Integer
  # ======================================================================= #
  # Ensure that we will not grab too many entries.
  # ======================================================================= #
  if i > available_programs?.size.to_i
    i = available_programs?.size.to_i
  end
  @display_top_n = i
end
show_help() click to toggle source
#

show_help (help tag)

#
# File lib/rbt/cookbooks/highest.rb, line 181
def show_help
  unless Object.const_defined? :ClassDocuShower
    begin
      require 'class_docu_shower'
    rescue LoadError; end
  end
  if Object.const_defined? :ClassDocuShower
    ClassDocuShower[__FILE__]
  end
  e 'This class does not have many special commands.'
  e
  e 'You can pass in a number, which simply means how many'
  e 'programs will be displayed, e. g. as in:'
  e
  e '  highest 250'
  e
end
sort_results() click to toggle source
#

sort_results

#
# File lib/rbt/cookbooks/highest.rb, line 120
def sort_results
  @results = @available_programs.sort_by {|name, size|
    size.to_i # We sort by size.
  }.reverse[0...@display_top_n]
end
startup() click to toggle source
#

startup

#
# File lib/rbt/cookbooks/highest.rb, line 90
def startup
  @available_programs = available_programs?
end
use_the_expanded_dataset?() click to toggle source
#

use_the_expanded_dataset?

#
# File lib/rbt/cookbooks/highest.rb, line 243
def use_the_expanded_dataset?
  @use_this_dataset == :use_the_expanded_dataset
end