Skip to content

Commit 3c82794

Browse files
author
Inbal Tako
committed
Restructure project
1 parent 02d0150 commit 3c82794

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+122
-315
lines changed

README.md

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<a href="https://codecov.io/gh/securenative/securenative-ruby">
1313
<img src="https://codecov.io/gh/securenative/securenative-ruby/branch/master/graph/badge.svg" />
1414
</a>
15+
<a href="https://badge.fury.io/rb/securenative"><img src="https://badge.fury.io/rb/securenative.svg" alt="Gem Version" height="18"></a>
1516
</p>
1617
<p align="center">
1718
<a href="https://docs.securenative.com">Documentation</a> |
@@ -60,19 +61,15 @@ secureative = SecureNative.init
6061
require 'securenative'
6162

6263

63-
securenative = SecureNative.init_with_api_key("YOUR_API_KEY")
64+
securenative = SecureNative.init_with_api_key('YOUR_API_KEY')
6465
```
6566

6667
### Option 3: Initialize via ConfigurationBuilder
6768
```ruby
6869
require 'securenative'
6970

7071

71-
securenative = SecureNative.init_with_options(SecureNative.config_builder
72-
.with_api_key("API_KEY")
73-
.with_max_events(10)
74-
.with_log_level("ERROR")
75-
.build)
72+
securenative = SecureNative.init_with_options(SecureNative.config_builder(api_key = 'API_KEY', max_event = 10, log_level = 'ERROR'))
7673
```
7774

7875
## Getting SecureNative instance
@@ -94,22 +91,17 @@ require 'securenative'
9491
require 'securenative/enums/event_types'
9592
require 'securenative/event_options_builder'
9693
require 'securenative/models/user_traits'
94+
require 'securenative/context/context_builder'
9795

9896

9997
securenative = SecureNative.instance
10098

101-
context = SecureNative.context_builder
102-
.with_ip("127.0.0.1")
103-
.with_client_token("SECURED_CLIENT_TOKEN")
104-
.with_headers({"user-agent" => "Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405"})
105-
.build
99+
context = securenative.context_builder(ip = '127.0.0.1', client_token = 'SECURED_CLIENT_TOKEN',
100+
headers = { 'user-agent' => 'Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405' })
106101

107-
event_options = EventOptionsBuilder(EventTypes::LOG_IN)
108-
.with_user_id("USER_ID")
109-
.with_user_traits(UserTraits("USER_NAME", "USER_EMAIL"))
110-
.with_context(context)
111-
.with_properties({"prop1" => "CUSTOM_PARAM_VALUE", "prop2" => true, "prop3" => 3})
112-
.build
102+
event_options = EventOptionsBuilder(event_type = EventTypes::LOG_IN,
103+
user_id = 'USER_ID', user_traits = UserTraits('USER_NAME', 'USER_EMAIL'),
104+
context = context, properties = {prop1 => 'CUSTOM_PARAM_VALUE', prop2 => true, prop3 => 3}).build
113105

114106
securenative.track(event_options)
115107
```
@@ -127,12 +119,9 @@ def track(request)
127119
securenative = SecureNative.instance
128120
context = SecureNative.context_builder.from_http_request(request).build
129121

130-
event_options = EventOptionsBuilder(EventTypes::LOG_IN)
131-
.with_user_id("USER_ID")
132-
.with_user_traits(UserTraits("USER_NAME", "USER_EMAIL"))
133-
.with_context(context)
134-
.with_properties({"prop1" => "CUSTOM_PARAM_VALUE", "prop2" => true, "prop3" => 3})
135-
.build
122+
event_options = EventOptionsBuilder(event_type = EventTypes::LOG_IN,
123+
user_id = 'USER_ID', user_traits = UserTraits('USER_NAME', 'USER_EMAIL'),
124+
context = context, properties = {prop1 => 'CUSTOM_PARAM_VALUE', prop2 => true, prop3 => 3}).build
136125

137126
securenative.track(event_options)
138127
end
@@ -153,12 +142,9 @@ def track(request)
153142
securenative = SecureNative.instance
154143
context = SecureNative.context_builder.from_http_request(request).build
155144

156-
event_options = EventOptionsBuilder(EventTypes::LOG_IN)
157-
.with_user_id("USER_ID")
158-
.with_user_traits(UserTraits("USER_NAME", "USER_EMAIL"))
159-
.with_context(context)
160-
.with_properties({"prop1" => "CUSTOM_PARAM_VALUE", "prop2" => true, "prop3" => 3})
161-
.build
145+
event_options = EventOptionsBuilder(event_type = EventTypes::LOG_IN,
146+
user_id = 'USER_ID', user_traits = UserTraits('USER_NAME', 'USER_EMAIL'),
147+
context = context, properties = {prop1 => 'CUSTOM_PARAM_VALUE', prop2 => true, prop3 => 3}).build
162148

163149
verify_result = securenative.verify(event_options)
164150
verify_result.risk_level # Low, Medium, High

lib/config/configuration_builder.rb

Lines changed: 0 additions & 69 deletions
This file was deleted.

lib/models/request_context.rb

Lines changed: 0 additions & 75 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class ConfigurationBuilder
2+
attr_reader :api_key, :api_url, :interval, :max_events, :timeout, :auto_send, :disable, :log_level, :fail_over_strategy
3+
attr_writer :api_key, :api_url, :interval, :max_events, :timeout, :auto_send, :disable, :log_level, :fail_over_strategy
4+
5+
def initialize(api_key = nil, api_url = 'https://api.securenative.com/collector/api/v1', interval = 1000,
6+
max_events = 1000, timeout = 1500, auto_send = true, disable = false, log_level = 'FATAL',
7+
fail_over_strategy = FailOverStrategy::FAIL_OPEN)
8+
@api_key = api_key
9+
@api_url = api_url
10+
@interval = interval
11+
@max_events = max_events
12+
@timeout = timeout
13+
@auto_send = auto_send
14+
@disable = disable
15+
@log_level = log_level
16+
@fail_over_strategy = fail_over_strategy
17+
end
18+
19+
def self.default_config_builder
20+
ConfigurationBuilder()
21+
end
22+
23+
def self.default_securenative_options
24+
SecureNativeOptions()
25+
end
26+
end

lib/config/configuration_manager.rb renamed to lib/securenative/config/configuration_manager.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,14 @@ def self.load_config
4040

4141
properties = read_resource_file(resource_path)
4242

43-
ConfigurationBuilder()
44-
.with_api_key(_get_env_or_default(properties, 'SECURENATIVE_API_KEY', options.api_key))
45-
.with_api_url(_get_env_or_default(properties, 'SECURENATIVE_API_URL', options.api_url))
46-
.with_interval(_get_env_or_default(properties, 'SECURENATIVE_INTERVAL', options.interval))
47-
.with_max_events(_get_env_or_default(properties, 'SECURENATIVE_MAX_EVENTS', options.max_events))
48-
.with_timeout(_get_env_or_default(properties, 'SECURENATIVE_TIMEOUT', options.timeout))
49-
.with_auto_send(_get_env_or_default(properties, 'SECURENATIVE_AUTO_SEND', options.auto_send))
50-
.with_disable(_get_env_or_default(properties, 'SECURENATIVE_DISABLE', options.disable))
51-
.with_log_level(_get_env_or_default(properties, 'SECURENATIVE_LOG_LEVEL', options.log_level))
52-
.with_fail_over_strategy(_get_env_or_default(properties, 'SECURENATIVE_FAILOVER_STRATEGY', options.fail_over_strategy))
43+
ConfigurationBuilder(_get_env_or_default(properties, 'SECURENATIVE_API_KEY', options.api_key),
44+
_get_env_or_default(properties, 'SECURENATIVE_API_URL', options.api_url),
45+
_get_env_or_default(properties, 'SECURENATIVE_INTERVAL', options.interval),
46+
_get_env_or_default(properties, 'SECURENATIVE_MAX_EVENTS', options.max_events),
47+
_get_env_or_default(properties, 'SECURENATIVE_TIMEOUT', options.timeout),
48+
_get_env_or_default(properties, 'SECURENATIVE_AUTO_SEND', options.auto_send),
49+
_get_env_or_default(properties, 'SECURENATIVE_DISABLE', options.disable),
50+
_get_env_or_default(properties, 'SECURENATIVE_LOG_LEVEL', options.log_level),
51+
_get_env_or_default(properties, 'SECURENATIVE_FAILOVER_STRATEGY', options.fail_over_strategy))
5352
end
5453
end
File renamed without changes.
Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,36 @@
11
class ContextBuilder
2-
def initialize
3-
@context = SecureNativeContext()
2+
attr_reader :context
3+
4+
def initialize(client_token = nil, ip = nil, remote_ip = nil, headers = nil, url = nil, method = nil, body = nil)
5+
@context = SecureNativeContext(client_token, ip, remote_ip, headers, url, method, body)
46
end
57

6-
def with_client_token(client_token)
8+
def client_token(client_token)
79
@context.client_token = client_token
8-
self
910
end
1011

11-
def with_ip(ip)
12+
def ip(ip)
1213
@context.ip = ip
13-
self
1414
end
1515

16-
def with_remote_ip(remote_ip)
16+
def remote_ip(remote_ip)
1717
@context.remote_ip = remote_ip
18-
self
1918
end
2019

21-
def with_headers(headers)
20+
def headers(headers)
2221
@context.headers = headers
23-
self
2422
end
2523

26-
def with_url(url)
24+
def url(url)
2725
@context.url = url
28-
self
2926
end
3027

31-
def with_method(method)
28+
def method(method)
3229
@context.method = method
33-
self
3430
end
3531

36-
def with_body(body)
32+
def body(body)
3733
@context.body = body
38-
self
3934
end
4035

4136
def self.default_context_builder
@@ -57,17 +52,8 @@ def self.from_http_request(request)
5752

5853
client_token = RequestUtils.get_secure_header_from_request(headers) if Utils.null_or_empty?(client_token)
5954

60-
ContextBuilder()
61-
.with_url(request.url)
62-
.with_method(request.method)
63-
.with_headers(headers)
64-
.with_client_token(client_token)
65-
.with_ip(RequestUtils.get_client_ip_from_request(request))
66-
.with_remote_ip(RequestUtils.get_remote_ip_from_request(request))
67-
.with_body(nil)
68-
end
69-
70-
def build
71-
@context
55+
ContextBuilder(url = request.url, method = request.method, header = headers, client_token = client_token,
56+
client_ip = RequestUtils.get_client_ip_from_request(request),
57+
remote_ip = RequestUtils.get_remote_ip_from_request(request), nil)
7258
end
7359
end
File renamed without changes.

0 commit comments

Comments
 (0)