Class: QuidaxBaseObject

Inherits:
Object
  • Object
show all
Defined in:
lib/quidax/objects/base.rb

Overview

Base object for HTTP requests

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(quidax_object) ⇒ QuidaxBaseObject

Returns a new instance of QuidaxBaseObject.

Raises:

  • (ArgumentError)


9
10
11
12
13
# File 'lib/quidax/objects/base.rb', line 9

def initialize(quidax_object)
  raise ArgumentError, "Quidax object cannot be nil!" if quidax_object.nil?

  @quidax = quidax_object
end

Instance Attribute Details

#quidaxObject (readonly)

Returns the value of attribute quidax.



7
8
9
# File 'lib/quidax/objects/base.rb', line 7

def quidax
  @quidax
end

Class Method Details

.get_request(q_object, path, params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/quidax/objects/base.rb', line 15

def self.get_request(q_object, path, params = {})
  result = nil
  begin
    response = Faraday.get(url(path), params, { "Authorization" => "Bearer #{q_object.secret_key}" })

    raise QuidaxServerError, response unless response.status == 200 || response.status == 201

    result = JSON.parse(response.body)
  rescue QuidaxServerError => e
    Utils.handle_server_error(e)
  rescue JSON::ParserError => e
    raise QuidaxServerError.new(response),
          "Invalid result data. Could not parse JSON response body \n #{e.message}"
  end
  result
end

.post_request(q_object, path, body = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/quidax/objects/base.rb', line 32

def self.post_request(q_object, path, body = {})
  result = nil

  begin
    response = Faraday.post(url(path), body.to_json,
                            { "Authorization" => "Bearer #{q_object.secret_key}", "Content-Type" => "application/json", "Accept" => "application/json" })
    raise QuidaxServerError, response unless response.status == 200 || response.status == 201

    result = JSON.parse(response.body)
  rescue QuidaxServerError => e
    Utils.handle_server_error(e)
  end
  result
end

.put_request(q_object, path, body = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/quidax/objects/base.rb', line 47

def self.put_request(q_object, path, body = {})
  response = Faraday.put("#{API::BASE_URL}#{path}", body,
                         { "Authorization" => "Bearer #{q_object.secret_key}" })
  raise QuidaxServerError, response unless response.status == 200 || response.status == 201

  JSON.parse(response.body)
rescue QuidaxServerError => e
  Utils.handle_server_error(e)
end

.url(path) ⇒ Object



57
58
59
# File 'lib/quidax/objects/base.rb', line 57

def self.url(path)
  "#{API::BASE_URL}#{path}"
end