Class: Deribit::API
- Inherits:
-
Object
- Object
- Deribit::API
- Defined in:
- lib/deribit/api.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
Returns the value of attribute key.
-
#secret ⇒ Object
Returns the value of attribute secret.
Instance Method Summary collapse
- #generate_signature(uri, params = {}) ⇒ Object
-
#initialize(key = nil, secret = nil, test_server: false) ⇒ API
constructor
A new instance of API.
- #is_error_response?(response) ⇒ Boolean
-
#method_missing(name, **params, &block) ⇒ Object
For direct calls like ‘api.get_account_summary` Trying to find API method in Deribit::REST_METHODS.
- #process(response) ⇒ Object
- #send(route, params = {}) ⇒ Object
Constructor Details
#initialize(key = nil, secret = nil, test_server: false) ⇒ API
Returns a new instance of API.
10 11 12 13 14 |
# File 'lib/deribit/api.rb', line 10 def initialize(key = nil, secret = nil, test_server: false) @key = key @secret = secret @server = set_server_uri(test_server) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, **params, &block) ⇒ Object
For direct calls like ‘api.get_account_summary` Trying to find API method in Deribit::REST_METHODS
18 19 20 21 |
# File 'lib/deribit/api.rb', line 18 def method_missing(name, **params, &block) method = Deribit.find_method(name, params) send(method[:path], params) end |
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
8 9 10 |
# File 'lib/deribit/api.rb', line 8 def key @key end |
#secret ⇒ Object
Returns the value of attribute secret.
8 9 10 |
# File 'lib/deribit/api.rb', line 8 def secret @secret end |
Instance Method Details
#generate_signature(uri, params = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/deribit/api.rb', line 52 def generate_signature(uri, params = {}) = (Time.now.utc.to_f * 1000).to_i nonce = rand(100000000) http_method = "GET" path = uri.path path << "?" << uri.query if uri.query body = "" data = [, nonce, http_method, path, body, ""].join("\n") sig = OpenSSL::HMAC.hexdigest("SHA256", secret, data) { signature: sig, header: "deri-hmac-sha256 id=#{key},ts=#{},sig=#{sig},nonce=#{nonce}", } end |
#is_error_response?(response) ⇒ Boolean
68 69 70 |
# File 'lib/deribit/api.rb', line 68 def is_error_response?(response) response.code.to_i.yield_self { |code| code == 0 || code >= 400 } end |
#process(response) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/deribit/api.rb', line 38 def process(response) json = JSON.parse(response.body, symbolize_names: true) if json.include?(:error) raise Error.new(message: "Failed for #{key}. " + json[:error]) elsif json.include?(:result) json[:result] elsif json.include?(:message) json[:message] else "ok" end end |
#send(route, params = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/deribit/api.rb', line 23 def send(route, params = {}) uri = URI(@server + route.to_s) response = get(uri, params) if is_error_response?(response) json = JSON.parse(response.body) rescue nil = "Failed for #{key}. " << json["error"].to_s if json raise Error.new(code: response.code, message: ) else process(response) end end |