Class: ElasticSearch::ActionListener

Inherits:
Object
  • Object
show all
Defined in:
lib/jruby-elasticsearch/actionlistener.rb

Instance Method Summary collapse

Constructor Details

#initializeActionListener

Returns a new instance of ActionListener.



7
8
9
10
# File 'lib/jruby-elasticsearch/actionlistener.rb', line 7

def initialize
  @failure_callbacks = []
  @success_callbacks = []
end

Instance Method Details

#on(what, &block) ⇒ Object

Helper for registering callbacks. ‘what’ should be either :failure or :success

You can register multiple callbacks if you wish. Callbacks are invoked in order of addition.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/jruby-elasticsearch/actionlistener.rb', line 17

def on(what, &block)
  case what
  when :failure
    @failure_callbacks << block
  when :success
    @success_callbacks << block
  else
    raise "Unknown event '#{what}' for #{self.class.name}"
  end
  return self
end

#onFailure(exception) ⇒ Object

Conforming to Interface org.elasticsearch.action.ActionListener



30
31
32
33
34
35
36
37
# File 'lib/jruby-elasticsearch/actionlistener.rb', line 30

def onFailure(exception)
  if !@failure_callbacks.empty?
    @failure_callbacks.each { |c| c.call(exception) }
  else
    # Default is no failure callbacks
    raise exception
  end
end

#onResponse(response) ⇒ Object

Conforming to Interface org.elasticsearch.action.ActionListener



40
41
42
43
44
45
46
47
# File 'lib/jruby-elasticsearch/actionlistener.rb', line 40

def onResponse(response)
  if !@success_callbacks.empty?
    @success_callbacks.each { |c| c.call(response) }
  else
    # Default if no success callbacks
    puts "#{self.class.name}#onResponse => #{response.inspect} (#{self})"
  end
end