Class: Bitmex::Rest

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/bitmex/rest.rb

Overview

REST API support www.bitmex.com/api/explorer/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, api_key: nil, api_secret: nil) ⇒ Bitmex::Rest

Create new rest instance

Parameters:

  • host (String)

    the underlying host to connect to

  • api_key (String) (defaults to: nil)

    the api key

  • api_secret (String) (defaults to: nil)

    the api secret



15
16
17
18
19
# File 'lib/bitmex/rest.rb', line 15

def initialize(host, api_key: nil, api_secret: nil)
  @host = host
  @api_key = api_key
  @api_secret = api_secret
end

Instance Attribute Details

#api_keyObject (readonly)

logger ::Logger.new(STDOUT), :debug, :curl



8
9
10
# File 'lib/bitmex/rest.rb', line 8

def api_key
  @api_key
end

#api_secretObject (readonly)

logger ::Logger.new(STDOUT), :debug, :curl



8
9
10
# File 'lib/bitmex/rest.rb', line 8

def api_secret
  @api_secret
end

#hostObject (readonly)

logger ::Logger.new(STDOUT), :debug, :curl



8
9
10
# File 'lib/bitmex/rest.rb', line 8

def host
  @host
end

Instance Method Details

#base_path(resource, action = '') ⇒ Object



71
72
73
# File 'lib/bitmex/rest.rb', line 71

def base_path(resource, action = '')
  "/api/v1/#{resource}/#{action}"
end

#delete(path, params: {}, auth: true, json: true) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/bitmex/rest.rb', line 60

def delete(path, params: {}, auth: true, json: true)
  body = json ? params.to_json.to_s : URI.encode_www_form(params)

  options = {}
  options[:body] = body
  options[:headers] = rest_headers 'DELETE', path, body, nil, json: json if auth

  response = self.class.delete "#{domain_url}#{path}", options
  block_given? ? yield(response) : response_handler(response)
end

#domain_urlObject



99
100
101
# File 'lib/bitmex/rest.rb', line 99

def domain_url
  "https://#{host}"
end

#get(path, params: {}, auth: false) {|HTTParty::Response| ... } ⇒ Hash, Array

Execute GET request

Parameters:

  • path (String)

    either absolute or relative URI path

  • params (Hash) (defaults to: {})

    extra parameters to pass to GET request

  • auth (Boolean) (defaults to: false)

    if the request needs authentication

Yields:

  • (HTTParty::Response)

    the underlying response

Returns:

  • (Hash, Array)

    response wrapped in either array or hash



27
28
29
30
31
32
33
34
35
36
# File 'lib/bitmex/rest.rb', line 27

def get(path, params: {}, auth: false, &ablock)
  path = base_path(path) unless path.to_s.start_with?('/')

  options = {}
  options[:query] = params unless params.empty?
  options[:headers] = rest_headers 'GET', path, '', params if auth

  response = self.class.get "#{domain_url}#{path}", options
  block_given? ? yield(response) : response_handler(response)
end

#headers(verb, path, body, query) ⇒ Object



85
86
87
# File 'lib/bitmex/rest.rb', line 85

def headers(verb, path, body, query)
  Bitmex.headers api_key, api_secret, verb, path, body, query
end

#post(path, params: {}, auth: true, json: true) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/bitmex/rest.rb', line 49

def post(path, params: {}, auth: true, json: true)
  body = json ? params.to_json.to_s : URI.encode_www_form(params)

  options = {}
  options[:body] = body
  options[:headers] = rest_headers 'POST', path, body, nil, json: json if auth

  response = self.class.post "#{domain_url}#{path}", options
  block_given? ? yield(response) : response_handler(response)
end

#put(path, params: {}, auth: true, json: true) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/bitmex/rest.rb', line 38

def put(path, params: {}, auth: true, json: true)
  body = json ? params.to_json.to_s : URI.encode_www_form(params)

  options = {}
  options[:body] = body
  options[:headers] = rest_headers 'PUT', path, body, nil, json: json if auth

  response = self.class.put "#{domain_url}#{path}", options
  block_given? ? yield(response) : response_handler(response)
end

#response_handler(response) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/bitmex/rest.rb', line 89

def response_handler(response)
  raise Bitmex::ForbiddenError, response.body unless response.success?

  if response.parsed_response.is_a? Array
    response.to_a.map { |s| Bitmex::Mash.new s }
  else
    Bitmex::Mash.new response
  end
end

#rest_headers(verb, path, body, query, json: true) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/bitmex/rest.rb', line 75

def rest_headers(verb, path, body, query, json: true)
  headers = headers verb, path, body, query
  if json
    headers['Content-Type'] = 'application/json'
  else
    headers['Content-Type'] = 'application/x-www-form-urlencoded'
  end
  headers
end