Class: Nem::Endpoint::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/nem/endpoint/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Base

Returns a new instance of Base.



4
5
6
7
8
9
10
# File 'lib/nem/endpoint/base.rb', line 4

def initialize(node)
  @node = if node.is_a?(Nem::NodePool)
    node
  else
    Nem::NodePool.new([node])
  end
end

Instance Method Details

#current_nodeObject



12
13
14
# File 'lib/nem/endpoint/base.rb', line 12

def current_node
  @node.first
end

#next_node!Object



16
17
18
# File 'lib/nem/endpoint/base.rb', line 16

def next_node!
  @node.next!
end

#request(method, path, params = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/nem/endpoint/base.rb', line 20

def request(method, path, params = {})
  begin
    res = current_node.request(method, path, params)
    block_given? ? yield(res) : res
  rescue Faraday::ConnectionFailed => err
    Nem.logger.error err.message
    next_node!
    retry
  end
end

#request!(method, path, params = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/nem/endpoint/base.rb', line 31

def request!(method, path, params = {})
  begin
    res = current_node.request!(method, path, params)
    block_given? ? yield(res) : res
  rescue Faraday::ConnectionFailed => err
    Nem.logger.error err.message
    next_node!
    retry
  rescue => err
    raise err
  end
end