Class: SVBClient

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

Defined Under Namespace

Classes: Onboarding

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of SVBClient.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/svbclient.rb', line 19

def initialize(api_key, hmac, enviro='', base_url='https://api.svb.com', company_id=nil, key_id=nil, partner_id=nil)
  @API_KEY = api_key
  @HMAC_SECRET = hmac
  if ['live', 'test'].include? enviro
    @ENVIRO = enviro
  else
    @ENVIRO = nil
  end
  @BASE_URL = base_url
  @COMPANY_ID = company_id
  @KEY_ID = key_id
  @PARTNER_ID = partner_id
end

Instance Method Details

#delete(path) ⇒ Object



55
56
57
58
# File 'lib/svbclient.rb', line 55

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

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



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

def get(path, query="")
  hs = headers('GET', path, query, '')
  RestClient.get(@BASE_URL + path + '?' + query, headers=hs)
end

#headers(method, path, query, body) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/svbclient.rb', line 38

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

  hs = {
    "X-Timestamp": mytimestamp,
    "X-Signature": signature,
    "Authorization": "Bearer " + @API_KEY,
    "Content-Type": "application/json"
  }
  hs["X-Environment"] = @ENVIRO unless @ENVIRO.nil?
  hs["X-Company-Id"] = @COMPANY_ID unless @COMPANY_ID.nil?
  hs["X-Key-Id"] = @KEY_ID unless @KEY_ID.nil?
  hs["X-Partner-Id"] = @PARTNER_ID unless @PARTNER_ID.nil?
  hs
end

#patch(path, jsonbody) ⇒ Object



65
66
67
68
69
# File 'lib/svbclient.rb', line 65

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



71
72
73
74
75
# File 'lib/svbclient.rb', line 71

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



33
34
35
36
# File 'lib/svbclient.rb', line 33

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



77
78
79
80
81
82
83
84
85
# File 'lib/svbclient.rb', line 77

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