amplitude-experiment 1.2.6 → 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: 2c2b4d645d6bc1a63154135eca42b2123dbaef5fb6597e77ea3c4d602235bbe7
4
- data.tar.gz: b521d6b1f7af00b62fc9ec14e8c8e1db63a63b9330e8e73139fcf09ffb5f5dca
3
+ metadata.gz: 5bebb227696dfbf198819fdba2640a000c67ca3a9c89d593cc03a4433ef532c8
4
+ data.tar.gz: fb79230b6409d82cf3d849f07cb1705abc22ae10a27e7bdfbbe6d1add1e100d0
5
5
  SHA512:
6
- metadata.gz: 68d5921c26baf78c5f1dcff97e4b58124e35f5be6de93b02be240e593c12f67d32ecba973ea95cb8df66908132c280a55e94d9be2025f351c536129663359570
7
- data.tar.gz: 5a66ce54c1ed89ae0bef261578ad006151c34292805aa6cf68725b7a61085089de4a96769401a52631fb2a57a759d89b0b7fe6a2e570ca25af49415badd5a1c2
6
+ metadata.gz: bc2885c715208465d15a6b4217e684a2caa0eb055963b1a5ff0fcdc7829239b894d685f305383cd483fff2b082592c395672c1d75140e0537543b5479e425557
7
+ data.tar.gz: 6d1c6b2488ddc5e28d0162e3535be65c3ed3c5767e08af71acdf7933a6f42a20c3d6994638b45713c8f9a9dd20ccfe054a9323465badff1acdd6960fb5cf9376
@@ -19,24 +19,37 @@ module AmplitudeExperiment
19
19
  else
20
20
  Logger::INFO
21
21
  end
22
- endpoint = "#{@config.server_url}/sdk/vardata"
22
+ endpoint = "#{@config.server_url}/sdk/v2/vardata?v=0"
23
23
  @uri = URI(endpoint)
24
24
  raise ArgumentError, 'Experiment API key is empty' if @api_key.nil? || @api_key.empty?
25
25
  end
26
26
 
27
- # Fetch all variants for a user synchronous.
27
+ # Fetch all variants for a user synchronously.
28
28
  #
29
29
  # This method will automatically retry if configured (default).
30
30
  # @param [User] user
31
31
  # @return [Hash] Variants Hash
32
32
  def fetch(user)
33
+ filter_default_variants(fetch_internal(user))
34
+ rescue StandardError => e
35
+ @logger.error("[Experiment] Failed to fetch variants: #{e.message}")
36
+ {}
37
+ end
38
+
39
+ # Fetch all variants for a user synchronously.
40
+ #
41
+ # This method will automatically retry if configured (default). This function differs from fetch as it will
42
+ # return a default variant object if the flag was evaluated but the user was not assigned (i.e. off).
43
+ # @param [User] user
44
+ # @return [Hash] Variants Hash
45
+ def fetch_v2(user)
33
46
  fetch_internal(user)
34
47
  rescue StandardError => e
35
48
  @logger.error("[Experiment] Failed to fetch variants: #{e.message}")
36
49
  {}
37
50
  end
38
51
 
39
- # Fetch all variants for a user asynchronous.
52
+ # Fetch all variants for a user asynchronously.
40
53
  #
41
54
  # This method will automatically retry if configured (default).
42
55
  # @param [User] user
@@ -53,6 +66,24 @@ module AmplitudeExperiment
53
66
  end
54
67
  end
55
68
 
69
+ # Fetch all variants for a user asynchronously. This function differs from fetch as it will
70
+ # return a default variant object if the flag was evaluated but the user was not assigned (i.e. off).
71
+ #
72
+ # This method will automatically retry if configured (default).
73
+ # @param [User] user
74
+ # @yield [User, Hash] callback block takes user object and variants hash
75
+ def fetch_async_v2(user, &callback)
76
+ Thread.new do
77
+ variants = fetch_internal(user)
78
+ yield(user, filter_default_variants(variants)) unless callback.nil?
79
+ variants
80
+ rescue StandardError => e
81
+ @logger.error("[Experiment] Failed to fetch variants: #{e.message}")
82
+ yield(user, {}) unless callback.nil?
83
+ {}
84
+ end
85
+ end
86
+
56
87
  private
57
88
 
58
89
  # @param [User] user
@@ -132,7 +163,7 @@ module AmplitudeExperiment
132
163
  # value was previously under the "key" field
133
164
  variant_value = value.fetch('key')
134
165
  end
135
- variants.store(key, Variant.new(variant_value, value.fetch('payload', nil)))
166
+ variants.store(key, Variant.new(variant_value, value.fetch('payload', nil), value.fetch('key', nil), value.fetch('metadata', nil)))
136
167
  end
137
168
  variants
138
169
  end
@@ -150,5 +181,18 @@ module AmplitudeExperiment
150
181
 
151
182
  true
152
183
  end
184
+
185
+ def filter_default_variants(variants)
186
+ variants.each do |key, value|
187
+ default = value&.metadata&.fetch('default', nil)
188
+ deployed = value&.metadata&.fetch('deployed', nil)
189
+ default = false if default.nil?
190
+ deployed = true if deployed.nil?
191
+ variants.delete(key) if default || !deployed
192
+ end
193
+ variants
194
+ end
195
+
196
+ private :filter_default_variants
153
197
  end
154
198
  end
@@ -1,6 +1,10 @@
1
1
  module AmplitudeExperiment
2
2
  # Variant
3
3
  class Variant
4
+ # The key of the variant determined by the flag configuration.
5
+ # @return [String] the value of variant key
6
+ attr_accessor :key
7
+
4
8
  # The value of the variant determined by the flag configuration.
5
9
  # @return [String] the value of variant value
6
10
  attr_accessor :value
@@ -9,17 +13,21 @@ module AmplitudeExperiment
9
13
  # @return [Object, nil] the value of variant payload
10
14
  attr_accessor :payload
11
15
 
16
+ attr_accessor :metadata
17
+
12
18
  # @param [String] value The value of the variant determined by the flag configuration.
13
19
  # @param [Object, nil] payload The attached payload, if any.
14
- def initialize(value, payload = nil)
20
+ def initialize(value, payload = nil, key = nil, metadata = nil)
21
+ @key = key
15
22
  @value = value
16
23
  @payload = payload
24
+ @metadata = metadata
17
25
  end
18
26
 
19
27
  # Determine if current variant equal other variant
20
28
  # @param [Variant] other
21
29
  def ==(other)
22
- value == other.value &&
30
+ key == other.key && value == other.value &&
23
31
  payload == other.payload
24
32
  end
25
33
  end
@@ -1,3 +1,3 @@
1
1
  module AmplitudeExperiment
2
- VERSION = '1.2.6'.freeze
2
+ VERSION = '1.3.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amplitude-experiment
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.6
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amplitude
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-29 00:00:00.000000000 Z
11
+ date: 2024-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby