Class: Castle::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Client.



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

def initialize(request, options = {})
  @do_not_track = default_tracking(options)
  @context = setup_context(request, options[:cookies], options[:context])
  @api = API.new
end

Instance Attribute Details

#apiObject

Returns the value of attribute api.



5
6
7
# File 'lib/castle/client.rb', line 5

def api
  @api
end

Instance Method Details

#authenticate(options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/castle/client.rb', line 13

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

  if tracked?
    command = Castle::Commands::Authenticate.new(@context).build(options)
    begin
      @api.request(command).merge('failover' => false, 'failover_reason' => nil)
    rescue Castle::RequestError, Castle::InternalServerError => error
      failover_response_or_raise(FailoverAuthResponse.new(options[:user_id], reason: error.to_s), error)
    end
  else
    FailoverAuthResponse.new(options[:user_id], strategy: :allow, reason: 'Castle set to do not track.').generate
  end
end

#disable_trackingObject



46
47
48
# File 'lib/castle/client.rb', line 46

def disable_tracking
  @do_not_track = true
end

#enable_trackingObject



50
51
52
# File 'lib/castle/client.rb', line 50

def enable_tracking
  @do_not_track = false
end

#identify(options = {}) ⇒ Object



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

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

  return unless tracked?

  command = Castle::Commands::Identify.new(@context).build(options)
  @api.request(command)
end

#track(options = {}) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/castle/client.rb', line 37

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

  return unless tracked?

  command = Castle::Commands::Track.new(@context).build(options)
  @api.request(command)
end

#tracked?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/castle/client.rb', line 54

def tracked?
  !@do_not_track
end