securenative 0.1.16 → 0.1.22

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 (119) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +7 -15
  3. data/.github/workflows/publish.yml +1 -1
  4. data/.github/workflows/test.yml +5 -2
  5. data/.gitignore +3 -1
  6. data/Gemfile +6 -4
  7. data/Gemfile.lock +234 -23
  8. data/README.md +38 -33
  9. data/lib/api_manager.rb +39 -0
  10. data/lib/config/configuration_builder.rb +26 -0
  11. data/lib/config/configuration_manager.rb +55 -0
  12. data/lib/config/securenative_options.rb +22 -0
  13. data/lib/context/hanami_context.rb +42 -0
  14. data/lib/context/rails_context.rb +44 -0
  15. data/lib/context/securenative_context.rb +67 -0
  16. data/lib/context/sinatra_context.rb +42 -0
  17. data/lib/enums/api_route.rb +6 -0
  18. data/lib/enums/event_types.rb +23 -0
  19. data/lib/enums/failover_strategy.rb +6 -0
  20. data/lib/enums/risk_level.rb +7 -0
  21. data/lib/errors/securenative_config_error.rb +4 -0
  22. data/lib/errors/securenative_http_error.rb +4 -0
  23. data/lib/errors/securenative_invalid_options_error.rb +4 -0
  24. data/lib/errors/securenative_invalid_uri_error.rb +4 -0
  25. data/lib/errors/securenative_parse_error.rb +4 -0
  26. data/lib/errors/securenative_sdk_Illegal_state_error.rb +4 -0
  27. data/lib/errors/securenative_sdk_error.rb +4 -0
  28. data/lib/event_manager.rb +157 -0
  29. data/lib/http/secure_native_http_response.rb +12 -0
  30. data/lib/http/securenative_http_client.rb +50 -0
  31. data/lib/{securenative/models → models}/client_token.rb +2 -0
  32. data/lib/{securenative/models → models}/device.rb +3 -1
  33. data/lib/models/event_options.rb +37 -0
  34. data/lib/models/request_context.rb +18 -0
  35. data/lib/models/request_options.rb +12 -0
  36. data/lib/models/sdk_event.rb +49 -0
  37. data/lib/models/user_traits.rb +13 -0
  38. data/lib/models/verify_result.rb +16 -0
  39. data/lib/securenative.rb +83 -0
  40. data/lib/utils/date_utils.rb +9 -0
  41. data/lib/utils/encryption_utils.rb +49 -0
  42. data/lib/{securenative/utils → utils}/ip_utils.rb +7 -6
  43. data/lib/utils/request_utils.rb +54 -0
  44. data/lib/{securenative/logger.rb → utils/secure_native_logger.rb} +5 -3
  45. data/lib/{securenative/utils → utils}/signature_utils.rb +4 -2
  46. data/lib/utils/utils.rb +9 -0
  47. data/lib/utils/version_utils.rb +11 -0
  48. data/{lib/securenative → out/production/securenative-ruby}/api_manager.rb +6 -5
  49. data/{lib/securenative → out/production/securenative-ruby}/config/configuration_builder.rb +7 -3
  50. data/{lib/securenative → out/production/securenative-ruby}/config/configuration_manager.rb +5 -3
  51. data/{lib/securenative → out/production/securenative-ruby}/config/securenative_options.rb +3 -1
  52. data/out/production/securenative-ruby/context/securenative_context.rb +40 -0
  53. data/out/production/securenative-ruby/enums/api_route.rb +6 -0
  54. data/out/production/securenative-ruby/enums/event_types.rb +23 -0
  55. data/out/production/securenative-ruby/enums/failover_strategy.rb +6 -0
  56. data/out/production/securenative-ruby/enums/risk_level.rb +7 -0
  57. data/out/production/securenative-ruby/errors/securenative_config_error.rb +4 -0
  58. data/out/production/securenative-ruby/errors/securenative_http_error.rb +4 -0
  59. data/out/production/securenative-ruby/errors/securenative_invalid_options_error.rb +4 -0
  60. data/out/production/securenative-ruby/errors/securenative_invalid_uri_error.rb +4 -0
  61. data/out/production/securenative-ruby/errors/securenative_parse_error.rb +4 -0
  62. data/out/production/securenative-ruby/errors/securenative_sdk_Illegal_state_error.rb +4 -0
  63. data/out/production/securenative-ruby/errors/securenative_sdk_error.rb +4 -0
  64. data/out/production/securenative-ruby/event_manager.rb +156 -0
  65. data/out/production/securenative-ruby/event_options.rb +32 -0
  66. data/{lib/securenative → out/production/securenative-ruby}/http/http_response.rb +3 -1
  67. data/out/production/securenative-ruby/http/securenative_http_client.rb +32 -0
  68. data/out/production/securenative-ruby/models/client_token.rb +12 -0
  69. data/out/production/securenative-ruby/models/device.rb +10 -0
  70. data/{lib/securenative → out/production/securenative-ruby}/models/event_options.rb +3 -1
  71. data/{lib/securenative → out/production/securenative-ruby}/models/request_context.rb +5 -2
  72. data/out/production/securenative-ruby/models/request_options.rb +12 -0
  73. data/{lib/securenative → out/production/securenative-ruby}/models/sdk_event.rb +11 -3
  74. data/out/production/securenative-ruby/models/user_traits.rb +13 -0
  75. data/{lib/securenative → out/production/securenative-ruby}/models/verify_result.rb +3 -1
  76. data/{lib/securenative → out/production/securenative-ruby}/securenative.rb +24 -15
  77. data/out/production/securenative-ruby/utils/date_utils.rb +9 -0
  78. data/{lib/securenative → out/production/securenative-ruby}/utils/encryption_utils.rb +1 -4
  79. data/out/production/securenative-ruby/utils/ip_utils.rb +23 -0
  80. data/{lib/securenative → out/production/securenative-ruby}/utils/request_utils.rb +5 -3
  81. data/out/production/securenative-ruby/utils/secure_native_logger.rb +44 -0
  82. data/out/production/securenative-ruby/utils/signature_utils.rb +16 -0
  83. data/out/production/securenative-ruby/utils/utils.rb +9 -0
  84. data/{lib/securenative → out/production/securenative-ruby}/utils/version_utils.rb +3 -1
  85. data/out/test/securenative-ruby/spec_api_manager.rb +81 -0
  86. data/out/test/securenative-ruby/spec_context_builder.rb +69 -0
  87. data/out/test/securenative-ruby/spec_date_utils.rb +13 -0
  88. data/out/test/securenative-ruby/spec_encryption_utils.rb +26 -0
  89. data/out/test/securenative-ruby/spec_event_manager.rb +59 -0
  90. data/out/test/securenative-ruby/spec_helper.rb +20 -0
  91. data/out/test/securenative-ruby/spec_ip_utils.rb +41 -0
  92. data/out/test/securenative-ruby/spec_securenative.rb +65 -0
  93. data/out/test/securenative-ruby/spec_securenative_http_client.rb +23 -0
  94. data/out/test/securenative-ruby/spec_signature_utils.rb +18 -0
  95. data/securenative.gemspec +2 -2
  96. metadata +88 -43
  97. data/.travis.yml +0 -6
  98. data/VERSION +0 -1
  99. data/lib/securenative/context/context_builder.rb +0 -59
  100. data/lib/securenative/context/securenative_context.rb +0 -14
  101. data/lib/securenative/enums/api_route.rb +0 -4
  102. data/lib/securenative/enums/event_types.rb +0 -21
  103. data/lib/securenative/enums/failover_strategy.rb +0 -4
  104. data/lib/securenative/enums/risk_level.rb +0 -5
  105. data/lib/securenative/event_manager.rb +0 -149
  106. data/lib/securenative/event_options_builder.rb +0 -21
  107. data/lib/securenative/exceptions/securenative_config_exception.rb +0 -2
  108. data/lib/securenative/exceptions/securenative_http_exception.rb +0 -2
  109. data/lib/securenative/exceptions/securenative_invalid_options_exception.rb +0 -2
  110. data/lib/securenative/exceptions/securenative_invalid_uri_exception.rb +0 -2
  111. data/lib/securenative/exceptions/securenative_parse_exception.rb +0 -2
  112. data/lib/securenative/exceptions/securenative_sdk_Illegal_state_exception.rb +0 -2
  113. data/lib/securenative/exceptions/securenative_sdk_exception.rb +0 -2
  114. data/lib/securenative/http/securenative_http_client.rb +0 -30
  115. data/lib/securenative/models/request_options.rb +0 -10
  116. data/lib/securenative/models/user_traits.rb +0 -10
  117. data/lib/securenative/securenative.iml +0 -9
  118. data/lib/securenative/utils/date_utils.rb +0 -7
  119. data/lib/securenative/utils/utils.rb +0 -9
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'logger'
2
4
 
3
- class Logger
5
+ class SecureNativeLogger
4
6
  @logger = Logger.new(STDOUT)
5
7
 
6
- def self.init_logger(level)
8
+ def self.init_logger(level = 'DEBUG')
7
9
  @logger.level = case level
8
10
  when 'WARN'
9
11
  Logger::WARN
@@ -39,4 +41,4 @@ class Logger
39
41
  def self.error(msg)
40
42
  @logger.error(msg)
41
43
  end
42
- end
44
+ end
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'openssl'
2
4
 
3
5
  class SignatureUtils
4
- SIGNATURE_HEADER = 'x-securenative'.freeze
6
+ SIGNATURE_HEADER = 'x-securenative'
5
7
 
6
8
  def self.valid_signature?(api_key, payload, header_signature)
7
9
  key = api_key.encode('utf-8')
@@ -11,4 +13,4 @@ class SignatureUtils
11
13
  rescue StandardError
12
14
  false
13
15
  end
14
- end
16
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Utils
4
+ def self.null_or_empty?(string)
5
+ return true if !string || string.empty? || string.nil?
6
+
7
+ false
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class VersionUtils
4
+ def self.version
5
+ begin
6
+ Gem.loaded_specs['securenative'].version.to_s
7
+ rescue StandardError
8
+ 'unknown'
9
+ end
10
+ end
11
+ end
@@ -1,5 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'json'
2
- require_relative 'logger'
3
4
 
4
5
  class ApiManager
5
6
  def initialize(event_manager, securenative_options)
@@ -8,23 +9,23 @@ class ApiManager
8
9
  end
9
10
 
10
11
  def track(event_options)
11
- Logger.debug('Track event call')
12
+ SecureNativeLogger.debug('Track event call')
12
13
  event = SDKEvent.new(event_options, @options)
13
14
  @event_manager.send_async(event, ApiRoute::TRACK)
14
15
  end
15
16
 
16
17
  def verify(event_options)
17
- Logger.debug('Verify event call')
18
+ SecureNativeLogger.debug('Verify event call')
18
19
  event = SDKEvent.new(event_options, @options)
19
20
 
20
21
  begin
21
22
  res = JSON.parse(@event_manager.send_sync(event, ApiRoute::VERIFY, false))
22
23
  return VerifyResult.new(res['riskLevel'], res['score'], res['triggers'])
23
24
  rescue StandardError => e
24
- Logger.debug('Failed to call verify; {}'.format(e))
25
+ SecureNativeLogger.debug('Failed to call verify; {}'.format(e))
25
26
  end
26
27
  return VerifyResult.new(RiskLevel::LOW, 0, nil) if @options.fail_over_strategy == FailOverStrategy::FAIL_OPEN
27
28
 
28
29
  VerifyResult.new(RiskLevel::HIGH, 1, nil)
29
30
  end
30
- end
31
+ end
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'enums/failover_strategy'
4
+
1
5
  class ConfigurationBuilder
2
6
  attr_reader :api_key, :api_url, :interval, :max_events, :timeout, :auto_send, :disable, :log_level, :fail_over_strategy
3
7
  attr_writer :api_key, :api_url, :interval, :max_events, :timeout, :auto_send, :disable, :log_level, :fail_over_strategy
@@ -17,10 +21,10 @@ class ConfigurationBuilder
17
21
  end
18
22
 
19
23
  def self.default_config_builder
20
- ConfigurationBuilder()
24
+ ConfigurationBuilder.new
21
25
  end
22
26
 
23
27
  def self.default_securenative_options
24
- SecureNativeOptions()
28
+ SecureNativeOptions.new
25
29
  end
26
- end
30
+ end
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'parseconfig'
2
4
 
3
5
  class ConfigurationManager
4
- DEFAULT_CONFIG_FILE = 'securenative.cfg'.freeze
5
- CUSTOM_CONFIG_FILE_ENV_NAME = 'SECURENATIVE_COMFIG_FILE'.freeze
6
+ DEFAULT_CONFIG_FILE = 'securenative.cfg'
7
+ CUSTOM_CONFIG_FILE_ENV_NAME = 'SECURENATIVE_COMFIG_FILE'
6
8
  @config = nil
7
9
 
8
10
  def self.read_resource_file(resource_path)
@@ -50,4 +52,4 @@ class ConfigurationManager
50
52
  _get_env_or_default(properties, 'SECURENATIVE_LOG_LEVEL', options.log_level),
51
53
  _get_env_or_default(properties, 'SECURENATIVE_FAILOVER_STRATEGY', options.fail_over_strategy))
52
54
  end
53
- end
55
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class SecureNativeOptions
2
4
  attr_reader :api_key, :api_url, :interval, :max_events, :timeout, :auto_send, :disable, :log_level, :fail_over_strategy
3
5
  attr_writer :api_key, :api_url, :interval, :max_events, :timeout, :auto_send, :disable, :log_level, :fail_over_strategy
@@ -15,4 +17,4 @@ class SecureNativeOptions
15
17
  @log_level = log_level
16
18
  @fail_over_strategy = fail_over_strategy
17
19
  end
18
- end
20
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SecureNativeContext
4
+ attr_reader :client_token, :ip, :remote_ip, :headers, :url, :http_method, :body
5
+ attr_writer :client_token, :ip, :remote_ip, :headers, :url, :http_method, :body
6
+
7
+ def initialize(client_token = nil, ip = nil, remote_ip = nil, headers = nil, url = nil, http_method = nil, body = nil)
8
+ @client_token = client_token
9
+ @ip = ip
10
+ @remote_ip = remote_ip
11
+ @headers = headers
12
+ @url = url
13
+ @http_method = http_method
14
+ @body = body
15
+ end
16
+
17
+ def self.default_context_builder
18
+ SecureNativeContext.new
19
+ end
20
+
21
+ def self.from_http_request(request)
22
+ begin
23
+ client_token = request.cookies[RequestUtils.SECURENATIVE_COOKIE]
24
+ rescue StandardError
25
+ client_token = nil
26
+ end
27
+
28
+ begin
29
+ headers = request.headers
30
+ rescue StandardError
31
+ headers = nil
32
+ end
33
+
34
+ client_token = RequestUtils.get_secure_header_from_request(headers) if Utils.null_or_empty?(client_token)
35
+
36
+ SecureNativeContext.new(url: request.url, method: request.http_method, header: headers, client_token: client_token,
37
+ client_ip: RequestUtils.get_client_ip_from_request(request),
38
+ remote_ip: RequestUtils.get_remote_ip_from_request(request), body: nil)
39
+ end
40
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApiRoute
4
+ TRACK = 'track'
5
+ VERIFY = 'verify'
6
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EventTypes
4
+ LOG_IN = 'sn.user.login'
5
+ LOG_IN_CHALLENGE = 'sn.user.login.challenge'
6
+ LOG_IN_FAILURE = 'sn.user.login.failure'
7
+ LOG_OUT = 'sn.user.logout'
8
+ SIGN_UP = 'sn.user.signup'
9
+ AUTH_CHALLENGE = 'sn.user.auth.challenge'
10
+ AUTH_CHALLENGE_SUCCESS = 'sn.user.auth.challenge.success'
11
+ AUTH_CHALLENGE_FAILURE = 'sn.user.auth.challenge.failure'
12
+ TWO_FACTOR_DISABLE = 'sn.user.2fa.disable'
13
+ EMAIL_UPDATE = 'sn.user.email.update'
14
+ PASSWORD_REST = 'sn.user.password.reset'
15
+ PASSWORD_REST_SUCCESS = 'sn.user.password.reset.success'
16
+ PASSWORD_UPDATE = 'sn.user.password.update'
17
+ PASSWORD_REST_FAILURE = 'sn.user.password.reset.failure'
18
+ USER_INVITE = 'sn.user.invite'
19
+ ROLE_UPDATE = 'sn.user.role.update'
20
+ PROFILE_UPDATE = 'sn.user.profile.update'
21
+ PAGE_VIEW = 'sn.user.page.view'
22
+ VERIFY = 'sn.verify'
23
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FailOverStrategy
4
+ FAIL_OPEN = 'fail-open'
5
+ FAIL_CLOSED = 'fail-closed'
6
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RiskLevel
4
+ LOW = 'low'
5
+ MEDIUM = 'medium'
6
+ HIGH = 'high'
7
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SecureNativeConfigError < StandardError
4
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SecureNativeHttpError < StandardError
4
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SecureNativeInvalidOptionsError < StandardError
4
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SecureNativeInvalidUriError < StandardError
4
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SecureNativeParseError < StandardError
4
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SecureNativeSDKIllegalStateError < StandardError
4
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SecureNativeSDKError < StandardError
4
+ end
@@ -0,0 +1,156 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'utils/secure_native_logger'
4
+ require 'config/securenative_options'
5
+ require 'http/securenative_http_client'
6
+ require 'errors/securenative_sdk_error'
7
+
8
+ class QueueItem
9
+ attr_reader :url, :body, :retry_sending
10
+ attr_writer :url, :body, :retry_sending
11
+
12
+ def initialize(url, body, retry_sending)
13
+ @url = url
14
+ @body = body
15
+ @retry = retry_sending
16
+ end
17
+ end
18
+
19
+ class EventManager
20
+ def initialize(options = SecureNativeOptions.new, http_client = nil)
21
+ if options.api_key.nil?
22
+ raise SecureNativeSDKError, 'API key cannot be None, please get your API key from SecureNative console.'
23
+ end
24
+
25
+ @http_client = if http_client.nil?
26
+ SecureNativeHttpClient.new(options)
27
+ else
28
+ http_client
29
+ end
30
+
31
+ @queue = []
32
+ @semaphore = Mutex.new
33
+ @interval = options.interval
34
+ @options = options
35
+ @send_enabled = false
36
+ @attempt = 0
37
+ @coefficients = [1, 1, 2, 3, 5, 8, 13]
38
+
39
+ @thread = Thread.new {run}
40
+ end
41
+
42
+ def send_async(event, resource_path)
43
+ if @options.disable
44
+ SecureNativeLogger.warning('SDK is disabled. no operation will be performed')
45
+ return
46
+ end
47
+
48
+ item = QueueItem(resource_path, JSON.parse(EventManager.serialize(event)), false)
49
+ @queue.append(item)
50
+ end
51
+
52
+ def flush
53
+ @queue.each do |item|
54
+ @http_client.post(item.url, item.body)
55
+ end
56
+ end
57
+
58
+ def send_sync(event, resource_path, retry_sending)
59
+ if @options.disable
60
+ SecureNativeLogger.warning('SDK is disabled. no operation will be performed')
61
+ return
62
+ end
63
+
64
+ SecureNativeLogger.debug("Attempting to send event #{event}")
65
+ res = @http_client.post(resource_path, JSON.parse(EventManager.serialize(event)))
66
+
67
+ if res.status_code != 200
68
+ SecureNativeLogger.info('SecureNative failed to call endpoint {} with event {}. adding back to queue'.format(resource_path, event))
69
+ item = QueueItem(resource_path, JSON.parse(EventManager.serialize(event)), retry_sending)
70
+ @queue.append(item)
71
+ end
72
+
73
+ res
74
+ end
75
+
76
+ def run
77
+ loop do
78
+ @semaphore.synchronize do
79
+ next unless !@queue.empty? && @send_enabled
80
+
81
+ @queue.each do |item|
82
+ begin
83
+ res = @http_client.post(item.url, item.body)
84
+ if res.status_code == 401
85
+ item.retry_sending = false
86
+ elsif res.status_code != 200
87
+ raise SecureNativeHttpError, res.status_code
88
+ end
89
+ SecureNativeLogger.debug('Event successfully sent; {}'.format(item.body))
90
+ return res
91
+ rescue StandardError => e
92
+ SecureNativeLogger.error('Failed to send event; {}'.format(e))
93
+ if item.retry_sending
94
+ @attempt = 0 if @coefficients.length == @attempt + 1
95
+
96
+ back_off = @coefficients[@attempt] * @options.interval
97
+ SecureNativeLogger.debug('Automatic back-off of {}'.format(back_off))
98
+ @send_enabled = false
99
+ sleep back_off
100
+ @send_enabled = true
101
+ end
102
+ end
103
+ end
104
+ end
105
+ sleep @interval / 1000
106
+ end
107
+ end
108
+
109
+ def start_event_persist
110
+ SecureNativeLogger.debug('Starting automatic event persistence')
111
+ if @options.auto_send || @send_enabled
112
+ @send_enabled = true
113
+ else
114
+ SecureNativeLogger.debug('Automatic event persistence is disabled, you should persist events manually')
115
+ end
116
+ end
117
+
118
+ def stop_event_persist
119
+ if @send_enabled
120
+ SecureNativeLogger.debug('Attempting to stop automatic event persistence')
121
+ begin
122
+ flush
123
+ @thread&.stop
124
+ SecureNativeLogger.debug('Stopped event persistence')
125
+ rescue StandardError => e
126
+ SecureNativeLogger.error('Could not stop event scheduler; {}'.format(e))
127
+ end
128
+ end
129
+ end
130
+
131
+ def self.serialize(obj)
132
+ {
133
+ rid: obj.rid,
134
+ eventType: obj.event_type,
135
+ userId: obj.user_id,
136
+ userTraits: {
137
+ name: obj.user_traits.name,
138
+ email: obj.user_traits.email,
139
+ phone: obj.user_traits.phone,
140
+ createdAt: obj.user_traits.created_at
141
+ },
142
+ request: {
143
+ cid: obj.request.cid,
144
+ vid: obj.request.vid,
145
+ fp: obj.request.fp,
146
+ ip: obj.request.ip,
147
+ remoteIp: obj.request.remote_ip,
148
+ http_method: obj.request.http_method,
149
+ url: obj.request.url,
150
+ headers: obj.request.headers
151
+ },
152
+ timestamp: obj.timestamp,
153
+ properties: obj.properties
154
+ }
155
+ end
156
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EventOptions
4
+ MAX_PROPERTIES_SIZE = 10
5
+
6
+ def initialize(event_type, user_id, user_traits, user_name, email, phone, created_at, context, properties, timestamp)
7
+ traits = UserTraits(user_name)
8
+ if user_name && email && phone && created_at
9
+ traits = UserTraits(user_name, email, phone, created_at)
10
+ elsif user_name && email && phone
11
+ traits = UserTraits(user_name, email, phone)
12
+ elsif user_name && email
13
+ traits = UserTraits(user_name, email)
14
+ end
15
+
16
+ @event_options = EventOptions(event_type)
17
+ @event_options.user_id = user_id
18
+ @event_options.user_traits = user_traits if user_traits
19
+ @event_options.user_traits = traits
20
+ @event_options.context = context
21
+ @event_options.properties = properties
22
+ @event_options.timestamp = timestamp
23
+ end
24
+
25
+ def build
26
+ if !@event_options.properties.nil? && @event_options.properties.length > MAX_PROPERTIES_SIZE
27
+ raise SecureNativeInvalidOptionsError('You can have only up to {} custom properties', MAX_PROPERTIES_SIZE)
28
+ end
29
+
30
+ @event_options
31
+ end
32
+ end