Module: Tradecow::Network
- Defined in:
- lib/tradecow/network.rb
Constant Summary collapse
- SPOT_URL =
Tradecow.spot_url
- SPOT_FUL_URL =
Tradecow.spot_ful_url
- FUTURE_URL =
Tradecow.future_url
- FUTURE_FUL_URL =
Tradecow.future_ful_url
Class Method Summary collapse
- .build_query(data) ⇒ Object
- .dm_url(method, path, req_data = nil) ⇒ Object
- .hash_sort(hsh) ⇒ Object
- .rest_auth_token(method, host, path, req_data = nil) ⇒ Object
- .sign(sign_data) ⇒ Object
- .url(method, path, req_data = nil) ⇒ Object
Class Method Details
.build_query(data) ⇒ Object
12 13 14 15 16 |
# File 'lib/tradecow/network.rb', line 12 def self.build_query(data) data.map do |k, v| URI.encode_www_form_component(k) << '=' << URI.encode_www_form_component(v) end.join('&') end |
.dm_url(method, path, req_data = nil) ⇒ Object
45 46 47 48 49 |
# File 'lib/tradecow/network.rb', line 45 def self.dm_url method, path, req_data = nil h = Tradecow::Network.rest_auth_token(method, FUTURE_URL, path, req_data) params = Tradecow::Network.build_query(h) Tradecow.req_url(FUTURE_FUL_URL, path, params) end |
.hash_sort(hsh) ⇒ Object
18 19 20 |
# File 'lib/tradecow/network.rb', line 18 def self.hash_sort(hsh) Hash[hsh.sort_by { |k, _| k }] end |
.rest_auth_token(method, host, path, req_data = nil) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/tradecow/network.rb', line 26 def self.rest_auth_token(method, host, path, req_data = nil) params = { 'AccessKeyId' => Tradecow.access_key, 'SignatureMethod' => 'HmacSHA256', 'SignatureVersion' => 2, 'Timestamp' => Time.now.getutc.strftime('%Y-%m-%dT%H:%M:%S') } params = method == 'GET' && !req_data.nil? ? params.merge(req_data.stringify_keys) : params sign_data = "#{method}\n#{host}\n#{path}\n#{build_query(hash_sort(params))}" params = params.merge({ 'Signature' => sign(sign_data) }) params end |
.sign(sign_data) ⇒ Object
22 23 24 |
# File 'lib/tradecow/network.rb', line 22 def self.sign(sign_data) Base64.encode64(OpenSSL::HMAC.digest('sha256', Tradecow.secret_key, sign_data)).gsub("\n", '') end |
.url(method, path, req_data = nil) ⇒ Object
39 40 41 42 43 |
# File 'lib/tradecow/network.rb', line 39 def self.url method, path, req_data = nil h = Tradecow::Network.rest_auth_token(method, SPOT_URL, path, req_data) params = Tradecow::Network.build_query(h) Tradecow.req_url(SPOT_FUL_URL, path, params) end |