class Cryptsy::API2::Request

Public Class Methods

send(path, query={}, public_key=nil, private_key=nil, method="GET") click to toggle source
# 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"
    params[:body] = options
    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

Private Class Methods

nonce() click to toggle source
# File lib/cryptsy/api2.rb, line 59
def self.nonce
  (Time.now.to_f * 1000).round
end
sign(query, private_key=nil) click to toggle source
# File lib/cryptsy/api2.rb, line 63
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