class SmartFilter

Attributes

input[RW]
output[RW]
response[RW]
rule_key[RW]

Public Class Methods

config(opts = {}) click to toggle source

Public: allows changing the thresholds at which the xss, spam & offensive methods will trigger

Returns a hash

# File lib/stoolie/clients/smart_filter.rb, line 31
def self.config(opts =  {})
  opts[:blacklisted_phrases_threshold] ||= 1
  opts[:flagged_phrases_threshold]     ||= 6
  opts[:profanity_threshold]           ||= 3
  opts[:link_density_threshold]        ||= 3
  opts[:spam_features_threshold]       ||= 2
  opts[:javascript_threshold]          ||= 1
  opts[:invalid_tags_threshold]        ||= 1
  opts
end
new(key = nil) click to toggle source
Calls superclass method PrevotySmartFilter::new
# File lib/stoolie/clients/smart_filter.rb, line 12
def initialize(key = nil)
  @config        = SmartFilter.config(Stoolie.config.smart_filter)

  super(key)

  @rule_key       = key || @config[:rule_key]
  @api_key = @key = @config[:api_key]
end
stats_to_thresholds() click to toggle source

Public: retrieves the threshold (configured or default) for a given statistic

Returns a hash of the form {'statistic' => ${threshold value}}

# File lib/stoolie/clients/smart_filter.rb, line 45
def self.stats_to_thresholds
  map = {}
  map['blacklisted_phrases']            = config[:blacklisted_phrases_threshold]
  map['flagged_phrases']                = config[:flagged_phrases_threshold]
  map['prevoty_profanity_features']     = config[:profanity_threshold]
  map['prevoty_link_density']           = config[:link_density_threshold]
  map['prevoty_spam_features']          = config[:spam_features_threshold]

  ['invalid_protocols', 'invalid_attributes', 'invalid_tags'].each do |s|
    map[s] = config[:invalid_tags_threshold]
  end
  ['javascript_tags', 'javascript_protocols', 'javascript_attributes'].each do |s|
    map[s] = config[:javascript_threshold]
  end

  map
end

Public Instance Methods

analyze(input = '') click to toggle source

Public: calls the filter method to test the input

input - the text to be analyzed

Examples

result = analyze('<script>text</script>')
# => #<Stoolie::Result...>
result.insecure?
# => true

Returns a Stoolie::Result object

# File lib/stoolie/clients/smart_filter.rb, line 79
def analyze(input = '')
  @input    = input
  @response = filter!(@input, rule_key)
  @output   = @response['output']
  @stats    = @response['statistics']

  @result   = Stoolie::Result.new(self)
end
filter!(input, rule_key) click to toggle source
Calls superclass method PrevotySmartFilter#filter!
# File lib/stoolie/clients/smart_filter.rb, line 21
def filter!(input, rule_key)
  rule_key ||= @rule_key
  raise SmartFilterBadRuleKey.new unless rule_key

  super(input, rule_key)
end
is_blacklisted?() click to toggle source

Public: checks the result statistics against the thresholds for blacklisted phrases

Returns a boolean

# File lib/stoolie/clients/smart_filter.rb, line 109
def is_blacklisted?
  return false unless @stats

  beyond_threshold?(['blacklisted_phrases'])
end
is_insecure?() click to toggle source

Public: checks the result statistics against the thresholds for security

Returns a boolean

# File lib/stoolie/clients/smart_filter.rb, line 91
def is_insecure?
  return false unless @stats

  beyond_threshold?(['javascript_tags', 'javascript_protocols', 'javascript_attributes', 'invalid_protocols', 'invalid_attributes'])
end
is_offensive?() click to toggle source

Public: checks the result statistics against the thresholds for profanity

Returns a boolean

# File lib/stoolie/clients/smart_filter.rb, line 118
def is_offensive?
  return false unless @stats

  beyond_threshold?(['flagged_phrases', 'prevoty_profanity_features'])
end
is_spam?() click to toggle source

Public: checks the result statistics against the thresholds for spam

Returns a boolean

# File lib/stoolie/clients/smart_filter.rb, line 100
def is_spam?
  return false unless @stats

  beyond_threshold?(['prevoty_spam_features']) || link_density(@stats['prevoty_link_density']) >= stat_to_threshold('prevoty_link_density')
end
stat_to_threshold(stat) click to toggle source
# File lib/stoolie/clients/smart_filter.rb, line 63
def stat_to_threshold(stat)
  SmartFilter.stats_to_thresholds[stat.to_s]
end

Private Instance Methods

beyond_threshold?(stats = []) click to toggle source
# File lib/stoolie/clients/smart_filter.rb, line 130
def beyond_threshold?(stats = [])
  stats.any? { |stat| @stats[stat] >= stat_to_threshold(stat) }
end