Catalyst::Plugin::Twitter - simple sending of tweets from within Catalyst


Catalyst-Plugin-Twitter documentation Contained in the Catalyst-Plugin-Twitter distribution.

Index


Code Index:

NAME

Top

Catalyst::Plugin::Twitter - simple sending of tweets from within Catalyst

SYNOPSIS

Top

    # configure your app in MyApp.pm
    MyApp->config(
        name    => 'TestApp',
        twitter => {
            username => 'twitter_account_username',
            password => 'twitter_account_password',
        }
    );

    # then somewhere in your controllers:
    sub register : Local {
        my ( $self, $c ) = @_;

        # --- register new user here ---

        # Send a twitter update about the new user
        $c->tweet("We've got another member - $username just registered!");

        return 1;
    }

DESCRIPTION

Top

Twitter (http://www.twitter.com) is a micro-blogging service that lets you post little updates (up to 140 characters) about yourself. It is used for everything from documenting the trivial events of a person's day to providing instantanous updates on road traffic conditions.

This module makes it trivial to send twitter updates (or 'tweets') from within a Catalyst application. It also attempts to do this is an efficient manner.

METHODS

Top

tweet

    $c->tweet('Hello World');
    $c->tweet(
        {   status => 'Hello World',
            # ...any other arguments that Net::Twitter::update accepts
        }
    );

Send out a tweet.

The arguments can either be a simple string (in which case it is used as the 'status') or a hashref of arguments that the Net::Twitter update method would accept.

The tweet is actually added to a queue and is sent at the end of the request. This means that any delay caused by sending the tweet should not stall your request.

twitter

    my $net_twitter_object = $c->twitter();

Returns the twitter object. Created the first time it is called and then cached for the duration of the request.

NOTE: If you manipulate the object returned then you may affect where the stored tweets are sent at the end of the request so don't do that. Even calling clone on the object may not do what you expect - see the Net::Twitter documentation for more details. If you must amnipulate it and want to make sure that there are no side effects clear the cache afterwards ($C-_twitter(undef)>) and a new object will be created as required.

PRIVATE METHODS

Top

You should not ned to use these methods directly - but they are documented here so that you know what they are and what they do.

setup

Check that the required username and password have been provided. Does not check that the username or password are actually valid.

finalize

Send any tweets that have been queued up after the request is finalized.

_twitter

Accessor to the cached Net::Twitter object for this request.

_twitter_queued_tweets

Accessor to the array of tweets that were generated during this request.

_twitter_send_queued_tweets

    my $count = $c->_twitter_send_queued_tweets;

Method to send all the queued tweets and clear the queue. Returns the number of tweets that were successfully sent.

SEE ALSO

Top

Net::Twitter for the documentation of the object returned by $c-twitter>.

Text::Variations for ways to make your twitter updates less repetitive.

AUTHOR

Top

Edmund von der Burg <evdb@ecclestoad.co.uk>.

http://www.ecclestoad.co.uk/

LICENCE AND COPYRIGHT

Top


Catalyst-Plugin-Twitter documentation Contained in the Catalyst-Plugin-Twitter distribution.
package Catalyst::Plugin::Twitter;
use base qw/Class::Accessor::Fast/;

use strict;
use warnings;

use MRO::Compat;
use Net::Twitter;
use Data::Dumper;

our $VERSION = '0.01';

BEGIN {
    __PACKAGE__->mk_accessors(
        '_twitter',                  # cached Net::Twitter object
        '_twitter_queued_tweets',    # tweets to send at end of request
    );
}

sub tweet {
    my $c    = shift;
    my $args = shift;

    # Convert the args to a hashref if they are a string
    $args = { status => $args } if !ref $args;

    # get the queue, or create it if it does not exist
    my $queue = $c->_twitter_queued_tweets
        || $c->_twitter_queued_tweets( [] );

    # add these argumentns to the queue
    push @$queue, $args;

    return 1;
}

sub twitter {
    my $c = shift;

    my $twitter = $c->_twitter;

    if ( !$twitter ) {
        $twitter = Net::Twitter->new( $c->config->{twitter} );
        $c->_twitter($twitter);
    }

    return $twitter;

}

sub setup {
    my $c = shift;

    my $config = $c->config->{twitter};

    my $err
        = !$config             ? "Could not find 'twitter' section in config"
        : !$config->{username} ? "Must supply a 'username' in config->twitter"
        : !$config->{password} ? "Must supply a 'password' in config->twitter"
        :                        '';

    if ($err) {
        $c->log->fatal($err);
        Catalyst::Exception->throw($err);
    }

    return $c->next::method(@_);
}

sub finalize {
    my $c = shift;

    # process the request
    my $result = $c->next::method(@_);

    # send the queued tweets
    $c->_twitter_send_queued_tweets;

    return $result;
}

sub _twitter_send_queued_tweets {
    my $c     = shift;
    my $count = 0;

    # get the queue of tweets and then clear it
    my $queue = $c->_twitter_queued_tweets || [];
    $c->_twitter_queued_tweets(undef);

    foreach my $args (@$queue) {

        # Send the actual tweet.
        if ( $c->twitter->update($args) ) {
            $count++;
            next;
        }

        # something went wrong
        warn "ERROR with tweet:\n";
        warn Dumper( $c->twitter->get_error );
        warn "---";

    }

    return $count;
}

1;