Class: Cryptsy::API2::Request

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

Class Method Summary collapse

Class Method Details

.nonceObject



58
59
60
# File 'lib/cryptsy/api2.rb', line 58

def self.nonce
  (Time.now.to_f * 1000).round
end

.send(path, query = {}, public_key = nil, private_key = nil, method = "GET") ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cryptsy/api2.rb', line 32

def self.send(path, query={}, public_key=nil, private_key=nil, method="GET")
  auth = !public_key.nil? && !private_key.nil?
  url = "https://api.cryptsy.com/api/v2/#{path}"
  query[:nonce] = nonce if auth
  # options = URI.encode_www_form(query) unless query.empty?

  headers = { "Sign" => sign(query, private_key), "Key" => public_key } if auth
  params = {}
  params[:headers] = headers unless headers.nil?
  params[:query] = query unless query.empty?
  case method
  when "POST"
    response = HTTParty.post(url, params)
  when "DELETE"
    response = HTTParty.delete(url, params)
  else
    response = HTTParty.get(url, params)
  end
  output = JSON.parse(response.body)
  return output['data'] if output['success']
  return output
rescue JSON::ParserError => e
  return response.body
end

.sign(query, private_key = nil) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/cryptsy/api2.rb', line 62

def self.sign(query, private_key=nil)
  OpenSSL::HMAC.hexdigest(
    OpenSSL::Digest.new('sha512'),
    private_key.encode('utf-8'),
    URI.encode( query.map{ |k,v| "#{k}=#{v}" }.join("&") )
  )
end