absmartly-sdk 1.3.0 → 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 (44) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +2 -1
  3. data/CHANGELOG.md +52 -1
  4. data/Gemfile +4 -0
  5. data/Gemfile.lock +5 -1
  6. data/README.md +463 -145
  7. data/lib/a_b_smartly.rb +118 -22
  8. data/lib/absmartly/version.rb +1 -1
  9. data/lib/absmartly.rb +4 -0
  10. data/lib/audience_matcher.rb +93 -1
  11. data/lib/client.rb +44 -12
  12. data/lib/client_config.rb +3 -1
  13. data/lib/context.rb +1288 -236
  14. data/lib/context_data_result.rb +27 -0
  15. data/lib/context_event_logger.rb +3 -0
  16. data/lib/context_event_logger_callback.rb +1 -1
  17. data/lib/context_publisher.rb +9 -0
  18. data/lib/default_audience_deserializer.rb +2 -2
  19. data/lib/default_context_data_deserializer.rb +3 -3
  20. data/lib/default_context_event_serializer.rb +2 -1
  21. data/lib/default_context_publisher.rb +9 -0
  22. data/lib/default_http_client.rb +26 -5
  23. data/lib/default_http_client_config.rb +6 -1
  24. data/lib/default_variable_parser.rb +1 -1
  25. data/lib/hashing.rb +39 -2
  26. data/lib/json/context_data.rb +26 -3
  27. data/lib/json/experiment.rb +36 -10
  28. data/lib/json/exposure.rb +8 -4
  29. data/lib/json_expr/expr_evaluator.rb +7 -9
  30. data/lib/json_expr/json_expr.rb +23 -17
  31. data/lib/json_expr/operators/and_combinator.rb +2 -1
  32. data/lib/json_expr/operators/in_operator.rb +3 -1
  33. data/lib/json_expr/operators/match_operator.rb +9 -5
  34. data/lib/json_expr/operators/or_combinator.rb +4 -2
  35. data/lib/json_expr/operators/semver_operator.rb +17 -0
  36. data/lib/json_expr/operators/semver_operators.rb +50 -0
  37. data/lib/json_expr/semver_comparator.rb +98 -0
  38. data/lib/publish_error.rb +11 -0
  39. data/lib/scheduled_thread_pool_executor.rb +8 -0
  40. data/lib/type_utils.rb +59 -0
  41. data/lib/variant_assigner.rb +1 -1
  42. metadata +10 -4
  43. data/absmartly.gemspec +0 -45
  44. data/example/example.rb +0 -85
@@ -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,14 +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
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - absmartly
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-01-26 00:00:00.000000000 Z
11
+ date: 2026-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: base64
@@ -98,8 +98,6 @@ files:
98
98
  - LICENSE.txt
99
99
  - README.md
100
100
  - Rakefile
101
- - absmartly.gemspec
102
- - example/example.rb
103
101
  - lib/a_b_smartly.rb
104
102
  - lib/a_b_smartly_config.rb
105
103
  - lib/absmartly.rb
@@ -112,15 +110,18 @@ files:
112
110
  - lib/context_config.rb
113
111
  - lib/context_data_deserializer.rb
114
112
  - lib/context_data_provider.rb
113
+ - lib/context_data_result.rb
115
114
  - lib/context_event_handler.rb
116
115
  - lib/context_event_logger.rb
117
116
  - lib/context_event_logger_callback.rb
118
117
  - lib/context_event_serializer.rb
118
+ - lib/context_publisher.rb
119
119
  - lib/default_audience_deserializer.rb
120
120
  - lib/default_context_data_deserializer.rb
121
121
  - lib/default_context_data_provider.rb
122
122
  - lib/default_context_event_handler.rb
123
123
  - lib/default_context_event_serializer.rb
124
+ - lib/default_context_publisher.rb
124
125
  - lib/default_http_client.rb
125
126
  - lib/default_http_client_config.rb
126
127
  - lib/default_variable_parser.rb
@@ -153,12 +154,17 @@ files:
153
154
  - lib/json_expr/operators/nil_operator.rb
154
155
  - lib/json_expr/operators/not_operator.rb
155
156
  - lib/json_expr/operators/or_combinator.rb
157
+ - lib/json_expr/operators/semver_operator.rb
158
+ - lib/json_expr/operators/semver_operators.rb
156
159
  - lib/json_expr/operators/unary_operator.rb
157
160
  - lib/json_expr/operators/value_operator.rb
158
161
  - lib/json_expr/operators/var_operator.rb
162
+ - lib/json_expr/semver_comparator.rb
163
+ - lib/publish_error.rb
159
164
  - lib/scheduled_executor_service.rb
160
165
  - lib/scheduled_thread_pool_executor.rb
161
166
  - lib/string.rb
167
+ - lib/type_utils.rb
162
168
  - lib/variable_parser.rb
163
169
  - lib/variant_assigner.rb
164
170
  - sig/absmartly.rbs
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