posthog-ruby 1.2.3 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3b90a7b41e6a5a6fbbf48f555b9948e05b1d4caec377ca715a07701e5d655123
4
- data.tar.gz: cab835193f7c6ecba36d5121201ef5c216b9bdf31762124811a1fa43754c29e1
3
+ metadata.gz: 4cc2c1e12c53fc80013ff8d49d082d1d35ad625fa0db972e53afbc205f595664
4
+ data.tar.gz: f95c1cb5502e0ae2d58e10b0575065fb797bbccd73d96098249a50ab6f60ee0a
5
5
  SHA512:
6
- metadata.gz: bdee6bf1dc34a46de738bfa5c73386d6054677080a25ab79ea37311c01992963b4731a432e2705d96e6fc10f66fed21f8a0438d4b8be3117c866fb4dd622f3b4
7
- data.tar.gz: 91f6a9b65ebbcd9d95536f8542b96b777d2a47ece861912331d5764973101c77818f6104bc1b857f4f458883fe159ed46bd8029eb6076de541705b15f11c70a7
6
+ metadata.gz: 8947ba953b888db638dff4ee913fa697eb80d0ac4e8796a705143278bae9aef67d7d966cdb6112309e2a51b5432fe340676036f2756bcbef885f801c929de13d
7
+ data.tar.gz: c95c85e49f02acd99043120ccc6b7889b30553da9d968cdc08f7bfe13e130bd5f42380a68714c712f0444777a24bfcb29e252a28ce319f01242a362c9ca2e4e0
@@ -4,7 +4,8 @@ require 'time'
4
4
  require 'posthog/defaults'
5
5
  require 'posthog/logging'
6
6
  require 'posthog/utils'
7
- require 'posthog/worker'
7
+ require 'posthog/send_worker'
8
+ require 'posthog/noop_worker'
8
9
  require 'posthog/feature_flags'
9
10
 
10
11
  class PostHog
@@ -15,7 +16,9 @@ class PostHog
15
16
  # @param [Hash] opts
16
17
  # @option opts [String] :api_key Your project's api_key
17
18
  # @option opts [FixNum] :max_queue_size Maximum number of calls to be
18
- # remain queued.
19
+ # remain queued. Defaults to 10_000.
20
+ # @option opts [Bool] :test_mode +true+ if messages should remain
21
+ # queued for testing. Defaults to +false+.
19
22
  # @option opts [Proc] :on_error Handles error calls from the API.
20
23
  def initialize(opts = {})
21
24
  symbolize_keys!(opts)
@@ -24,7 +27,11 @@ class PostHog
24
27
  @api_key = opts[:api_key]
25
28
  @max_queue_size = opts[:max_queue_size] || Defaults::Queue::MAX_SIZE
26
29
  @worker_mutex = Mutex.new
27
- @worker = Worker.new(@queue, @api_key, opts)
30
+ @worker = if opts[:test_mode]
31
+ NoopWorker.new(@queue)
32
+ else
33
+ SendWorker.new(@queue, @api_key, opts)
34
+ end
28
35
  @worker_thread = nil
29
36
  @feature_flags_poller = nil
30
37
  @personal_api_key = nil
@@ -45,7 +52,7 @@ class PostHog
45
52
  at_exit { @worker_thread && @worker_thread[:should_exit] = true }
46
53
  end
47
54
 
48
- # Synchronously waits until the worker has flushed the queue.
55
+ # Synchronously waits until the worker has cleared the queue.
49
56
  #
50
57
  # Use only for scripts which are not long-running, and will specifically
51
58
  # exit
@@ -56,6 +63,13 @@ class PostHog
56
63
  end
57
64
  end
58
65
 
66
+ # Clears the queue without waiting.
67
+ #
68
+ # Use only in test mode
69
+ def clear
70
+ @queue.clear
71
+ end
72
+
59
73
  # @!macro common_attrs
60
74
  # @option attrs [String] :message_id ID that uniquely
61
75
  # identifies a message across the API. (optional)
@@ -96,6 +110,11 @@ class PostHog
96
110
  enqueue(FieldParser.parse_for_alias(attrs))
97
111
  end
98
112
 
113
+ # @return [Hash] pops the last message from the queue
114
+ def dequeue_last_message
115
+ @queue.pop
116
+ end
117
+
99
118
  # @return [Fixnum] number of messages in the queue
100
119
  def queued_messages
101
120
  @queue.length
@@ -54,7 +54,7 @@ class PostHog
54
54
  def parse_for_alias(fields)
55
55
  common = parse_common_fields(fields)
56
56
 
57
- distinct_id = common[:distinct_id] # must move to properties...
57
+ distinct_id = common[:distinct_id] # must both be set and move to properties
58
58
 
59
59
  alias_field = fields[:alias]
60
60
  check_presence! alias_field, 'alias'
@@ -63,7 +63,7 @@ class PostHog
63
63
  {
64
64
  type: 'alias',
65
65
  event: '$create_alias',
66
- distinct_id: nil,
66
+ distinct_id: distinct_id,
67
67
  properties:
68
68
  { distinct_id: distinct_id, alias: alias_field }.merge(
69
69
  common[:properties] || {}
@@ -0,0 +1,16 @@
1
+ # A worker that doesn't consume jobs
2
+ class PostHog
3
+ class NoopWorker
4
+ def initialize(queue)
5
+ @queue = queue
6
+ end
7
+
8
+ def run
9
+ # Does nothing
10
+ end
11
+
12
+ def is_requesting?
13
+ false
14
+ end
15
+ end
16
+ end
@@ -4,7 +4,7 @@ require 'posthog/transport'
4
4
  require 'posthog/utils'
5
5
 
6
6
  class PostHog
7
- class Worker
7
+ class SendWorker
8
8
  include PostHog::Utils
9
9
  include PostHog::Defaults
10
10
  include PostHog::Logging
@@ -1,3 +1,3 @@
1
1
  class PostHog
2
- VERSION = '1.2.3'
2
+ VERSION = '1.3.0'
3
3
  end
data/lib/posthog.rb CHANGED
@@ -3,7 +3,7 @@ require 'posthog/defaults'
3
3
  require 'posthog/utils'
4
4
  require 'posthog/field_parser'
5
5
  require 'posthog/client'
6
- require 'posthog/worker'
6
+ require 'posthog/send_worker'
7
7
  require 'posthog/transport'
8
8
  require 'posthog/response'
9
9
  require 'posthog/logging'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: posthog-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ''
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-18 00:00:00.000000000 Z
11
+ date: 2022-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -153,16 +153,17 @@ files:
153
153
  - lib/posthog/field_parser.rb
154
154
  - lib/posthog/logging.rb
155
155
  - lib/posthog/message_batch.rb
156
+ - lib/posthog/noop_worker.rb
156
157
  - lib/posthog/response.rb
158
+ - lib/posthog/send_worker.rb
157
159
  - lib/posthog/transport.rb
158
160
  - lib/posthog/utils.rb
159
161
  - lib/posthog/version.rb
160
- - lib/posthog/worker.rb
161
162
  homepage: https://github.com/PostHog/posthog-ruby
162
163
  licenses:
163
164
  - MIT
164
165
  metadata: {}
165
- post_install_message:
166
+ post_install_message:
166
167
  rdoc_options: []
167
168
  require_paths:
168
169
  - lib
@@ -177,8 +178,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
177
178
  - !ruby/object:Gem::Version
178
179
  version: '0'
179
180
  requirements: []
180
- rubygems_version: 3.0.3.1
181
- signing_key:
181
+ rubygems_version: 3.1.6
182
+ signing_key:
182
183
  specification_version: 4
183
184
  summary: PostHog library
184
185
  test_files: []