module Wcx

Constants

VERSION

Public Class Methods

method_missing(m, *args, &block) click to toggle source
# File lib/wcx.rb, line 30
def self.method_missing(m, *args, &block)      

        method_name = m.to_s
        params = get_params(args.first)
        self.api_call([method_name, args])

end
set_options(args = {}) click to toggle source
# File lib/wcx.rb, line 15
def self.set_options(args = {})
        # initialize BlockIo
        @api_key = args[:api_key]
        @secret = args[:secret]
        @request = args[:request]
        @args = args
        # @encryptionKey = Helper.pinToAesKey(@pin) if !@pin.nil?

        @conn_pool = ConnectionPool.new(size: 5, timeout: 300) { HTTPClient.new }

        @version = args[:version] || 1 # default version is 2

        # self.api_call(['Tickerprice',""],args)
end

Private Class Methods

api_call(endpoint) click to toggle source
# File lib/wcx.rb, line 40
def self.api_call(endpoint)

        body = nil

        @conn_pool.with do |hc|
        # prevent initiation of HTTPClients every time we make this call, use a connection_pool
        options = Hash[*endpoint[1]]
        # payload = Hash["request" => endpoint[0], "nonce" => ((Time.now.to_f * 1_000_000_000).to_i).to_s]
        payload = {}
        payload['request'] = endpoint[0]
        payload['nonce'] = ((Time.now.to_f * 1_000_000_000).to_i).to_s
        
        if !options.empty?
                
                payload.merge!(options)
        end
        
        payload_enc = Base64.encode64(payload.to_json).gsub(/\s/, '')
        digest = OpenSSL::Digest.new('sha384')
sig = OpenSSL::HMAC.hexdigest(digest, @secret, payload_enc)

          header = { "X-WCX-APIKEY" => @api_key, "X-WCX-PAYLOAD" => payload_enc, 'X-WCX-SIGNATURE' => sig}
          # puts header
          hc.ssl_config.ssl_version = :TLSv1
          response = hc.get("#{@base_url.gsub('API_CALL',endpoint[0]).gsub('VERSION', 'v'+@version.to_s)}", endpoint[1],header)
           # puts response.body
           body = JSON.parse(response.body)
            # puts 'rest'+body
          begin
            body = JSON.parse(response.body)

            raise Exception.new(body['result']) if !body['status'].eql?('0')
          rescue
            raise Exception.new('Unknown error occurred. Please report this.')
          end
        end

        return body
end
get_params(args = {}) click to toggle source
# File lib/wcx.rb, line 82
def self.get_params(args = {})
        # construct the parameter string
        params = ""
        args = {} if args.nil?

        args.each do |k,v|
          params += '&' if params.length > 0
          params += "#{k.to_s}=#{v.to_s}"
        end

        return params
end