-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
725 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
module Castle | ||
module API | ||
# Module for filter endpoint | ||
module Filter | ||
class << self | ||
# @param options [Hash] | ||
# return [Hash] | ||
def call(options = {}) | ||
unless options[:no_symbolize] | ||
options = Castle::Utils::DeepSymbolizeKeys.call(options || {}) | ||
end | ||
options.delete(:no_symbolize) | ||
http = options.delete(:http) | ||
config = options.delete(:config) || Castle.config | ||
|
||
response = Castle::API.call( | ||
Castle::Commands::Filter.build(options), | ||
{}, | ||
http, | ||
config | ||
) | ||
response.merge(failover: false, failover_reason: nil) | ||
rescue Castle::RequestError, Castle::InternalServerError => e | ||
unless config.failover_strategy == :throw | ||
return Castle::Failover::PrepareResponse.new(options[:user][:id], reason: e.to_s).call | ||
end | ||
|
||
raise e | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
module Castle | ||
module API | ||
# Module for log endpoint | ||
module Log | ||
class << self | ||
# @param options [Hash] | ||
# return [Hash] | ||
def call(options = {}) | ||
unless options[:no_symbolize] | ||
options = Castle::Utils::DeepSymbolizeKeys.call(options || {}) | ||
end | ||
options.delete(:no_symbolize) | ||
http = options.delete(:http) | ||
config = options.delete(:config) || Castle.config | ||
|
||
response = Castle::API.call( | ||
Castle::Commands::Log.build(options), | ||
{}, | ||
http, | ||
config | ||
) | ||
response.merge(failover: false, failover_reason: nil) | ||
rescue Castle::RequestError, Castle::InternalServerError => e | ||
unless config.failover_strategy == :throw | ||
return Castle::Failover::PrepareResponse.new(options[:user][:id], reason: e.to_s).call | ||
end | ||
|
||
raise e | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
module Castle | ||
module API | ||
# Module for risk endpoint | ||
module Risk | ||
class << self | ||
# @param options [Hash] | ||
# return [Hash] | ||
def call(options = {}) | ||
unless options[:no_symbolize] | ||
options = Castle::Utils::DeepSymbolizeKeys.call(options || {}) | ||
end | ||
options.delete(:no_symbolize) | ||
http = options.delete(:http) | ||
config = options.delete(:config) || Castle.config | ||
|
||
response = Castle::API.call( | ||
Castle::Commands::Risk.build(options), | ||
{}, | ||
http, | ||
config | ||
) | ||
response.merge(failover: false, failover_reason: nil) | ||
rescue Castle::RequestError, Castle::InternalServerError => e | ||
unless config.failover_strategy == :throw | ||
return Castle::Failover::PrepareResponse.new(options[:user][:id], reason: e.to_s).call | ||
end | ||
|
||
raise e | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module Castle | ||
module Commands | ||
# Generates the payload for the filter request | ||
class Filter | ||
class << self | ||
# @param options [Hash] | ||
# @return [Castle::Command] | ||
def build(options = {}) | ||
Castle::Validators::Present.call(options, %i[event]) | ||
context = Castle::Context::Sanitize.call(options[:context]) | ||
|
||
Castle::Command.new( | ||
'filter', | ||
options.merge(context: context, sent_at: Castle::Utils::GetTimestamp.call), | ||
:post | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module Castle | ||
module Commands | ||
# Generates the payload for the log request | ||
class Log | ||
class << self | ||
# @param options [Hash] | ||
# @return [Castle::Command] | ||
def build(options = {}) | ||
Castle::Validators::Present.call(options, %i[event]) | ||
context = Castle::Context::Sanitize.call(options[:context]) | ||
|
||
Castle::Command.new( | ||
'log', | ||
options.merge(context: context, sent_at: Castle::Utils::GetTimestamp.call), | ||
:post | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module Castle | ||
module Commands | ||
# Generates the payload for the risk request | ||
class Risk | ||
class << self | ||
# @param options [Hash] | ||
# @return [Castle::Command] | ||
def build(options = {}) | ||
Castle::Validators::Present.call(options, %i[event]) | ||
context = Castle::Context::Sanitize.call(options[:context]) | ||
|
||
Castle::Command.new( | ||
'risk', | ||
options.merge(context: context, sent_at: Castle::Utils::GetTimestamp.call), | ||
:post | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Castle::API::Filter do | ||
pending | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Castle::API::Log do | ||
pending | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Castle::API::Risk do | ||
pending | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Castle::Commands::Filter do | ||
subject(:instance) { described_class } | ||
|
||
let(:context) { { test: { test1: '1' } } } | ||
let(:user) { { id: '1234', email: 'foobar@mail.com' } } | ||
let(:default_payload) do | ||
{ | ||
request_token: '7e51335b-f4bc-4bc7-875d-b713fb61eb23-bf021a3022a1a302', | ||
event: '$registration', | ||
user: user, | ||
sent_at: time_auto, | ||
context: context | ||
} | ||
end | ||
let(:time_now) { Time.now } | ||
let(:time_auto) { time_now.utc.iso8601(3) } | ||
|
||
before { Timecop.freeze(time_now) } | ||
|
||
after { Timecop.return } | ||
|
||
describe '.build' do | ||
subject(:command) { instance.build(payload) } | ||
|
||
context 'with properties' do | ||
let(:payload) { default_payload.merge(properties: { test: '1' }) } | ||
let(:command_data) do | ||
default_payload.merge(properties: { test: '1' }, context: context) | ||
end | ||
|
||
it { expect(command.method).to be_eql(:post) } | ||
it { expect(command.path).to be_eql('filter') } | ||
it { expect(command.data).to be_eql(command_data) } | ||
end | ||
|
||
context 'with user_traits' do | ||
let(:payload) { default_payload.merge(user_traits: { test: '1' }) } | ||
let(:command_data) do | ||
default_payload.merge(user_traits: { test: '1' }, context: context) | ||
end | ||
|
||
it { expect(command.method).to be_eql(:post) } | ||
it { expect(command.path).to be_eql('filter') } | ||
it { expect(command.data).to be_eql(command_data) } | ||
end | ||
|
||
context 'when active true' do | ||
let(:payload) { default_payload.merge(context: context.merge(active: true)) } | ||
let(:command_data) do | ||
default_payload.merge(context: context.merge(active: true)) | ||
end | ||
|
||
it { expect(command.method).to be_eql(:post) } | ||
it { expect(command.path).to be_eql('filter') } | ||
it { expect(command.data).to be_eql(command_data) } | ||
end | ||
|
||
context 'when active false' do | ||
let(:payload) { default_payload.merge(context: context.merge(active: false)) } | ||
let(:command_data) do | ||
default_payload.merge(context: context.merge(active: false)) | ||
end | ||
|
||
it { expect(command.method).to be_eql(:post) } | ||
it { expect(command.path).to be_eql('filter') } | ||
it { expect(command.data).to be_eql(command_data) } | ||
end | ||
|
||
context 'when active string' do | ||
let(:payload) { default_payload.merge(context: context.merge(active: 'string')) } | ||
let(:command_data) { default_payload.merge(context: context) } | ||
|
||
it { expect(command.method).to be_eql(:post) } | ||
it { expect(command.path).to be_eql('filter') } | ||
it { expect(command.data).to be_eql(command_data) } | ||
end | ||
end | ||
|
||
describe '#validate!' do | ||
subject(:validate!) { instance.build(payload) } | ||
|
||
context 'with event not present' do | ||
let(:payload) { {} } | ||
|
||
it do | ||
expect do | ||
validate! | ||
end.to raise_error(Castle::InvalidParametersError, 'event is missing or empty') | ||
end | ||
end | ||
|
||
context 'with user not present' do | ||
let(:payload) { { event: '$login' } } | ||
|
||
it { expect { validate! }.not_to raise_error } | ||
end | ||
|
||
context 'with event and user present' do | ||
let(:payload) { { event: '$login', user: user } } | ||
|
||
it { expect { validate! }.not_to raise_error } | ||
end | ||
end | ||
end |
Oops, something went wrong.