Class: CLIHelper
- Inherits:
-
Object
- Object
- CLIHelper
- Defined in:
- lib/cli.rb
Instance Method Summary collapse
- #best_n_matches(arr, comp, n) ⇒ Object
- #fetch(job_name) ⇒ Object
- #fetch_all ⇒ Object
- #init_logger ⇒ Object
-
#initialize(options) ⇒ CLIHelper
constructor
A new instance of CLIHelper.
- #load_auth_file ⇒ Object
- #load_conf_file ⇒ Object
- #load_url_type_cache_file ⇒ Object
- #print_job_name_guess(job_name) ⇒ Object
Constructor Details
#initialize(options) ⇒ CLIHelper
Returns a new instance of CLIHelper.
48 49 50 51 52 53 54 55 |
# File 'lib/cli.rb', line 48 def initialize = `mkdir -p #{kitgrawler_dir}` init_logger load_conf_file load_auth_file load_url_type_cache_file end |
Instance Method Details
#best_n_matches(arr, comp, n) ⇒ Object
151 152 153 154 155 156 157 158 159 |
# File 'lib/cli.rb', line 151 def best_n_matches arr, comp, n require 'damerau-levenshtein' map = {} dl = DamerauLevenshtein arr.each do |str| map[str] = dl.distance(str, comp, 2) end return arr.sort {|a, b| map[a] <=> map[b] } end |
#fetch(job_name) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/cli.rb', line 120 def fetch job_name grawl_location = job_name conf = @conf[job_name] if conf == nil print_job_name_guess job_name return end begin service = BaseService::get_service grawl_location, conf, @auth_conf, @log_level, @url_type_cache begin service.execute rescue => ex @log.error "Failed executing #{grawl_location}" @log.error ex end rescue => ex @log.error "Failed to instantiate #{grawl_location}" @log.error ex end end |
#fetch_all ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/cli.rb', line 104 def fetch_all begin @conf.each_key do |grawl_job| fetch grawl_job end rescue => ex @log.fatal "Error grawling configured locations" @log.fatal ex exit 1 ensure File.open([:url_cache_file], "w") do |f| f.puts JSON::pretty_generate @url_type_cache end end end |
#init_logger ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/cli.rb', line 57 def init_logger @log = Logger.new STDOUT @log_level = Logger::WARN @log_level = Logger::DEBUG if [:debug] @log_level = Logger::INFO if [:verbose] @log_level = Logger::WARN if [:warn] @log_level = Logger::UNKOWN if [:quiet] @log.level = @log_level @log.progname = "cli" end |
#load_auth_file ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/cli.rb', line 80 def load_auth_file @auth_conf = {} return unless File.exists?([:auth_file]) begin @auth_conf = JSON.load File.read([:auth_file]) rescue => ex @log.fatal "Cannot load authentication config file #{@options[:auth_file]}" @log.fatal ex exit 1 end end |
#load_conf_file ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/cli.rb', line 68 def load_conf_file @conf = {} return unless File.exists?([:config_file]) begin @conf = JSON.load File.read([:config_file]) rescue => ex @log.fatal "Cannot load config file #{@options[:config_file]}" @log.fatal ex exit 1 end end |
#load_url_type_cache_file ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/cli.rb', line 92 def load_url_type_cache_file @url_type_cache = {} return unless File.exists?([:url_cache_file]) begin @url_type_cache = JSON.load File.read([:url_cache_file]) rescue => ex @log.fatal "Cannot load url type cache file #{@options[:url_cache_file]}" @log.fatal ex exit 1 end end |
#print_job_name_guess(job_name) ⇒ Object
141 142 143 144 145 146 147 148 149 |
# File 'lib/cli.rb', line 141 def print_job_name_guess job_name unless [:quiet] puts "There is no job '#{job_name}'." puts "Maybe you meant one of the following" best_n_matches(@conf.keys, job_name, 3).each do |name| puts " #{name}" end end end |