berbix 0.0.7 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/berbix.rb +49 -47
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e6256df46bb179723f2707ce437068058b7422d493e4fbb6e882824d3de12c91
4
- data.tar.gz: e62c31fb105893010ac0814c876412b214f66f4eaf001afe594c797648c6da5b
3
+ metadata.gz: 5ea5395ef3e8e47cebb65f69c83faa5c6befc4cf1d47b78f758d46e7293b1263
4
+ data.tar.gz: 9f04b290e0b12d58d337185e5edb30df7b0372b2aa53702e59cec6eba518b284
5
5
  SHA512:
6
- metadata.gz: c2037dcff8958a90f5ddc79cb8d7ad292401a51834fb1a5e5237821ba984e5e6e3cb9963f57d7c7e9c89030838c6583d2dd9dc6c7b88d8e7b50d26d36f433f19
7
- data.tar.gz: d168dc65d635a55391186a1338f5f29d83f48d6bbef675235bcfbc7b1be0737153379cfe28c155acb056d4c14fbbc731397386b7570f63a5933babcb8d9fe515
6
+ metadata.gz: 4934d3f1bd936605c9105bdfb81b7f2d758df5fbefdaf003f1701a8fa92b316dbbc7ba99ee67433e96a4d3ccba1855bfa61e63c30ca00e8d20c9e39a91f37d8c
7
+ data.tar.gz: 388dd769a94142986e96288335016700e1126f8b7cb28a365aab9888a4e46e3f40fe734da397029cd7bba17a1f02abe5ba175f6b60d8e3f26bf4d4f2919b33be
data/lib/berbix.rb CHANGED
@@ -2,17 +2,25 @@ require 'net/https'
2
2
  require 'json'
3
3
 
4
4
  module Berbix
5
-
6
- SDK_VERSION = '0.0.7'
5
+ SDK_VERSION = '1.0.0'
7
6
  CLOCK_DRIFT = 300
8
7
 
9
8
  class HTTPClient
10
9
  def request(method, url, headers, opts={})
11
- raise 'subclass must implement request'
10
+ raise(Berbix::BerbixError, 'subclass must implement request')
12
11
  end
13
12
  end
14
13
 
15
14
  class NetHTTPClient < HTTPClient
15
+ attr_reader :read_timeout
16
+ attr_reader :open_timeout
17
+
18
+ def initialize(opts={})
19
+ # Sets the defaults to align with the Net::HTTP defaults
20
+ @open_timeout = opts[:open_timeout] || 60
21
+ @read_timeout = opts[:read_timeout] || 60
22
+ end
23
+
16
24
  def request(method, url, headers, opts={})
17
25
  uri = URI(url)
18
26
  klass = if method == :post
@@ -33,11 +41,13 @@ module Berbix
33
41
  end
34
42
  cli = Net::HTTP.new(uri.host, uri.port).tap do |http|
35
43
  http.use_ssl = true
44
+ http.read_timeout = read_timeout
45
+ http.open_timeout = open_timeout
36
46
  end
37
47
  res = cli.request(req)
38
48
  code = res.code.to_i
39
49
  if code < 200 || code >= 300
40
- raise 'unexpected status code returned'
50
+ raise(Berbix::BerbixError, "unexpected status code returned: #{code}")
41
51
  end
42
52
  if code == 204
43
53
  return
@@ -47,15 +57,15 @@ module Berbix
47
57
  end
48
58
 
49
59
  class Tokens
50
- attr_reader :access_token, :client_token, :refresh_token, :expiry, :transaction_id, :user_id
60
+ attr_reader :access_token, :client_token, :refresh_token, :expiry, :transaction_id, :user_id, :response
51
61
 
52
- def initialize(refresh_token, access_token=nil, client_token=nil, expiry=nil, transaction_id=nil)
62
+ def initialize(refresh_token, access_token=nil, client_token=nil, expiry=nil, transaction_id=nil, response=nil)
53
63
  @refresh_token = refresh_token
54
64
  @access_token = access_token
55
65
  @client_token = client_token
56
66
  @expiry = expiry
57
67
  @transaction_id = transaction_id
58
- @user_id = transaction_id
68
+ @response = response
59
69
  end
60
70
 
61
71
  def refresh!(access_token, client_token, expiry, transaction_id)
@@ -63,7 +73,6 @@ module Berbix
63
73
  @client_token = client_token
64
74
  @expiry = expiry
65
75
  @transaction_id = transaction_id
66
- @user_id = transaction_id
67
76
  end
68
77
 
69
78
  def needs_refresh?
@@ -75,6 +84,15 @@ module Berbix
75
84
  end
76
85
  end
77
86
 
87
+ class HostedTransactionResponse
88
+ attr_reader :tokens, :hosted_url
89
+
90
+ def initialize(tokens, hosted_url)
91
+ @tokens = tokens
92
+ @hosted_url = hosted_url
93
+ end
94
+ end
95
+
78
96
  class Client
79
97
  def initialize(opts={})
80
98
  @api_secret = opts[:api_secret] || opts[:client_secret]
@@ -82,7 +100,7 @@ module Berbix
82
100
  @http_client = opts[:http_client] || NetHTTPClient.new
83
101
 
84
102
  if @api_secret.nil?
85
- raise ':api_secret must be provided when instantiating Berbix client'
103
+ raise(Berbix::BerbixError, ':api_secret must be provided when instantiating Berbix client')
86
104
  end
87
105
  end
88
106
 
@@ -90,14 +108,24 @@ module Berbix
90
108
  payload = {}
91
109
  payload[:email] = opts[:email] unless opts[:email].nil?
92
110
  payload[:phone] = opts[:phone] unless opts[:phone].nil?
93
- payload[:customer_uid] = opts[:customer_uid].to_s unless opts[:customer_uid].nil?
94
- payload[:template_key] = opts[:template_key] unless opts[:template_key].nil?
111
+ if opts[:customer_uid].nil?
112
+ raise(Berbix::BerbixError, ':customer_uid must be provided when creating a transaction')
113
+ else
114
+ payload[:customer_uid] = opts[:customer_uid].to_s
115
+ end
116
+ if opts[:template_key].nil?
117
+ raise(Berbix::BerbixError, ':template_key must be provided when creating a transaction')
118
+ else
119
+ payload[:template_key] = opts[:template_key]
120
+ end
121
+ payload[:hosted_options] = opts[:hosted_options] unless opts[:hosted_options].nil?
95
122
  fetch_tokens('/v0/transactions', payload)
96
123
  end
97
124
 
98
- # This method is deprecated, please use create_transaction instead
99
- def create_user(opts={})
100
- create_transaction(opts)
125
+ def create_hosted_transaction(opts={})
126
+ opts[:hosted_options] = {} if opts[:hosted_options].nil?
127
+ tokens = create_transaction(opts)
128
+ HostedTransactionResponse.new(tokens, tokens.response["hosted_url"])
101
129
  end
102
130
 
103
131
  def refresh_tokens(tokens)
@@ -107,13 +135,6 @@ module Berbix
107
135
  })
108
136
  end
109
137
 
110
- def exchange_code(code)
111
- fetch_tokens('/v0/tokens', {
112
- 'code' => code,
113
- 'grant_type' => 'authorization_code',
114
- })
115
- end
116
-
117
138
  def fetch_transaction(tokens)
118
139
  token_auth_request(:get, tokens, '/v0/transactions')
119
140
  end
@@ -133,19 +154,10 @@ module Berbix
133
154
  payload = {}
134
155
  payload[:response_payload] = opts[:response_payload] unless opts[:response_payload].nil?
135
156
  payload[:flags] = opts[:flags] unless opts[:flags].nil?
157
+ payload[:override_fields] = opts[:override_fields] unless opts[:override_fields].nil?
136
158
  token_auth_request(:patch, tokens, '/v0/transactions/override', data: payload)
137
159
  end
138
160
 
139
- # This method is deprecated, please use fetch_transaction instead
140
- def fetch_user(tokens)
141
- fetch_transaction(tokens)
142
- end
143
-
144
- def create_continuation(tokens)
145
- result = token_auth_request(:post, tokens, '/v0/continuations')
146
- result['value']
147
- end
148
-
149
161
  def validate_signature(secret, body, header)
150
162
  parts = header.split(',')
151
163
  # Version (parts[0]) is currently unused
@@ -199,7 +211,8 @@ module Berbix
199
211
  result['access_token'],
200
212
  result['client_token'],
201
213
  Time.now + result['expires_in'],
202
- result['transaction_id'])
214
+ result['transaction_id'],
215
+ result)
203
216
  end
204
217
 
205
218
  def auth
@@ -207,22 +220,11 @@ module Berbix
207
220
  end
208
221
 
209
222
  def api_host(opts)
210
- unless opts[:api_host].nil?
211
- return opts[:api_host]
212
- end
213
-
214
- opts[:environment] ||= :production
215
- case opts[:environment]
216
- when :production
217
- return 'https://api.berbix.com'
218
- when :staging
219
- return 'https://api.staging.berbix.com'
220
- when :sandbox
221
- return 'https://api.sandbox.berbix.com'
222
- else
223
- raise 'invalid environment value specified';
224
- end
223
+ opts[:api_host].nil? ? 'https://api.berbix.com' : opts[:api_host]
225
224
  end
225
+
226
226
  end
227
227
 
228
+ class BerbixError < StandardError
229
+ end
228
230
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: berbix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Levine
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-05 00:00:00.000000000 Z
11
+ date: 2021-03-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Backend SDKs to interact with the Berbix Verify API endpoints.
14
14
  email: eric@berbix.com