Class: Bitcoin::RPC::HttpServer
- Inherits:
-
EM::Connection
- Object
- EM::Connection
- Bitcoin::RPC::HttpServer
- Includes:
- RequestHandler, EM::HttpServer
- Defined in:
- lib/bitcoin/rpc/http_server.rb
Overview
Bitcoinrb RPC server.
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#node ⇒ Object
readonly
Returns the value of attribute node.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(node) ⇒ HttpServer
constructor
A new instance of HttpServer.
-
#parse_json_params ⇒ Array
parse request parameter.
- #post_init ⇒ Object
-
#process_http_request ⇒ Object
process http request.
Methods included from RequestHandler
#createwallet, #decoderawtransaction, #decodescript, #encryptwallet, #getblockchaininfo, #getblockheader, #getnewaddress, #getpeerinfo, #getwalletinfo, #listaccounts, #listwallets, #sendrawtransaction, #stop
Constructor Details
#initialize(node) ⇒ HttpServer
Returns a new instance of HttpServer.
15 16 17 18 |
# File 'lib/bitcoin/rpc/http_server.rb', line 15 def initialize(node) @node = node @logger = Bitcoin::Logger.create(:debug) end |
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
13 14 15 |
# File 'lib/bitcoin/rpc/http_server.rb', line 13 def logger @logger end |
#node ⇒ Object (readonly)
Returns the value of attribute node.
12 13 14 |
# File 'lib/bitcoin/rpc/http_server.rb', line 12 def node @node end |
Class Method Details
.run(node, port = 8332) ⇒ Object
25 26 27 |
# File 'lib/bitcoin/rpc/http_server.rb', line 25 def self.run(node, port = 8332) EM.start_server('0.0.0.0', port, HttpServer, node) end |
Instance Method Details
#parse_json_params ⇒ Array
parse request parameter.
57 58 59 60 |
# File 'lib/bitcoin/rpc/http_server.rb', line 57 def parse_json_params params = JSON.parse(@http_post_content) [params['method'], params['params']] end |
#post_init ⇒ Object
20 21 22 23 |
# File 'lib/bitcoin/rpc/http_server.rb', line 20 def post_init super logger.debug 'start http server.' end |
#process_http_request ⇒ Object
process http request.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/bitcoin/rpc/http_server.rb', line 30 def process_http_request operation = proc { command, args = parse_json_params logger.debug("process http request. command = #{command}") begin send(command, *args).to_json rescue Exception => e e end } callback = proc{ |result| response = EM::DelegatedHttpResponse.new(self) if result.is_a?(Exception) response.status = 500 response.content = result. else response.status = 200 response.content = result end response.content_type 'application/json' response.send_response } EM.defer(operation, callback) end |