Changeset efcd223 for lib


Ignore:
Timestamp:
Jul 22, 2009, 8:39:21 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:
a8a0b0a
Parents:
1c45137
Message:
Some code cleanup.

Refactor finding a named account into a find_account function, and set
defaults for config options initially, which lets us remove 'exists'
calls everywhere.
Location:
lib/BarnOwl/Module
Files:
2 edited

Legend:

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

    r1c45137 refcd223  
    1818use Net::Twitter;
    1919use JSON;
     20use List::Util qw(first);
    2021
    2122use BarnOwl;
     
    123124    }
    124125    push @twitter_handles, $twitter_handle;
    125     $default_handle = $twitter_handle if (!defined $twitter_handle && exists $cfg->{default_sender} && $cfg->{default_sender});
     126}
     127
     128$default_handle = first {$_->{cfg}->{default_sender}} @twitter_handles;
     129if (!$default_handle && @twitter_handles) {
     130    $default_handle = $twitter_handles[0];
    126131}
    127132
     
    141146       && $m->auth eq 'YES') {
    142147        for my $handle (@twitter_handles) {
    143             $handle->twitter($m->body) if (!exists $handle->{cfg}->{publish_tweets} || $handle->{cfg}->{publish_tweets});
     148            $handle->twitter($m->body) if $handle->{cfg}->{publish_tweets};
    144149        }
    145150    }
     
    152157    $next_service_to_poll = ($next_service_to_poll + 1) % scalar(@twitter_handles);
    153158   
    154     $handle->poll_twitter() if (!exists $handle->{cfg}->{poll_for_tweets} || $handle->{cfg}->{poll_for_tweets});
    155     $handle->poll_direct() if (!exists $handle->{cfg}->{poll_for_dms} || $handle->{cfg}->{poll_for_dms});
     159    $handle->poll_twitter() if $handle->{cfg}->{poll_for_tweets};
     160    $handle->poll_direct() if $handle->{cfg}->{poll_for_dms};
     161}
     162
     163sub find_account {
     164    my $name = shift;
     165    my $handle = first {$_->{cfg}->{account_nickname} eq $name} @twitter_handles;
     166    if ($handle) {
     167        return $handle;
     168    } else {
     169        die("No such Twitter account: $name\n");
     170    }
    156171}
    157172
     
    161176    my $sent = 0;
    162177    if (defined $account) {
    163         for my $handle (@twitter_handles) {
    164             if (defined $handle->{cfg}->{account_nickname} && $account eq $handle->{cfg}->{account_nickname}) {
    165                 $handle->twitter(@_);
    166                 $sent = 1;
    167                 last;
    168             }
    169         }
    170         BarnOwl::message("No Twitter account named " . $account) unless $sent == 1
     178        my $handle = find_account($account);
     179        $handle->twitter(@_);
    171180    }
    172181    else {
    173182        # broadcast
    174183        for my $handle (@twitter_handles) {
    175             $handle->twitter(@_) if (!exists $handle->{cfg}->{publish_tweets} || $handle->{cfg}->{publish_tweets});
     184            $handle->twitter(@_) if $handle->{cfg}->{publish_tweets};
    176185        }
    177186    }
     
    183192    my $sent = 0;
    184193    if (defined $account) {
    185         for my $handle (@twitter_handles) {
    186             if (defined $handle->{cfg}->{account_nickname} && $account eq $handle->{cfg}->{account_nickname}) {
    187                 $handle->twitter_direct(@_);
    188                 $sent = 1;
    189                 last;
    190             }
    191         }
    192         BarnOwl::message("No Twitter account named " . $account) unless $sent == 1
    193     }
    194     elsif (defined $default_handle) {
     194        my $handle = find_account($account);
     195        $handle->twitter_direct(@_);
     196    } elsif (defined $default_handle) {
    195197        $default_handle->twitter_direct(@_);
    196198    }
     
    205207    my $sent = 0;
    206208    if (defined $account) {
    207         for my $handle (@twitter_handles) {
    208             if (defined $handle->{cfg}->{account_nickname} && $account eq $handle->{cfg}->{account_nickname}) {
    209                 $handle->twitter_atreply(@_);
    210                 $sent = 1;
    211                 last;
    212             }
    213         }
    214         BarnOwl::message("No Twitter account named " . $account) unless $sent == 1
     209        my $handle = find_account($account);
     210        $handle->twitter_atreply(@_);
    215211    }
    216212    elsif (defined $default_handle) {
  • lib/BarnOwl/Module/Twitter/Handle.pm

    r82fd1e6 refcd223  
    3434
    3535sub new {
    36 
    3736    my $class = shift;
    3837    my $cfg = shift;
    3938
     39    $cfg = {
     40        account_nickname => '',
     41        default_sender   => 0,
     42        poll_for_tweets  => 1,
     43        poll_for_dms     => 1,
     44        publish_tweets   => 1,
     45        %$cfg
     46       };
     47
    4048    my $self = {
    41         'user' => undef,
    4249        'cfg'  => $cfg,
    4350        'twitter' => undef,
Note: See TracChangeset for help on using the changeset viewer.