class Locked::Client

Attributes

context[RW]

Public Class Methods

failover_response_or_raise(failover_response, error) click to toggle source
# 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 = {}) click to toggle source
# File lib/locked/client.rb, line 6
def from_request(request, options = {})
  new(
    to_context(request, options),
    to_options(options)
  )
end
new(context, options = {}) click to toggle source
# File lib/locked/client.rb, line 32
def initialize(context, options = {})
  @timestamp = options[:timestamp]
  @context = context
end
to_context(request, options = {}) click to toggle source
# 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 = {}) click to toggle source
# 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

Public Instance Methods

authenticate(options = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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

Private Instance Methods

add_timestamp_if_necessary(options) click to toggle source
# File lib/locked/client.rb, line 86
def add_timestamp_if_necessary(options)
  options[:timestamp] ||= @timestamp if @timestamp
end