Class: Coindcx::Rest::Base

Inherits:
Config
  • Object
show all
Defined in:
lib/coindcx/rest/base.rb

Direct Known Subclasses

Client

Constant Summary

Constants inherited from Config

Config::BASE_URL, Config::ENDPOINTS

Instance Method Summary collapse

Instance Method Details

#endpoint(method, method_data, payload) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/coindcx/rest/base.rb', line 40

def endpoint method, method_data, payload
  if method_data["parameters"]["type"] == "url"
    endpoint = method_data["endpoint"]
    method_data["parameters"]["replace"].each do |key, value|
      endpoint.gsub!(key, payload[value])
    end
  elsif method_data["parameters"]["type"] == "query"
    query_string = URI.encode(payload.map{|k,v| "#{k}=#{v}"}.join("&"))
    endpoint = "#{method_data["endpoint"]}?#{query_string}"
  elsif method_data["parameters"]["type"] == "body"
    endpoint = method_data["endpoint"]
  end
  "#{BASE_URL}#{endpoint}"
end

#http_request(method, method_data, payload) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/coindcx/rest/base.rb', line 5

def http_request method, method_data, payload
  headers, params, endpoint = request_params(method, method_data, payload)
  uri = URI.parse(endpoint)
  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = true
  if method == "POST"
    request = Net::HTTP::Post.new(uri.path, headers)
    request.body = params.to_json
  elsif method == "PUT"
    request = Net::HTTP::Put.new(uri.path, headers)
    request.body = params.to_json
  else method == "GET"
    request = Net::HTTP::Get.new(uri.path, headers)
  end
  response = https.request(request)
  JSON.parse(response.body)
end

#request_params(method, method_data, payload) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/coindcx/rest/base.rb', line 23

def request_params method, method_data, payload
  if method_data["authentication"]
    signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), @secret, payload.to_json)
    headers = {
      'Content-Type' => 'application/json',
      'X-AUTH-APIKEY' => @key, 
      'X-AUTH-SIGNATURE' => signature
    }
    [headers, payload, endpoint(method, method_data, payload)]
  else
    headers = {
      'Content-Type' => 'application/json',
    }
    [headers, payload, endpoint(method, method_data, payload)]
  end
end