Sha256: 5652f9fc5beabb3ffefe72cf19ef7c21755058ee9a5e258f96671c948ebea7ea

Contents?: true

Size: 1.28 KB

Versions: 37

Compression:

Stored size: 1.28 KB

Contents

# encoding: utf-8

module OneApm
  module Support
    class RulesEngine
      class SegmentTermsRule
        SEGMENT_PLACEHOLDER               = '*'.freeze
        ADJACENT_PLACEHOLDERS_REGEX       = %r{((?:^|/)\*)(?:/\*)*}.freeze
        ADJACENT_PLACEHOLDERS_REPLACEMENT = '\1'.freeze

        attr_reader :prefix, :terms

        def initialize(options)
          @prefix          = options['prefix']
          @terms           = options['terms']
          @trim_range      = (@prefix.size..-1)
        end

        def terminal?
          true
        end

        def matches?(string)
          string.start_with?(@prefix)
        end

        def apply(string)
          rest          = string[@trim_range]
          leading_slash = rest.slice!(LEADING_SLASH_REGEX)

          segments = rest.split(SEGMENT_SEPARATOR)
          segments.map! { |s| @terms.include?(s) ? s : SEGMENT_PLACEHOLDER }
          transformed_suffix = collapse_adjacent_placeholder_segments(segments)

          "#{@prefix}#{leading_slash}#{transformed_suffix}"
        end

        def collapse_adjacent_placeholder_segments(segments)
          joined = segments.join(SEGMENT_SEPARATOR)
          joined.gsub!(ADJACENT_PLACEHOLDERS_REGEX, ADJACENT_PLACEHOLDERS_REPLACEMENT)
          joined
        end
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
oneapm_rpm-1.3.6 lib/one_apm/support/rules_engine/segment_terms_rule.rb
oneapm_rpm-1.3.6.rc1 lib/one_apm/support/rules_engine/segment_terms_rule.rb
oneapm_rpm-1.3.5 lib/one_apm/support/rules_engine/segment_terms_rule.rb
oneapm_rpm-1.3.5.rc1 lib/one_apm/support/rules_engine/segment_terms_rule.rb
oneapm_rpm-1.3.4 lib/one_apm/support/rules_engine/segment_terms_rule.rb
oneapm_rpm-1.3.4.rc1 lib/one_apm/support/rules_engine/segment_terms_rule.rb
oneapm_rpm-1.3.3 lib/one_apm/support/rules_engine/segment_terms_rule.rb
oneapm_rpm-1.3.3.rc1 lib/one_apm/support/rules_engine/segment_terms_rule.rb
oneapm_rpm-1.3.2 lib/one_apm/support/rules_engine/segment_terms_rule.rb
oneapm_rpm-1.3.2.rc1 lib/one_apm/support/rules_engine/segment_terms_rule.rb
oneapm_rpm-1.3.1 lib/one_apm/support/rules_engine/segment_terms_rule.rb
oneapm_rpm-1.3.1.rc1 lib/one_apm/support/rules_engine/segment_terms_rule.rb
oneapm_rpm-1.3.0 lib/one_apm/support/rules_engine/segment_terms_rule.rb
oneapm_rpm-1.3.0.rc1 lib/one_apm/support/rules_engine/segment_terms_rule.rb
oneapm_rpm-1.2.9 lib/one_apm/support/rules_engine/segment_terms_rule.rb
oneapm_rpm-1.2.9.rc1 lib/one_apm/support/rules_engine/segment_terms_rule.rb
oneapm_rpm-1.2.8 lib/one_apm/support/rules_engine/segment_terms_rule.rb
oneapm_rpm-1.2.8.rc1 lib/one_apm/support/rules_engine/segment_terms_rule.rb
oneapm_rpm-1.2.7 lib/one_apm/support/rules_engine/segment_terms_rule.rb
oneapm_rpm-1.2.7.rc1 lib/one_apm/support/rules_engine/segment_terms_rule.rb