Class: SVBClient

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

Defined Under Namespace

Classes: ACH, ACHHandler, Onboarding

Instance Method Summary collapse

Constructor Details

#initialize(api_key, hmac, 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, base_url: 'https://api.svb.com')
  @API_KEY = api_key
  @HMAC_SECRET = hmac
  @BASE_URL = base_url
end

Instance Method Details

#delete(path) ⇒ Object



42
43
44
45
# File 'lib/svbclient.rb', line 42

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

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



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

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
# File 'lib/svbclient.rb', line 30

def headers(method, path, query, body)
  mytimestamp = Time.now.to_i.to_s
  signature = signature(mytimestamp, method, path, query, body)

  {
    "X-Timestamp": mytimestamp,
    "X-Signature": signature,
    "Authorization": "Bearer " + @API_KEY,
    "Content-Type": "application/json"
  }
end

#patch(path, jsonbody) ⇒ Object



52
53
54
55
56
# File 'lib/svbclient.rb', line 52

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



58
59
60
61
62
# File 'lib/svbclient.rb', line 58

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



64
65
66
67
68
69
70
71
72
# File 'lib/svbclient.rb', line 64

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