Sha256: b974cc43cb9caa191e8ff8f263f805e5b85b1ee30557d0cffa34bf7c1c2440d8

Contents?: true

Size: 1.68 KB

Versions: 4

Compression:

Stored size: 1.68 KB

Contents

module Bitshares

  class Client

    def self.init
      @user = ENV['BITSHARES_ACCOUNT']
      @password = ENV['BITSHARES_PASSWORD']
      @@rpc_instance = Bitshares::Client::Rpc.new(@user, @password)
      return self
    end

    def self.rpc
      @@rpc_instance
    end

    def self.method_missing(method, *args)
      @@rpc_instance.request(method, args)
    end

    class Rpc

      class Err < RuntimeError; end

      def initialize(user, password)
        bitshares_running?
        @uri = URI("http://localhost:#{rpc_http_port}/rpc")
        @req = Net::HTTP::Post.new(@uri)
        @req.content_type = 'application/json'
        @req.basic_auth user, password
      end

      def request(m, args = [])
        resp = nil
        Net::HTTP.start(@uri.hostname, @uri.port) do |http|
          @req.body = { method: m, params: args, jsonrpc: '2.0', id: 0 }.to_json
          resp = http.request(@req)
        end
        raise Err, 'Bad credentials' if resp.class == Net::HTTPUnauthorized
        result = JSON.parse(resp.body)
        e = result['error']
        raise Err, JSON.pretty_generate(e), "#{m} #{args.join(' ') if args}" if e
        return result['result']
      end

      private

      def bitshares_running?
        raise Err, 'Server not running!' unless rpc_ports.count == 2
      end

      def rpc_http_port
        rpc_ports.each do |port| # only http RPC port raises a non-empty response
          return port unless `curl -s -I -L http://localhost:#{port}`.empty?
        end
      end

      def rpc_ports # returns bitshares HTTP JSON RPC and JSON RPC server ports
        `lsof -iTCP@localhost | grep bitshares`.scan(/:(\d+) \(LISTEN\)/).flatten
      end

    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
bitshares-0.1.3.pre lib/bitshares/client.rb
bitshares-0.1.2.pre lib/bitshares/client.rb
bitshares-0.1.1.pre lib/bitshares/client.rb
bitshares-0.1.0.pre lib/bitshares/client.rb