class PowerTrack::Rule
A PowerTrack
rule with its components and restrictions.
Constants
- DEFAULT_RULE_FEATURES
The default rule features
- MAX_NEGATIVE_TERMS
The maximum number of negative terms in a single rule value
- MAX_POSITIVE_TERMS
The maximum number of positive terms in a single rule value
- MAX_RULES_BODY_SIZE
The maximum size of the HTTP body accepted by
PowerTrack
/rules calls (in bytes) 5MB since v2- MAX_RULE_VALUE_LENGTH
The maximum lengh of the value of a rule
- MAX_TAG_LENGTH
The maximum length of a rule tag.
Attributes
Public Class Methods
Builds a new rule based on a value and some optional features (:id, :tag).
# File lib/powertrack/rules/rule.rb, line 35 def initialize(value, features=nil) @value = value || '' features = DEFAULT_RULE_FEATURES.merge(features || {}) @tag = features[:tag] @id = features[:id] @error = nil end
Public Instance Methods
Returns true when the rule is equal to the other rule provided.
# File lib/powertrack/rules/rule.rb, line 89 def ==(other) other.class == self.class && other.value == @value && other.tag == @tag end
Returns a hash for the rule based on its components. Useful for using rules as Hash keys.
# File lib/powertrack/rules/rule.rb, line 99 def hash # let's assume a nil value for @value or @tag is not different from the empty value "v:#{@value},t:#{@tag}".hash end
Returns the maximum length of the rule value.
# File lib/powertrack/rules/rule.rb, line 105 def max_value_length MAX_RULE_VALUE_LENGTH end
Converts the rule in a Hash.
# File lib/powertrack/rules/rule.rb, line 76 def to_hash res = {:value => @value} res[:tag] = @tag unless @tag.nil? res[:id] = @id unless @id.nil? res end
Dumps the rule in a valid JSON format.
# File lib/powertrack/rules/rule.rb, line 71 def to_json(options={}) MultiJson.encode(to_hash, options) end
Converts the rule in a string, the JSON representation of the rule actually.
# File lib/powertrack/rules/rule.rb, line 84 def to_s to_json end
Returns true if the rule is valid, false otherwise. The validation error can be through the error method.
# File lib/powertrack/rules/rule.rb, line 45 def valid? # reset error @error = nil validation_rules = [ :too_long_value?, :contains_empty_source?, :contains_negated_or?, :too_long_tag?, :contains_explicit_and?, :contains_lowercase_or?, :contains_explicit_not? ] validation_rules.each do |validator| # stop when 1 validator fails if self.send(validator) @error = validator.to_s.gsub(/_/, ' ').gsub(/\?/, '').capitalize return false end end true end
Protected Instance Methods
Does the rule value contain an empty source ?
# File lib/powertrack/rules/rule.rb, line 137 def contains_empty_source? !@value[/source\:\s/].nil? end
Does the rule value contain a forbidden AND ?
# File lib/powertrack/rules/rule.rb, line 122 def contains_explicit_and? !@value[/ AND /].nil? end
Does the rule value contain a forbidden NOT ?
# File lib/powertrack/rules/rule.rb, line 132 def contains_explicit_not? !@value[/(^| )NOT /].nil? end
Does the rule value contain a forbidden lowercase or ?
# File lib/powertrack/rules/rule.rb, line 127 def contains_lowercase_or? !@value[/ or /].nil? end
Does the rule value contain a forbidden negated OR ?
# File lib/powertrack/rules/rule.rb, line 117 def contains_negated_or? !@value[/\-\w+ OR/].nil? || !@value[/OR \-\w+/].nil? end
Is the rule tag too long ?
# File lib/powertrack/rules/rule.rb, line 142 def too_long_tag? @tag && @tag.size > MAX_TAG_LENGTH end
Is the rule value too long ?
# File lib/powertrack/rules/rule.rb, line 112 def too_long_value? @value.size > max_value_length end