Class: ClientRPC::Connect

Inherits:
Object
  • Object
show all
Defined in:
lib/rpc.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

DEFAULTS =
{
  host: "localhost",
  port: 18332,
# dev: true,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ Connect

Returns a new instance of Connect.



31
32
33
34
# File 'lib/rpc.rb', line 31

def initialize(host)
  @options = DEFAULTS.dup.merge(host.dup)
  @uri = @options[:uri] ? URI(@options[:uri]) : URI(uri_check())
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



23
24
25
# File 'lib/rpc.rb', line 23

def options
  @options
end

#sslObject

Returns the value of attribute ssl.



23
24
25
# File 'lib/rpc.rb', line 23

def ssl
  @ssl
end

#uriObject

Returns the value of attribute uri.



23
24
25
# File 'lib/rpc.rb', line 23

def uri
  @uri
end

Instance Method Details

#ping_nodeObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rpc.rb', line 53

def ping_node
  user = uri.user
  pass = uri.password
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if @ssl
  request = Net::HTTP::Get.new(uri.request_uri)
  request.basic_auth(user, pass)
  request.body = request_body("", nil)
  request["Content-Type"] = "application/json".freeze
  response = http.request(request)
  @pingres = (response.code).to_i.dup
end

#request(name, *args) ⇒ Object

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rpc.rb', line 36

def request(name, *args)
  puts "\n> #{name}: #{args.join(",")}" if options[:dev]
  response = request_http_post(name, args)
  puts "< #{response.code} #{response.message}" if options[:dev]
  raise Error, response.message unless (200...300).cover?(response.code.to_i)
  begin
    response = Oj.load(response.body, symbol_keys: true, bigdecimal_load: true)
  rescue StandardError => e
    puts "WARN < Failed to parse JSON response: #{e}" if options[:dev]
    raise
  end
  puts "\n> #{name}: #{args.join(",")}" if options[:dev]
  puts response[:result] if options[:dev]
  raise Error, response[:error] if response[:error]
  response[:result]
end