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.
33 34 35 36 37 |
# File 'lib/castle/client.rb', line 33 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.
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, = {}) 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
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/castle/client.rb', line 39 def authenticate( = {}) = Castle::Utils.deep_symbolize_keys( || {}) return generate_do_not_track_response([:user_id]) unless tracked? () command = Castle::Commands::Authenticate.new(@context).build() 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([:user_id], reason: e.to_s), e ) end end |
#disable_tracking ⇒ Object
86 87 88 |
# File 'lib/castle/client.rb', line 86 def disable_tracking @do_not_track = true end |
#enable_tracking ⇒ Object
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( = {}) = Castle::Utils.deep_symbolize_keys( || {}) return unless tracked? () command = Castle::Commands::Identify.new(@context).build() 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( = {}) = Castle::Utils.deep_symbolize_keys( || {}) () command = Castle::Commands::Impersonate.new(@context).build() 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( = {}) = Castle::Utils.deep_symbolize_keys( || {}) return unless tracked? () command = Castle::Commands::Track.new(@context).build() Castle::API.call(command) end |
#tracked? ⇒ Boolean
94 95 96 |
# File 'lib/castle/client.rb', line 94 def tracked? !@do_not_track end |