Skip to content

Commit c2b5916

Browse files
committed
refactor: uses rubocop for style
1 parent 067c217 commit c2b5916

30 files changed

+2255
-2746
lines changed

Gemfile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
# frozen_string_literal: true
22
source "https://rubygems.org"
33

4-
gem 'rest-client'
4+
gem 'rest-client'
5+
gem 'openssl'
6+
gem 'eventmachine'
7+
gem 'faye-websocket'
8+
9+
10+
group :test do
11+
gem 'test-unit'
12+
gem 'rubocop'
13+
end

Gemfile.lock

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,40 @@
11
GEM
22
remote: https://rubygems.org/
33
specs:
4-
domain_name (0.5.20190701)
5-
unf (>= 0.0.5, < 1.0.0)
4+
domain_name (0.6.20240107)
5+
eventmachine (1.2.7)
6+
faye-websocket (0.11.3)
7+
eventmachine (>= 0.12.0)
8+
websocket-driver (>= 0.5.1)
69
http-accept (1.7.0)
7-
http-cookie (1.0.4)
10+
http-cookie (1.0.5)
811
domain_name (~> 0.5)
9-
mime-types (3.4.1)
12+
mime-types (3.5.2)
1013
mime-types-data (~> 3.2015)
11-
mime-types-data (3.2022.0105)
14+
mime-types-data (3.2024.0206)
1215
netrc (0.11.0)
16+
openssl (3.2.0)
17+
power_assert (2.0.3)
1318
rest-client (2.1.0)
1419
http-accept (>= 1.7.0, < 2.0)
1520
http-cookie (>= 1.0.2, < 2.0)
1621
mime-types (>= 1.16, < 4.0)
1722
netrc (~> 0.8)
18-
unf (0.1.4)
19-
unf_ext
20-
unf_ext (0.0.8)
23+
test-unit (3.6.2)
24+
power_assert
25+
websocket-driver (0.7.6)
26+
websocket-extensions (>= 0.1.0)
27+
websocket-extensions (0.1.5)
2128

2229
PLATFORMS
2330
x86_64-linux
2431

2532
DEPENDENCIES
33+
eventmachine
34+
faye-websocket
35+
openssl
2636
rest-client
37+
test-unit
2738

2839
BUNDLED WITH
29-
2.3.8
40+
2.3.5
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
require 'net/http'
2+
require 'uri'
3+
require 'json'
4+
require 'base64'
5+
require 'rest-client'
6+
require 'date'
7+
require_relative 'exceptions'
8+
9+
module Cryptomarket
10+
# Builds a credential used by the cryptomarket server
11+
class CredentialFactory
12+
def initialize(api_key:, api_secret:, window: nil)
13+
@api_key = api_key
14+
@api_secret = api_secret
15+
@window = window
16+
end
17+
18+
def get_credential(http_method, method, params)
19+
timestamp = DateTime.now.strftime('%Q')
20+
msg = build_credential_message(http_method, method, timestamp, params)
21+
digest = OpenSSL::Digest.new 'sha256'
22+
signature = OpenSSL::HMAC.hexdigest digest, @api_secret, msg
23+
signed = "#{@api_key}:#{signature}:#{timestamp}"
24+
signed += ":#{@window}" unless @window.nil?
25+
encoded = Base64.encode64(signed).delete "\n"
26+
"HS256 #{encoded}"
27+
end
28+
29+
def build_credential_message(http_method, method, timestamp, params)
30+
msg = http_method + @@api_version + method
31+
msg += if http_method.upcase == 'POST'
32+
params
33+
else
34+
not_post_params(http_method, params)
35+
end
36+
msg += timestamp
37+
msg += @window unless @window.nil?
38+
msg
39+
end
40+
41+
def not_post_params(http_method, params)
42+
msg = ''
43+
if !params.nil? && params.keys.any?
44+
msg += '?' if http_method.upcase == 'GET'
45+
msg += URI.encode_www_form(params)
46+
end
47+
msg
48+
end
49+
end
50+
end

0 commit comments

Comments
 (0)