class Kucoin::Api::REST::Connection

Attributes

client[R]
endpoint[R]

Public Class Methods

new(endpoint, options={}) click to toggle source
# File lib/kucoin/api/rest/connection.rb, line 7
def initialize endpoint, options={}, &block
  @endpoint = endpoint
  @client = ::Faraday.new(options, &block)
end

Public Instance Methods

ku_request(method, subpath, options = {}) click to toggle source
# File lib/kucoin/api/rest/connection.rb, line 12
def ku_request method, subpath, options = {}
  response = client.public_send(method) do |req|

    # substitute path parameters and remove from options hash
    endpoint_url = endpoint.url(subpath).dup
    options.each do |option, value|
      path_param = /:#{option}/
      if endpoint_url.match? path_param
        options.delete(option)
        endpoint_url.gsub!(path_param, value.to_s)
      end
    end

    req.url endpoint_url

    # parameters go into request body, not headers on POSTs
    if method == :post
      req.body = options
    else
      req.params.merge!(options)
    end
  end
  success_or_error response
end

Private Instance Methods

success_or_error(response) click to toggle source
# File lib/kucoin/api/rest/connection.rb, line 39
def success_or_error response
  body = response.body.is_a?(Hash) ? response.body : JSON.parse(response.body)
  return body['data'] if body['code'].to_i == 200000
  return body if response.status == 200
  raise Kucoin::Api::ClientError.new("#{body["code"] || body["status"]} - #{body["msg"] || body["message"]}")
rescue => e
  raise Kucoin::Api::ClientError.new("#{e.message}\n#{response.body}")
end