Class: Dispatcher::CLIDispatcher

Inherits:
CoreDispatcher show all
Defined in:
lib/dispatcher/cli.rb

Overview

Command line Dispatcher interface.

The CLI interface allows for easy command line debugging of Responders.

Usage: script.rb [URI] [switches]

Constant Summary collapse

OPTS =
GetoptLong.new(
  ['--help', '-h', GetoptLong::NO_ARGUMENT],
  ['--clean', '-c', GetoptLong::NO_ARGUMENT]
)

Instance Method Summary collapse

Methods inherited from CoreDispatcher

#handle, inherited, #initialize

Constructor Details

This class inherits a constructor from Dispatcher::CoreDispatcher

Instance Method Details

#display_helpObject

Displays the command line help and usage details



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dispatcher/cli.rb', line 63

def display_help
  print %{
Usage: #{File.basename($0)} [URI] [options]

  Options:
-c/--clean   Cleans up any HTML output for easy screen reading
-h/--help    Help. You're looking at it

  Further Information:
http://dispatcher.rubyforge.org
}
end

#handle_cleaned(request) ⇒ Object

Displays a nicely formatted copy of the response



51
52
53
54
55
56
57
58
59
60
# File 'lib/dispatcher/cli.rb', line 51

def handle_cleaned(request)
  response = Response.new(@responder, true)
  response.render(request)
  body = response.body
   
  # strip tags
  body.gsub!(/<\/?[^>]*>/, "")
  
  print body
end

#listenObject

Sets up the request, and handles the response.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dispatcher/cli.rb', line 18

def listen
  # read the options into a Hash
  options = Hash.new
  OPTS.quiet = true # shut it up
  
  begin
    OPTS.each do |opt, value|
      options[opt] = value
    end
  # catch invalid option exceptions
  rescue GetoptLong::InvalidOption => err
    puts err.to_s + '  (-h will show valid options)'
  end
  
  # help?
  if options.has_key? '--help'
    display_help
  
  # otherwise we're going to handle a request
  else
    # set up our request
    request = Request.new
    
    # and handle it
    if options.has_key? '--clean'
      handle_cleaned(request)
    else
      handle(request)
    end
  end
end