lib/berycoin/gem.rb in berycoin-gem-0.1.1 vs lib/berycoin/gem.rb in berycoin-gem-0.1.2

- old
+ new

@@ -1,11 +1,34 @@ require "berycoin/gem/version" require 'net/http' require 'uri' require 'json' +class BerycoinRPC + def initialize(service_url) + @uri = URI.parse(service_url) + end + def method_missing(name, *args) + post_body = { 'method' => name, 'params' => args, 'id' => 'jsonrpc' }.to_json + resp = JSON.parse( http_post_request(post_body) ) + raise JSONRPCError, resp['error'] if resp['error'] + resp['result'] + end + + def http_post_request(post_body) + http = Net::HTTP.new(@uri.host, @uri.port) + request = Net::HTTP::Post.new(@uri.request_uri) + request.basic_auth @uri.user, @uri.password + request.content_type = 'application/json' + request.body = post_body + http.request(request).body + end + + class JSONRPCError < RuntimeError; end +end + module Berycoin RPCUSER = ENV['RPCUSER'] RPCPASS = ENV['RPCPASS'] RPCHOST = ENV['RPCHOST'] RPCPORT = ENV['RPCPORT'] @@ -323,30 +346,6 @@ end def Wallet.signmessage(berycoinaddress,message) H.signmessage(berycoinaddress,message) end end -end - -class BerycoinRPC - def initialize(service_url) - @uri = URI.parse(service_url) - end - - def method_missing(name, *args) - post_body = { 'method' => name, 'params' => args, 'id' => 'jsonrpc' }.to_json - resp = JSON.parse( http_post_request(post_body) ) - raise JSONRPCError, resp['error'] if resp['error'] - resp['result'] - end - - def http_post_request(post_body) - http = Net::HTTP.new(@uri.host, @uri.port) - request = Net::HTTP::Post.new(@uri.request_uri) - request.basic_auth @uri.user, @uri.password - request.content_type = 'application/json' - request.body = post_body - http.request(request).body - end - - class JSONRPCError < RuntimeError; end end \ No newline at end of file