class SVBClient

Public Class Methods

new(api_key, hmac = nil, base_url: 'https://api.svb.com') click to toggle source
# File lib/svbclient.rb, line 19
def initialize(api_key, hmac = nil, base_url: 'https://api.svb.com')
  @API_KEY = api_key
  @HMAC_SECRET = hmac
  @BASE_URL = base_url
end

Public Instance Methods

delete(path) click to toggle source
# File lib/svbclient.rb, line 46
def delete(path)
  hs = headers('DELETE', path, '', '')
  RestClient.delete(@BASE_URL + path, headers=hs)
end
get(path, query="") click to toggle source
# File lib/svbclient.rb, line 51
def get(path, query="")
  hs = headers('GET', path, query, '')
  RestClient.get(@BASE_URL + path + '?' + query, headers=hs)
end
headers(method, path, query, body) click to toggle source
# File lib/svbclient.rb, line 30
def headers(method, path, query, body)
  hs = {
    "Authorization": "Bearer " + @API_KEY,
    "Content-Type": "application/json"
  }

  if @HMAC_SECRET
    mytimestamp = Time.now.to_i.to_s
    signature = signature(mytimestamp, method, path, query, body)
    hs["X-Timestamp"] = mytimestamp
    hs["X-Signature"] = signature
  end

  hs
end
patch(path, jsonbody) click to toggle source
# File lib/svbclient.rb, line 56
def patch(path, jsonbody)
  jsonbody = { data: jsonbody } unless jsonbody.key? 'data'
  hs = headers('PATCH', path, '', jsonbody.to_json)
  RestClient.patch(@BASE_URL + path, jsonbody.to_json, headers=hs)
end
post(path, jsonbody) click to toggle source
# File lib/svbclient.rb, line 62
def post(path, jsonbody)
  jsonbody = { data: jsonbody } unless jsonbody.key? 'data'
  hs = headers('POST', path, '', jsonbody.to_json)
  RestClient.post(@BASE_URL + path, jsonbody.to_json, headers=hs)
end
signature(timestamp, method, path, query, body) click to toggle source
# File lib/svbclient.rb, line 25
def signature(timestamp, method, path, query, body)
  message = [timestamp, method, path, query, body].join("\n")
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), @HMAC_SECRET, message)
end
upload(path, filesrc, mimetype) click to toggle source
# File lib/svbclient.rb, line 68
def upload(path, filesrc, mimetype)
  mytimestamp = Time.now.to_i.to_s
  signature = signature(mytimestamp, 'POST', path, '', '')

  hs = headers('POST', path, '', '')
  hs["Content-Type"] = "multipart/form-data"

  RestClient.post(@BASE_URL + path, { :file => filesrc, :multipart => true, 'Content-Type': mimetype }, headers=hs)
end