Class: SVBClient

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

Defined Under Namespace

Classes: ACH, ACHHandler, Account, BookTransfer, BookTransferHandler, Onboarding, VirtualCard, VirtualCardHandler, Wire, WireHandler

Instance Method Summary collapse

Constructor Details

#initialize(api_key, hmac = nil, base_url: 'https://api.svb.com') ⇒ SVBClient

Returns a new instance of SVBClient.



19
20
21
22
23
# 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

Instance Method Details

#delete(path) ⇒ Object



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

def delete(path)
  hs = headers('DELETE', path, '', '')
  RestClient.delete(@BASE_URL + path, headers=hs)
end

#get(path, query = "") ⇒ Object



51
52
53
54
# 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) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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) ⇒ Object



56
57
58
59
60
# 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) ⇒ Object



62
63
64
65
66
# 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) ⇒ Object



25
26
27
28
# 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) ⇒ Object



68
69
70
71
72
73
74
75
76
# 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