Class: Xapixctl::PhoenixClient::ResultHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/xapixctl/phoenix_client/result_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(default_success_handler:, default_error_handler:) {|_self| ... } ⇒ ResultHandler

Returns a new instance of ResultHandler.

Yields:

  • (_self)

Yield Parameters:



6
7
8
9
10
11
# File 'lib/xapixctl/phoenix_client/result_handler.rb', line 6

def initialize(default_success_handler:, default_error_handler:)
  @success_handler = default_success_handler
  @error_handler = default_error_handler
  @result_handler = nil
  yield self if block_given?
end

Instance Method Details

#formatter(proc) ⇒ Object



19
# File 'lib/xapixctl/phoenix_client/result_handler.rb', line 19

def formatter(proc); @formatter = proc; self; end

#on_error(&block) ⇒ Object



15
# File 'lib/xapixctl/phoenix_client/result_handler.rb', line 15

def on_error(&block); @error_handler = block; self; end

#on_success(&block) ⇒ Object



13
# File 'lib/xapixctl/phoenix_client/result_handler.rb', line 13

def on_success(&block); @success_handler = block; self; end

#prepare_data(proc) ⇒ Object



17
# File 'lib/xapixctl/phoenix_client/result_handler.rb', line 17

def prepare_data(proc); @result_handler = proc; self; end

#runObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/xapixctl/phoenix_client/result_handler.rb', line 21

def run
  res = yield
  res = res.present? ? JSON.parse(res) : res
  res = @result_handler ? @result_handler.call(res) : res
  res = @formatter ? @formatter.call(res) : res
  @success_handler.call(res)
rescue RestClient::Exception => err
  response = JSON.parse(err.response) rescue {}
  @error_handler.call(err, response)
rescue SocketError, Errno::ECONNREFUSED => err
  @error_handler.call(err, nil)
end