Class: CLIHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/cli.rb

Instance Method Summary collapse

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 options
  @options = options
  `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_allObject



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(@options[:url_cache_file], "w") do |f|
      f.puts JSON::pretty_generate @url_type_cache
    end
  end
end

#init_loggerObject



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 @options[:debug]
  @log_level = Logger::INFO   if @options[:verbose]
  @log_level = Logger::WARN   if @options[:warn]
  @log_level = Logger::UNKOWN if @options[:quiet]
  @log.level = @log_level
  @log.progname = "cli"
end

#load_auth_fileObject



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?(@options[:auth_file])
  begin
    @auth_conf = JSON.load File.read(@options[:auth_file])
  rescue => ex
    @log.fatal "Cannot load authentication config file #{@options[:auth_file]}"
    @log.fatal ex
    exit 1
  end
end

#load_conf_fileObject



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?(@options[:config_file])
  begin
    @conf = JSON.load File.read(@options[:config_file])
  rescue => ex
    @log.fatal "Cannot load config file #{@options[:config_file]}"
    @log.fatal ex
    exit 1
  end
end

#load_url_type_cache_fileObject



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?(@options[:url_cache_file])
  begin
    @url_type_cache = JSON.load File.read(@options[:url_cache_file])
  rescue => ex
    @log.fatal "Cannot load url type cache file #{@options[:url_cache_file]}"
    @log.fatal ex
    exit 1
  end
end


141
142
143
144
145
146
147
148
149
# File 'lib/cli.rb', line 141

def print_job_name_guess job_name
  unless @options[: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