class Btcmrb::Client

Constants

BASE_URI
BTCM_ACCESS_KEY
BTCM_SECRET_KEY
DC_BTCM_ACCESS_KEY
DC_BTCM_SECRET_KEY

Public Class Methods

new() click to toggle source
# File lib/btcmrb/client.rb, line 15
def initialize
end

Public Instance Methods

send_request(object) click to toggle source
# File lib/btcmrb/client.rb, line 18
def send_request(object)
  if object[:auth] == false
    # Hooray! We can write less code, authentication is not necessary.
    response = send(object[:uri])
  else
    # Extra work is necessary, we need to prepare and sign the request
    timestamp = create_timestamp
    options = {
      :uri => object[:uri],
      :timestamp => timestamp,
      :body => object[:body]
    }
    request = format_request(options)
    signature = sign_request(request)
    encoded_signature = Base64.encode64(signature).to_s.gsub("\n",'')

    send(object[:uri],
      { :method => object[:verb],
        :authentication => object[:auth],
        :timestamp => timestamp,
        :encoded_signature => encoded_signature,
        :body => object[:body]
    })
  end

end

Private Instance Methods

create_timestamp() click to toggle source
# File lib/btcmrb/client.rb, line 47
def create_timestamp
  DateTime.now.strftime('%Q')
end
format_request(options) click to toggle source
# File lib/btcmrb/client.rb, line 51
def format_request(options)
  default = options[:uri] + "\n" + options[:timestamp] + "\n"
  unless options[:body].empty?
    default = default + options[:body].to_json.to_s
  end
  default
end
send(uri, options={}) click to toggle source
# File lib/btcmrb/client.rb, line 59
def send(uri, options={})
  response = nil
  headers = {
    'Accept-Charset' => 'UTF-8',
    'Accept' => 'application/json',
    'Content-Type' => 'application/json',
    'User-Agent' => "BTCMRB Ruby Gem v#{Btcmrb::VERSION}"
  }

  if options[:authentication]
    headers.merge!(
    { 'apikey' => BTCM_ACCESS_KEY ,
      'signature' => options[:encoded_signature],
      'timestamp' => options[:timestamp]
      }).to_json
  end

  if options[:authentication] && options[:method] == "POST"
    HTTParty.post(BASE_URI + uri, :headers => headers, :body => options[:body].to_json)
  elsif options[:authentication] && options[:method] == "GET"
    HTTParty.get(BASE_URI + uri, :headers => headers)
  else
    HTTParty.get(BASE_URI + uri)
  end
end
sign_request(str) click to toggle source
# File lib/btcmrb/client.rb, line 85
def sign_request(str)
  OpenSSL::HMAC.digest('sha512', DC_BTCM_SECRET_KEY, str)
end