absmartly-sdk 1.3.0.pre.1 → 1.4.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.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +2 -1
  3. data/.ruby-version +1 -1
  4. data/CHANGELOG.md +55 -1
  5. data/Gemfile +4 -0
  6. data/Gemfile.lock +39 -32
  7. data/README.md +461 -139
  8. data/lib/a_b_smartly.rb +118 -22
  9. data/lib/absmartly/version.rb +1 -1
  10. data/lib/absmartly.rb +4 -0
  11. data/lib/audience_matcher.rb +93 -1
  12. data/lib/client.rb +44 -12
  13. data/lib/client_config.rb +3 -1
  14. data/lib/context.rb +1302 -222
  15. data/lib/context_data_result.rb +27 -0
  16. data/lib/context_event_logger.rb +3 -0
  17. data/lib/context_event_logger_callback.rb +1 -1
  18. data/lib/context_publisher.rb +9 -0
  19. data/lib/default_audience_deserializer.rb +2 -2
  20. data/lib/default_context_data_deserializer.rb +3 -3
  21. data/lib/default_context_event_serializer.rb +2 -1
  22. data/lib/default_context_publisher.rb +9 -0
  23. data/lib/default_http_client.rb +28 -7
  24. data/lib/default_http_client_config.rb +10 -1
  25. data/lib/default_variable_parser.rb +1 -1
  26. data/lib/hashing.rb +39 -2
  27. data/lib/json/context_data.rb +26 -3
  28. data/lib/json/experiment.rb +36 -10
  29. data/lib/json/exposure.rb +8 -4
  30. data/lib/json_expr/expr_evaluator.rb +7 -9
  31. data/lib/json_expr/json_expr.rb +23 -17
  32. data/lib/json_expr/operators/and_combinator.rb +2 -1
  33. data/lib/json_expr/operators/in_operator.rb +3 -1
  34. data/lib/json_expr/operators/match_operator.rb +9 -5
  35. data/lib/json_expr/operators/or_combinator.rb +4 -2
  36. data/lib/json_expr/operators/semver_operator.rb +17 -0
  37. data/lib/json_expr/operators/semver_operators.rb +50 -0
  38. data/lib/json_expr/semver_comparator.rb +98 -0
  39. data/lib/publish_error.rb +11 -0
  40. data/lib/scheduled_thread_pool_executor.rb +8 -0
  41. data/lib/type_utils.rb +59 -0
  42. data/lib/variant_assigner.rb +1 -1
  43. metadata +14 -5
  44. data/absmartly.gemspec +0 -45
  45. data/example/example.rb +0 -85
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "semver_operator"
4
+
5
+ module Absmartly
6
+ class SemverEqualsOperator
7
+ include SemverOperator
8
+
9
+ private
10
+ def compare_result(result)
11
+ result.zero?
12
+ end
13
+ end
14
+
15
+ class SemverGreaterThanOperator
16
+ include SemverOperator
17
+
18
+ private
19
+ def compare_result(result)
20
+ result.positive?
21
+ end
22
+ end
23
+
24
+ class SemverGreaterThanOrEqualOperator
25
+ include SemverOperator
26
+
27
+ private
28
+ def compare_result(result)
29
+ result >= 0
30
+ end
31
+ end
32
+
33
+ class SemverLessThanOperator
34
+ include SemverOperator
35
+
36
+ private
37
+ def compare_result(result)
38
+ result.negative?
39
+ end
40
+ end
41
+
42
+ class SemverLessThanOrEqualOperator
43
+ include SemverOperator
44
+
45
+ private
46
+ def compare_result(result)
47
+ result <= 0
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A port of the JavaScript SDK's versionCompare; keep the two in step.
4
+ module Absmartly
5
+ module SemverComparator
6
+ NUMERIC_IDENTIFIER = /\A\d+\z/.freeze
7
+ FIXED_DIGITS = 15
8
+ FIXED_SCALE = 10**FIXED_DIGITS
9
+ TRAILING_FIXED_ZEROS = /\.?0{0,15}\z/.freeze
10
+
11
+ module_function
12
+
13
+ def compare(evaluator, lhs, rhs)
14
+ lhs_str = to_version_string(evaluator, lhs)
15
+ rhs_str = to_version_string(evaluator, rhs)
16
+ return nil if lhs_str.nil? || rhs_str.nil? || lhs_str.empty? || rhs_str.empty?
17
+
18
+ l = parse(lhs_str)
19
+ r = parse(rhs_str)
20
+ return nil if l.nil? || r.nil?
21
+
22
+ [l[:parts].size, r[:parts].size].max.times do |i|
23
+ result = compare_identifiers(l[:parts][i] || "0", r[:parts][i] || "0")
24
+ return result unless result.zero?
25
+ end
26
+
27
+ return 0 if l[:pre_release].empty? && r[:pre_release].empty?
28
+ return 1 if l[:pre_release].empty?
29
+ return -1 if r[:pre_release].empty?
30
+
31
+ l_pre = l[:pre_release].split(".", -1)
32
+ r_pre = r[:pre_release].split(".", -1)
33
+ [l_pre.size, r_pre.size].max.times do |i|
34
+ return -1 if i >= l_pre.size
35
+ return 1 if i >= r_pre.size
36
+
37
+ result = compare_identifiers(l_pre[i], r_pre[i])
38
+ return result unless result.zero?
39
+ end
40
+ 0
41
+ end
42
+
43
+ # Numbers are converted as JavaScript's stringConvert does,
44
+ # x.toFixed(15) with trailing zeros trimmed, so 1.0000000000000002 is "1".
45
+ # The general string_convert is left alone for the other operators.
46
+ def to_version_string(evaluator, value)
47
+ return evaluator.string_convert(value) unless value.is_a?(Numeric)
48
+
49
+ to_fixed(value.to_f).sub(TRAILING_FIXED_ZEROS, "")
50
+ end
51
+
52
+ # Number.prototype.toFixed(15): the exact binary value rounded half up,
53
+ # or the shortest round-trip text in exponent form from 1e21 up.
54
+ def to_fixed(x)
55
+ return x.to_s if x.nan? || x.infinite?
56
+ return x.to_s.sub(/\.0(?=e)/, "") if x.abs >= 1e21
57
+
58
+ scaled = (x.abs.to_r * FIXED_SCALE).round(half: :up)
59
+ int, frac = scaled.divmod(FIXED_SCALE)
60
+ "#{x.negative? ? '-' : ''}#{int}.#{frac.to_s.rjust(FIXED_DIGITS, '0')}"
61
+ end
62
+
63
+ def parse(version)
64
+ v = version
65
+ v = v[1..] if v.start_with?("v", "V")
66
+ plus = v.index("+")
67
+ v = v[0, plus] unless plus.nil?
68
+ return nil if v.empty?
69
+
70
+ core, *pre_release = v.split("-", -1)
71
+ return nil if core.nil? || core.empty?
72
+
73
+ { parts: core.split(".", -1), pre_release: pre_release.join("-") }
74
+ end
75
+
76
+ def compare_identifiers(a, b)
77
+ a_num = a.match?(NUMERIC_IDENTIFIER)
78
+ b_num = b.match?(NUMERIC_IDENTIFIER)
79
+
80
+ if a_num && b_num
81
+ a_norm = strip_leading_zeros(a)
82
+ b_norm = strip_leading_zeros(b)
83
+ return a_norm.size <=> b_norm.size unless a_norm.size == b_norm.size
84
+
85
+ return a_norm <=> b_norm
86
+ end
87
+ return -1 if a_num
88
+ return 1 if b_num
89
+
90
+ a <=> b
91
+ end
92
+
93
+ def strip_leading_zeros(s)
94
+ stripped = s.sub(/\A0+/, "")
95
+ stripped.empty? ? "0" : stripped
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Raised/returned for a routine publish rejection from the ABSmartly collector
4
+ # (for example an HTTP 5xx). It descends from StandardError so applications can
5
+ # recover from an operational failure with an ordinary `rescue => e` /
6
+ # `rescue StandardError` around close, finalize or publish, rather than being
7
+ # forced to rescue Ruby's base Exception.
8
+ module Absmartly
9
+ class PublishError < StandardError
10
+ end
11
+ end
@@ -6,9 +6,17 @@ class ScheduledThreadPoolExecutor < AudienceDeserializer
6
6
  attr_accessor :log, :reader
7
7
 
8
8
  def initialize(timer = 1)
9
+ @timer = timer
9
10
  end
10
11
 
11
12
  def deserialize(bytes, offset, length)
12
13
  @reader.read_value(bytes, offset, length)
13
14
  end
15
+
16
+ def execute(&block)
17
+ block.call if block
18
+ end
19
+
20
+ def shutdown
21
+ end
14
22
  end
data/lib/type_utils.rb ADDED
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Internal helpers, namespaced so they can't collide with application constants.
4
+ module Absmartly
5
+ module TypeUtils
6
+ def self.object?(value)
7
+ value.is_a?(Hash)
8
+ end
9
+
10
+ def self.numeric?(value)
11
+ value.is_a?(Numeric)
12
+ end
13
+
14
+ def self.boolean?(value)
15
+ case value
16
+ when String then %w[true false].include?(value)
17
+ when Array then false
18
+ else false
19
+ end
20
+ end
21
+
22
+ def self.compare_strings(str1, str2)
23
+ value = str1.bytes
24
+ other = str2.bytes
25
+ len1 = value.size
26
+ len2 = other.size
27
+ lim = [len1, len2].min
28
+
29
+ 0.upto(lim - 1) do |k|
30
+ if value[k] != other[k]
31
+ return get_char(value, k) - get_char(other, k)
32
+ end
33
+ end
34
+ len1 - len2
35
+ end
36
+
37
+ def self.get_char(val, index)
38
+ val[index] & 0xff
39
+ end
40
+
41
+ def self.underscore(str)
42
+ str.gsub(/::/, "/")
43
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
44
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
45
+ .tr("-", "_")
46
+ .downcase
47
+ end
48
+
49
+ def self.wrap_array(object)
50
+ if object.nil?
51
+ []
52
+ elsif object.respond_to?(:to_ary)
53
+ object.to_ary || [object]
54
+ else
55
+ [object]
56
+ end
57
+ end
58
+ end
59
+ end
@@ -7,7 +7,7 @@ class VariantAssigner
7
7
  attr_reader :key
8
8
 
9
9
  def initialize(key)
10
- md5 = Hashing.hash_unit(key.to_s)
10
+ md5 = Hashing.hash_unit(key.is_a?(Numeric) ? key : key.to_s)
11
11
  @key = MurmurHash3::V32.str_hash(md5)
12
12
  @normalizer = 1.0 / 0xffffffff
13
13
  end
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: absmartly-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0.pre.1
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - absmartly
8
+ autorequire:
8
9
  bindir: exe
9
10
  cert_chain: []
10
- date: 1980-01-02 00:00:00.000000000 Z
11
+ date: 2026-11-27 00:00:00.000000000 Z
11
12
  dependencies:
12
13
  - !ruby/object:Gem::Dependency
13
14
  name: base64
@@ -97,8 +98,6 @@ files:
97
98
  - LICENSE.txt
98
99
  - README.md
99
100
  - Rakefile
100
- - absmartly.gemspec
101
- - example/example.rb
102
101
  - lib/a_b_smartly.rb
103
102
  - lib/a_b_smartly_config.rb
104
103
  - lib/absmartly.rb
@@ -111,15 +110,18 @@ files:
111
110
  - lib/context_config.rb
112
111
  - lib/context_data_deserializer.rb
113
112
  - lib/context_data_provider.rb
113
+ - lib/context_data_result.rb
114
114
  - lib/context_event_handler.rb
115
115
  - lib/context_event_logger.rb
116
116
  - lib/context_event_logger_callback.rb
117
117
  - lib/context_event_serializer.rb
118
+ - lib/context_publisher.rb
118
119
  - lib/default_audience_deserializer.rb
119
120
  - lib/default_context_data_deserializer.rb
120
121
  - lib/default_context_data_provider.rb
121
122
  - lib/default_context_event_handler.rb
122
123
  - lib/default_context_event_serializer.rb
124
+ - lib/default_context_publisher.rb
123
125
  - lib/default_http_client.rb
124
126
  - lib/default_http_client_config.rb
125
127
  - lib/default_variable_parser.rb
@@ -152,12 +154,17 @@ files:
152
154
  - lib/json_expr/operators/nil_operator.rb
153
155
  - lib/json_expr/operators/not_operator.rb
154
156
  - lib/json_expr/operators/or_combinator.rb
157
+ - lib/json_expr/operators/semver_operator.rb
158
+ - lib/json_expr/operators/semver_operators.rb
155
159
  - lib/json_expr/operators/unary_operator.rb
156
160
  - lib/json_expr/operators/value_operator.rb
157
161
  - lib/json_expr/operators/var_operator.rb
162
+ - lib/json_expr/semver_comparator.rb
163
+ - lib/publish_error.rb
158
164
  - lib/scheduled_executor_service.rb
159
165
  - lib/scheduled_thread_pool_executor.rb
160
166
  - lib/string.rb
167
+ - lib/type_utils.rb
161
168
  - lib/variable_parser.rb
162
169
  - lib/variant_assigner.rb
163
170
  - sig/absmartly.rbs
@@ -168,6 +175,7 @@ metadata:
168
175
  homepage_uri: https://github.com/absmartly/ruby-sdk
169
176
  source_code_uri: https://github.com/absmartly/ruby-sdk
170
177
  changelog_uri: https://github.com/absmartly/ruby-sdk
178
+ post_install_message:
171
179
  rdoc_options: []
172
180
  require_paths:
173
181
  - lib
@@ -182,7 +190,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
182
190
  - !ruby/object:Gem::Version
183
191
  version: '0'
184
192
  requirements: []
185
- rubygems_version: 3.7.1
193
+ rubygems_version: 3.5.22
194
+ signing_key:
186
195
  specification_version: 4
187
196
  summary: Absmartly gem
188
197
  test_files: []
data/absmartly.gemspec DELETED
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # $:.push File.expand_path("../lib", __FILE__)
4
- require File.expand_path("lib/absmartly/version", __dir__)
5
- Gem::Specification.new do |spec|
6
- spec.name = "absmartly-sdk"
7
- spec.version = Absmartly::VERSION
8
- spec.authors = ["absmartly"]
9
- spec.email = ["sdks@absmartly.com"]
10
-
11
- spec.summary = "Absmartly gem"
12
- spec.description = "Absmartly gem"
13
-
14
- spec.homepage = "https://github.com/absmartly/ruby-sdk"
15
-
16
- spec.license = "MIT"
17
- spec.required_ruby_version = ">= 2.7.0"
18
- spec.extra_rdoc_files = ["README.md"]
19
-
20
- spec.metadata["homepage_uri"] = spec.homepage
21
- spec.metadata["source_code_uri"] = "https://github.com/absmartly/ruby-sdk"
22
- spec.metadata["changelog_uri"] = "https://github.com/absmartly/ruby-sdk"
23
-
24
- # Specify which files should be added to the gem when it is released.
25
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
- spec.files = Dir.chdir(__dir__) do
27
- `git ls-files -z`.split("\x0").reject do |f|
28
- (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
29
- end
30
- end
31
- spec.bindir = "exe"
32
- spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
- spec.require_paths = ["lib"]
34
-
35
- # Required for Ruby 3.4+ where base64 is no longer a default gem.
36
- # Faraday 2.7.x uses base64 but doesn't declare it as a dependency.
37
- spec.add_dependency "base64", "~> 0.2"
38
- spec.add_dependency "faraday", "~> 2.0"
39
- spec.add_dependency "faraday-net_http_persistent", "~> 2.0"
40
- spec.add_dependency "faraday-retry", "~> 2.0"
41
- spec.add_dependency "murmurhash3", "~> 0.1.7"
42
-
43
- # For more information and examples about making a new gem, check out our
44
- # guide at: https://bundler.io/guides/creating_gem.html
45
- end
data/example/example.rb DELETED
@@ -1,85 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "../lib/absmartly"
4
-
5
- # config file
6
- Absmartly.configure_client do |config|
7
- config.endpoint = "https://demo.absmartly.io/v1"
8
- config.api_key = "x3ZyxmeKmb6n3VilTGs5I6-tBdaS9gYyr3i4YQXmUZcpPhH8nd8ev44NoEL_3yvA"
9
- config.application = "www"
10
- config.environment = "prod"
11
- end
12
-
13
- # define a new context request
14
- context_config = Absmartly.create_context_config
15
- context_config.set_unit("session_id", "bf06d8cb5d8137290c4abb64155584fbdb64d8")
16
- context_config.set_unit("user_id", "123456")
17
-
18
- ctx = Absmartly.create_context(context_config)
19
-
20
- treatment = ctx.treatment("exp_test_ab")
21
- puts(treatment) # 0
22
- treatment1 = ctx.treatment("net_seasons")
23
- puts(treatment1) # 1
24
- treatment2 = ctx.treatment("Experimento!")
25
- puts(treatment2) # 1
26
- treatment3 = ctx.treatment("test")
27
- puts(treatment3) # 1
28
-
29
- ctx.set_unit("db_user_id", 1000013)
30
- ctx.set_units(db_user_id2: 1000013, session_id2: 12311)
31
-
32
- ctx.set_attribute("user_agent", "Chrome 2022")
33
- ctx.set_attributes(
34
- customer_age: "new_customer",
35
- customer_point: 20,
36
- )
37
-
38
- ctx.set_override("new_exp", 3)
39
- ctx.set_overrides(
40
- exp_test_experiment: 1,
41
- exp_another_experiment: 0,
42
- )
43
- ctx.publish
44
- properties = {
45
- value: 125,
46
- fee: 125
47
- }
48
- ctx.track("payment", properties)
49
-
50
- ctx.close
51
-
52
- context_data = Absmartly.context_data
53
- ctx2 = Absmartly.create_context_with(context_config, context_data)
54
-
55
- treatment = ctx2.treatment("exp_test_ab")
56
- puts(treatment) # 0
57
- treatment1 = ctx2.treatment("net_seasons")
58
- puts(treatment1) # 1
59
- treatment2 = ctx2.treatment("Experimento!")
60
- puts(treatment2) # 1
61
- treatment3 = ctx2.treatment("test")
62
- puts(treatment3) # 1
63
-
64
- ctx2.set_unit("db_user_id", 1000013)
65
- ctx2.set_units(db_user_id2: 1000013, session_id2: 12311)
66
-
67
- ctx2.set_attribute("user_agent", "Chrome 2022")
68
- ctx2.set_attributes(
69
- customer_age: "new_customer",
70
- customer_point: 20,
71
- )
72
-
73
- ctx2.set_override("new_exp", 3)
74
- ctx2.set_overrides(
75
- exp_test_experiment: 1,
76
- exp_another_experiment: 0,
77
- )
78
- ctx2.publish
79
- properties = {
80
- value: 125,
81
- fee: 125
82
- }
83
- ctx2.track("payment", properties)
84
-
85
- ctx2.close