Class: NewRelic::Agent::RulesEngine::SegmentTermsRule
- Inherits:
-
Object
- Object
- NewRelic::Agent::RulesEngine::SegmentTermsRule
- Defined in:
- lib/new_relic/agent/rules_engine/segment_terms_rule.rb
Constant Summary collapse
- PREFIX_KEY =
'prefix'.freeze
- TERMS_KEY =
'terms'.freeze
- SEGMENT_PLACEHOLDER =
'*'.freeze
- ADJACENT_PLACEHOLDERS_REGEX =
%r{((?:^|/)\*)(?:/\*)*}.freeze
- ADJACENT_PLACEHOLDERS_REPLACEMENT =
'\1'.freeze
Instance Attribute Summary collapse
- #prefix ⇒ Object readonly
- #terms ⇒ Object readonly
Class Method Summary collapse
Instance Method Summary collapse
- #apply(string) ⇒ Object
- #collapse_adjacent_placeholder_segments(segments) ⇒ Object
-
#initialize(options) ⇒ SegmentTermsRule
constructor
A new instance of SegmentTermsRule.
- #matches?(string) ⇒ Boolean
- #prefix_matches_on_segment_boundary?(string) ⇒ Boolean
- #terminal? ⇒ Boolean
Constructor Details
#initialize(options) ⇒ SegmentTermsRule
Returns a new instance of SegmentTermsRule.
31 32 33 34 35 |
# File 'lib/new_relic/agent/rules_engine/segment_terms_rule.rb', line 31 def initialize() @prefix = [PREFIX_KEY] @terms = [TERMS_KEY] @trim_range = (@prefix.size..-1) end |
Instance Attribute Details
#prefix ⇒ Object (readonly)
15 16 17 |
# File 'lib/new_relic/agent/rules_engine/segment_terms_rule.rb', line 15 def prefix @prefix end |
#terms ⇒ Object (readonly)
15 16 17 |
# File 'lib/new_relic/agent/rules_engine/segment_terms_rule.rb', line 15 def terms @terms end |
Class Method Details
.valid?(rule_spec) ⇒ Boolean
17 18 19 20 21 |
# File 'lib/new_relic/agent/rules_engine/segment_terms_rule.rb', line 17 def self.valid?(rule_spec) rule_spec[PREFIX_KEY].kind_of?(String) && rule_spec[TERMS_KEY].kind_of?(Array) && valid_prefix_segment_count?(rule_spec[PREFIX_KEY]) end |
.valid_prefix_segment_count?(prefix) ⇒ Boolean
23 24 25 26 27 28 29 |
# File 'lib/new_relic/agent/rules_engine/segment_terms_rule.rb', line 23 def self.valid_prefix_segment_count?(prefix) count = prefix.count(SEGMENT_SEPARATOR) rindex = prefix.rindex(SEGMENT_SEPARATOR) (count == 2 && prefix[rindex + 1].nil?) || (count == 1 && !prefix[rindex + 1].nil?) end |
Instance Method Details
#apply(string) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/new_relic/agent/rules_engine/segment_terms_rule.rb', line 51 def apply(string) rest = string[@trim_range] leading_slash = rest.slice!(LEADING_SLASH_REGEX) segments = rest.split(SEGMENT_SEPARATOR, -1) segments.map! { |s| @terms.include?(s) ? s : SEGMENT_PLACEHOLDER } transformed_suffix = collapse_adjacent_placeholder_segments(segments) "#{@prefix}#{leading_slash}#{transformed_suffix}" end |
#collapse_adjacent_placeholder_segments(segments) ⇒ Object
61 62 63 64 65 |
# File 'lib/new_relic/agent/rules_engine/segment_terms_rule.rb', line 61 def collapse_adjacent_placeholder_segments(segments) joined = segments.join(SEGMENT_SEPARATOR) joined.gsub!(ADJACENT_PLACEHOLDERS_REGEX, ADJACENT_PLACEHOLDERS_REPLACEMENT) joined end |
#matches?(string) ⇒ Boolean
41 42 43 44 |
# File 'lib/new_relic/agent/rules_engine/segment_terms_rule.rb', line 41 def matches?(string) string.start_with?(@prefix) && (prefix_matches_on_segment_boundary?(string) || string.size == @prefix.size) end |
#prefix_matches_on_segment_boundary?(string) ⇒ Boolean
46 47 48 49 |
# File 'lib/new_relic/agent/rules_engine/segment_terms_rule.rb', line 46 def prefix_matches_on_segment_boundary?(string) string.size > @prefix.size && string[@prefix.chomp(SEGMENT_SEPARATOR).size].chr == SEGMENT_SEPARATOR end |
#terminal? ⇒ Boolean
37 38 39 |
# File 'lib/new_relic/agent/rules_engine/segment_terms_rule.rb', line 37 def terminal? true end |