Class: Ronin::CLI::Commands::Tips Private

Inherits:
Ronin::CLI::Command show all
Defined in:
lib/ronin/cli/commands/tips.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Prints a random tip on how to use ronin.

Usage

ronin tips [options]

Options

   --list-categories            Prints all category names

-c, --category STR Print a random tip from the category -s, --search TEXT Searches all tip files for the text -h, --help Print help information

Since:

  • 2.0.0

Constant Summary collapse

TIPS_DIR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Path to the data/tips/ directory.

Since:

  • 2.0.0

File.join(__dir__,'..','..','..','..','data','tips')

Instance Method Summary collapse

Instance Method Details

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prints all tips that contain the given text.

Since:

  • 2.0.0



181
182
183
184
185
# File 'lib/ronin/cli/commands/tips.rb', line 181

def print_matching_tips(text, category: nil)
  search_tip_files(text, category: category).each do |path|
    print_tip(path)
  end
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prints a random tip.

Parameters:

  • category (String, nil) (defaults to: nil)

    The optional tips category to select the random tip from.

Since:

  • 2.0.0



174
175
176
# File 'lib/ronin/cli/commands/tips.rb', line 174

def print_random_tip(category=nil)
  print_tip(random_tip_path(category))
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prints a tip at the given path.

Parameters:

  • path (String)

    The path to the tip file.

Since:

  • 2.0.0



155
156
157
158
159
160
161
162
163
164
# File 'lib/ronin/cli/commands/tips.rb', line 155

def print_tip(path)
  contents = File.read(path)

  puts contents

  unless contents.end_with?("#{$/}#{$/}")
    puts
    puts
  end
end

#random_tip_path(category = nil) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Gets a path to a random tip.

Parameters:

  • category (String, nil) (defaults to: nil)

    The optional tips category to select the random tip from.

Returns:

  • (String)

    The path to the random tip.

Since:

  • 2.0.0



127
128
129
# File 'lib/ronin/cli/commands/tips.rb', line 127

def random_tip_path(category=nil)
  tip_paths(category).sample
end

#runObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs the tip command.

Since:

  • 2.0.0



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ronin/cli/commands/tips.rb', line 63

def run
  if options[:list_categories]
    puts tip_category_names
  else
    category = options[:category]

    if category && !tip_category_names.include?(category)
      print_error "unknown category name: #{category}."
      print_error "please see `ronin tips --list-categories` for all category names."
      exit(1)
    end

    if options[:search]
      print_matching_tips(options[:search], category: category)
    else
      print_random_tip(category)
    end
  end
end

#search_tip_files(text, category: nil) ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Searches through all tip files for the given text.

Parameters:

  • text (String)

    The text to search for.

  • category (String, nil) (defaults to: nil)

    The optional tips category to search within.

Returns:

  • (Array<String>)

    The paths to the tip files containing the matching text.

Since:

  • 2.0.0



143
144
145
146
147
# File 'lib/ronin/cli/commands/tips.rb', line 143

def search_tip_files(text, category: nil)
  tip_paths(category).select do |path|
    File.read(path).include?(text)
  end
end

#tip_category_namesArray<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

All of the tip category names.

Returns:

  • (Array<String>)

Since:

  • 2.0.0



97
98
99
# File 'lib/ronin/cli/commands/tips.rb', line 97

def tip_category_names
  tip_category_paths.map { |path| File.basename(path) }
end

#tip_category_pathsArray<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

All of the paths to the tip category directories.

Returns:

  • (Array<String>)

Since:

  • 2.0.0



88
89
90
# File 'lib/ronin/cli/commands/tips.rb', line 88

def tip_category_paths
  Dir[File.join(TIPS_DIR,'*/')]
end

#tip_paths(category = nil) ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Gets the paths to all of the tip files.

Parameters:

  • category (String, nil) (defaults to: nil)

    The optional tips category to select the tip files from.

Returns:

  • (Array<String>)

    The paths to all files in data/tips/.

Since:

  • 2.0.0



110
111
112
113
114
115
116
# File 'lib/ronin/cli/commands/tips.rb', line 110

def tip_paths(category=nil)
  glob_pattern = if category then File.join(TIPS_DIR,category,'*.txt')
                 else             File.join(TIPS_DIR,'{*/}*.txt')
                 end

  return Dir[glob_pattern]
end