Skip to content

Commit 1217481

Browse files
authored
Merge pull request #2 from securenative/dev
Dev
2 parents 026f0e0 + 36b5246 commit 1217481

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

lib/securenative.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
require_relative 'securenative/sn_exception'
22
require_relative 'securenative/secure_native_sdk'
3+
require 'logger'
34

45
$securenative = nil
6+
$logger = Logger.new(STDOUT)
7+
$logger.level = Logger::INFO
58

69
module SecureNative
710
def self.init(api_key, options: SecureNativeOptions.new)
811
if $securenative == nil
912
$securenative = SecureNativeSDK.new(api_key, options: options)
13+
else
14+
$logger.info("This SDK was already initialized")
15+
raise StandardError.new("This SDK was already initialized")
1016
end
1117
end
1218

lib/securenative/secure_native_sdk.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@
33
require_relative 'sn_exception'
44
require_relative 'utils'
55
require 'json'
6+
require 'logger'
67

78
class SecureNativeSDK
89
def initialize(api_key, options: SecureNativeOptions.new)
910
if api_key == nil
1011
raise SecureNativeSDKException.new
1112
end
1213

14+
if options.debug
15+
@logger = Logger.new(STDOUT)
16+
@logger.level = Logger::INFO
17+
@logger.info("sn logging was activated")
18+
end
19+
1320
@api_key = api_key
1421
@options = options
1522
@event_manager = EventManager.new(@api_key, options: @options)
@@ -24,11 +31,17 @@ def version
2431
end
2532

2633
def track(event)
34+
if @options.debug
35+
@logger.info("Track event was called")
36+
end
2737
validate_event(event)
2838
@event_manager.send_async(event, Config::TRACK_EVENT)
2939
end
3040

3141
def verify(event)
42+
if @options.debug
43+
@logger.info("Verify event was called")
44+
end
3245
validate_event(event)
3346
res = @event_manager.send_sync(event, Config::VERIFY_EVENT)
3447
if res.status_code == 200
@@ -43,6 +56,9 @@ def flow(event) # Note: For future purposes
4356
end
4457

4558
def verify_webhook(hmac_header, body)
59+
if @options.debug
60+
@logger.info("Verify webhook was called")
61+
end
4662
Utils.verify_signature(@api_key, body, hmac_header)
4763
end
4864

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
require_relative 'config'
22

33
class SecureNativeOptions
4-
def initialize(api_url: Config::API_URL_PROD, interval: 1000, max_events: 1000, timeout: 1500, auto_send: true)
4+
def initialize(api_url: Config::API_URL_PROD, interval: 1000, max_events: 1000, timeout: 1500, auto_send: true, debug: false)
55
@timeout = timeout
66
@max_events = max_events
77
@api_url = api_url
88
@interval = interval
99
@auto_send = auto_send
10+
@debug = debug
1011
end
1112

1213
attr_reader :timeout
1314
attr_reader :max_events
1415
attr_reader :api_url
1516
attr_reader :interval
1617
attr_reader :auto_send
18+
attr_reader :debug
1719
end

test/test_sdk.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,31 @@ def build_event(type = EventType::LOG_IN, id)
1515

1616
describe SecureNativeSDK do
1717
let(:sn_options) {SecureNativeOptions.new}
18+
let(:sn) {nil}
1819

1920
it "use sdk without api key" do
2021
api_key = nil
2122
expect {SecureNative.init(api_key, options: sn_options)}.to raise_error(SecureNativeSDKException)
2223
end
2324

25+
it "check for singleton use" do
26+
sn = SecureNative.init(ENV["SN_API_KEY"], options: sn_options)
27+
expect {sn.init(ENV["SN_API_KEY"], options: sn_options)}.to raise_error(StandardError)
28+
end
29+
2430
it "track an event" do
31+
sn = SecureNative._get_or_throw
2532
user_id = SecureRandom.uuid
2633
event = build_event(EventType::LOG_IN, user_id)
27-
SecureNative.init(ENV["SN_API_KEY"], options: sn_options)
28-
SecureNative.track(event)
29-
expect(SecureNative.flush).not_to be_empty
34+
sn.track(event)
35+
expect(sn.flush).not_to be_empty
3036
end
3137

3238
it "verify an event" do
39+
sn = SecureNative._get_or_throw
3340
user_id = SecureRandom.uuid
3441
event = build_event(EventType::LOG_OUT, user_id)
35-
SecureNative.init(ENV["SN_API_KEY"], options: sn_options)
36-
res = SecureNative.verify(event)
42+
res = sn.verify(event)
3743

3844
expect(res).not_to be_empty
3945
expect(res['triggers']).not_to be_empty

0 commit comments

Comments
 (0)