Class: Quoinex::API

Inherits:
Object
  • Object
show all
Defined in:
lib/quoinex/quoinex.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, secret:, url: 'https://api.quoine.com') ⇒ API

Returns a new instance of API.



13
14
15
16
17
# File 'lib/quoinex/quoinex.rb', line 13

def initialize(key:, secret:, url: 'https://api.quoine.com')
  @key = key
  @secret = secret
  @url = url
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



9
10
11
# File 'lib/quoinex/quoinex.rb', line 9

def key
  @key
end

#secretObject (readonly)

Returns the value of attribute secret.



9
10
11
# File 'lib/quoinex/quoinex.rb', line 9

def secret
  @secret
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/quoinex/quoinex.rb', line 9

def url
  @url
end

Instance Method Details

#auth_headers(path) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/quoinex/quoinex.rb', line 130

def auth_headers(path)
  sign = signature(path)

  {
    'Content-Type' => 'application/json',
    'X-Quoine-API-Version' => 2,
    'X-Quoine-Auth' => sign,
  }
end

#balancesObject



23
24
25
# File 'lib/quoinex/quoinex.rb', line 23

def balances
  get('/accounts/balance')
end

#cancel_order(id) ⇒ Object



39
40
41
42
43
# File 'lib/quoinex/quoinex.rb', line 39

def cancel_order(id)
  put("/orders/#{id}/cancel")
rescue => e
  handle_error(e, Quoinex::CancelOrderException)
end

#create_order(side:, size:, price:, product_id:) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/quoinex/quoinex.rb', line 45

def create_order(side:, size:, price:, product_id:)
  opts = {
    order: {
      order_type: :limit,
      product_id: product_id,
      side: side,
      quantity: size.to_f.to_s,
      price: price.to_f.to_s,
    }
  }
  order = post('/orders', opts)

  if !order['id']
    error ||= order
    raise Quoinex::CreateOrderException, error
  end

  order
rescue => e
  handle_error(e, Quoinex::CreateOrderException)
end

#crypto_accountsObject



19
20
21
# File 'lib/quoinex/quoinex.rb', line 19

def crypto_accounts
  get('/crypto_accounts')
end

#delete(path, opts = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
# File 'lib/quoinex/quoinex.rb', line 120

def delete(path, opts = {})
  response = RestClient.delete("#{@url}#{path}", auth_headers(path))

  if !opts[:skip_json]
    JSON.parse(response.body)
  else
    response.body
  end
end

#get(path, opts = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/quoinex/quoinex.rb', line 86

def get(path, opts = {})
  uri = URI.parse("#{@url}#{path}")
  uri.query = URI.encode_www_form(opts[:params]) if opts[:params]

  response = RestClient.get(uri.to_s, auth_headers(uri.request_uri))

  if !opts[:skip_json]
    JSON.parse(response.body)
  else
    response.body
  end
end

#handle_error(e, exception) ⇒ Object

private

Raises:

  • (exception)


69
70
71
72
73
74
# File 'lib/quoinex/quoinex.rb', line 69

def handle_error(e, exception)
  error = (JSON.parse(e.http_body) rescue nil) if e.http_body
  error ||= e.message

  raise exception, error
end

#order(id) ⇒ Object



27
28
29
# File 'lib/quoinex/quoinex.rb', line 27

def order(id)
  get("/orders/#{id}")
end

#ordersObject



31
32
33
# File 'lib/quoinex/quoinex.rb', line 31

def orders
  get('/orders')
end

#post(path, payload, opts = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/quoinex/quoinex.rb', line 99

def post(path, payload, opts = {})
  data = JSON.unparse(payload)
  response = RestClient.post("#{@url}#{path}", data, auth_headers(path))

  if !opts[:skip_json]
    JSON.parse(response.body)
  else
    response.body
  end
end

#productsObject



35
36
37
# File 'lib/quoinex/quoinex.rb', line 35

def products
  get('/products')
end

#put(path, opts = {}) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/quoinex/quoinex.rb', line 110

def put(path, opts = {})
  response = RestClient.put("#{@url}#{path}", nil, auth_headers(path))

  if !opts[:skip_json]
    JSON.parse(response.body)
  else
    response.body
  end
end

#signature(path) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/quoinex/quoinex.rb', line 76

def signature(path)
  auth_payload = {
    path: path,
    nonce: DateTime.now.strftime('%Q'),
    token_id: @key
  }

  JWT.encode(auth_payload, @secret, 'HS256')
end