Class: Kucoin::Api::REST::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/kucoin/api/rest/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, options = {}, &block) ⇒ Connection

Returns a new instance of Connection.



7
8
9
10
# File 'lib/kucoin/api/rest/connection.rb', line 7

def initialize endpoint, options={}, &block
  @endpoint = endpoint
  @client = ::Faraday.new(options, &block)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/kucoin/api/rest/connection.rb', line 6

def client
  @client
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



6
7
8
# File 'lib/kucoin/api/rest/connection.rb', line 6

def endpoint
  @endpoint
end

Instance Method Details

#ku_request(method, subpath, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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