Class: Castle::Client
- Inherits:
-
Object
- Object
- Castle::Client
- Defined in:
- lib/castle/client.rb
Instance Attribute Summary collapse
-
#api ⇒ Object
Returns the value of attribute api.
Class Method Summary collapse
Instance Method Summary collapse
- #authenticate(options = {}) ⇒ Object
- #disable_tracking ⇒ Object
- #enable_tracking ⇒ Object
- #identify(options = {}) ⇒ Object
-
#initialize(context, do_not_track = false) ⇒ Client
constructor
A new instance of Client.
- #track(options = {}) ⇒ Object
- #tracked? ⇒ Boolean
Constructor Details
#initialize(context, do_not_track = false) ⇒ Client
Returns a new instance of Client.
18 19 20 21 22 |
# File 'lib/castle/client.rb', line 18 def initialize(context, do_not_track = false) @do_not_track = do_not_track @context = context @api = API.new end |
Instance Attribute Details
#api ⇒ Object
Returns the value of attribute api.
16 17 18 |
# File 'lib/castle/client.rb', line 16 def api @api end |
Class Method Details
.from_request(request, options = {}) ⇒ Object
6 7 8 |
# File 'lib/castle/client.rb', line 6 def from_request(request, = {}) new(to_context(request, ), [:do_not_track]) end |
.to_context(request, options = {}) ⇒ Object
10 11 12 13 |
# File 'lib/castle/client.rb', line 10 def to_context(request, = {}) default_context = Castle::DefaultContext.new(request, [:cookies]).call Castle::ContextMerger.new(default_context).call([:context] || {}) end |
Instance Method Details
#authenticate(options = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/castle/client.rb', line 24 def authenticate( = {}) = Castle::Utils.deep_symbolize_keys( || {}) if tracked? command = Castle::Commands::Authenticate.new(@context).build() begin @api.request(command).merge(failover: false, failover_reason: nil) rescue Castle::RequestError, Castle::InternalServerError => error failover_response_or_raise(FailoverAuthResponse.new([:user_id], reason: error.to_s), error) end else FailoverAuthResponse.new([:user_id], strategy: :allow, reason: 'Castle set to do not track.').generate end end |
#disable_tracking ⇒ Object
57 58 59 |
# File 'lib/castle/client.rb', line 57 def disable_tracking @do_not_track = true end |
#enable_tracking ⇒ Object
61 62 63 |
# File 'lib/castle/client.rb', line 61 def enable_tracking @do_not_track = false end |
#identify(options = {}) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/castle/client.rb', line 39 def identify( = {}) = Castle::Utils.deep_symbolize_keys( || {}) return unless tracked? command = Castle::Commands::Identify.new(@context).build() @api.request(command) end |
#track(options = {}) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/castle/client.rb', line 48 def track( = {}) = Castle::Utils.deep_symbolize_keys( || {}) return unless tracked? command = Castle::Commands::Track.new(@context).build() @api.request(command) end |
#tracked? ⇒ Boolean
65 66 67 |
# File 'lib/castle/client.rb', line 65 def tracked? !@do_not_track end |