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



129
130
131
# File 'lib/celerity/listener.rb', line 129

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

#handleAlert(page, message) ⇒ Object

interface AlertHandler



81
82
83
# File 'lib/celerity/listener.rb', line 81

def handleAlert(page, message)
  @procs[:alert].each { |handler| handler.call(page, message) }
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.

See Also:



94
95
96
97
# File 'lib/celerity/listener.rb', line 94

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

#handlePrompt(page, message) ⇒ Object

interface PromptHandler



111
112
113
# File 'lib/celerity/listener.rb', line 111

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

#notify(message, origin) ⇒ Object

interface IncorrectnessListener



138
139
140
# File 'lib/celerity/listener.rb', line 138

def notify(message, origin)
  @procs[:incorrectness].each { |handler| handler.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
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, message)
  @procs[:status].each { |handler| handler.call(page, message) }
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