Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/castle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,23 @@
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/log
castle/commands/report_device
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/log
castle/api/report_device
castle/api/risk
castle/api/start_impersonation
castle/api/track
castle/payload/prepare
Expand Down
37 changes: 37 additions & 0 deletions lib/castle/api/filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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
strategy = (config || Castle.config).failover_strategy
return(
Castle::Failover::PrepareResponse.new(
options[:user][:id],
reason: e.to_s,
strategy: strategy
).call
)
end

raise e
end
end
end
end
end
37 changes: 37 additions & 0 deletions lib/castle/api/log.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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
strategy = (config || Castle.config).failover_strategy
return(
Castle::Failover::PrepareResponse.new(
options[:user][:id],
reason: e.to_s,
strategy: strategy
).call
)
end

raise e
end
end
end
end
end
37 changes: 37 additions & 0 deletions lib/castle/api/risk.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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
strategy = (config || Castle.config).failover_strategy
return(
Castle::Failover::PrepareResponse.new(
options[:user][:id],
reason: e.to_s,
strategy: strategy
).call
)
end

raise e
end
end
end
end
end
43 changes: 42 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 @@ -44,6 +45,45 @@ 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 @@ -74,13 +114,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 @@ -360,4 +360,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