class Earch::Rule

Public Class Methods

new(rule) click to toggle source
# File lib/twing_earch/rule.rb, line 2
def initialize(rule)
  @rule = rule
end

Public Instance Methods

match?(object) click to toggle source
# File lib/twing_earch/rule.rb, line 6
def match?(object)
  @rule.map { |k,v| send(k, parse(v), object )}.all?
end

Private Instance Methods

compare(rule, target) click to toggle source
# File lib/twing_earch/rule.rb, line 84
def compare(rule, target)
  case rule
  when Regexp
    rule =~ target
  when Integer
    rule <= target
  when Range
    rule.include?(target)
  when Array
    if target.is_a?(Array)
      !(rule & target).empty?
    else
      rule.include?(target)
    end
  else
    rule == target
  end
end
favorite_count(rule, object) click to toggle source
# File lib/twing_earch/rule.rb, line 41
def favorite_count(rule, object)
  compare(rule, object.favorite_count)
end
hashtag(rule, object) click to toggle source
# File lib/twing_earch/rule.rb, line 65
def hashtag(rule, object)
  compare(rule, object.hashtags.map { |h| h.text })
end
lang(rule, object) click to toggle source
# File lib/twing_earch/rule.rb, line 49
def lang(rule, object)
  compare(rule, object.lang)
end
mention(rule, object) click to toggle source
# File lib/twing_earch/rule.rb, line 69
def mention(rule, object)
  if !rule.nil? && !rule.empty?
    key = rule.first.is_a?(Integer) ? :id : :screen_name
    compare(rule, object.user_mentions.map { |h| h.send(key) })
  end
end
parse(condition) click to toggle source
# File lib/twing_earch/rule.rb, line 12
def parse(condition)
  if condition.is_a?(Hash)
    [:type, :match].each do |key|
      raise ArgumentError, "'#{key}' key not found" if condition.send(key).nil?
    end

    return Regexp.new(condition.match) if condition.type == 'regexp'
    return Range.new(*condition.match.split('..').map(&:to_i)) if condition.type == 'range'
  end

  condition
end
quote(rule, object) click to toggle source
# File lib/twing_earch/rule.rb, line 76
def quote(rule, object)
  compare(rule, object.quote?)
end
reply(rule, object) click to toggle source
# File lib/twing_earch/rule.rb, line 61
def reply(rule, object)
  compare(rule, object.reply?)
end
retweet(rule, object) click to toggle source
# File lib/twing_earch/rule.rb, line 80
def retweet(rule, object)
  compare(rule, object.retweet?)
end
retweet_count(rule, object) click to toggle source
# File lib/twing_earch/rule.rb, line 45
def retweet_count(rule, object)
  compare(rule, object.retweet_count)
end
screen_name(rule, object) click to toggle source
# File lib/twing_earch/rule.rb, line 37
def screen_name(rule, object)
  compare(rule, object.user.screen_name)
end
source(rule, object) click to toggle source
# File lib/twing_earch/rule.rb, line 57
def source(rule, object)
  compare(rule, object.source.gsub(/<(.+)\">/,"").gsub(/<\/a>/,""))
end
text(rule, object) click to toggle source
# File lib/twing_earch/rule.rb, line 25
def text(rule, object)
  compare(rule, object.text)
end
user_id(rule, object) click to toggle source
# File lib/twing_earch/rule.rb, line 29
def user_id(rule, object)
  compare(rule, object.user.id_str)
end
user_lang(rule, object) click to toggle source
# File lib/twing_earch/rule.rb, line 53
def user_lang(rule, object)
  compare(rule, object.user.lang)
end
user_name(rule, object) click to toggle source
# File lib/twing_earch/rule.rb, line 33
def user_name(rule, object)
  compare(rule, object.user.name)
end