Class: Castle::Client

Inherits:
Object
  • Object
show all
Includes:
Castle::ClientActions::ListItems, Castle::ClientActions::Lists, Castle::ClientActions::Privacy
Defined in:
lib/castle/client.rb

Overview

Castle’s client.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Castle::ClientActions::Privacy

#delete_user_data, #request_user_data

Methods included from Castle::ClientActions::Lists

#create_list, #delete_list, #get_all_lists, #get_list, #query_lists, #update_list

Methods included from Castle::ClientActions::ListItems

#archive_list_item, #count_list_items, #create_batch_list_items, #create_list_item, #get_list_item, #query_list_items, #unarchive_list_item, #update_list_item

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • options (Hash) (defaults to: {})


19
20
21
22
23
24
# File 'lib/castle/client.rb', line 19

def initialize(options = {})
  options = Castle::Utils::DeepSymbolizeKeys.call(options || {})
  @do_not_track = options.fetch(:do_not_track, false)
  @timestamp = options.fetch(:timestamp) { Castle::Utils::GetTimestamp.call }
  @context = options.fetch(:context) { {} }
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



16
17
18
# File 'lib/castle/client.rb', line 16

def context
  @context
end

Class Method Details

.from_request(request, options = {}) ⇒ Object



11
12
13
# File 'lib/castle/client.rb', line 11

def from_request(request, options = {})
  new(options.merge(context: Castle::Context::Prepare.call(request, options)))
end

Instance Method Details

#disable_trackingObject



65
66
67
# File 'lib/castle/client.rb', line 65

def disable_tracking
  @do_not_track = true
end

#enable_trackingObject



69
70
71
# File 'lib/castle/client.rb', line 69

def enable_tracking
  @do_not_track = false
end

#filter(options = {}) ⇒ Object

Parameters:

  • options (Hash) (defaults to: {})


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/castle/client.rb', line 27

def filter(options = {})
  options = Castle::Utils::DeepSymbolizeKeys.call(options || {})

  return generate_do_not_track_response(failover_user_id(options)) 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

#log(options = {}) ⇒ Object

Parameters:

  • options (Hash) (defaults to: {})


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/castle/client.rb', line 53

def log(options = {})
  options = Castle::Utils::DeepSymbolizeKeys.call(options || {})

  return generate_do_not_track_response(failover_user_id(options)) 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

#risk(options = {}) ⇒ Object

Parameters:

  • options (Hash) (defaults to: {})


40
41
42
43
44
45
46
47
48
49
50
# File 'lib/castle/client.rb', line 40

def risk(options = {})
  options = Castle::Utils::DeepSymbolizeKeys.call(options || {})

  return generate_do_not_track_response(failover_user_id(options)) 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

#tracked?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/castle/client.rb', line 74

def tracked?
  !@do_not_track
end