Sha256: a2e2a433e10fa8d087a6b33c45667e2afc44bd5aeabad20d1188e5c9e74822aa

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

require 'json'

module GnipRule
  class Rule
    attr_accessor :value, :tag

    def initialize(value, tag=nil)
      @value = value
      @tag = tag
    end

    def valid?
      return false if too_long?
      return false if too_many_positive_terms?
      return false if contains_stop_word?
      return false if contains_empty_source?
      return false if contains_negated_or?
      true
    end

    # NOTE: Unused variable for consistency with other to_rule impls
    def to_rule(_=nil)
      self
    end

    def to_json
      to_hash.to_json
    end

    def to_hash
      obj = {:value => @value}
      obj[:tag] = @tag unless @tag.nil?
      obj
    end

    def to_s
      to_json
    end

    protected
    def contains_stop_word?
      stop_words = %W(a an and AND at but by com from http https if in is it its me my NOT or rt the this to too via we www you)
      (stop_words & @value.gsub(/\"[^\"]*\"/, '').split(/\s/)).size > 0
    end

    def too_long?
      @value.size > 2048
    end

    def contains_negated_or?
      !@value[/\-\w+ OR/].nil? || !@value[/OR \-\w+/].nil?
    end

    def too_many_positive_terms?
      @value.scan(/\b\w+|\"[\-\s\w]+\"\b/).size > 30
    end

    def contains_empty_source?
      !@value[/source\:\s/].nil?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gnip-rule-1.0.0 lib/gnip-rule/rule.rb