Class: Ant::API

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, api_key, api_secret) ⇒ API

Returns a new instance of API.



16
17
18
19
20
# File 'lib/ant.rb', line 16

def initialize(username, api_key, api_secret)
  self.username = username
  self.api_key = api_key
  self.api_secret = api_secret
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



14
15
16
# File 'lib/ant.rb', line 14

def api_key
  @api_key
end

#api_secretObject

Returns the value of attribute api_secret.



14
15
16
# File 'lib/ant.rb', line 14

def api_secret
  @api_secret
end

#nonce_vObject

Returns the value of attribute nonce_v.



14
15
16
# File 'lib/ant.rb', line 14

def nonce_v
  @nonce_v
end

#usernameObject

Returns the value of attribute username.



14
15
16
# File 'lib/ant.rb', line 14

def username
  @username
end

Instance Method Details

#accountObject



39
40
41
# File 'lib/ant.rb', line 39

def 
  self.api_call('account.htm', {}, true)
end

#api_call(method, param = {}, priv = false, is_json = true) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ant.rb', line 22

def api_call(method, param = {}, priv = false, is_json = true)
  url = "https://www.antpool.com/api/#{ method }"
  if priv
    self.nonce
    param.merge!(:key => self.api_key, :signature => self.signature.to_s.upcase, :nonce => self.nonce_v)
  end
  answer = self.post(url, param)

  # unfortunately, the API does not always respond with JSON, so we must only
  # parse as JSON if is_json is true.
  if is_json
    JSON.parse(answer)
  else
    answer
  end
end

#hashrateObject



43
44
45
# File 'lib/ant.rb', line 43

def hashrate
  self.api_call('hashrate.htm', {}, true)
end

#nonceObject



47
48
49
# File 'lib/ant.rb', line 47

def nonce
  self.nonce_v = (Time.now.to_f * 1000000).to_i.to_s
end

#post(url, param) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/ant.rb', line 56

def post(url, param)
  # 由于服务器采用不安全openssl
  # uri = URI.parse(url)
  # https = Net::HTTP.new(uri.host, uri.port)
  # https.use_ssl = false
  # params = Addressable::URI.new
  # params.query_values = param
  # https.post(uri.path, params.query).body
  RestClient.post(url, param)
end

#signatureObject



51
52
53
54
# File 'lib/ant.rb', line 51

def signature
  str = self.username + self.api_key + self.nonce_v
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha256'), self.api_secret ,str)
end