Twitter4R v0.2.0: Status API

Written on 9:35:00 PM by S. Potter

The Status API deals with access to single status objects on the Twitter server. Since "updating" (in the traditional sense) a status in Twitter is non-existent (you update your timeline with a brand new status, but you cannot edit individual status text after created), Twitter4R provide a CRD-style interface (rather than true CRUD-style). For example, to post a new status to your timeline you might write code like the following:


gem('twitter4r', '>=0.2.0')
require('twitter')
client = Twitter::Client.new(:login => 'mylogin', :password => 'mypassword')
status = client.status(:post, 'Learning the Twitter4R Status API in 60 seconds.')
To retrieve the full status object (Twitter::Status instance - see Twitter4R Model API documentation) given a unique status ID you can code:

# Assume code in previous code snippet
status = client.status(:get, 140684282) # => should be announcement status of Twitter4R v0.2.0 release
# Now you can query the attributes of the status like...
puts "#{status.user.screen_name}: #{status.text} @ #{status.created_at}"
To delete the status we initially posted above (or any other status of yours that you have a handle to (a handle can be just the unique integer status ID or the status object itself) we can code:

# Again assume code from first code snippet above...
client.status(:delete, status) # here we can pass in the full object or just the id, it doesn't matter.  Same as in #status(:get, ...) case.

Upgrading from 0.1.x

To upgrade from Twitter4R v0.1.x you probably need to replace code like:

client.update('My status message text')
With code like the following:

client.status(:post, 'My status message text')
There were no APIs in Twitter4R v0.1.x for the single status :get and :delete use cases.

If you enjoyed this post Subscribe to our feed

2 Comments

  1. Brad Y |

    Hi is it possible to run twitter4r at the Observer or Model level, rather than through a controller? I would like to send messages to twitter seperately from the user controller actions.

     
  2. S. Potter |

    Brad: it is certainly possible to use the Twitter4R API from any Ruby code, when inside or outside of a Rails application. Also it does not matter which layer (model or controller) of a Rails application the Twitter4R code appears in (although I would strongly suggest against putting it in the view:)).

    There is nothing in the Twitter4R gem (except the Rails "bridge" which only applied to 1.2x that attempted to patch to_xml defect) that depends on any part of the Rails stack. This dependency is only there if you do a "require('twitter/rails')" and nobody using Rails 2.x or above should be requiring this file is it breaks now (the 0.3.1 development release on GitHub tries to resolve this breakage).

     

Post a Comment