Class: Bitmex::Rest
Overview
REST API support www.bitmex.com/api/explorer/
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
logger ::Logger.new(STDOUT), :debug, :curl.
-
#api_secret ⇒ Object
readonly
logger ::Logger.new(STDOUT), :debug, :curl.
-
#host ⇒ Object
readonly
logger ::Logger.new(STDOUT), :debug, :curl.
Instance Method Summary collapse
- #base_path(resource, action = '') ⇒ Object
- #delete(path, params: {}, auth: true, json: true) ⇒ Object
- #domain_url ⇒ Object
-
#get(path, params: {}, auth: false) {|HTTParty::Response| ... } ⇒ Hash, Array
Execute GET request.
- #headers(verb, path, body, query) ⇒ Object
-
#initialize(host, api_key: nil, api_secret: nil) ⇒ Bitmex::Rest
constructor
Create new rest instance.
- #post(path, params: {}, auth: true, json: true) ⇒ Object
- #put(path, params: {}, auth: true, json: true) ⇒ Object
- #response_handler(response) ⇒ Object
- #rest_headers(verb, path, body, query, json: true) ⇒ Object
Constructor Details
#initialize(host, api_key: nil, api_secret: nil) ⇒ Bitmex::Rest
Create new rest instance
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_key ⇒ Object (readonly)
logger ::Logger.new(STDOUT), :debug, :curl
8 9 10 |
# File 'lib/bitmex/rest.rb', line 8 def api_key @api_key end |
#api_secret ⇒ Object (readonly)
logger ::Logger.new(STDOUT), :debug, :curl
8 9 10 |
# File 'lib/bitmex/rest.rb', line 8 def api_secret @api_secret end |
#host ⇒ Object (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) = {} [:body] = body [:headers] = rest_headers 'DELETE', path, body, nil, json: json if auth response = self.class.delete "#{domain_url}#{path}", block_given? ? yield(response) : response_handler(response) end |
#domain_url ⇒ Object
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
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?('/') = {} [:query] = params unless params.empty? [:headers] = rest_headers 'GET', path, '', params if auth response = self.class.get "#{domain_url}#{path}", 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) = {} [:body] = body [:headers] = rest_headers 'POST', path, body, nil, json: json if auth response = self.class.post "#{domain_url}#{path}", 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) = {} [:body] = body [:headers] = rest_headers 'PUT', path, body, nil, json: json if auth response = self.class.put "#{domain_url}#{path}", 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 |