Class: Locked::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/locked/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.



32
33
34
35
# File 'lib/locked/client.rb', line 32

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

Instance Attribute Details

#contextObject

Returns the value of attribute context.



30
31
32
# File 'lib/locked/client.rb', line 30

def context
  @context
end

Class Method Details

.failover_response_or_raise(failover_response, error) ⇒ Object



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

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

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



6
7
8
9
10
11
# File 'lib/locked/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/locked/client.rb', line 13

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

.to_options(options = {}) ⇒ Object



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

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

Instance Method Details

#authenticate(options = {}) ⇒ Object



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

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

  add_timestamp_if_necessary(options)
  command = Locked::Commands::Authenticate.new(@context).build(options)
  begin
    Locked::API.request(command).merge(failover: false, failover_reason: nil)
  rescue Locked::RequestError, Locked::InternalServerError => error
    self.class.failover_response_or_raise(
      Locked::FailoverResponse::Auth.new(options[:user_id], reason: error.to_s), error
    )
  end
end

#failed_in_verification(token = {}) ⇒ Object



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

def failed_in_verification(token = {})
  command = Locked::Commands::Verify.new(@context).build('deny_verification', token)
  begin
    response = Locked::API.request(command)
    response.merge(failover: false, failover_reason: nil)
  rescue Locked::RequestError, Locked::InternalServerError => error
    self.class.failover_response_or_raise(
      Locked::FailoverResponse::Verification.new(reason: error.to_s), error
    )
  end
end

#identify(options = {}) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/locked/client.rb', line 75

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

  add_timestamp_if_necessary(options)

  command = Locked::Commands::Identify.new(@context).build(options)
  Locked::API.request(command)
end

#succeeded_in_verification(token = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/locked/client.rb', line 63

def succeeded_in_verification(token = {})
  command = Locked::Commands::Verify.new(@context).build('allow_verification', token)
  begin
    response = Locked::API.request(command)
    response.merge(failover: false, failover_reason: nil)
  rescue Locked::RequestError, Locked::InternalServerError => error
    self.class.failover_response_or_raise(
      Locked::FailoverResponse::Verification.new(reason: error.to_s), error
    )
  end
end