Module: Chatterbot::Search

Included in:
Bot
Defined in:
lib/chatterbot/search.rb

Overview

handle Twitter searches

Constant Summary collapse

MAX_SEARCH_TWEETS =

set a reasonable limit on the maximum number of tweets we will ever return. otherwise it is possible to exceed Twitter’s rate limits

1000

Instance Method Summary collapse

Instance Method Details

#exclude_retweetsObject

exclude retweets from searches



16
17
18
# File 'lib/chatterbot/search.rb', line 16

def exclude_retweets
  @skip_retweets = true
end

#include_retweetsObject

include retweets from searches



23
24
25
# File 'lib/chatterbot/search.rb', line 23

def include_retweets
  @skip_retweets = false
end

#search(queries, opts = {}, &block) ⇒ Object

internal search code



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/chatterbot/search.rb', line 36

def search(queries, opts = {}, &block)
  debug "check for tweets since #{since_id}"
  
  max_tweets = opts.delete(:limit) || MAX_SEARCH_TWEETS
  
  if queries.is_a?(String)
    queries = [queries]
  end

  query = queries.join(" OR ")
  
  #
  # search twitter
  #

  debug "search: #{query} #{default_opts.merge(opts)}"
  @current_tweet = nil
  client.search( query, default_opts.merge(opts) ).take(max_tweets).each { |s|
    update_since_id(s)
    debug s.text

    if block_given? && valid_tweet?(s)
      @current_tweet = s
      yield s
    end
  }
  @current_tweet = nil

end

#skippable_retweet?(t) ⇒ Boolean

check if this is a retweet that we want to skip

Returns:

  • (Boolean)


31
32
33
# File 'lib/chatterbot/search.rb', line 31

def skippable_retweet?(t)
  @skip_retweets && t.retweeted_status?
end