Class: Elrond::Api::Node::Client

Inherits:
Object
  • Object
show all
Includes:
Endpoints::Address, Endpoints::Nodes
Defined in:
lib/elrond/api/node/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Endpoints::Address

#account, #balance

Methods included from Endpoints::Nodes

#address, #heartbeat, #start, #statistics, #status, #stop

Constructor Details

#initialize(host:, port: 8080, proxy: nil, user_agent: "curl/7.58.0", configuration: ::Elrond::Api.configuration, options: {}) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
17
# File 'lib/elrond/api/node/client.rb', line 8

def initialize(host:, port: 8080, proxy: nil, user_agent: "curl/7.58.0", configuration: ::Elrond::Api.configuration, options: {})
  self.host             =   host
  self.port             =   port
  self.proxy            =   proxy
  self.user_agent       =   user_agent
  self.configuration    =   configuration

  set_headers
  set_connection
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



6
7
8
# File 'lib/elrond/api/node/client.rb', line 6

def configuration
  @configuration
end

#connectionObject

Returns the value of attribute connection.



6
7
8
# File 'lib/elrond/api/node/client.rb', line 6

def connection
  @connection
end

#headersObject

Returns the value of attribute headers.



6
7
8
# File 'lib/elrond/api/node/client.rb', line 6

def headers
  @headers
end

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/elrond/api/node/client.rb', line 6

def host
  @host
end

#portObject

Returns the value of attribute port.



6
7
8
# File 'lib/elrond/api/node/client.rb', line 6

def port
  @port
end

#proxyObject

Returns the value of attribute proxy.



6
7
8
# File 'lib/elrond/api/node/client.rb', line 6

def proxy
  @proxy
end

#user_agentObject

Returns the value of attribute user_agent.



6
7
8
# File 'lib/elrond/api/node/client.rb', line 6

def user_agent
  @user_agent
end

Instance Method Details

#delete(path, parameters: {}, data: {}, headers: {}, options: {}) ⇒ Object



62
63
64
# File 'lib/elrond/api/node/client.rb', line 62

def delete(path, parameters: {}, data: {}, headers: {}, options: {})
  request path, method: :delete, parameters: parameters, data: data, headers: headers, options: options
end

#get(path, parameters: {}, headers: {}, options: {}) ⇒ Object



46
47
48
# File 'lib/elrond/api/node/client.rb', line 46

def get(path, parameters: {}, headers: {}, options: {})
  request path, method: :get, parameters: parameters, headers: headers, options: options
end

#log(tag = self.class.name, message) ⇒ Object



99
100
101
# File 'lib/elrond/api/node/client.rb', line 99

def log(tag = self.class.name, message)
  puts "[#{tag}] - #{Time.now}: #{message}" if self.configuration.verbose
end

#patch(path, parameters: {}, data: {}, headers: {}, options: {}) ⇒ Object



58
59
60
# File 'lib/elrond/api/node/client.rb', line 58

def patch(path, parameters: {}, data: {}, headers: {}, options: {})
  request path, method: :patch, parameters: parameters, data: data, headers: headers, options: options
end

#post(path, parameters: {}, data: {}, headers: {}, options: {}) ⇒ Object



50
51
52
# File 'lib/elrond/api/node/client.rb', line 50

def post(path, parameters: {}, data: {}, headers: {}, options: {})
  request path, method: :post, parameters: parameters, data: data, headers: headers, options: options
end

#put(path, parameters: {}, data: {}, headers: {}, options: {}) ⇒ Object



54
55
56
# File 'lib/elrond/api/node/client.rb', line 54

def put(path, parameters: {}, data: {}, headers: {}, options: {})
  request path, method: :put, parameters: parameters, data: data, headers: headers, options: options
end

#request(path, method: :get, parameters: {}, data: {}, headers: {}, options: {}, retries: 3) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/elrond/api/node/client.rb', line 66

def request(path, method: :get, parameters: {}, data: {}, headers: {}, options: {}, retries: 3)
  response                  =   nil

  response                  =   case method
    when :get
      self.connection.get do |request|
        request.url path
        request.headers     =   self.connection.headers.merge(headers)
        request.params      =   parameters if parameters && !parameters.empty?
      end
    when :head
      self.connection.head do |request|
        request.url path
        request.headers     =   self.connection.headers.merge(headers)
        request.params      =   parameters if parameters && !parameters.empty?
      end
    when :post, :put, :patch, :delete
      self.connection.send(method) do |request|
        request.url path
        request.headers     =   self.connection.headers.merge(headers)
        request.body        =   data if data && !data.empty?
        request.params      =   parameters if parameters && !parameters.empty?
      end
  end
      
  return response
end

#set_connectionObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/elrond/api/node/client.rb', line 19

def set_connection
  self.connection       =   ::Faraday.new(url) do |builder|
    builder.options[:timeout]         =   self.configuration.faraday.fetch(:timeout, nil) if self.configuration.faraday.fetch(:timeout, nil)
    builder.options[:open_timeout]    =   self.configuration.faraday.fetch(:open_timeout, nil) if self.configuration.faraday.fetch(:open_timeout, nil)
      
    builder.headers     =   self.headers if self.headers && !self.headers.empty?
  
    builder.request  :json
      
    builder.response :logger, ::Logger.new(STDOUT), bodies: true if self.configuration.verbose
    builder.response :json, content_type: /\bjson$/
      
    builder.use ::FaradayMiddleware::FollowRedirects, limit: 10
      
    if self.proxy
      builder.proxy     =   self.proxy
      log("Will use proxy: #{builder.proxy.inspect}")
    end
  
    builder.adapter self.configuration.faraday.fetch(:adapter, ::Faraday.default_adapter)
  end
end

#set_headersObject



94
95
96
97
# File 'lib/elrond/api/node/client.rb', line 94

def set_headers
  self.headers                =   {}
  self.headers["User-Agent"]  =   self.user_agent unless self.user_agent.to_s.empty?
end

#urlObject



42
43
44
# File 'lib/elrond/api/node/client.rb', line 42

def url
  "http://#{self.host}:#{self.port}"
end