Class: Castle::Client

Inherits:
Object
  • Object
show all
Includes:
Castle::ClientActions::ListItems, Castle::ClientActions::Lists
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::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_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: {})


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

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.



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

def context
  @context
end

Class Method Details

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



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

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

Instance Method Details

#authenticate(options = {}) ⇒ Object

Parameters:

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


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

def authenticate(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::Authenticate.call(options.merge(context: new_context, no_symbolize: true))
end

#disable_trackingObject



112
113
114
# File 'lib/castle/client.rb', line 112

def disable_tracking
  @do_not_track = true
end

#enable_trackingObject



116
117
118
# File 'lib/castle/client.rb', line 116

def enable_tracking
  @do_not_track = false
end

#end_impersonation(options = {}) ⇒ Object

Parameters:

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


102
103
104
105
106
107
108
109
110
# File 'lib/castle/client.rb', line 102

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

  add_timestamp_if_necessary(options)

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

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

#filter(options = {}) ⇒ Object

Parameters:

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


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

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

#log(options = {}) ⇒ Object

Parameters:

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


78
79
80
81
82
83
84
85
86
87
88
# File 'lib/castle/client.rb', line 78

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

#risk(options = {}) ⇒ Object

Parameters:

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


65
66
67
68
69
70
71
72
73
74
75
# File 'lib/castle/client.rb', line 65

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

#start_impersonation(options = {}) ⇒ Object

Parameters:

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


91
92
93
94
95
96
97
98
99
# File 'lib/castle/client.rb', line 91

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

  add_timestamp_if_necessary(options)

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

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

#track(options = {}) ⇒ Object

Parameters:

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


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

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

  return unless tracked?

  add_timestamp_if_necessary(options)

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

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

#tracked?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/castle/client.rb', line 121

def tracked?
  !@do_not_track
end