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.
-
#clear ⇒ Object
Clears the queue without waiting.
-
#dequeue_last_message ⇒ Hash
Pops the last message from the queue.
-
#flush ⇒ Object
Synchronously waits until the worker has cleared the queue.
-
#identify(attrs) ⇒ Object
Identifies a user.
-
#initialize(opts = {}) ⇒ Client
constructor
A new instance of Client.
- #is_feature_enabled(flag_key, distinct_id, default_value = false) ⇒ Object
-
#queued_messages ⇒ Fixnum
Number of messages in the queue.
- #reload_feature_flags ⇒ Object
- #shutdown ⇒ Object
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.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/posthog/client.rb', line 23 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 = if opts[:test_mode] NoopWorker.new(@queue) else SendWorker.new(@queue, @api_key, opts) end @worker_thread = nil @feature_flags_poller = nil @personal_api_key = nil check_api_key! if opts[:personal_api_key] @personal_api_key = opts[:personal_api_key] @feature_flags_poller = FeatureFlagsPoller.new( opts[:feature_flags_polling_interval], opts[:personal_api_key], @api_key, opts[:host] ) end at_exit { @worker_thread && @worker_thread[:should_exit] = true } end |
Instance Method Details
#alias(attrs) ⇒ Object
Aliases a user from one id to another
108 109 110 111 |
# File 'lib/posthog/client.rb', line 108 def alias(attrs) symbolize_keys! attrs enqueue(FieldParser.parse_for_alias(attrs)) end |
#capture(attrs) ⇒ Object
Captures an event
86 87 88 89 |
# File 'lib/posthog/client.rb', line 86 def capture(attrs) symbolize_keys! attrs enqueue(FieldParser.parse_for_capture(attrs)) end |
#clear ⇒ Object
Clears the queue without waiting.
Use only in test mode
69 70 71 |
# File 'lib/posthog/client.rb', line 69 def clear @queue.clear end |
#dequeue_last_message ⇒ Hash
Returns pops the last message from the queue.
114 115 116 |
# File 'lib/posthog/client.rb', line 114 def @queue.pop end |
#flush ⇒ Object
Synchronously waits until the worker has cleared the queue.
Use only for scripts which are not long-running, and will specifically exit
59 60 61 62 63 64 |
# File 'lib/posthog/client.rb', line 59 def flush while !@queue.empty? || @worker.is_requesting? ensure_worker_running sleep(0.1) end end |
#identify(attrs) ⇒ Object
Identifies a user
97 98 99 100 |
# File 'lib/posthog/client.rb', line 97 def identify(attrs) symbolize_keys! attrs enqueue(FieldParser.parse_for_identify(attrs)) end |
#is_feature_enabled(flag_key, distinct_id, default_value = false) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/posthog/client.rb', line 123 def is_feature_enabled(flag_key, distinct_id, default_value = false) unless @personal_api_key logger.error( 'You need to specify a personal_api_key to use feature flags' ) return end is_enabled = @feature_flags_poller.is_feature_enabled( flag_key, distinct_id, default_value ) capture( { 'distinct_id': distinct_id, 'event': '$feature_flag_called', 'properties': { '$feature_flag': flag_key, '$feature_flag_response': is_enabled } } ) return is_enabled end |
#queued_messages ⇒ Fixnum
Returns number of messages in the queue.
119 120 121 |
# File 'lib/posthog/client.rb', line 119 def @queue.length end |
#reload_feature_flags ⇒ Object
149 150 151 152 153 154 155 156 157 |
# File 'lib/posthog/client.rb', line 149 def reload_feature_flags unless @personal_api_key logger.error( 'You need to specify a personal_api_key to use feature flags' ) return end @feature_flags_poller.load_feature_flags(true) end |
#shutdown ⇒ Object
159 160 161 162 |
# File 'lib/posthog/client.rb', line 159 def shutdown @feature_flags_poller.shutdown_poller flush end |