Class: RDig::Application

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

Constant Summary collapse

OPTIONS =
[
  ['--config',   '-c', GetoptLong::REQUIRED_ARGUMENT,
    "Read aplication configuration from CONFIG."],
  ['--help',     '-h', GetoptLong::NO_ARGUMENT,
    "Display this help message."],
  ['--query',   '-q', GetoptLong::REQUIRED_ARGUMENT,
    "Execute QUERY."],
  ['--version',  '-v', GetoptLong::NO_ARGUMENT,
   	"Display the program version."],
]

Instance Method Summary collapse

Instance Method Details

#command_line_optionsObject

Return a list of the command line options supported by the program.



228
229
230
# File 'lib/rdig.rb', line 228

def command_line_options
  OPTIONS.collect { |lst| lst[0..-2] }
end

#do_option(opt, value) ⇒ Object

Do the option defined by opt and value.



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/rdig.rb', line 233

def do_option(opt, value)
  case opt
  when '--help'
    help
    exit
  when '--config'
    options.config_file = value
  when '--query'
    options.query = value
  when '--version'
    exit
  else
    fail "Unknown option: #{opt}"
  end 
end

#handle_optionsObject

Read and handle the command line options.



250
251
252
253
# File 'lib/rdig.rb', line 250

def handle_options
  opts = GetoptLong.new(*command_line_options)
  opts.each { |opt, value| do_option(opt, value) }
end

#helpObject

Display the rake command line help.



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/rdig.rb', line 210

def help
  usage
  puts
  puts "Options are ..."
  puts
  OPTIONS.sort.each do |long, short, mode, desc|
    if mode == GetoptLong::REQUIRED_ARGUMENT
      if desc =~ /\b([A-Z]{2,})\b/
        long = long + "=#{$1}"
      end
    end
    printf "  %-20s (%s)\n", long, short
    printf "      %s\n", desc
  end
end

#load_configfileObject

Load the configuration



256
257
258
# File 'lib/rdig.rb', line 256

def load_configfile
  load File.expand_path(options.config_file)
end

#optionsObject

Application options from the command line



200
201
202
# File 'lib/rdig.rb', line 200

def options
  @options ||= OpenStruct.new
end

#runObject

Run the rdig application.



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/rdig.rb', line 261

def run
  puts "RDig version #{RDIGVERSION}"
  handle_options
  begin
    load_configfile
  rescue
    puts $!.backtrace
    fail "No Configfile found!\n#{$!}"
  end

  puts "using Ferret #{Ferret::VERSION}"

  if options.query
    # query the index
    puts "executing query >#{options.query}<"
    results = RDig.searcher.search(options.query)
    puts "total results: #{results[:hitcount]}"
    results[:list].each { |result|
      puts <<-EOF
#{result[:url]}
  #{result[:title]}
  #{result[:extract]}

      EOF
    }
  else
    # rebuild index
    @crawler = Crawler.new
    @crawler.run
  end
end

#usageObject

Display the program usage line.



205
206
207
# File 'lib/rdig.rb', line 205

def usage
  puts "rdig -c configfile {options}"
end