Class: Btce::Bus

Inherits:
Object
  • Object
show all
Defined in:
lib/btce/bus.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_secret) ⇒ Bus

Returns a new instance of Bus.



25
26
27
28
29
# File 'lib/btce/bus.rb', line 25

def initialize(api_key, api_secret)
  @api_key = api_key
  @api_secret = api_secret
  @nonce = 0
end

Instance Method Details

#request(data) ⇒ Object



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/btce/bus.rb', line 31

def request(data)
  # Timestamp for the request
  data[:nonce] = Time::now.to_i + @nonce
  @nonce += 1

  # Form parametrized string
  postdata = data.collect do |key, value|
    "#{key}=#{CGI::escape(value.to_s)}"
  end.join('&')

  # Open HTTPS connection
  Net::HTTP.start(Btce::Api::HOST, Btce::Api::PORT, :use_ssl => true) do |http|
    # Headers setup
    headers = {
        'Sign' => sign(postdata),
        'Key' => @api_key,
        'User-Agent' => Btce::Api::AGENT
    }

    # Send signed data
    return http.post(Btce::Api::API_URL, postdata, headers).body
  end
end

#sign(data) ⇒ Object



55
56
57
58
59
# File 'lib/btce/bus.rb', line 55

def sign(data)
  # Sign data with the HMAC SHA-512 algorhythm
  # Read https://btc-e.com/defines/documentation
  OpenSSL::HMAC.hexdigest(Btce::Api::HMAC_SHA, @api_secret, data)
end