Ignore:
Timestamp:
Jul 21, 2009, 9:46:50 PM (15 years ago)
Author:
Nelson Elhage <nelhage@mit.edu>
Branches:
master, release-1.10, release-1.7, release-1.8, release-1.9
Children:
d748296
Parents:
159aaad
Message:
Fix error handling for incorrect credentials.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/BarnOwl/Module/Twitter/Handle.pm

    r159aaad r7430aa4  
    1919use BarnOwl;
    2020use BarnOwl::Message::Twitter;
     21
    2122sub fail {
    2223    my $self = shift;
    2324    my $msg = shift;
    2425    undef $self->{twitter};
    25     BarnOwl::admin_message('Twitter Error', $msg);
    26     die("Twitter Error: $msg\n");
     26    my $nickname = $self->{cfg}->{account_nickname} || "";
     27    die("[Twitter $nickname] Error: $msg\n");
    2728}
    2829
     
    3738    my $cfg = shift;
    3839
    39     my %obj = (
     40    my $self = {
    4041        'user' => undef,
    4142        'cfg'  => $cfg,
     
    4546        'last_id' => undef,
    4647        'last_direct' => undef,
    47     );
     48    };
     49
     50    bless($self, $class);
    4851
    4952    my %twitter_args = @_;
    5053
    51     $obj{twitter}  = Net::Twitter->new(%twitter_args);
    52 
    53     if(!defined($obj{twitter}->verify_credentials())) {
    54         fail("Invalid twitter credentials");
    55     }
    56 
    57     unless(defined($obj{last_id})) {
     54    $self->{twitter}  = Net::Twitter->new(%twitter_args);
     55
     56    if(!defined($self->{twitter}->verify_credentials())) {
     57        $self->fail("Invalid credentials");
     58    }
     59
     60    unless(defined($self->{last_id})) {
    5861        eval {
    59             $obj{last_id} = $obj{twitter}->friends_timeline({count => 1})->[0]{id};
     62            $self->{last_id} = $self->{twitter}->friends_timeline({count => 1})->[0]{id};
    6063        };
    61         $obj{last_id} = 0 unless defined($obj{last_id});
    62     }
    63 
    64     unless(defined($obj{last_direct})) {
     64        $self->{last_id} = 0 unless defined($self->{last_id});
     65    }
     66
     67    unless(defined($self->{last_direct})) {
    6568        eval {
    66             $obj{last_direct} = $obj{twitter}->direct_messages()->[0]{id};
     69            $self->{last_direct} = $self->{twitter}->direct_messages()->[0]{id};
    6770        };
    68         $obj{last_direct} = 0 unless defined($obj{last_direct});
     71        $self->{last_direct} = 0 unless defined($self->{last_direct});
    6972    }
    7073
    7174    eval {
    72         $obj{twitter}->{ua}->timeout(1);
    73     };
    74 
    75     return bless {%obj}, $class;
    76 
     75        $self->{twitter}->{ua}->timeout(1);
     76    };
     77
     78    return $self;
    7779}
    7880
Note: See TracChangeset for help on using the changeset viewer.