Class: Castle::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/castle/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, options = {}) ⇒ Client

Returns a new instance of Client.



33
34
35
36
37
# File 'lib/castle/client.rb', line 33

def initialize(context, options = {})
  @do_not_track = options.fetch(:do_not_track, false)
  @timestamp = options[:timestamp]
  @context = context
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



31
32
33
# File 'lib/castle/client.rb', line 31

def context
  @context
end

Class Method Details

.failover_response_or_raise(failover_response, error) ⇒ Object



24
25
26
27
28
# File 'lib/castle/client.rb', line 24

def failover_response_or_raise(failover_response, error)
  return failover_response.generate unless Castle.config.failover_strategy == :throw

  raise error
end

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



6
7
8
9
10
11
# File 'lib/castle/client.rb', line 6

def from_request(request, options = {})
  new(
    to_context(request, options),
    to_options(options)
  )
end

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



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

def to_context(request, options = {})
  default_context = Castle::Context::Default.new(request, options[:cookies]).call
  Castle::Context::Merger.call(default_context, options[:context])
end

.to_options(options = {}) ⇒ Object



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

def to_options(options = {})
  options[:timestamp] ||= Castle::Utils::Timestamp.call
  warn '[DEPRECATION] use user_traits instead of traits key' if options.key?(:traits)
  options
end

Instance Method Details

#authenticate(options = {}) ⇒ Object



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

def authenticate(options = {})
  options = Castle::Utils.deep_symbolize_keys(options || {})

  return generate_do_not_track_response(options[:user_id]) unless tracked?

  add_timestamp_if_necessary(options)
  command = Castle::Commands::Authenticate.new(@context).build(options)
  begin
    Castle::API.call(command).merge(failover: false, failover_reason: nil)
  rescue Castle::RequestError, Castle::InternalServerError => e
    self.class.failover_response_or_raise(
      FailoverAuthResponse.new(options[:user_id], reason: e.to_s), e
    )
  end
end

#disable_trackingObject



86
87
88
# File 'lib/castle/client.rb', line 86

def disable_tracking
  @do_not_track = true
end

#enable_trackingObject



90
91
92
# File 'lib/castle/client.rb', line 90

def enable_tracking
  @do_not_track = false
end

#identify(options = {}) ⇒ Object



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

def identify(options = {})
  options = Castle::Utils.deep_symbolize_keys(options || {})

  return unless tracked?

  add_timestamp_if_necessary(options)

  command = Castle::Commands::Identify.new(@context).build(options)
  Castle::API.call(command)
end

#impersonate(options = {}) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/castle/client.rb', line 77

def impersonate(options = {})
  options = Castle::Utils.deep_symbolize_keys(options || {})
  add_timestamp_if_necessary(options)
  command = Castle::Commands::Impersonate.new(@context).build(options)
  Castle::API.call(command).tap do |response|
    raise Castle::ImpersonationFailed unless response[:success]
  end
end

#track(options = {}) ⇒ Object



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

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

  return unless tracked?

  add_timestamp_if_necessary(options)

  command = Castle::Commands::Track.new(@context).build(options)
  Castle::API.call(command)
end

#tracked?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/castle/client.rb', line 94

def tracked?
  !@do_not_track
end