Class: BirdGrinder::Tweeter

Inherits:
Object
  • Object
show all
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

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.

Parameters:

  • delegate (Delegate)

    the delegate class



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_methodObject



194
195
196
# File 'lib/bird_grinder/tweeter.rb', line 194

def authorization_method
  @authorization_method ||= (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

Parameters:

  • opts (Hash) (defaults to: {})

    options to pass in the query string



140
141
142
143
144
145
146
147
148
# File 'lib/bird_grinder/tweeter.rb', line 140

def direct_messages(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.receive_message(: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

Parameters:

  • user (String)

    the screen_name of the user you wish to dm

  • text (String)

    the text to send to the user

  • opts (Hash) (defaults to: {})

    extra options to pass in the query string



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.receive_message(:outgoing_direct_message, status_to_args(json, :direct_message))
  end
end

#fetch(*fetches) ⇒ Object

Automates fetching mentions / direct messages at the same time.

Parameters:

  • fetches (Array<Symbol>)

    what to load - :all for all fetches, or names of the fetches otherwise



45
46
47
48
49
50
51
# File 'lib/bird_grinder/tweeter.rb', line 45

def fetch(*fetches)
  options = fetches.extract_options!
  fetches = VALID_FETCHES if fetches == [:all]
  (fetches & VALID_FETCHES).each do |fetch_type|
    send(fetch_type, options.dup)
  end
end

#follow(user, opts = {}) ⇒ Object

Tells the twitter api to follow a specific user

Parameters:

  • user (String)

    the screen_name of the user to follow

  • opts (Hash) (defaults to: {})

    extra options to pass in the query string



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.receive_message(:outgoing_follow, N(:user => user))
  end
end

#follower_ids(id, opts = {}) ⇒ Object

Gets a list ids who are following a given user id / screenname

Parameters:

  • id (String, Integer)

    the user id or screen name to get followers for.

  • opts (Hash) (defaults to: {})

    extra options to pass in the query string.

Options Hash (opts):

  • :cursor (Integer)

    the cursor offset in the results



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.receive_message(: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

Parameters:

  • opts (Hash) (defaults to: {})

    options to pass in the query string



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.receive_message(: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.

Parameters:

  • user (String)

    the user to reply to’s screen name

  • test (String)

    the text to reply with

  • opts (Hash) (defaults to: {})

    the options to pass in the query string



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.

Parameters:

  • query (String)

    the query you wish to search for

  • opts (Hash) (defaults to: {})

    the opts to query, all except :repeat are sent to twitter.

Options Hash (opts):

  • :repeat (Boolean)

    repeat the query indefinitely, fetching new messages each time



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

#streamingObject

Returns an instance of BirdGrinder::Tweeter::Streaming, used for accessing the alpha streaming api for twitter.

See Also:



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

Parameters:

  • message (String)

    the contents of your tweet

  • opts (Hash) (defaults to: {})

    extra options to pass in the query string



81
82
83
84
85
86
87
# File 'lib/bird_grinder/tweeter.rb', line 81

def tweet(message, opts = {})
  message = message.to_s.strip
  logger.debug "Tweeting #{message}"
  post("statuses/update.json", opts.merge(:status => message)) do |json|
    delegate.receive_message(:outgoing_tweet, status_to_args(json))
  end
end

#unfollow(user, opts = {}) ⇒ Object

Tells the twitter api to unfollow a specific user

Parameters:

  • user (String)

    the screen_name of the user to unfollow

  • opts (Hash) (defaults to: {})

    extra options to pass in the query string



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.receive_message(:outgoing_unfollow, N(:user => user))
  end
end