berbix 0.0.1

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 +7 -0
  2. data/lib/berbix.rb +163 -0
  3. metadata +44 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: '09d316a9a15556d0c37596baad39ac5587081a11'
4
+ data.tar.gz: aca1d811cd0f1cba23f20faa2a08d73b8d2fea52
5
+ SHA512:
6
+ metadata.gz: 1f94b05dec6e3f05ac39d1b66b2f2a7f8f6ba48e928076122a9cf06d24780164465ace0b5c035be484cd4851a501cf25d11b932a046f784fce15912934f4f0fa
7
+ data.tar.gz: 6832284bdbe869d94290a72ccd2d7da14f9a2592bf41f299c89d0e61a610d7206780a5ce04afc4b4a7b8385ef56654150d7b8b7ccb13bd3484b261464b0e0dea
@@ -0,0 +1,163 @@
1
+ require 'net/https'
2
+ require 'json'
3
+
4
+ module Berbix
5
+
6
+ class HTTPClient
7
+ def request(method, url, headers, opts={})
8
+ raise 'subclass must implement request'
9
+ end
10
+ end
11
+
12
+ class NetHTTPClient < HTTPClient
13
+ def request(method, url, headers, opts={})
14
+ uri = URI(url)
15
+ klass = if method == :post
16
+ Net::HTTP::Post
17
+ else
18
+ Net::HTTP::Get
19
+ end
20
+ req = klass.new(uri.to_s, headers)
21
+ unless opts[:data].nil?
22
+ req.body = opts[:data].to_json
23
+ end
24
+ unless opts[:auth].nil?
25
+ req.basic_auth(opts[:auth][:user], opts[:auth][:pass])
26
+ end
27
+ cli = Net::HTTP.new(uri.host, uri.port).tap do |http|
28
+ http.use_ssl = true
29
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
30
+ end
31
+ res = cli.request(req)
32
+ code = res.code.to_i
33
+ if code < 200 || code >= 300
34
+ raise 'unexpected status code returned'
35
+ end
36
+ JSON.parse(res.body)
37
+ end
38
+ end
39
+
40
+ class UserTokens
41
+ attr_reader :access_token, :refresh_token, :expiry, :user_id
42
+
43
+ def initialize(refresh_token, access_token=nil, expiry=nil, user_id=nil)
44
+ @refresh_token = refresh_token
45
+ @access_token = access_token
46
+ @expiry = expiry
47
+ @user_id = user_id
48
+ end
49
+
50
+ def refresh!(access_token, expiry, user_id)
51
+ @access_token = access_token
52
+ @expiry = expiry
53
+ @user_id = user_id
54
+ end
55
+
56
+ def needs_refresh?
57
+ @access_token.nil? || @expiry.nil? || @expiry < Time.now
58
+ end
59
+ end
60
+
61
+ class Client
62
+ def initialize(opts={})
63
+ @client_id = opts[:client_id]
64
+ @client_secret = opts[:client_secret]
65
+ @api_host = api_host(opts)
66
+ @http_client = opts[:http_client] || NetHTTPClient.new
67
+
68
+ if @client_id.nil?
69
+ raise ':client_id must be provided when instantiating Berbix client'
70
+ end
71
+ if @client_secret.nil?
72
+ raise ':client_secret must be provided when instantiating Berbix client'
73
+ end
74
+ end
75
+
76
+ def create_user(opts={})
77
+ payload = {}
78
+ payload[:email] = opts[:email] unless opts[:email].nil?
79
+ payload[:phone] = opts[:phone] unless opts[:phone].nil?
80
+ payload[:customer_uid] = opts[:customer_uid] unless opts[:customer_uid].nil?
81
+ fetch_tokens('/v0/users', payload)
82
+ end
83
+
84
+ def refresh_tokens(user_tokens)
85
+ fetch_tokens('/v0/tokens', {
86
+ 'refresh_token' => user_tokens.refresh_token,
87
+ 'grant_type' => 'refresh_token',
88
+ })
89
+ end
90
+
91
+ def exchange_code(code)
92
+ fetch_tokens('/v0/tokens', {
93
+ 'code' => code,
94
+ 'grant_type' => 'authorization_code',
95
+ })
96
+ end
97
+
98
+ def fetch_user(user_tokens)
99
+ token_auth_request(:get, user_tokens, '/v0/users')
100
+ end
101
+
102
+ def create_continuation(user_tokens)
103
+ result = token_auth_request(:post, user_tokens, '/v0/continuations')
104
+ result['value']
105
+ end
106
+
107
+ private
108
+
109
+ def refresh_if_necessary!(user_tokens)
110
+ if user_tokens.needs_refresh?
111
+ refreshed = refresh_tokens(user_tokens)
112
+ user_tokens.refresh!(refreshed.access_token, refreshed.expiry, refreshed.user_id)
113
+ end
114
+ end
115
+
116
+ def token_auth_request(method, user_tokens, path)
117
+ refresh_if_necessary!(user_tokens)
118
+ headers = {
119
+ 'Authorization' => 'Bearer ' + user_tokens.access_token,
120
+ 'Content-Type' => 'application/json',
121
+ }
122
+ @http_client.request(method, @api_host + path, headers)
123
+ end
124
+
125
+ def fetch_tokens(path, payload)
126
+ headers = { 'Content-Type' => 'application/json' }
127
+ result = @http_client.request(
128
+ :post,
129
+ @api_host + path,
130
+ headers,
131
+ data: payload,
132
+ auth: auth())
133
+ UserTokens.new(
134
+ result['refresh_token'],
135
+ result['access_token'],
136
+ Time.now + result['expires_in'],
137
+ result['user_id'])
138
+ end
139
+
140
+ def auth
141
+ { user: @client_id, pass: @client_secret }
142
+ end
143
+
144
+ def api_host(opts)
145
+ unless opts[:api_host].nil?
146
+ return opts[:api_host]
147
+ end
148
+
149
+ opts[:environment] ||= :production
150
+ case opts[:environment]
151
+ when :production
152
+ return 'https://api.berbix.com'
153
+ when :staging
154
+ return 'https://api.staging.berbix.com'
155
+ when :sandbox
156
+ return 'https://api.sandbox.berbix.com'
157
+ else
158
+ raise 'invalid environment value specified';
159
+ end
160
+ end
161
+ end
162
+
163
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: berbix
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Eric Levine
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-06-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Backend SDKs to interact with the Berbix Verify API endpoints.
14
+ email: eric@berbix.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/berbix.rb
20
+ homepage: https://github.com/berbix/berbix-ruby
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.5.2.3
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Berbix Ruby SDK
44
+ test_files: []