Class: PostHog::Client
- Inherits:
-
Object
- Object
- PostHog::Client
- Defined in:
- lib/posthog/client.rb
Constant Summary
Constants included from Utils
Utils::UTC_OFFSET_WITHOUT_COLON, Utils::UTC_OFFSET_WITH_COLON
Instance Method Summary collapse
-
#alias(attrs) ⇒ Object
Aliases a user from one id to another.
-
#capture(attrs) ⇒ Object
Captures an event.
-
#flush ⇒ Object
Synchronously waits until the worker has flushed the queue.
-
#identify(attrs) ⇒ Object
Identifies a user.
-
#initialize(opts = {}) ⇒ Client
constructor
A new instance of Client.
-
#queued_messages ⇒ Fixnum
Number of messages in the queue.
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.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/posthog/client.rb', line 19 def initialize(opts = {}) symbolize_keys!(opts) @queue = Queue.new @api_key = opts[:api_key] @max_queue_size = opts[:max_queue_size] || Defaults::Queue::MAX_SIZE @worker_mutex = Mutex.new @worker = Worker.new(@queue, @api_key, opts) @worker_thread = nil check_api_key! at_exit { @worker_thread && @worker_thread[:should_exit] = true } end |
Instance Method Details
#alias(attrs) ⇒ Object
Aliases a user from one id to another
80 81 82 83 |
# File 'lib/posthog/client.rb', line 80 def alias(attrs) symbolize_keys! attrs enqueue(FieldParser.parse_for_alias(attrs)) end |
#capture(attrs) ⇒ Object
Captures an event
58 59 60 61 |
# File 'lib/posthog/client.rb', line 58 def capture(attrs) symbolize_keys! attrs enqueue(FieldParser.parse_for_capture(attrs)) end |
#flush ⇒ Object
Synchronously waits until the worker has flushed the queue.
Use only for scripts which are not long-running, and will specifically exit
38 39 40 41 42 43 |
# File 'lib/posthog/client.rb', line 38 def flush while !@queue.empty? || @worker.is_requesting? ensure_worker_running sleep(0.1) end end |
#identify(attrs) ⇒ Object
Identifies a user
69 70 71 72 |
# File 'lib/posthog/client.rb', line 69 def identify(attrs) symbolize_keys! attrs enqueue(FieldParser.parse_for_identify(attrs)) end |
#queued_messages ⇒ Fixnum
Returns number of messages in the queue.
86 87 88 |
# File 'lib/posthog/client.rb', line 86 def @queue.length end |