class Kucoin::Rest::Client

Attributes

configuration[RW]
url[RW]

Public Class Methods

new(configuration: ::Kucoin.configuration) click to toggle source
# File lib/kucoin/rest/client.rb, line 6
def initialize(configuration: ::Kucoin.configuration)
  self.configuration    =   configuration
  self.url              =   "#{self.configuration.api_url}/v#{self.configuration.api_version}"
end

Public Instance Methods

check_credentials!() click to toggle source
# File lib/kucoin/rest/client.rb, line 34
def check_credentials!
  unless configured?
    raise ::Kucoin::Errors::MissingConfigError.new("Kucoin gem hasn't been properly configured.")
  end
end
configured?() click to toggle source
# File lib/kucoin/rest/client.rb, line 30
def configured?
  !self.configuration.key.to_s.empty? && !self.configuration.secret.to_s.empty?
end
get(path, params: {}, options: {}) click to toggle source
# File lib/kucoin/rest/client.rb, line 53
def get(path, params: {}, options: {})
  request path, method: :get, params: params, options: options
end
parse(response) click to toggle source
# File lib/kucoin/rest/client.rb, line 48
def parse(response)
  error?(response)
  response
end
post(path, params: {}, data: {}, options: {}) click to toggle source
# File lib/kucoin/rest/client.rb, line 57
def post(path, params: {}, data: {}, options: {})
  request path, method: :post, params: params, data: data, options: options
end
request(path, method: :get, params: {}, data: {}, options: {}) click to toggle source
# File lib/kucoin/rest/client.rb, line 61
def request(path, method: :get, params: {}, data: {}, options: {})
  should_auth   =   options.fetch(:authenticate, false)
  user_agent    =   options.fetch(:user_agent, self.configuration.faraday.fetch(:user_agent, nil))
  proxy         =   options.fetch(:proxy, nil)
    
  connection    =   Faraday.new(url: to_uri(path)) do |builder|
    builder.headers["User-Agent"]       =   user_agent if !user_agent.to_s.empty?
    builder.headers["Accept-Language"]  =   "en_EN"
    
    builder.headers.merge!(authenticate!(path, params)) if should_auth && method.eql?(:get)
    builder.headers.merge!(authenticate!(path, data))   if should_auth && method.eql?(:post)
    
    builder.request  :url_encoded if method.eql?(:post)
    builder.response :logger      if self.configuration.verbose_faraday?
    builder.response :json

    if proxy
      puts "[Kucoin::Rest::Client] - Will connect to Kucoin using proxy: #{proxy.inspect}" if self.configuration.verbose_faraday?
      builder.proxy = proxy
    end

    builder.adapter self.configuration.faraday.fetch(:adapter, :net_http)
  end
    
  response = case method
    when :get
      connection.get do |request|
        request.params  =   params if params && !params.empty?
      end&.body
    when :post
      connection.post do |request|
        request.body    =   data
        request.params  =   params if params && !params.empty?
      end&.body
  end
  
  parse(response)
end
signature_path(path) click to toggle source
# File lib/kucoin/rest/client.rb, line 44
def signature_path(path)
  "/v#{self.configuration.api_version}#{path}"
end
to_uri(path) click to toggle source
# File lib/kucoin/rest/client.rb, line 40
def to_uri(path)
  "#{self.url}#{path}"
end