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,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The immutable outcome of one context data fetch. Client#context_data returns
4
+ # a new one per call, so concurrent fetches on a shared Client can't observe
5
+ # each other's in-flight state.
6
+ module Absmartly
7
+ class ContextDataResult
8
+ attr_reader :data_future, :exception, :promise
9
+
10
+ def self.failure(exception, promise = nil)
11
+ new(nil, exception, promise)
12
+ end
13
+
14
+ def initialize(data_future, exception = nil, promise = nil)
15
+ @data_future = data_future
16
+ @exception = exception
17
+ @promise = promise
18
+ freeze
19
+ end
20
+
21
+ def success?
22
+ @exception.nil? && !@data_future.nil?
23
+ end
24
+
25
+ alias data data_future
26
+ end
27
+ end
@@ -8,7 +8,10 @@ class ContextEventLogger
8
8
  PUBLISH = "publish"
9
9
  EXPOSURE = "exposure"
10
10
  GOAL = "goal"
11
+ # Logged by Context#close, as in v1.3.0.
11
12
  CLOSE = "close"
13
+ # Logged by Context#finalize, the name the other ABsmartly SDKs use.
14
+ FINALIZE = "finalize"
12
15
  end
13
16
 
14
17
  def handle_event(event, data)
@@ -8,6 +8,6 @@ class ContextEventLoggerCallback < ContextEventLogger
8
8
  end
9
9
 
10
10
  def handle_event(event, data)
11
- @callable.call(event, data) if @callable.present?
11
+ @callable.call(event, data) if @callable
12
12
  end
13
13
  end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "context_event_handler"
4
+
5
+ module Absmartly
6
+ # The name the other ABsmartly SDKs use for ContextEventHandler. Namespaced
7
+ # so it can't clash with an application's own ContextPublisher.
8
+ ContextPublisher = ::ContextEventHandler
9
+ end
@@ -6,8 +6,8 @@ class DefaultAudienceDeserializer < AudienceDeserializer
6
6
  attr_accessor :log, :reader
7
7
 
8
8
  def deserialize(bytes, offset, length)
9
- JSON.parse(bytes[offset..length], symbolize_names: true)
10
- rescue JSON::ParserError
9
+ JSON.parse(bytes[offset, length], symbolize_names: true)
10
+ rescue StandardError
11
11
  nil
12
12
  end
13
13
  end
@@ -8,9 +8,9 @@ class DefaultContextDataDeserializer < ContextDataDeserializer
8
8
  attr_accessor :log, :reader
9
9
 
10
10
  def deserialize(bytes, offset, length)
11
- parse = JSON.parse(bytes[offset..length], symbolize_names: true)
12
- @reader = ContextData.new(parse[:experiments])
13
- rescue JSON::ParserError
11
+ parse = JSON.parse(bytes[offset, length], symbolize_names: true)
12
+ @reader = ContextData.new(parse[:experiments], parse[:holdouts])
13
+ rescue StandardError
14
14
  nil
15
15
  end
16
16
  end
@@ -39,7 +39,8 @@ class DefaultContextEventSerializer < ContextEventSerializer
39
39
  overridden: x.overridden,
40
40
  fullOn: x.full_on,
41
41
  custom: x.custom,
42
- audienceMismatch: x.audience_mismatch
42
+ audienceMismatch: x.audience_mismatch,
43
+ ruleOverride: x.rule_override
43
44
  }
44
45
  end unless event.exposures.nil?
45
46
 
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "context_publisher"
4
+ require_relative "default_context_event_handler"
5
+
6
+ module Absmartly
7
+ # The name the other ABsmartly SDKs use for DefaultContextEventHandler.
8
+ DefaultContextPublisher = ::DefaultContextEventHandler
9
+ end
@@ -7,6 +7,12 @@ require "uri"
7
7
  require_relative "http_client"
8
8
 
9
9
  class DefaultHttpClient < HttpClient
10
+ RETRY_METHODS = %i[get put].freeze
11
+ RETRY_IF = lambda do |env, exception|
12
+ RETRY_METHODS.include?(env[:method]) &&
13
+ (env[:method] == :get || !exception.is_a?(Faraday::ConnectionFailed))
14
+ end
15
+
10
16
  attr_accessor :config, :session
11
17
 
12
18
  def self.create(config)
@@ -16,19 +22,34 @@ class DefaultHttpClient < HttpClient
16
22
  def initialize(config)
17
23
  @config = config
18
24
  @session = Faraday.new("") do |f|
19
- f.request :retry,
20
- max: config.max_retries,
21
- interval: config.retry_interval,
22
- interval_randomness: 0.5,
23
- backoff_factor: 2
25
+ f.request :retry, **self.class.retry_options(config)
24
26
  f.options.timeout = config.connect_timeout
25
27
  f.options.open_timeout = config.connection_request_timeout
26
- f.adapter :net_http_persistent, pool_size: 20 do |http|
27
- http.idle_timeout = 5
28
+ f.adapter :net_http_persistent, pool_size: config.pool_size do |http|
29
+ http.idle_timeout = config.pool_idle_timeout
28
30
  end
29
31
  end
30
32
  end
31
33
 
34
+ # Timeouts are retried on GET and PUT, as in v1.3.0. Refused connections
35
+ # (GET only) and status codes are opt-in: with the default backoff a refused
36
+ # connection would otherwise block for ~15 s, and a retried publish may
37
+ # duplicate events.
38
+ def self.retry_options(config)
39
+ exceptions = Faraday::Retry::Middleware::DEFAULT_EXCEPTIONS
40
+ exceptions += [Faraday::ConnectionFailed] if config.retry_connection_failures
41
+ {
42
+ max: config.max_retries,
43
+ interval: config.retry_interval,
44
+ interval_randomness: 0.5,
45
+ backoff_factor: 2,
46
+ exceptions: exceptions,
47
+ retry_statuses: Array(config.retry_statuses),
48
+ methods: [],
49
+ retry_if: RETRY_IF
50
+ }
51
+ end
52
+
32
53
  # def context_data
33
54
  # end
34
55
 
@@ -4,7 +4,11 @@ class DefaultHttpClientConfig
4
4
  attr_accessor :connect_timeout,
5
5
  :connection_request_timeout,
6
6
  :retry_interval,
7
- :max_retries
7
+ :max_retries,
8
+ :pool_size,
9
+ :pool_idle_timeout,
10
+ :retry_statuses,
11
+ :retry_connection_failures
8
12
 
9
13
  def self.create
10
14
  DefaultHttpClientConfig.new
@@ -15,5 +19,10 @@ class DefaultHttpClientConfig
15
19
  @connection_request_timeout = 3.0
16
20
  @retry_interval = 0.5
17
21
  @max_retries = 5
22
+ @pool_size = 20
23
+ @pool_idle_timeout = 5
24
+ # e.g. [502, 503, 504]
25
+ @retry_statuses = []
26
+ @retry_connection_failures = false
18
27
  end
19
28
  end
@@ -7,7 +7,7 @@ class DefaultVariableParser < VariableParser
7
7
 
8
8
  def parse(context, experiment_name, variant_name, config)
9
9
  JSON.parse(config, symbolize_names: true)
10
- rescue JSON::ParserError
10
+ rescue StandardError
11
11
  nil
12
12
  end
13
13
  end
data/lib/hashing.rb CHANGED
@@ -3,7 +3,44 @@
3
3
  require "digest"
4
4
 
5
5
  class Hashing
6
- def self.hash_unit(str)
7
- Digest::MD5.base64digest(str.to_s).gsub("==", "").gsub("+", "-").gsub("/", "_")
6
+ REPLACEMENT = "�"
7
+ # A UTF-16 surrogate pair written as two 3-byte sequences (CESU-8 / WTF-8).
8
+ ENCODED_SURROGATE_PAIR = /\xED([\xA0-\xAF])([\x80-\xBF])\xED([\xB0-\xBF])([\x80-\xBF])/n.freeze
9
+ # A lone surrogate written as a 3-byte sequence.
10
+ ENCODED_SURROGATE = /\xED[\xA0-\xBF][\x80-\xBF]/n.freeze
11
+
12
+ # Hashes the unit's to_s bytes as v1.3.0 does, so every unit keeps its
13
+ # bucket: integers from their exact decimal text, floats from Float#to_s and
14
+ # strings from their own bytes, whatever their encoding.
15
+ def self.hash_unit(value)
16
+ base64_url_no_padding(Digest::MD5.digest(value.to_s.b))
17
+ end
18
+
19
+ def self.base64_url_no_padding(bytes)
20
+ [bytes.to_s.b].pack("m0").delete("=").tr("+/", "-_")
21
+ end
22
+
23
+ # Returns the UTF-8 form of the unit's Unicode text (hash_unit keeps v1.3.0's
24
+ # raw bytes). Characters outside the
25
+ # BMP become the standard 4-byte sequence and each lone surrogate becomes
26
+ # U+FFFD, matching the JavaScript SDK's TextEncoder-based encoding. Strings in
27
+ # another encoding (e.g. UTF-16) are transcoded; binary strings are read as
28
+ # UTF-8 bytes.
29
+ def self.to_utf8(value)
30
+ str = value.to_s
31
+ if str.encoding == Encoding::UTF_8 || str.encoding == Encoding::BINARY || str.encoding == Encoding::US_ASCII
32
+ return str if str.encoding == Encoding::UTF_8 && str.valid_encoding?
33
+
34
+ bytes = str.b
35
+ bytes = bytes.gsub(ENCODED_SURROGATE_PAIR) do
36
+ high = 0xD800 | ((Regexp.last_match(1).ord & 0x0F) << 6) | (Regexp.last_match(2).ord & 0x3F)
37
+ low = 0xDC00 | ((Regexp.last_match(3).ord & 0x0F) << 6) | (Regexp.last_match(4).ord & 0x3F)
38
+ [0x10000 + ((high - 0xD800) << 10) + (low - 0xDC00)].pack("U").b
39
+ end
40
+ bytes = bytes.gsub(ENCODED_SURROGATE, REPLACEMENT.b)
41
+ bytes.force_encoding(Encoding::UTF_8).scrub(REPLACEMENT)
42
+ else
43
+ str.encode(Encoding::UTF_8, invalid: :replace, undef: :replace, replace: REPLACEMENT)
44
+ end
8
45
  end
9
46
  end
@@ -3,12 +3,16 @@
3
3
  require_relative "experiment"
4
4
 
5
5
  class ContextData
6
- attr_accessor :experiments
6
+ attr_accessor :experiments, :holdouts
7
7
 
8
- def initialize(experiments = [])
8
+ # Holdouts decode with the Experiment model. @holdouts is only set when the
9
+ # payload has a holdouts array, so a payload without one builds the same
10
+ # object as before. An entry that can't be decoded is dropped on its own.
11
+ def initialize(experiments = [], holdouts = nil)
9
12
  @experiments = experiments.map do |experiment|
10
13
  Experiment.new(experiment)
11
14
  end unless experiments.nil?
15
+ @holdouts = holdouts.filter_map { |holdout| decode_holdout(holdout) } if holdouts.is_a?(Array)
12
16
  self
13
17
  end
14
18
 
@@ -16,7 +20,7 @@ class ContextData
16
20
  return true if self.object_id == o.object_id
17
21
  return false if o.nil? || self.class != o.class
18
22
 
19
- @experiments == o.experiments
23
+ @experiments == o.experiments && holdouts == o.holdouts
20
24
  end
21
25
 
22
26
  def hash_code
@@ -28,4 +32,23 @@ class ContextData
28
32
  "experiments='" + @experiments.join +
29
33
  "}"
30
34
  end
35
+
36
+ private
37
+ # A holdout only needs its id, iteration, seeds and split, so nested fields
38
+ # that can't be decoded are dropped rather than the whole holdout.
39
+ def decode_holdout(holdout)
40
+ return holdout if holdout.is_a?(Experiment)
41
+ return nil unless holdout.is_a?(Hash)
42
+
43
+ begin
44
+ Experiment.new(holdout)
45
+ rescue StandardError
46
+ Experiment.new(holdout.reject { |key, _| NESTED_HOLDOUT_KEYS.include?(key.to_s) })
47
+ end
48
+ rescue StandardError
49
+ nil
50
+ end
51
+
52
+ NESTED_HOLDOUT_KEYS = %w[applications variants customFieldValues custom_field_values].freeze
53
+ private_constant :NESTED_HOLDOUT_KEYS
31
54
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../string"
4
+ require_relative "../type_utils"
4
5
  require_relative "experiment_application"
5
6
  require_relative "experiment_variant"
6
7
  require_relative "custom_field_value"
@@ -8,32 +9,53 @@ require_relative "custom_field_value"
8
9
  class Experiment
9
10
  attr_accessor :id, :name, :unit_type, :iteration, :seed_hi, :seed_lo, :split,
10
11
  :traffic_seed_hi, :traffic_seed_lo, :traffic_split, :full_on_variant,
11
- :applications, :variants, :audience_strict, :audience, :custom_field_values
12
+ :applications, :variants, :audience_strict, :audience, :custom_field_values,
13
+ :assignment_rules, :holdout_ids, :holdout_type
14
+
15
+ ALLOWED_KEYS = %i[
16
+ id name unitType unit_type iteration seedHi seed_hi seedLo seed_lo split
17
+ trafficSeedHi traffic_seed_hi trafficSeedLo traffic_seed_lo trafficSplit traffic_split
18
+ fullOnVariant full_on_variant applications variants audienceStrict audience_strict
19
+ audience customFieldValues custom_field_values assignmentRules assignment_rules
20
+ holdoutIds holdout_ids holdoutType holdout_type
21
+ ].freeze
12
22
 
13
23
  def initialize(args = {})
14
24
  args.each do |name, value|
15
- if name == :applications
25
+ key_sym = name.to_sym
26
+ key_str = Absmartly::TypeUtils.underscore(name.to_s)
27
+
28
+ next unless ALLOWED_KEYS.include?(key_sym) || ALLOWED_KEYS.include?(key_str.to_sym)
29
+
30
+ if key_str == "applications"
16
31
  @applications = assign_to_klass(ExperimentApplication, value)
17
- elsif name == :variants
32
+ elsif key_str == "variants"
18
33
  @variants = assign_to_klass(ExperimentVariant, value)
19
- elsif name == :customFieldValues
34
+ elsif key_str == "custom_field_values"
20
35
  if value != nil
21
36
  @custom_field_values = assign_to_klass(CustomFieldValue, value)
22
37
  end
23
38
  else
24
- self.instance_variable_set("@#{name.to_s.underscore}", value)
39
+ self.instance_variable_set("@#{key_str}", value)
25
40
  end
26
41
  end
27
42
  @audience_strict ||= false
28
43
  self
29
44
  end
30
45
 
46
+ NESTED_FIELDS = {
47
+ ExperimentApplication => %w[name],
48
+ ExperimentVariant => %w[name config],
49
+ CustomFieldValue => %w[name value type]
50
+ }.freeze
51
+
31
52
  def assign_to_klass(klass, arr)
32
53
  arr.map do |item|
33
- return item if item.is_a?(klass)
54
+ next if item.nil?
55
+ next item if item.is_a?(klass)
34
56
 
35
- klass.new(*item.values)
36
- end
57
+ klass.new(*NESTED_FIELDS.fetch(klass).map { |field| item.key?(field.to_sym) ? item[field.to_sym] : item[field] })
58
+ end.compact
37
59
  end
38
60
 
39
61
  def ==(o)
@@ -47,7 +69,9 @@ class Experiment
47
69
  @unit_type == that.unit_type && @split == that.split &&
48
70
  @traffic_split == that.traffic_split && @applications == that.applications &&
49
71
  @variants == that.variants && @audience_strict == that.audience_strict &&
50
- @audience == that.audience && @custom_field_values == that.custom_field_values
72
+ @audience == that.audience && @custom_field_values == that.custom_field_values &&
73
+ @assignment_rules == that.assignment_rules &&
74
+ holdout_ids == that.holdout_ids && holdout_type == that.holdout_type
51
75
  end
52
76
 
53
77
  def hash_code
@@ -63,7 +87,8 @@ class Experiment
63
87
  full_on_variant: @full_on_variant,
64
88
  audience_strict: @audience_strict,
65
89
  audience: @audience,
66
- custom_field_values: @custom_field_values
90
+ custom_field_values: @custom_field_values,
91
+ assignment_rules: @assignment_rules
67
92
  }
68
93
  end
69
94
 
@@ -85,6 +110,7 @@ class Experiment
85
110
  ", audienceStrict=#{@audience_strict}" +
86
111
  ", audience='#{@audience}'" +
87
112
  ", custom_field_values='#{@custom_field_values}'" +
113
+ ", assignment_rules='#{@assignment_rules}'" +
88
114
  "}"
89
115
  end
90
116
  end
data/lib/json/exposure.rb CHANGED
@@ -2,12 +2,12 @@
2
2
 
3
3
  class Exposure
4
4
  attr_accessor :id, :name, :unit, :variant, :exposed_at, :assigned, :eligible,
5
- :overridden, :full_on, :custom, :audience_mismatch
5
+ :overridden, :full_on, :custom, :audience_mismatch, :rule_override
6
6
 
7
7
  def initialize(id = nil, name = nil, unit = nil, variant = nil,
8
8
  exposed_at = nil, assigned = nil, eligible = nil,
9
9
  overridden = nil, full_on = nil, custom = nil,
10
- audience_mismatch = nil)
10
+ audience_mismatch = nil, rule_override = false)
11
11
  @id = id || 0
12
12
  @name = name
13
13
  @unit = unit
@@ -19,6 +19,7 @@ class Exposure
19
19
  @full_on = full_on
20
20
  @custom = custom
21
21
  @audience_mismatch = audience_mismatch
22
+ @rule_override = rule_override
22
23
  end
23
24
 
24
25
  def ==(o)
@@ -29,7 +30,8 @@ class Exposure
29
30
  @variant == o.variant && @exposed_at == o.exposed_at &&
30
31
  @assigned == o.assigned && @eligible == o.eligible &&
31
32
  @overridden == o.overridden && @full_on == o.full_on &&
32
- @custom == o.custom && @audience_mismatch == o.audience_mismatch
33
+ @custom == o.custom && @audience_mismatch == o.audience_mismatch &&
34
+ @rule_override == o.rule_override
33
35
  end
34
36
 
35
37
  def hash_code
@@ -38,7 +40,8 @@ class Exposure
38
40
  variant: @variant, exposed_at: @exposed_at,
39
41
  assigned: @assigned, eligible: @eligible,
40
42
  overridden: @overridden, full_on: @full_on,
41
- custom: @custom, audience_mismatch: @audience_mismatch
43
+ custom: @custom, audience_mismatch: @audience_mismatch,
44
+ rule_override: @rule_override
42
45
  }
43
46
  end
44
47
 
@@ -55,6 +58,7 @@ class Exposure
55
58
  ", full_on=#{@full_on}" +
56
59
  ", custom=#{@custom}" +
57
60
  ", audience_mismatch=#{@audience_mismatch}" +
61
+ ", rule_override=#{@rule_override}" +
58
62
  "}"
59
63
  end
60
64
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../string"
4
+ require_relative "../type_utils"
4
5
  require_relative "./evaluator"
5
6
  EMPTY_MAP = {}
6
7
  EMPTY_LIST = []
@@ -94,10 +95,11 @@ class ExprEvaluator < Evaluator
94
95
 
95
96
  if lhs.is_a?(Numeric)
96
97
  rvalue = number_convert(rhs)
97
- return lhs.to_f.to_s.casecmp(rvalue.to_s) unless rvalue.nil?
98
+ return nil if rvalue.nil?
99
+ return lhs.to_f <=> rvalue.to_f
98
100
  elsif lhs.is_a?(String)
99
101
  rvalue = string_convert(rhs)
100
- return lhs.compare_to(rvalue) unless rvalue.nil?
102
+ return Absmartly::TypeUtils.compare_strings(lhs, rvalue) unless rvalue.nil?
101
103
  elsif lhs.is_a?(TrueClass) || lhs.is_a?(FalseClass)
102
104
  rvalue = boolean_convert(rhs)
103
105
  return lhs.to_s.casecmp(rvalue.to_s) unless rvalue.nil?
@@ -108,14 +110,10 @@ class ExprEvaluator < Evaluator
108
110
  end
109
111
  end
110
112
 
113
+ # Kept from v1.3.0, which defined it here; the SDK itself uses
114
+ # Absmartly::TypeUtils.wrap_array.
111
115
  class Array
112
116
  def self.wrap(object)
113
- if object.nil?
114
- []
115
- elsif object.respond_to?(:to_ary)
116
- object.to_ary || [object]
117
- else
118
- [object]
119
- end
117
+ Absmartly::TypeUtils.wrap_array(object)
120
118
  end
121
119
  end
@@ -1,22 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "./expr_evaluator"
4
- require 'json_expr/operators/and_combinator'
5
- require 'json_expr/operators/binary_operator'
6
- require 'json_expr/operators/boolean_combinator'
7
- require 'json_expr/operators/equals_operator'
8
- require 'json_expr/operators/greater_than_operator'
9
- require 'json_expr/operators/greater_than_or_equal_operator'
10
- require 'json_expr/operators/in_operator'
11
- require 'json_expr/operators/less_than_operator'
12
- require 'json_expr/operators/less_than_or_equal_operator'
13
- require 'json_expr/operators/match_operator'
14
- require 'json_expr/operators/nil_operator'
15
- require 'json_expr/operators/not_operator'
16
- require 'json_expr/operators/or_combinator'
17
- require 'json_expr/operators/unary_operator'
18
- require 'json_expr/operators/value_operator'
19
- require 'json_expr/operators/var_operator'
4
+ require "json_expr/operators/and_combinator"
5
+ require "json_expr/operators/binary_operator"
6
+ require "json_expr/operators/boolean_combinator"
7
+ require "json_expr/operators/equals_operator"
8
+ require "json_expr/operators/greater_than_operator"
9
+ require "json_expr/operators/greater_than_or_equal_operator"
10
+ require "json_expr/operators/in_operator"
11
+ require "json_expr/operators/less_than_operator"
12
+ require "json_expr/operators/less_than_or_equal_operator"
13
+ require "json_expr/operators/match_operator"
14
+ require "json_expr/operators/nil_operator"
15
+ require "json_expr/operators/not_operator"
16
+ require "json_expr/operators/or_combinator"
17
+ require "json_expr/operators/unary_operator"
18
+ require "json_expr/operators/value_operator"
19
+ require "json_expr/operators/var_operator"
20
+ require "json_expr/operators/semver_operators"
20
21
 
21
22
  class JsonExpr
22
23
  attr_accessor :operators
@@ -36,7 +37,12 @@ class JsonExpr
36
37
  "gt": GreaterThanOperator.new,
37
38
  "gte": GreaterThanOrEqualOperator.new,
38
39
  "lt": LessThanOperator.new,
39
- "lte": LessThanOrEqualOperator.new
40
+ "lte": LessThanOrEqualOperator.new,
41
+ "semver_eq": Absmartly::SemverEqualsOperator.new,
42
+ "semver_gt": Absmartly::SemverGreaterThanOperator.new,
43
+ "semver_gte": Absmartly::SemverGreaterThanOrEqualOperator.new,
44
+ "semver_lt": Absmartly::SemverLessThanOperator.new,
45
+ "semver_lte": Absmartly::SemverLessThanOrEqualOperator.new
40
46
  }
41
47
  end
42
48
 
@@ -1,12 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "boolean_combinator"
4
+ require_relative "../../type_utils"
4
5
 
5
6
  class AndCombinator
6
7
  include BooleanCombinator
7
8
 
8
9
  def combine(evaluator, exprs)
9
- Array.wrap(exprs).each do |expr|
10
+ Absmartly::TypeUtils.wrap_array(exprs).each do |expr|
10
11
  return false unless evaluator.boolean_convert(evaluator.evaluate(expr))
11
12
  end
12
13
  true
@@ -16,7 +16,9 @@ class InOperator
16
16
  return !needle_string.nil? && haystack.include?(needle_string)
17
17
  elsif haystack.is_a?(Hash)
18
18
  needle_string = evaluator.string_convert(needle)
19
- return !needle_string.nil? && haystack.key?(needle_string)
19
+ # Keys may be strings or, for audience JSON parsed with
20
+ # symbolize_names, symbols.
21
+ return !needle_string.nil? && (haystack.key?(needle_string) || haystack.key?(needle_string.to_sym))
20
22
  end
21
23
  nil
22
24
  end
@@ -7,11 +7,15 @@ class MatchOperator
7
7
 
8
8
  def binary(evaluator, lhs, rhs)
9
9
  text = evaluator.string_convert(lhs)
10
- unless text.nil?
11
- pattern = evaluator.string_convert(rhs)
12
- unless pattern.nil?
13
- text.match(pattern)
14
- end
10
+ return nil if text.nil?
11
+
12
+ pattern = evaluator.string_convert(rhs)
13
+ return nil if pattern.nil?
14
+
15
+ begin
16
+ Regexp.new(pattern).match?(text)
17
+ rescue RegexpError
18
+ nil
15
19
  end
16
20
  end
17
21
  end
@@ -1,14 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "boolean_combinator"
4
+ require_relative "../../type_utils"
4
5
 
5
6
  class OrCombinator
6
7
  include BooleanCombinator
7
8
 
8
9
  def combine(evaluator, exprs)
9
- Array.wrap(exprs).each do |expr|
10
+ wrapped = Absmartly::TypeUtils.wrap_array(exprs)
11
+ wrapped.each do |expr|
10
12
  return true if evaluator.boolean_convert(evaluator.evaluate(expr))
11
13
  end
12
- Array.wrap(exprs).empty?
14
+ wrapped.empty?
13
15
  end
14
16
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "binary_operator"
4
+ require_relative "../semver_comparator"
5
+
6
+ module Absmartly
7
+ module SemverOperator
8
+ include BinaryOperator
9
+
10
+ def binary(evaluator, lhs, rhs)
11
+ result = Absmartly::SemverComparator.compare(evaluator, lhs, rhs)
12
+ return nil if result.nil?
13
+
14
+ compare_result(result)
15
+ end
16
+ end
17
+ end