Skip to content

Commit af303df

Browse files
author
Inbal Tako
committed
Fix namespace
1 parent 10900f0 commit af303df

12 files changed

Lines changed: 132 additions & 130 deletions

File tree

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ GEM
208208
rspec-core (~> 3.9.0)
209209
rspec-expectations (~> 3.9.0)
210210
rspec-mocks (~> 3.9.0)
211-
rspec-core (3.9.2)
211+
rspec-core (3.9.3)
212212
rspec-support (~> 3.9.3)
213213
rspec-expectations (3.9.2)
214214
diff-lcs (>= 1.2.0, < 2.0)

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ SecureNative can automatically load your config from *securenative.yml* file or
5353
require 'securenative'
5454

5555

56-
secureative = SecureNativeSDK.init
56+
secureative = SecureNative::Client.init
5757
```
5858
### Option 2: Initialize via API Key
5959

6060
```ruby
6161
require 'securenative'
6262

6363

64-
securenative = SecureNativeSDK.init_with_api_key('YOUR_API_KEY')
64+
securenative = SecureNative::Client.init_with_api_key('YOUR_API_KEY')
6565
```
6666

6767
### Option 3: Initialize via ConfigurationBuilder
@@ -70,7 +70,7 @@ require 'securenative'
7070

7171

7272
options = SecureNative::Config::ConfigurationBuilder.new(api_key: 'API_KEY', max_events: 10, log_level: 'ERROR')
73-
SecureNativeSDK.init_with_options(options)
73+
SecureNative::Client.init_with_options(options)
7474
```
7575

7676
## Getting SecureNative instance
@@ -79,7 +79,7 @@ Once initialized, sdk will create a singleton instance which you can get:
7979
require 'securenative'
8080

8181

82-
secureNative = SecureNativeSDK.instance
82+
secureNative = SecureNative::Client.instance
8383
```
8484

8585
## Tracking events
@@ -92,7 +92,7 @@ require 'securenative'
9292

9393

9494
def track
95-
securenative = SecureNativeSDK.instance
95+
securenative = SecureNative::Client.instance
9696
context = SecureNative::Context.new(client_token: 'SECURED_CLIENT_TOKEN', ip: '127.0.0.1',
9797
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' })
9898

@@ -113,7 +113,7 @@ require 'securenative'
113113

114114

115115
def track(request)
116-
securenative = SecureNativeSDK.instance
116+
securenative = SecureNative::Client.instance
117117
context = SecureNative::Context.from_http_request(request)
118118

119119
event_options = SecureNative::EventOptions.new(event: SecureNative::EventTypes::LOG_IN, user_id: '1234', context: context,
@@ -135,7 +135,7 @@ require 'securenative'
135135

136136

137137
def verify(request)
138-
securenative = SecureNativeSDK.instance
138+
securenative = SecureNative::Client.instance
139139
context = SecureNative::Context.from_http_request(request)
140140

141141
event_options = SecureNative::EventOptions.new(event: SecureNative::EventTypes::LOG_IN, user_id: '1234', context: context,
@@ -158,7 +158,7 @@ require 'securenative'
158158

159159

160160
def webhook_endpoint(request)
161-
securenative = SecureNativeSDK.instance
161+
securenative = SecureNative::Client.instance
162162

163163
# Checks if request is verified
164164
is_verified = securenative.verify_request_payload(request)
@@ -185,5 +185,5 @@ require 'securenative'
185185

186186
options = SecureNative::Options.new(api_key: 'API_KEY', max_events: 10, log_level: 'ERROR', proxy_headers: ['CF-Connecting-IP'])
187187

188-
SecureNativeSDK.init_with_options(options)
188+
SecureNative::Client.init_with_options(options)
189189
```

lib/securenative.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
require 'securenative/utils/utils'
1717
require 'securenative/utils/log'
1818
require 'securenative/utils/ip_utils'
19-
require 'securenative/context/rails_context'
20-
require 'securenative/context/hanami_context'
21-
require 'securenative/context/sinatra_context'
19+
require 'securenative/frameworks/hanami'
20+
require 'securenative/frameworks/sinatra'
21+
require 'securenative/frameworks/rails'
2222
require 'securenative/context'
2323
require 'securenative/event_options'
2424
require 'securenative/user_traits'
@@ -34,7 +34,7 @@
3434
require 'securenative/http_client'
3535
require 'securenative/event_manager'
3636
require 'securenative/api_manager'
37-
require 'securenative/sdk'
37+
require 'securenative/client'
3838
require 'securenative/version'
3939

4040
require 'yaml'

lib/securenative/client.rb

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# frozen_string_literal: true
2+
3+
module SecureNative
4+
class Client
5+
attr_reader :options
6+
7+
def initialize(options)
8+
@securenative = nil
9+
if SecureNative::Utils::Utils.null_or_empty?(options.api_key)
10+
raise SecureNativeSDKError, 'You must pass your SecureNative api key'
11+
end
12+
13+
@options = options
14+
@event_manager = EventManager.new(@options)
15+
16+
@api_manager = SecureNative::ApiManager.new(@event_manager, @options)
17+
SecureNative::Log.init_logger(@options.log_level)
18+
end
19+
20+
def self.init_with_options(options)
21+
if @securenative.nil?
22+
@securenative = SecureNative::Client.new(options)
23+
@securenative
24+
else
25+
SecureNative::Log.debug('This SDK was already initialized.')
26+
raise SecureNativeSDKError, 'This SDK was already initialized.'
27+
end
28+
end
29+
30+
def self.init_with_api_key(api_key)
31+
if SecureNative::Utils::Utils.null_or_empty?(api_key)
32+
raise SecureNativeConfigError, 'You must pass your SecureNative api key'
33+
end
34+
35+
if @securenative.nil?
36+
options = SecureNative::Config::ConfigurationBuilder.new(api_key: api_key)
37+
@securenative = SecureNative::Client.new(options)
38+
@securenative
39+
else
40+
SecureNative::Log.debug('This SDK was already initialized.')
41+
raise SecureNativeSDKError, 'This SDK was already initialized.'
42+
end
43+
end
44+
45+
def self.init
46+
options = SecureNative::Config::ConfigurationManager.load_config
47+
init_with_options(options)
48+
end
49+
50+
def self.instance
51+
raise SecureNativeSDKIllegalStateError if @securenative.nil?
52+
53+
@securenative
54+
end
55+
56+
def track(event_options)
57+
@api_manager.track(event_options)
58+
end
59+
60+
def verify(event_options)
61+
@api_manager.verify(event_options)
62+
end
63+
64+
def self._flush
65+
@securenative = nil
66+
end
67+
68+
def verify_request_payload(request)
69+
request_signature = request.header[SignatureUtils.SIGNATURE_HEADER]
70+
body = request.body
71+
72+
SignatureUtils.valid_signature?(@options.api_key, body, request_signature)
73+
end
74+
end
75+
end

lib/securenative/context.rb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,33 @@ def initialize(client_token: '', ip: '', remote_ip: '', headers: nil, url: '', h
1818
end
1919

2020
def self.default_context_builder
21-
Context.new
21+
SecureNative::Context.new
2222
end
2323

2424
def self.from_http_request(request)
25-
client_token = SecureNative::FrameWorkContext::RailsContext.get_client_token(request)
26-
client_token = SecureNative::FrameWorkContext::SinatraContext.get_client_token(request) if client_token.nil?
27-
client_token = SecureNative::FrameWorkContext::HanamiContext.get_client_token(request) if client_token.nil?
25+
client_token = SecureNative::Frameworks::Rails.get_client_token(request)
26+
client_token = SecureNative::Frameworks::Sinatra.get_client_token(request) if client_token.nil?
27+
client_token = SecureNative::Frameworks::Hanami.get_client_token(request) if client_token.nil?
2828

2929
begin
30-
headers = SecureNative::FrameWorkContext::RailsContext.get_headers(request)
31-
headers = SecureNative::FrameWorkContext::SinatraContext.get_headers(request) if headers.nil?
32-
headers = SecureNative::FrameWorkContext::HanamiContext.get_headers(request) if headers.nil?
30+
headers = SecureNative::Frameworks::Rails.get_headers(request)
31+
headers = SecureNative::Frameworks::Sinatra.get_headers(request) if headers.nil?
32+
headers = SecureNative::Frameworks::Hanami.get_headers(request) if headers.nil?
3333

3434
# Standard Ruby request
3535
headers = request.header.to_hash if headers.nil?
3636
rescue StandardError
3737
headers = []
3838
end
3939

40-
url = SecureNative::FrameWorkContext::RailsContext.get_url(request)
41-
url = SecureNative::FrameWorkContext::SinatraContext.get_url(request) if url.nil?
42-
url = SecureNative::FrameWorkContext::HanamiContext.get_url(request) if url.nil?
40+
url = SecureNative::Frameworks::Rails.get_url(request)
41+
url = SecureNative::Frameworks::Sinatra.get_url(request) if url.nil?
42+
url = SecureNative::Frameworks::Hanami.get_url(request) if url.nil?
4343
url = '' if url.nil?
4444

45-
method = SecureNative::FrameWorkContext::RailsContext.get_method(request)
46-
method = SecureNative::FrameWorkContext::SinatraContext.get_method(request) if method.nil?
47-
method = SecureNative::FrameWorkContext::HanamiContext.get_method(request) if method.nil?
45+
method = SecureNative::Frameworks::Rails.get_method(request)
46+
method = SecureNative::Frameworks::Sinatra.get_method(request) if method.nil?
47+
method = SecureNative::Frameworks::Hanami.get_method(request) if method.nil?
4848
method = '' if method.nil?
4949

5050
begin
@@ -57,9 +57,9 @@ def self.from_http_request(request)
5757
client_token = SecureNative::Utils::RequestUtils.get_secure_header_from_request(headers)
5858
end
5959

60-
Context.new(client_token: client_token, ip: SecureNative::Utils::RequestUtils.get_client_ip_from_request(request),
61-
remote_ip: SecureNative::Utils::RequestUtils.get_remote_ip_from_request(request),
62-
headers: headers, url: url, http_method: method || '', body: body)
60+
SecureNative::Context.new(client_token: client_token, ip: SecureNative::Utils::RequestUtils.get_client_ip_from_request(request),
61+
remote_ip: SecureNative::Utils::RequestUtils.get_remote_ip_from_request(request),
62+
headers: headers, url: url, http_method: method || '', body: body)
6363
end
6464
end
6565
end

lib/securenative/context/hanami_context.rb renamed to lib/securenative/frameworks/hanami.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# frozen_string_literal: true
22

33
module SecureNative
4-
module FrameWorkContext
5-
class HanamiContext
4+
module Frameworks
5+
class Hanami
66
SECURENATIVE_COOKIE = '_sn'
77

88
def self.get_client_token(request)
@@ -36,11 +36,11 @@ def self.get_method(request)
3636
def self.get_headers(request)
3737
begin
3838
# Note: At the moment we're filtering out everything but user-agent since ruby's payload is way too big
39-
{'user-agent' => request.env['HTTP_USER_AGENT']}
39+
{ 'user-agent' => request.env['HTTP_USER_AGENT'] }
4040
rescue StandardError
4141
nil
4242
end
4343
end
4444
end
4545
end
46-
end
46+
end
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# frozen_string_literal: true
22

33
module SecureNative
4-
module FrameWorkContext
5-
class RailsContext
4+
module Frameworks
5+
class Rails
66
SECURENATIVE_COOKIE = '_sn'
77

88
def self.get_client_token(request)

lib/securenative/context/sinatra_context.rb renamed to lib/securenative/frameworks/sinatra.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# frozen_string_literal: true
22

33
module SecureNative
4-
module FrameWorkContext
5-
class SinatraContext
4+
module Frameworks
5+
class Sinatra
66
SECURENATIVE_COOKIE = '_sn'
77

88
def self.get_client_token(request)

lib/securenative/sdk.rb

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

lib/securenative/sdk_event.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def initialize(event_options, securenative_options)
3737
end
3838

3939
def to_s
40-
"securenative.context: #{@context}, rid: #{@rid}, event_type: #{@event_type}, user_id: #{@user_id},
40+
"context: #{@context}, rid: #{@rid}, event_type: #{@event_type}, user_id: #{@user_id},
4141
user_traits: #{@user_traits}, request: #{@request}, timestamp: #{@timestamp}, properties: #{@properties}"
4242
end
4343
end

0 commit comments

Comments
 (0)