Class: BitcoinRpc::Client
- Inherits:
-
Object
- Object
- BitcoinRpc::Client
- Defined in:
- lib/bitcoin_rpc.rb
Defined Under Namespace
Classes: JsonRpcError
Instance Method Summary collapse
- #http_post_request(post_body) ⇒ Object
-
#initialize(service_url) ⇒ Client
constructor
A new instance of Client.
- #method_missing(name, *args) ⇒ Object
Constructor Details
#initialize(service_url) ⇒ Client
Returns a new instance of Client.
8 9 10 |
# File 'lib/bitcoin_rpc.rb', line 8 def initialize(service_url) @uri = URI.parse(service_url) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/bitcoin_rpc.rb', line 12 def method_missing(name, *args) post_body = Oj.dump({method: name, params: args, id: 'jsonrpc'}, mode: :compat) raw_response = http_post_request(post_body) resp = Oj.load( raw_response, symbol_keys: true, bigdecimal_load: true) raise JsonRpcError, resp[:error] if resp[:error] resp[:result] end |
Instance Method Details
#http_post_request(post_body) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/bitcoin_rpc.rb', line 20 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 |