Class: Celerity::Listener
- Inherits:
-
Object
- Object
- Celerity::Listener
- Defined in:
- lib/celerity/listener.rb
Overview
This class is used to wrap some of the listeners available from HtmlUnit’s WebClient.
Instance Method Summary collapse
-
#add_listener(type, &block) ⇒ Object
Add a listener block for one of the available types.
-
#error(message, url, line, column, key) ⇒ Object
(also: #warning)
interface HTMLParserListener.
-
#handleAlert(page, message) ⇒ Object
interface AlertHandler.
-
#handleAttachment(page) ⇒ Object
interface AttachmentHandler.
-
#handleConfirm(page, message) ⇒ Object
interface ConfirmHandler.
-
#handlePrompt(page, message) ⇒ Object
interface PromptHandler.
-
#initialize(webclient) ⇒ Listener
constructor
A new instance of Listener.
-
#notify(message, origin) ⇒ Object
interface IncorrectnessListener.
- #remove_listener(type, proc_or_index) ⇒ Object
-
#statusMessageChanged(page, message) ⇒ Object
interface StatusHandler.
-
#webWindowClosed(web_window_event) ⇒ Object
(also: #webWindowOpened, #webWindowContentChanged)
interface WebWindowListener.
Constructor Details
#initialize(webclient) ⇒ Listener
Returns a new instance of Listener.
17 18 19 20 |
# File 'lib/celerity/listener.rb', line 17 def initialize(webclient) @webclient = webclient @procs = Hash.new { |h, k| h[k] = [] } end |
Instance Method Details
#add_listener(type, &block) ⇒ Object
Add a listener block for one of the available types.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/celerity/listener.rb', line 27 def add_listener(type, &block) case type when :status @webclient.setStatusHandler(self) when :alert @webclient.setAlertHandler(self) when :attachment @webclient.setAttachmentHandler(self) when :web_window_event @webclient.addWebWindowListener(self) when :html_parser @webclient.setHTMLParserListener(self) when :incorrectness @webclient.setIncorrectnessListener(self) when :confirm @webclient.setConfirmHandler(self) when :prompt @webclient.setPromptHandler(self) else raise ArgumentError, "unknown listener type #{type.inspect}" end @procs[type] << block end |
#error(message, url, line, column, key) ⇒ Object Also known as: warning
interface HTMLParserListener
129 130 131 |
# File 'lib/celerity/listener.rb', line 129 def error(, url, line, column, key) @procs[:html_parser].each { |handler| handler.call(, url, line, column, key) } end |
#handleAlert(page, message) ⇒ Object
interface AlertHandler
81 82 83 |
# File 'lib/celerity/listener.rb', line 81 def handleAlert(page, ) @procs[:alert].each { |handler| handler.call(page, ) } end |
#handleAttachment(page) ⇒ Object
interface AttachmentHandler
103 104 105 |
# File 'lib/celerity/listener.rb', line 103 def handleAttachment(page) @procs[:attachment].each { |handler| handler.call(page) } end |
#handleConfirm(page, message) ⇒ Object
interface ConfirmHandler
The returned value is determined by the last registered :confirm listener proc. If it is nil, return true, otherwise return that value as a boolean.
94 95 96 97 |
# File 'lib/celerity/listener.rb', line 94 def handleConfirm(page, ) val = @procs[:confirm].map { |handler| handler.call(page, ) }.last val.nil? || !!val end |
#handlePrompt(page, message) ⇒ Object
interface PromptHandler
111 112 113 |
# File 'lib/celerity/listener.rb', line 111 def handlePrompt(page, ) @procs[:prompt].each { |handler| handler.call(page, ) } end |
#notify(message, origin) ⇒ Object
interface IncorrectnessListener
138 139 140 |
# File 'lib/celerity/listener.rb', line 138 def notify(, origin) @procs[:incorrectness].each { |handler| handler.call(, origin) } end |
#remove_listener(type, proc_or_index) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/celerity/listener.rb', line 52 def remove_listener(type, proc_or_index) unless @procs.has_key?(type) raise ArgumentError, "unknown listener type #{type.inspect}" end procs = @procs[type] case proc_or_index when Fixnum procs.delete_at proc_or_index when Proc procs.delete proc_or_index else raise TypeError, "must give proc or index" end end |
#statusMessageChanged(page, message) ⇒ Object
interface StatusHandler
73 74 75 |
# File 'lib/celerity/listener.rb', line 73 def statusMessageChanged(page, ) @procs[:status].each { |handler| handler.call(page, ) } end |
#webWindowClosed(web_window_event) ⇒ Object Also known as: webWindowOpened, webWindowContentChanged
interface WebWindowListener
119 120 121 |
# File 'lib/celerity/listener.rb', line 119 def webWindowClosed(web_window_event) @procs[:web_window_event].each { |handler| handler.call(web_window_event) } end |