Class: Celerity::Listener

Inherits:
Object
  • Object
show all
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

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



127
128
129
# File 'lib/celerity/listener.rb', line 127

def error(message, url, line, column, key)
  @procs[:html_parser].each { |h| h.call(message, url, line, column, key) }
end

#handleAlert(page, message) ⇒ Object

interface AlertHandler



79
80
81
# File 'lib/celerity/listener.rb', line 79

def handleAlert(page, message)
  @procs[:alert].each { |h| h.call(page, message) }
end

#handleAttachment(page) ⇒ Object

interface AttachmentHandler



101
102
103
# File 'lib/celerity/listener.rb', line 101

def handleAttachment(page)
  @procs[:attachment].each { |h| h.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.

See Also:



92
93
94
95
# File 'lib/celerity/listener.rb', line 92

def handleConfirm(page, message)
  val = @procs[:confirm].map { |h| h.call(page, message) }.last
  val.nil? || !!val
end

#handlePrompt(page, message) ⇒ Object

interface PromptHandler



109
110
111
# File 'lib/celerity/listener.rb', line 109

def handlePrompt(page, message)
  @procs[:prompt].each { |h| h.call(page, message) }
end

#notify(message, origin) ⇒ Object

interface IncorrectnessListener



136
137
138
# File 'lib/celerity/listener.rb', line 136

def notify(message, origin)
  @procs[:incorrectness].each { |h| h.call(message, origin) }
end

#remove_listener(type, proc_or_index) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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

  case proc_or_index
  when Fixnum
    @procs[type].delete_at proc_or_index
  when Proc
    @procs[type].delete proc_or_index
  else
    raise TypeError, "must give proc or index"
  end
end

#statusMessageChanged(page, message) ⇒ Object

interface StatusHandler



71
72
73
# File 'lib/celerity/listener.rb', line 71

def statusMessageChanged(page, message)
  @procs[:status].each { |h| h.call(page, message) }
end

#webWindowClosed(web_window_event) ⇒ Object Also known as: webWindowOpened, webWindowContentChanged

interface WebWindowListener



117
118
119
# File 'lib/celerity/listener.rb', line 117

def webWindowClosed(web_window_event)
  @procs[:web_window_event].each { |h| h.call(web_window_event) }
end