Class: Castle::Client
- Inherits:
-
Object
- Object
- Castle::Client
- Defined in:
- lib/castle/client.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
Class Method Summary collapse
- .failover_response_or_raise(failover_response, error) ⇒ Object
- .from_request(request, options = {}) ⇒ Object
- .to_context(request, options = {}) ⇒ Object
- .to_options(options = {}) ⇒ Object
Instance Method Summary collapse
- #authenticate(options = {}) ⇒ Object
- #disable_tracking ⇒ Object
- #enable_tracking ⇒ Object
- #identify(options = {}) ⇒ Object
- #impersonate(options = {}) ⇒ Object
-
#initialize(context, options = {}) ⇒ Client
constructor
A new instance of Client.
- #track(options = {}) ⇒ Object
- #tracked? ⇒ Boolean
Constructor Details
#initialize(context, options = {}) ⇒ Client
Returns a new instance of Client.
32 33 34 35 36 |
# File 'lib/castle/client.rb', line 32 def initialize(context, = {}) @do_not_track = .fetch(:do_not_track, false) = [:timestamp] @context = context end |
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
30 31 32 |
# File 'lib/castle/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/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, = {}) new( to_context(request, ), () ) end |
.to_context(request, options = {}) ⇒ Object
13 14 15 16 |
# File 'lib/castle/client.rb', line 13 def to_context(request, = {}) default_context = Castle::Context::Default.new(request, [:cookies]).call Castle::Context::Merger.call(default_context, [:context]) end |
.to_options(options = {}) ⇒ Object
18 19 20 21 22 |
# File 'lib/castle/client.rb', line 18 def ( = {}) [:timestamp] ||= Castle::Utils::Timestamp.call warn '[DEPRECATION] use user_traits instead of traits key' if .key?(:traits) end |
Instance Method Details
#authenticate(options = {}) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/castle/client.rb', line 38 def authenticate( = {}) = Castle::Utils.deep_symbolize_keys( || {}) if tracked? () command = Castle::Commands::Authenticate.new(@context).build() begin Castle::API.request(command).merge(failover: false, failover_reason: nil) rescue Castle::RequestError, Castle::InternalServerError => e self.class.failover_response_or_raise( FailoverAuthResponse.new([:user_id], reason: e.to_s), e ) end else FailoverAuthResponse.new( [:user_id], strategy: :allow, reason: 'Castle set to do not track.' ).generate end end |
#disable_tracking ⇒ Object
88 89 90 |
# File 'lib/castle/client.rb', line 88 def disable_tracking @do_not_track = true end |
#enable_tracking ⇒ Object
92 93 94 |
# File 'lib/castle/client.rb', line 92 def enable_tracking @do_not_track = false end |
#identify(options = {}) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/castle/client.rb', line 59 def identify( = {}) = Castle::Utils.deep_symbolize_keys( || {}) return unless tracked? () command = Castle::Commands::Identify.new(@context).build() Castle::API.request(command) end |
#impersonate(options = {}) ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/castle/client.rb', line 79 def impersonate( = {}) = Castle::Utils.deep_symbolize_keys( || {}) () command = Castle::Commands::Impersonate.new(@context).build() Castle::API.request(command).tap do |response| raise Castle::ImpersonationFailed unless response[:success] end end |
#track(options = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/castle/client.rb', line 69 def track( = {}) = Castle::Utils.deep_symbolize_keys( || {}) return unless tracked? () command = Castle::Commands::Track.new(@context).build() Castle::API.request(command) end |
#tracked? ⇒ Boolean
96 97 98 |
# File 'lib/castle/client.rb', line 96 def tracked? !@do_not_track end |