jrgifford
1/18/2012 - 9:48 PM

keyword plugin for Earthquake(Ruby based Twitter client)

keyword plugin for Earthquake(Ruby based Twitter client)

# keyword plugin
# ====
#
# config example:
#
#     Earthquake.config[:keyword] = {:pattern => /ruby|rails|earthquake/i}
#
Earthquake.init do
  _ = config[:keyword] ||= {}
  _[:notify] = true if _[:notify].nil?
  _[:filter] = false if _[:filter].nil?

  output_filter do |item|
    next if item["text"].nil? || item["user"]["id_str"] == twitter.info['id_str']
    next true if _[:pattern].nil?
    words = item["text"].scan(_[:pattern])
    unless words.empty?
      item["_highlights"] ||= []
      item["_highlights"] += words
      true
    else
      _[:filter] && item["_stream"] ? false : true
    end
  end

  output do |item|
    next if item["text"].nil? || item["user"]["id_str"] == twitter.info['id_str']
    next unless !_[:pattern].nil? && _[:notify] && item["_stream"] && item["text"]
    text = item["text"].u
    if text =~ _[:pattern] && item["user"]["screen_name"] != twitter.info["screen_name"]
      notify text, :title => item["user"]["screen_name"]
    end
  end
end