Class: ShopCircle::Orbit::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/shopcircle/orbit/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(config = nil, **options) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/shopcircle/orbit/client.rb', line 8

def initialize(config = nil, **options)
  @config = resolve_config(config, options)
  @config.validate!

  @default_device_id = @config.device_id
  @queue      = Thread::Queue.new
  @mutex      = Mutex.new
  @shutdown   = false

  transport = @config.stub ? TestTransport.new : Transport.new(@config)

  @worker = Worker.new(
    queue: @queue,
    config: @config,
    transport: transport
  )
  @worker.start

  register_at_exit_hook
end

Instance Method Details

#eventsObject

Returns recorded events (stub mode only).



101
102
103
104
# File 'lib/shopcircle/orbit/client.rb', line 101

def events
  return [] unless @config.stub
  @worker.transport.events
end

#flushObject

Force an immediate flush of queued events.



89
90
91
# File 'lib/shopcircle/orbit/client.rb', line 89

def flush
  @worker.flush
end

#identify(profile_id, traits = {}) ⇒ Object

Identify a user. Subsequent events will be associated with this profile.

orbit.identify("user_123", first_name: "John", email: "john@example.com")

Accepts both snake_case and camelCase trait keys:

first_name / firstName, last_name / lastName

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/shopcircle/orbit/client.rb', line 53

def identify(profile_id, traits = {})
  raise ValidationError, "profile_id is required" if profile_id.nil? || profile_id.to_s.strip.empty?

  Thread.current[:shopcircle_orbit_profile_id] = profile_id.to_s

  traits = traits.dup
  first_name = traits.delete(:first_name) || traits.delete(:firstName)
  last_name  = traits.delete(:last_name)  || traits.delete(:lastName)
  email      = traits.delete(:email)
  avatar     = traits.delete(:avatar)

  enqueue(Event.new(
    type: "identify",
    profile_id: profile_id.to_s,
    device_id: current_device_id,
    first_name: first_name,
    last_name: last_name,
    email: email,
    avatar: avatar,
    properties: traits.empty? ? nil : traits
  ))
end

#reset!Object

Clear current profile association (e.g. on logout).



77
78
79
80
# File 'lib/shopcircle/orbit/client.rb', line 77

def reset!
  Thread.current[:shopcircle_orbit_profile_id] = nil
  Thread.current[:shopcircle_orbit_device_id] = nil
end

#set_device_id(device_id) ⇒ Object

Set or update the device identifier at runtime.



83
84
85
86
# File 'lib/shopcircle/orbit/client.rb', line 83

def set_device_id(device_id)
  Validation.validate_device_id!(device_id)
  Thread.current[:shopcircle_orbit_device_id] = device_id&.to_s
end

#shutdown!Object

Flush remaining events and stop the background worker.



94
95
96
97
98
# File 'lib/shopcircle/orbit/client.rb', line 94

def shutdown!
  @mutex.synchronize { @shutdown = true }
  @worker.stop
  @worker.transport.shutdown if @worker.transport.respond_to?(:shutdown)
end

#track(name, properties = {}) ⇒ Object

Track a custom event with optional properties.

orbit.track("purchase", amount: 99.99, currency: "USD")


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/shopcircle/orbit/client.rb', line 33

def track(name, properties = {})
  Validation.validate_event_name!(name)
  Validation.validate_properties!(properties)

  enqueue(Event.new(
    type: "track",
    name: name.to_s,
    profile_id: current_profile_id,
    device_id: current_device_id,
    properties: properties
  ))
end