Class: BirdGrinder::Tweeter
- Inherits:
-
Object
- Object
- BirdGrinder::Tweeter
- Defined in:
- lib/bird_grinder/tweeter.rb,
lib/bird_grinder/tweeter/search.rb,
lib/bird_grinder/tweeter/streaming.rb,
lib/bird_grinder/tweeter/stream_processor.rb,
lib/bird_grinder/tweeter/streaming_request.rb,
lib/bird_grinder/tweeter/basic_authorization.rb,
lib/bird_grinder/tweeter/oauth_authorization.rb,
lib/bird_grinder/tweeter/abstract_authorization.rb
Overview
An asynchronous, delegate-based twitter client that uses em-http-request and yajl on the backend. It’s built to be fast, minimal and easy to use.
The delegate is simply any class - the tweeter will attempt to call receive_message(, [BirdGrinder::Nash]) every time it processes a message / item of some kind. This in turn makes it easy to process items. Also, it will dispatch both incoming (e.g. :incoming_mention, :incoming_direct_message) and outgoing (e.g. :outgoing_tweet) events.
It has support the twitter search api (via #search) and the currently- alpha twitter streaming api (using #streaming) built right in.
Defined Under Namespace
Classes: AbstractAuthorization, BasicAuthorization, OAuthAuthorization, Search, StreamProcessor, Streaming, StreamingRequest
Constant Summary collapse
- VALID_FETCHES =
[:direct_messages, :mentions]
Instance Method Summary collapse
- #authorization_method ⇒ Object
-
#direct_messages(opts = {}) ⇒ Object
Asynchronously fetches the current users (as specified by your settings) direct messages from the twitter api.
-
#dm(user, text, opts = {}) ⇒ Object
Sends a direct message to a given user.
-
#fetch(*fetches) ⇒ Object
Automates fetching mentions / direct messages at the same time.
-
#follow(user, opts = {}) ⇒ Object
Tells the twitter api to follow a specific user.
-
#follower_ids(id, opts = {}) ⇒ Object
Gets a list ids who are following a given user id / screenname.
-
#initialize(delegate) ⇒ Tweeter
constructor
Initializes the tweeter with a given delegate.
-
#mentions(opts = {}) ⇒ Object
Asynchronously fetches the current users (as specified by your settings) mentions from the twitter api.
-
#reply(user, text, opts = {}) ⇒ Object
Sends a correctly-formatted at reply to a given user.
-
#search(query, opts = {}) ⇒ Object
Uses the twitter search api to look up a given query, with a set of possible options.
-
#streaming ⇒ Object
Returns an instance of BirdGrinder::Tweeter::Streaming, used for accessing the alpha streaming api for twitter.
-
#tweet(message, opts = {}) ⇒ Object
Updates your current status on twitter with a specific message.
-
#unfollow(user, opts = {}) ⇒ Object
Tells the twitter api to unfollow a specific user.
Constructor Details
#initialize(delegate) ⇒ Tweeter
Initializes the tweeter with a given delegate. It will use username and password from your settings file for authorization with twitter.
37 38 39 40 |
# File 'lib/bird_grinder/tweeter.rb', line 37 def initialize(delegate) check_auth! delegate_to delegate end |
Instance Method Details
#authorization_method ⇒ Object
194 195 196 |
# File 'lib/bird_grinder/tweeter.rb', line 194 def ||= (OAuthAuthorization.enabled? ? OAuthAuthorization : BasicAuthorization).new end |
#direct_messages(opts = {}) ⇒ Object
Asynchronously fetches the current users (as specified by your settings) direct messages from the twitter api
140 141 142 143 144 145 146 147 148 |
# File 'lib/bird_grinder/tweeter.rb', line 140 def (opts = {}) logger.debug "Fetching direct messages..." get("direct_messages.json", opts) do |dms| logger.debug "Fetched a total of #{dms.size} direct message(s)" dms.each do |dm| delegate.(:incoming_direct_message, status_to_args(dm, :direct_message)) end end end |
#dm(user, text, opts = {}) ⇒ Object
Sends a direct message to a given user
94 95 96 97 98 99 100 101 |
# File 'lib/bird_grinder/tweeter.rb', line 94 def dm(user, text, opts = {}) text = text.to_s.strip user = user.to_s.strip logger.debug "DM'ing #{user}: #{text}" post("direct_messages/new.json", opts.merge(:user => user, :text => text)) do |json| delegate.(:outgoing_direct_message, status_to_args(json, :direct_message)) end end |
#fetch(*fetches) ⇒ Object
Automates fetching mentions / direct messages at the same time.
45 46 47 48 49 50 51 |
# File 'lib/bird_grinder/tweeter.rb', line 45 def fetch(*fetches) = fetches. fetches = VALID_FETCHES if fetches == [:all] (fetches & VALID_FETCHES).each do |fetch_type| send(fetch_type, .dup) end end |
#follow(user, opts = {}) ⇒ Object
Tells the twitter api to follow a specific user
57 58 59 60 61 62 63 |
# File 'lib/bird_grinder/tweeter.rb', line 57 def follow(user, opts = {}) user = user.to_s.strip logger.info "Following '#{user}'" post("friendships/create.json", opts.merge(:screen_name => user)) do delegate.(:outgoing_follow, N(:user => user)) end end |
#follower_ids(id, opts = {}) ⇒ Object
Gets a list ids who are following a given user id / screenname
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/bird_grinder/tweeter.rb', line 169 def follower_ids(id, opts = {}) cursor_list = [] if opts[:cursor].present? logger.info "Getting page w/ cursor #{opts[:cursor]} for #{id}" get_followers_page(id, opts) do |res| results = BirdGrinder::Nash.new results.cursor = opts[:cursor] results.user_id = id results.ids = res.ids? ? res.ids : [] results.next_cursor = res.next_cursor || 0 results.previous_cursor = res.previous_cursor || 0 results.all = (res.previous_cursor == 0 && res.next_cursor == 0) delegate.(:incoming_follower_ids, results) end else logger.info "Getting all followers for #{id}" get_followers(id, opts.merge(:cursor => -1), N({ :user_id => id, :all => true, :ids => [] })) end end |
#mentions(opts = {}) ⇒ Object
Asynchronously fetches the current users (as specified by your settings) mentions from the twitter api
154 155 156 157 158 159 160 161 162 |
# File 'lib/bird_grinder/tweeter.rb', line 154 def mentions(opts = {}) logger.debug "Fetching mentions..." get("statuses/mentions.json", opts) do |mentions| logger.debug "Fetched a total of #{mentions.size} mention(s)" mentions.each do |status| delegate.(:incoming_mention, status_to_args(status, :mention)) end end end |
#reply(user, text, opts = {}) ⇒ Object
Sends a correctly-formatted at reply to a given user. If the users screen_name isn’t at the start of the tweet, it will be appended accordingly.
129 130 131 132 133 134 |
# File 'lib/bird_grinder/tweeter.rb', line 129 def reply(user, text, opts = {}) user = user.to_s.strip text = text.to_s.strip text = "@#{user} #{text}".strip unless text =~ /^\@#{user}\b/i tweet(text, opts) end |
#search(query, opts = {}) ⇒ Object
Uses the twitter search api to look up a given query, with a set of possible options.
117 118 119 120 |
# File 'lib/bird_grinder/tweeter.rb', line 117 def search(query, opts = {}) @search ||= Search.new(self) @search.search_for(query, opts) end |
#streaming ⇒ Object
Returns an instance of BirdGrinder::Tweeter::Streaming, used for accessing the alpha streaming api for twitter.
107 108 109 |
# File 'lib/bird_grinder/tweeter.rb', line 107 def streaming @streaming ||= Streaming.new(self) end |
#tweet(message, opts = {}) ⇒ Object
Updates your current status on twitter with a specific message
81 82 83 84 85 86 87 |
# File 'lib/bird_grinder/tweeter.rb', line 81 def tweet(, opts = {}) = .to_s.strip logger.debug "Tweeting #{message}" post("statuses/update.json", opts.merge(:status => )) do |json| delegate.(:outgoing_tweet, status_to_args(json)) end end |
#unfollow(user, opts = {}) ⇒ Object
Tells the twitter api to unfollow a specific user
69 70 71 72 73 74 75 |
# File 'lib/bird_grinder/tweeter.rb', line 69 def unfollow(user, opts = {}) user = user.to_s.strip logger.info "Unfollowing '#{user}'" post("friendships/destroy.json", opts.merge(:screen_name => user)) do delegate.(:outgoing_unfollow, N(:user => user)) end end |