Class: Ronin::CLI::Commands::Tips Private
- Inherits:
-
Ronin::CLI::Command
- Object
- Core::CLI::Command
- Ronin::CLI::Command
- Ronin::CLI::Commands::Tips
- 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
--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
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. File.join(__dir__,'..','..','..','..','data','tips')
Instance Method Summary collapse
-
#print_matching_tips(text, category: nil) ⇒ Object
private
Prints all tips that contain the given text.
-
#print_random_tip(category = nil) ⇒ Object
private
Prints a random tip.
-
#print_tip(path) ⇒ Object
private
Prints a tip at the given path.
-
#random_tip_path(category = nil) ⇒ String
private
Gets a path to a random tip.
-
#run ⇒ Object
private
Runs the tip command.
-
#search_tip_files(text, category: nil) ⇒ Array<String>
private
Searches through all tip files for the given text.
-
#tip_category_names ⇒ Array<String>
private
All of the tip category names.
-
#tip_category_paths ⇒ Array<String>
private
All of the paths to the tip category directories.
-
#tip_paths(category = nil) ⇒ Array<String>
private
Gets the paths to all of the tip files.
Instance Method Details
#print_matching_tips(text, category: nil) ⇒ Object
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.
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 |
#print_random_tip(category = nil) ⇒ Object
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.
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 |
#print_tip(path) ⇒ Object
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.
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.
127 128 129 |
# File 'lib/ronin/cli/commands/tips.rb', line 127 def random_tip_path(category=nil) tip_paths(category).sample end |
#run ⇒ Object
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.
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 [:list_categories] puts tip_category_names else category = [: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 [:search] print_matching_tips([: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.
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_names ⇒ 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.
All of the tip category names.
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_paths ⇒ 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.
All of the paths to the tip category directories.
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.
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 |