class Silkroad::Client
Constants
- DEFAULT_RPC_PORT
- JSONRPC_VERSION
- TESTNET_RPC_PORT
Attributes
uri[R]
Public Class Methods
new(uri, opts={})
click to toggle source
# File lib/silkroad/client.rb, line 10 def initialize(uri, opts={}) @opts = opts @uri = URI uri raise Error, 'user and password required' unless @uri.user && @uri.password @uri.port = DEFAULT_RPC_PORT if @uri.port.nil? || (@uri.port == 80 && !uri.match(/:80/)) end
Public Instance Methods
batch(requests=nil, &block)
click to toggle source
# File lib/silkroad/client.rb, line 17 def batch(requests=nil, &block) requests ||= Batch.new(&block).requests requests.each {|r| r[:jsonrpc] = JSONRPC_VERSION unless r[:jsonrpc]} JSON.parse send(requests).body end
inspect()
click to toggle source
# File lib/silkroad/client.rb, line 56 def inspect "#<#{self.class} user=\"#{@user}\" @uri=\"#{@uri.to_s}\">" end
rpc(meth, *params)
click to toggle source
# File lib/silkroad/client.rb, line 23 def rpc(meth, *params) response = send jsonrpc: JSONRPC_VERSION, method: meth, params: params, id: 1 if response.code != '200' if response.body.nil? raise Error.new "bitcoind returned HTTP status #{response.status} with no body: #{response.http_header.reason_phrase}" else response_obj = JSON.parse response.body raise Error.new "bitcoind returned error code #{response_obj['error']['code']}: #{response_obj['error']['message']}" end else JSON.parse(response.body)['result'] end end
send(formdata)
click to toggle source
# File lib/silkroad/client.rb, line 38 def send(formdata) http = Net::HTTP.new(@uri.host, @uri.port) http.use_ssl = true if @uri.scheme == 'https' resp = http.start do req = Net::HTTP::Post.new '/' req.basic_auth @uri.user, @uri.password req.add_field 'Content-Type', 'application/json' req.body = formdata.to_json http.request req end if resp.code == '403' && resp.body.empty? raise Error, '403 Forbidden - check your user/pass and/or uri, and ensure IP is whitelisted for remote connections' end resp end