Skip to content

Commit

Permalink
Add risk endpoint client
Browse files Browse the repository at this point in the history
  • Loading branch information
dawlib committed May 25, 2021
1 parent dcfbc63 commit 32bc652
Show file tree
Hide file tree
Showing 16 changed files with 728 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/castle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,27 @@
castle/commands/approve_device
castle/commands/authenticate
castle/commands/end_impersonation
castle/commands/filter
castle/commands/get_device
castle/commands/get_devices_for_user
castle/commands/identify
castle/commands/log
castle/commands/report_device
castle/commands/review
castle/commands/risk
castle/commands/start_impersonation
castle/commands/track
castle/api/approve_device
castle/api/authenticate
castle/api/end_impersonation
castle/api/filter
castle/api/get_device
castle/api/get_devices_for_user
castle/api/identify
castle/api/log
castle/api/report_device
castle/api/review
castle/api/risk
castle/api/start_impersonation
castle/api/track
castle/payload/prepare
Expand Down
35 changes: 35 additions & 0 deletions lib/castle/api/filter.rb
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
35 changes: 35 additions & 0 deletions lib/castle/api/log.rb
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
35 changes: 35 additions & 0 deletions lib/castle/api/risk.rb
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
44 changes: 43 additions & 1 deletion lib/castle/client.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

module Castle
# Castle's client.
class Client
class << self
def from_request(request, options = {})
Expand Down Expand Up @@ -59,6 +60,46 @@ def track(options = {})
Castle::API::Track.call(options.merge(context: new_context, no_symbolize: true))
end

# @param options [Hash]
def filter(options = {})
options = Castle::Utils::DeepSymbolizeKeys.call(options || {})

return generate_do_not_track_response(options[:user][:id]) unless tracked?

add_timestamp_if_necessary(options)

new_context = Castle::Context::Merge.call(@context, options[:context])

Castle::API::Filter.call(options.merge(context: new_context, no_symbolize: true))
end

# @param options [Hash]
def risk(options = {})
options = Castle::Utils::DeepSymbolizeKeys.call(options || {})

return generate_do_not_track_response(options[:user][:id]) unless tracked?

add_timestamp_if_necessary(options)

new_context = Castle::Context::Merge.call(@context, options[:context])

Castle::API::Risk.call(options.merge(context: new_context, no_symbolize: true))
end

# @param options [Hash]
def log(options = {})
options = Castle::Utils::DeepSymbolizeKeys.call(options || {})

return generate_do_not_track_response(options[:user][:id]) unless tracked?

add_timestamp_if_necessary(options)

new_context = Castle::Context::Merge.call(@context, options[:context])

Castle::API::Log.call(options.merge(context: new_context, no_symbolize: true))
end


# @param options [Hash]
def start_impersonation(options = {})
options = Castle::Utils::DeepSymbolizeKeys.call(options || {})
Expand Down Expand Up @@ -96,13 +137,14 @@ def enable_tracking
@do_not_track = false
end

# @return [Boolean]
def tracked?
!@do_not_track
end

private

# @param user_id [String|Boolean]
# @param user_id [String, Boolean]
def generate_do_not_track_response(user_id)
Castle::Failover::PrepareResponse.new(
user_id,
Expand Down
23 changes: 23 additions & 0 deletions lib/castle/commands/filter.rb
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
23 changes: 23 additions & 0 deletions lib/castle/commands/log.rb
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
23 changes: 23 additions & 0 deletions lib/castle/commands/risk.rb
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
5 changes: 5 additions & 0 deletions spec/lib/castle/api/filter_spec.rb
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
5 changes: 5 additions & 0 deletions spec/lib/castle/api/log_spec.rb
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
5 changes: 5 additions & 0 deletions spec/lib/castle/api/risk_spec.rb
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
12 changes: 12 additions & 0 deletions spec/lib/castle/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,16 @@
it { expect(client).to be_tracked }
end
end

describe 'filter' do
it_behaves_like 'action request', :filter
end

describe 'risk' do
it_behaves_like 'action request', :risk
end

describe 'log' do
it_behaves_like 'action request', :log
end
end
Loading

0 comments on commit 32bc652

Please sign in to comment.