Class: Bento::Analytics::Client
- Inherits:
-
Object
- Object
- Bento::Analytics::Client
- Defined in:
- lib/bento/sdk/client.rb
Constant Summary
Constants included from Utils
Utils::UTC_OFFSET_WITHOUT_COLON, Utils::UTC_OFFSET_WITH_COLON
Instance Method Summary collapse
-
#flush ⇒ Object
Synchronously waits until the worker has flushed the queue.
-
#initialize(opts = {}) ⇒ Client
constructor
A new instance of Client.
-
#queued_messages ⇒ Fixnum
Number of messages in the queue.
-
#track(attrs) ⇒ Object
Tracks an event.
Methods included from Logging
Methods included from Utils
#date_in_iso8601, #datetime_in_iso8601, #formatted_offset, #isoify_dates, #isoify_dates!, #seconds_to_utc_offset, #stringify_keys, #symbolize_keys, #symbolize_keys!, #time_in_iso8601, #uid
Constructor Details
#initialize(opts = {}) ⇒ Client
Returns a new instance of Client.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/bento/sdk/client.rb', line 20 def initialize(opts = {}) symbolize_keys!(opts) @queue = Queue.new @write_key = opts[:write_key] logger.debug("🍱Tracked events will be sent to: #{@write_key}") @max_queue_size = opts[:max_queue_size] || Defaults::Queue::MAX_SIZE @worker_mutex = Mutex.new @worker = Bento::Analytics::Worker.new(@queue, @write_key, opts) @worker_thread = nil check_write_key! at_exit { @worker_thread && @worker_thread[:should_exit] = true } end |
Instance Method Details
#flush ⇒ Object
Synchronously waits until the worker has flushed the queue.
Use only for scripts which are not long-running, and will specifically exit
42 43 44 45 46 47 |
# File 'lib/bento/sdk/client.rb', line 42 def flush while !@queue.empty? || @worker.is_requesting? ensure_worker_running sleep(0.1) end end |
#queued_messages ⇒ Fixnum
Returns number of messages in the queue.
78 79 80 |
# File 'lib/bento/sdk/client.rb', line 78 def @queue.length end |
#track(attrs) ⇒ Object
Tracks an event
71 72 73 74 75 |
# File 'lib/bento/sdk/client.rb', line 71 def track(attrs) symbolize_keys! attrs track_but_parsed = FieldParser.parse_for_track(attrs, @write_key) enqueue(track_but_parsed) end |