Class: Castle::Client

Inherits:
Object
  • Object
show all
Includes:
Castle::ClientActions::Events, 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

Methods included from Castle::ClientActions::Events

#events_schema, #group_events, #query_events

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.

Parameters:

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


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

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.



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

def context
  @context
end

Class Method Details

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



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

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

Instance Method Details

#disable_trackingObject



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

def disable_tracking
  @do_not_track = true
end

#enable_trackingObject



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

def enable_tracking
  @do_not_track = false
end

#filter(options = {}) ⇒ Object

Parameters:

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


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

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: {})


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

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: {})


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

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)


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

def tracked?
  !@do_not_track
end