Skip to content

Commit 3a7cab4

Browse files
author
Inbal Tako
committed
Update readme
1 parent 7281659 commit 3a7cab4

7 files changed

Lines changed: 76 additions & 69 deletions

File tree

README.md

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Or install it yourself as:
4747
To get your *API KEY*, login to your SecureNative account and go to project settings page:
4848

4949
### Option 1: Initialize via Config file
50-
SecureNative can automatically load your config from *securenative.cfg* file or from the file that is specified in your *SECURENATIVE_CONFIG_FILE* env variable:
50+
SecureNative can automatically load your config from *securenative.yml* file or from the file that is specified in your *SECURENATIVE_CONFIG_FILE* env variable:
5151

5252
```ruby
5353
require 'securenative'
@@ -69,7 +69,8 @@ securenative = SecureNative.init_with_api_key('YOUR_API_KEY')
6969
require 'securenative'
7070

7171

72-
securenative = SecureNative.init_with_options(SecureNative.config_builder(api_key = 'API_KEY', max_event = 10, log_level = 'ERROR'))
72+
options = ConfigurationBuilder.new(api_key: 'API_KEY', max_events: 10, log_level: 'ERROR')
73+
SecureNative.init_with_options(options)
7374
```
7475

7576
## Getting SecureNative instance
@@ -88,42 +89,46 @@ instance. Make sure you build event with the EventBuilder:
8889

8990
```ruby
9091
require 'securenative'
91-
require 'securenative/enums/event_types'
92-
require 'securenative/event_options_builder'
93-
require 'securenative/models/user_traits'
94-
require 'securenative/context/context_builder'
92+
require 'models/event_options'
93+
require 'enums/event_types'
94+
require 'models/user_traits'
9595

9696

97-
securenative = SecureNative.instance
98-
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' })
101-
102-
event_options = SecureNativeEventOptions(event_type = EventTypes::LOG_IN,
103-
user_id = '1234', user_traits = UserTraits('Your Name', 'name@gmail.com', '+1234567890'),
104-
context = context, properties = {prop1 => 'CUSTOM_PARAM_VALUE', prop2 => true, prop3 => 3}).build
105-
106-
securenative.track(event_options)
97+
def track
98+
securenative = SecureNative.instance
99+
context = SecureNativeContext.new(client_token: '2a980d872b939c7e4f4378aa111a5eeffb22808b58b5372f658d34904ebd5b05fff0daab91921243ac08b72442a5b3992e402dc21df16aa7cc0e19f8bffa9d6cc59996d480d70aa22b857189403675d37fd144ebaf9dc697fed149b907678f2b1f964d73b332dc8ea7df63fcfc3c11f7bbb51ba2672652ca7d5d43f36a62e15db8b13dfd794a5eccfc5968ca514dd7cce59f2df2b9d8184d076eba808c81b311', ip: '127.0.0.1',
100+
headers: { 'user-agent' => 'Mozilla: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.3 Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/43.4' })
101+
102+
event_options = EventOptions.new(event: EventTypes::LOG_IN, user_id: '1234', context: context,
103+
user_traits: UserTraits.new(name: 'Your Name', email: 'name@gmail.com', phone: '+1234567890'),
104+
properties: { custom_param1: 'CUSTOM_PARAM_VALUE', custom_param2: true, custom_param3: 3 })
105+
106+
securenative.track(event_options)
107+
108+
@message = 'tracked'
109+
end
107110
```
108111

109112
You can also create request context from requests:
110113

111114
```ruby
112115
require 'securenative'
113-
require 'securenative/enums/event_types'
114-
require 'securenative/event_options_builder'
115-
require 'securenative/models/user_traits'
116+
require 'models/event_options'
117+
require 'enums/event_types'
118+
require 'models/user_traits'
116119

117120

118-
def track(request)
121+
def track
119122
securenative = SecureNative.instance
120-
context = SecureNative.context_builder.from_http_request(request).build
121-
122-
event_options = SecureNativeEventOptions(event_type = EventTypes::LOG_IN,
123-
user_id = '1234', user_traits = UserTraits('Your Name', 'name@gmail.com', '+1234567890'),
124-
context = context, properties = {prop1 => 'CUSTOM_PARAM_VALUE', prop2 => true, prop3 => 3}).build
123+
context = SecureNativeContext.from_http_request(request)
124+
125+
event_options = EventOptions.new(event: EventTypes::LOG_IN, user_id: '1234', context: context,
126+
user_traits: UserTraits.new(name: 'Your Name', email: 'name@gmail.com', phone: '+1234567890'),
127+
properties: { custom_param1: 'CUSTOM_PARAM_VALUE', custom_param2: true, custom_param3: 3 })
125128

126129
securenative.track(event_options)
130+
131+
@message = 'tracked'
127132
end
128133
```
129134

@@ -133,18 +138,18 @@ end
133138

134139
```ruby
135140
require 'securenative'
136-
require 'securenative/enums/event_types'
137-
require 'securenative/event_options_builder'
138-
require 'securenative/models/user_traits'
141+
require 'models/event_options'
142+
require 'enums/event_types'
143+
require 'models/user_traits'
139144

140145

141146
def verify(request)
142147
securenative = SecureNative.instance
143-
context = SecureNative.context_builder.from_http_request(request).build
148+
context = SecureNativeContext.from_http_request(request)
144149

145-
event_options = SecureNativeEventOptions(event_type = EventTypes::LOG_IN,
146-
user_id = '1234', user_traits = UserTraits('Your Name', 'name@gmail.com', '+1234567890'),
147-
context = context, properties = {prop1 => 'CUSTOM_PARAM_VALUE', prop2 => true, prop3 => 3}).build
150+
event_options = EventOptions.new(event: EventTypes::LOG_IN, user_id: '1234', context: context,
151+
user_traits: UserTraits.new(name: 'Your Name', email: 'name@gmail.com', phone: '+1234567890'),
152+
properties: { custom_param1: 'CUSTOM_PARAM_VALUE', custom_param2: true, custom_param3: 3 })
148153

149154
verify_result = securenative.verify(event_options)
150155
verify_result.risk_level # Low, Medium, High

lib/context/securenative_context.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def self.from_http_request(request)
3737
headers = HanamiContext.get_headers(request) if headers.nil?
3838

3939
# Standard Ruby request
40-
headers = request.header.to_h if headers.nil?
40+
headers = request.header.to_hash if headers.nil?
4141
rescue StandardError
4242
headers = []
4343
end

lib/utils/version_utils.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class VersionUtils
44
def self.version
55
begin
66
spec = Gem::Specification.load('securenative.gemspec')
7-
version = spec.version
7+
version = spec.version.to_s
88
rescue StandardError
99
version = 'unknown'
1010
end

spec/spec_api_manager.rb

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,21 @@
1616
it 'tracks an event' do
1717
options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', auto_send: true, interval: 10, api_url: 'https://api.securenative-stg.com/collector/api/v1')
1818

19-
expected = '{"eventType":"sn.user.login","userId":"USER_ID","userTraits":{' \
20-
'"name":"USER_NAME","email":"USER_EMAIL","phone":"+1234567890","createdAt":null},"request":{' \
21-
'"cid":null,"vid":null,"fp":null,"ip":"127.0.0.1","remoteIp":null,"headers":{' \
22-
'"user-agent":"Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) ' \
23-
'AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405"},"url":null,"method":null},' \
24-
'"properties":{"prop2":true,"prop1":"CUSTOM_PARAM_VALUE","prop3":3}}'
25-
26-
stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/track')
27-
.with(body: JSON.parse(expected)).to_return(status: 200)
19+
stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/track').to_return(status: 200)
2820
event_manager = EventManager.new(options)
2921
event_manager.start_event_persist
3022
api_manager = ApiManager.new(event_manager, options)
3123
event_options = EventOptions.new(event: EventTypes::LOG_IN, user_id: 'USER_ID',
32-
user_traits: UserTraits.new(name: 'USER_NAME', email: 'USER_EMAIL', phone: '+1234567890'),
33-
properties: { prop1: 'CUSTOM_PARAM_VALUE', prop2: true, prop3: 3 })
24+
user_traits: UserTraits.new(name: 'USER_NAME', email: 'USER_EMAIL', phone: '+1234567890'),
25+
properties: { prop1: 'CUSTOM_PARAM_VALUE', prop2: true, prop3: 3 })
3426

3527
begin
36-
api_manager.track(event_options)
28+
res = api_manager.track(event_options)
3729
ensure
3830
event_manager.stop_event_persist
3931
end
32+
33+
expect(res).to_not be_nil
4034
end
4135

4236
it 'uses invalid options' do
@@ -63,16 +57,26 @@
6357
it 'verifies an event' do
6458
options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
6559

66-
stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/track')
67-
.with(body: { riskLevel: 'medium', score: 0.32, triggers: ['New IP', 'New City'] }).to_return(status: 200)
60+
# stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/track')
61+
# .with(body: { riskLevel: 'medium', score: 0.32, triggers: ['New IP', 'New City'] }).to_return(status: 200)
62+
63+
stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/verify')
64+
.with(headers: {
65+
'Accept' => '*/*',
66+
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
67+
'Authorization' => 'YOUR_API_KEY',
68+
'Content-Type' => 'application/json',
69+
'Sn-Version' => '0.1.21',
70+
'User-Agent' => 'SecureNative-ruby'
71+
}).to_return(status: 200, body: '', headers: {})
6872
verify_result = VerifyResult.new(risk_level: RiskLevel::LOW, score: 0, triggers: nil)
6973

7074
event_manager = EventManager.new(options)
7175
event_manager.start_event_persist
7276
api_manager = ApiManager.new(event_manager, options)
7377
event_options = EventOptions.new(event: EventTypes::LOG_IN, user_id: 'USER_ID',
74-
user_traits: UserTraits.new(name: 'USER_NAME', email: 'USER_EMAIL', phone: '+1234567890'),
75-
properties: { prop1: 'CUSTOM_PARAM_VALUE', prop2: true, prop3: 3 })
78+
user_traits: UserTraits.new(name: 'USER_NAME', email: 'USER_EMAIL', phone: '+1234567890'),
79+
properties: { prop1: 'CUSTOM_PARAM_VALUE', prop2: true, prop3: 3 })
7680

7781
result = api_manager.verify(event_options)
7882

spec/spec_context_builder.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
RSpec.describe SecureNativeContext do
1111
it 'creates context from ruby default request' do
1212
stub_request(:any, 'www.example.com')
13-
.to_return(status: 200,
14-
headers: { '_sn': '71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a' })
13+
.to_return(status: 200,
14+
headers: { '_sn': '71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a' })
1515

1616
request = Net::HTTP.get_response('www.example.com', '/')
1717
context = SecureNativeContext.from_http_request(request)

spec/spec_event_manager.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@ def initialize
2626
options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
2727
event = SampleEvent.new
2828

29-
res_body = '{"data": true}'
30-
stub_request(:any, 'http://api.securenative-stg.com:443/collector/api/v1/some-path/to-api')
29+
stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/some-path/to-api')
3130
.with(headers: {
3231
'Accept' => '*/*',
3332
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
3433
'Authorization' => 'YOUR_API_KEY',
3534
'Content-Type' => 'application/json',
36-
'Sn-Version' => '0.1.19',
35+
'Sn-Version' => '0.1.21',
3736
'User-Agent' => 'SecureNative-ruby'
3837
}).to_return(status: 200, body: '', headers: {})
3938
event_manager = EventManager.new(options)
@@ -49,13 +48,13 @@ def initialize
4948
options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
5049
event = SampleEvent.new
5150

52-
stub_request(:any, 'http://api.securenative-stg.com:443/collector/api/v1/some-path/to-api')
51+
stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/some-path/to-api')
5352
.with(headers: {
5453
'Accept' => '*/*',
5554
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
5655
'Authorization' => 'YOUR_API_KEY',
5756
'Content-Type' => 'application/json',
58-
'Sn-Version' => '0.1.19',
57+
'Sn-Version' => '0.1.21',
5958
'User-Agent' => 'SecureNative-ruby'
6059
}).to_return(status: 401, body: '', headers: {})
6160

@@ -69,13 +68,13 @@ def initialize
6968
options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
7069
event = SampleEvent.new
7170

72-
stub_request(:post, 'http://api.securenative-stg.com:443/collector/api/v1/some-path/to-api')
71+
stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/some-path/to-api')
7372
.with(headers: {
7473
'Accept' => '*/*',
7574
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
7675
'Authorization' => 'YOUR_API_KEY',
7776
'Content-Type' => 'application/json',
78-
'Sn-Version' => '0.1.19',
77+
'Sn-Version' => '0.1.21',
7978
'User-Agent' => 'SecureNative-ruby'
8079
}).to_return(status: 500, body: '', headers: {})
8180
event_manager = EventManager.new(options)

spec/spec_securenative_http_client.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@
99
it 'makes a simple post call' do
1010
options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
1111

12-
stub_request(:post, 'http://api.securenative-stg.com:443/collector/api/v1/track')
13-
.with(body: '"{\\"event\\": \\"SOME_EVENT_NAME\\"}"',
12+
stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/track')
13+
.with(body: '{"event": "SOME_EVENT_NAME"}',
1414
headers: {
15-
'Accept': '*/*',
16-
'Accept-Encoding': 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
17-
'Authorization': 'YOUR_API_KEY',
18-
'Content-Type': 'application/json',
19-
'Sn-Version': '0.1.19',
20-
'User-Agent': 'SecureNative-ruby'
21-
})
22-
.to_return(status: 200, body: '', headers: {})
15+
'Accept' => '*/*',
16+
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
17+
'Authorization' => 'YOUR_API_KEY',
18+
'Content-Type' => 'application/json',
19+
'Sn-Version' => '0.1.21',
20+
'User-Agent' => 'SecureNative-ruby'
21+
}).to_return(status: 200, body: '', headers: {})
2322
client = SecureNativeHttpClient.new(options)
2423
payload = '{"event": "SOME_EVENT_NAME"}'
2524

0 commit comments

Comments
 (0)