class BlockchainNode::Client

Constants

AuthToken
Details

Attributes

configuration[RW]
node_id[RW]

Public Class Methods

new(node_id = nil) click to toggle source
# File lib/blockchain-node/client.rb, line 12
def initialize(node_id = nil)
  @node_id = node_id
  @configuration = BlockchainNode::Configuration.new
end

Public Instance Methods

auth_token() click to toggle source
# File lib/blockchain-node/client.rb, line 27
def auth_token
  @@_auth_token ||= get_new_auth_token
  @@_auth_token = get_new_auth_token if auth_token_expired?
  @@_auth_token.token
end
details() click to toggle source
# File lib/blockchain-node/client.rb, line 22
def details
  r = request.get(path: nodes_path, auth_token: auth_token)
  Details.new(r["id"], r["blockchain"], r["network"], r["status"], Integer(r["height"] || 0))
end
nodes() click to toggle source

convenience method to get nodes index

# File lib/blockchain-node/client.rb, line 18
def nodes
  request.get(path: node_index_path, auth_token: auth_token)
end

Private Instance Methods

auth_token_expired?() click to toggle source
# File lib/blockchain-node/client.rb, line 41
def auth_token_expired?
  @@_auth_token.nil? || @@_auth_token.expires_at < Time.now.utc + 30
end
get_new_auth_token() click to toggle source
# File lib/blockchain-node/client.rb, line 45
def get_new_auth_token
  data = {
    grant_type: "client_credentials",
    client_id: configuration.client_id,
    client_secret: configuration.client_secret,
  }
  response = request.post(path: oauth_token_path, data: data)
  token = response["access_token"]
  expires_at = Time.at(response["created_at"] + response["expires_in"]).utc
  AuthToken.new(token, expires_at)
end
method_missing(method, *args, &block) click to toggle source

catch all other method calls and assume its the RPC method call

# File lib/blockchain-node/client.rb, line 36
def method_missing(method, *args, &block)
  data = { method: method, parameters: args }
  request.post(path: nodes_path, data: data, auth_token: auth_token)
end
node_index_path() click to toggle source
# File lib/blockchain-node/client.rb, line 65
def node_index_path
  "/api/nodes"
end
nodes_path() click to toggle source
# File lib/blockchain-node/client.rb, line 69
def nodes_path
  raise Errors::ClientNotConfigured.new("Client Needs to be initialized with a node id.") unless @node_id
  "/api/nodes/#{@node_id}"
end
oauth_token_path() click to toggle source
# File lib/blockchain-node/client.rb, line 61
def oauth_token_path
  "/oauth/token"
end
request() click to toggle source
# File lib/blockchain-node/client.rb, line 57
def request
  @request ||= BlockchainNode::Request.new(configuration.request_options)
end