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