Sha256: 6a4f19a1bc302da33334c9baad711df2593de1b2f2d25dd598350ee532613d5e

Contents?: true

Size: 1.31 KB

Versions: 5

Compression:

Stored size: 1.31 KB

Contents

# encoding: utf-8

module OneApm
  module Support
    class RulesEngine
      class SegmentTermsRule
        OA_SEGMENT_PLACEHOLDER               = '*'.freeze
        OA_ADJACENT_PLACEHOLDERS_REGEX       = %r{((?:^|/)\*)(?:/\*)*}.freeze
        OA_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!(OA_LEADING_SLASH_REGEX)

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

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

        def collapse_adjacent_placeholder_segments(segments)
          joined = segments.join(OA_SEGMENT_SEPARATOR)
          joined.gsub!(OA_ADJACENT_PLACEHOLDERS_REGEX, OA_ADJACENT_PLACEHOLDERS_REPLACEMENT)
          joined
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
oneapm_rpm-1.4.2 lib/one_apm/support/rules_engine/segment_terms_rule.rb
oneapm_rpm-1.4.1 lib/one_apm/support/rules_engine/segment_terms_rule.rb
oneapm_rpm-1.4.0 lib/one_apm/support/rules_engine/segment_terms_rule.rb
oneapm_rpm-1.3.7 lib/one_apm/support/rules_engine/segment_terms_rule.rb
oneapm_rpm-1.3.7.rc1 lib/one_apm/support/rules_engine/segment_terms_rule.rb