Class: Bitex::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/bitex/api.rb

Overview

Documentation here!

Class Method Summary collapse

Class Method Details

.curl(verb, path, options = {}, files = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bitex/api.rb', line 23

def self.curl(verb, path, options = {}, files = {})
  verb = verb.upcase.to_sym
  query = verb == :GET ? "?#{options.to_query}" : ''
  prefix = Bitex.sandbox ? 'sandbox.' : ''

  curl = grab_curl
  curl.url = "https://#{prefix}bitex.la/api-v1/rest#{path}#{query}"

  if verb == :POST
    fields = []
    unless files.empty?
      fields += files.map { |k, v| Curl::PostField.file(k.to_s, v) }
      curl.multipart_form_post = true
    end
    fields += options.map do |k, v|
      next unless v
      Curl::PostField.content(k.to_s, v)
    end.compact
    curl.send("http_#{verb.downcase}", *fields)
  else
    curl.put_data = options.to_query if verb == :PUT
    curl.http(verb)
  end
  code = curl.response_code

  unless [200, 201, 202].include?(code)
    raise ApiError, "Got #{code} fetching #{path} with #{options}\n\n#{curl.head}\n\n#{curl.body}"
  end

  curl
end

.deserialize(object) ⇒ Object

Deserialize a single object from a json representation as specified on the bitex API class reference



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bitex/api.rb', line 68

def self.deserialize(object)
  {
    1 => Bid,
    2 => Ask,
    3 => Buy,
    4 => Sell,
    5 => SpecieDeposit,
    6 => SpecieWithdrawal,
    7 => UsdDeposit,
    8 => UsdWithdrawal
  }[object[0]].from_json(object)
end

.grab_curlObject



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/bitex/api.rb', line 9

def self.grab_curl
  if @curl
    @curl.reset
  else
    @curl = Curl::Easy.new
  end

  @curl.ssl_version = Curl::CURL_SSLVERSION_TLSv1
  @curl.on_debug { |t, d| puts "DEBUG SSL #{t}, #{d}" if d.to_s.size < 300 } if Bitex.debug
  @curl.connect_timeout = 30
  @curl.timeout = 30
  @curl
end

.private(verb, path, options = {}, files = {}) ⇒ Object

Raises:

  • (StandardError)


60
61
62
63
64
# File 'lib/bitex/api.rb', line 60

def self.private(verb, path, options = {}, files = {})
  raise StandardError, 'No api_key available to make private key calls' if Bitex.api_key.nil?
  response = curl(verb, path, options.merge(api_key: Bitex.api_key), files)
  JSON.parse(response.body)
end

.public(path, _options = {}) ⇒ Object



55
56
57
58
# File 'lib/bitex/api.rb', line 55

def self.public(path, _options = {})
  response = curl(:GET, path)
  JSON.parse(response.body)
end