Skip to content

Commit c9e9d5b

Browse files
author
Inbal Tako
committed
Use a singelton pattern
1 parent b2cff6b commit c9e9d5b

File tree

3 files changed

+92
-52
lines changed

3 files changed

+92
-52
lines changed

lib/securenative.rb

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,39 @@
1-
require_relative 'securenative/event_manager'
2-
require_relative 'securenative/config'
31
require_relative 'securenative/sn_exception'
4-
require_relative 'securenative/utils'
5-
require 'json'
2+
require_relative 'securenative/secure_native_sdk'
63

7-
class SecureNative
8-
def initialize(api_key, options: SecureNativeOptions.new)
9-
if api_key == nil
10-
raise SecureNativeSDKException.new
11-
end
12-
13-
@api_key = api_key
14-
@options = options
15-
@event_manager = EventManager.new(@api_key, options: @options)
16-
end
17-
18-
def api_key
19-
@api_key
20-
end
4+
$securenative = nil
215

22-
def version
23-
Config::SDK_VERSION
24-
end
25-
26-
def track(event)
27-
validate_event(event)
28-
@event_manager.send_async(event, Config::TRACK_EVENT)
29-
end
30-
31-
def verify(event)
32-
validate_event(event)
33-
res = @event_manager.send_sync(event, Config::VERIFY_EVENT)
34-
if res.status_code == 200
35-
return JSON.parse(res.body)
6+
module SecureNative
7+
def self.init(api_key, options: SecureNativeOptions.new)
8+
if $securenative == nil
9+
$securenative = SecureNativeSDK.new(api_key, options: options)
3610
end
37-
nil
3811
end
3912

40-
def flow(event) # Note: For future purposes
41-
validate_event(event)
42-
@event_manager.send_async(event, Config::FLOW_EVENT)
13+
def self.track(event)
14+
sdk = _get_or_throw
15+
sdk.track(event)
4316
end
4417

45-
def verify_webhook(hmac_header, body)
46-
Utils.verify_signature(@api_key, body, hmac_header)
18+
def self.verify(event)
19+
sdk = _get_or_throw
20+
sdk.verify(event)
4721
end
4822

49-
def flush
50-
@event_manager.flush
23+
def self.verify_webhook(hmac_header, body)
24+
sdk = _get_or_throw
25+
sdk.verify_webhook(hmac_header = hmac_header, body = body)
5126
end
5227

53-
private
28+
def self.flush
29+
sdk = _get_or_throw
30+
sdk.flush
31+
end
5432

55-
def validate_event(event)
56-
unless event.params.nil?
57-
if event.params.length > Config::MAX_ALLOWED_PARAMS
58-
event.params = event.params[0, Config::MAX_ALLOWED_PARAMS]
59-
end
33+
def self._get_or_throw
34+
if $securenative == nil
35+
raise SecureNativeSDKException.new
6036
end
37+
$securenative
6138
end
6239
end
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
require_relative 'event_manager'
2+
require_relative 'config'
3+
require_relative 'sn_exception'
4+
require_relative 'utils'
5+
require 'json'
6+
7+
class SecureNativeSDK
8+
def initialize(api_key, options: SecureNativeOptions.new)
9+
if api_key == nil
10+
raise SecureNativeSDKException.new
11+
end
12+
13+
@api_key = api_key
14+
@options = options
15+
@event_manager = EventManager.new(@api_key, options: @options)
16+
end
17+
18+
def api_key
19+
@api_key
20+
end
21+
22+
def version
23+
Config::SDK_VERSION
24+
end
25+
26+
def track(event)
27+
validate_event(event)
28+
@event_manager.send_async(event, Config::TRACK_EVENT)
29+
end
30+
31+
def verify(event)
32+
validate_event(event)
33+
res = @event_manager.send_sync(event, Config::VERIFY_EVENT)
34+
if res.status_code == 200
35+
return JSON.parse(res.body)
36+
end
37+
nil
38+
end
39+
40+
def flow(event) # Note: For future purposes
41+
validate_event(event)
42+
@event_manager.send_async(event, Config::FLOW_EVENT)
43+
end
44+
45+
def verify_webhook(hmac_header, body)
46+
Utils.verify_signature(@api_key, body, hmac_header)
47+
end
48+
49+
def flush
50+
@event_manager.flush
51+
end
52+
53+
private
54+
55+
def validate_event(event)
56+
unless event.params.nil?
57+
if event.params.length > Config::MAX_ALLOWED_PARAMS
58+
event.params = event.params[0, Config::MAX_ALLOWED_PARAMS]
59+
end
60+
end
61+
end
62+
end

test/test_sdk.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,27 @@ def build_event(type = EventType::LOG_IN, id)
1313
)
1414
end
1515

16-
describe SecureNative do
16+
describe SecureNativeSDK do
1717
let(:sn_options) {SecureNativeOptions.new}
18-
let(:sn_client) {SecureNative.new(ENV["SN_API_KEY"], options: sn_options)}
1918

2019
it "use sdk without api key" do
2120
api_key = nil
22-
expect {SecureNative.new(api_key, options: sn_options)}.to raise_error(SecureNativeSDKException)
21+
expect {SecureNative.init(api_key, options: sn_options)}.to raise_error(SecureNativeSDKException)
2322
end
2423

2524
it "track an event" do
2625
user_id = SecureRandom.uuid
2726
event = build_event(EventType::LOG_IN, user_id)
28-
sn_client.track(event)
29-
expect(sn_client.flush).not_to be_empty
27+
SecureNative.init(ENV["SN_API_KEY"], options: sn_options)
28+
SecureNative.track(event)
29+
expect(SecureNative.flush).not_to be_empty
3030
end
3131

3232
it "verify an event" do
3333
user_id = SecureRandom.uuid
3434
event = build_event(EventType::LOG_OUT, user_id)
35-
res = sn_client.verify(event)
35+
SecureNative.init(ENV["SN_API_KEY"], options: sn_options)
36+
res = SecureNative.verify(event)
3637

3738
expect(res).not_to be_empty
3839
expect(res['triggers']).not_to be_empty

0 commit comments

Comments
 (0)