Module: Tweetlr::Processors::Twitter
- Includes:
- LogAware
- Defined in:
- lib/tweetlr/processors/twitter.rb
Overview
utilities for dealing with twitter
Class Method Summary collapse
- .apply_twitter_api_configuration(config) ⇒ Object
- .call_twitter_api(search_call, config, lazy = false) ⇒ Object
- .call_twitter_with(search_call, config, lazy) ⇒ Object
-
.extract_links(tweet) ⇒ Object
extract the links from a given tweet.
-
.lazy_search(config) ⇒ Object
lazy update - search for a term or refresh the search if a response is available already.
- .log ⇒ Object
-
.retweet?(message) ⇒ Boolean
checks if the message is a retweet.
-
.search(config) ⇒ Object
fire a new search.
Methods included from LogAware
Class Method Details
.apply_twitter_api_configuration(config) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/tweetlr/processors/twitter.rb', line 67 def self.apply_twitter_api_configuration(config) ::Twitter.configure do |configuration| configuration.consumer_key = config[:twitter_app_consumer_key] configuration.consumer_secret = config[:twitter_app_consumer_secret] configuration.oauth_token = config[:twitter_oauth_token] configuration.oauth_token_secret = config[:twitter_oauth_token_secret] end end |
.call_twitter_api(search_call, config, lazy = false) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/tweetlr/processors/twitter.rb', line 51 def self.call_twitter_api(search_call, config, lazy=false) apply_twitter_api_configuration config max_attempts = 3 num_attempts = 0 begin num_attempts += 1 call_twitter_with search_call, config, lazy rescue ::Twitter::Error::TooManyRequests => error if num_attempts <= max_attempts sleep error.rate_limit.reset_in retry else log.error "Twitter API rate limit exceeded - going to sleep for error.rate_limit.reset_in seconds. (#{error})" end end end |
.call_twitter_with(search_call, config, lazy) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/tweetlr/processors/twitter.rb', line 75 def self.call_twitter_with(search_call, config, lazy) if lazy response = ::Twitter.search(search_call, :since_id => config['since_id'] || config[:since_id], :count => config[:results_per_page], :result_type => config[:result_type]) else response = ::Twitter.search(search_call, :count => config[:results_per_page], :result_type => config[:result_type]) end response end |
.extract_links(tweet) ⇒ Object
extract the links from a given tweet
20 21 22 23 24 25 |
# File 'lib/tweetlr/processors/twitter.rb', line 20 def self.extract_links(tweet) if tweet text = tweet['text'] text.gsub(/https?:\/\/[\S]+/).to_a if text end end |
.lazy_search(config) ⇒ Object
lazy update - search for a term or refresh the search if a response is available already
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/tweetlr/processors/twitter.rb', line 37 def self.lazy_search(config) log.debug "#{self}::lazy_search called with config #{config}" response = nil if config search_term = config['search_term'] || config[:search_term] || config['terms'] || config[:terms] search_call = "#{search_term.gsub('+', ' OR ')} filter:links" log.info "lazy search using '#{search_call}, :since_id => #{config['since_id'] || config[:since_id]}, :count => #{config['results_per_page']}, :result_type => #{config['result_type']})'" response = self.call_twitter_api(search_call, config, :lazy) else log.error "#{self}.lazy_search: no config given!" end response end |
.log ⇒ Object
10 11 12 |
# File 'lib/tweetlr/processors/twitter.rb', line 10 def self.log Tweetlr::LogAware.log #TODO why doesn't the include make the log method accessible? end |
.retweet?(message) ⇒ Boolean
checks if the message is a retweet
15 16 17 |
# File 'lib/tweetlr/processors/twitter.rb', line 15 def self.retweet?() .index('RT @') || .index(%{"@}) || .index("\u201c@") || .index('MT @') #detect retweets end |
.search(config) ⇒ Object
fire a new search
28 29 30 31 32 33 34 |
# File 'lib/tweetlr/processors/twitter.rb', line 28 def self.search(config) search_call = "#{config['search_term'].gsub('+', ' OR ')} filter:links" log.debug "#{self}::search search_call: #{search_call}" response = self.call_twitter_api(search_call, config) log.debug "#{self}::call_twitter_api response: #{response.inspect}" response end |