File Coverage

blib/lib/AnyEvent/Finger/Client.pm
Criterion Covered Total %
statement 45 51 88.2
branch 10 14 71.4
condition 7 11 63.6
subroutine 9 11 81.8
pod 1 2 50.0
total 72 89 80.9


line stmt bran cond sub pod time code
1             package AnyEvent::Finger::Client;
2              
3 6     6   271883 use strict;
  6         43  
  6         174  
4 6     6   29 use warnings;
  6         11  
  6         189  
5 6     6   2384 use AnyEvent::Socket qw( tcp_connect );
  6         133962  
  6         405  
6 6     6   2887 use AnyEvent::Handle;
  6         31988  
  6         209  
7 6     6   41 use Carp qw( carp );
  6         10  
  6         3765  
8              
9             # ABSTRACT: Simple asynchronous finger client
10             our $VERSION = '0.11'; # VERSION
11              
12              
13             sub new
14             {
15 8     8 0 2103 my $class = shift;
16 8 50       42 my $args = ref $_[0] eq 'HASH' ? (\%{$_[0]}) : ({@_});
  0         0  
17 8         20 my $port = $args->{port};
18 8 100       28 $port = 79 unless defined $port;
19             bless {
20             hostname => $args->{hostname} || '127.0.0.1',
21             port => $port,
22             timeout => $args->{timeout} || 60,
23 0     0   0 on_error => $args->{on_error} || sub { carp $_[0] },
24 8   100     121 }, $class;
      50        
      100        
25             }
26              
27              
28             sub finger
29             {
30 20     20 1 34341 my $self = shift;
31 20         35 my $request = shift;
32 20 50       52 $request = '' unless defined $request;
33 20   50 0   50 my $callback = shift || sub {};
34 20 100       59 my $args = ref $_[0] eq 'HASH' ? (\%{$_[0]}) : ({@_});
  7         14  
35            
36 20         48 for(qw( hostname port timeout on_error ))
37             {
38 80 100       172 next if defined $args->{$_};
39 71         149 $args->{$_} = $self->{$_};
40             }
41            
42             tcp_connect $args->{hostname}, $args->{port}, sub {
43            
44 20     20   1827 my($fh) = @_;
45 20 50       60 return $args->{on_error}->("unable to connect: $!") unless $fh;
46            
47 20         40 my @lines;
48            
49             my $handle;
50             $handle = AnyEvent::Handle->new(
51             fh => $fh,
52             on_error => sub {
53 0         0 my ($hdl, $fatal, $msg) = @_;
54 0         0 $args->{on_error}->($msg);
55 0         0 $_[0]->destroy;
56             },
57             on_eof => sub {
58 20         1432 $handle->destroy;
59 20         496 $callback->(\@lines);
60             },
61 20         183 );
62            
63 20 50 33     1949 if(ref $request && $request->isa('AnyEvent::Finger::Request'))
64 0         0 { $request = $request->{raw} }
65 20         130 $handle->push_write("$request\015\012");
66            
67             $handle->on_read(sub {
68             $handle->push_read( line => sub {
69 45         1964 my($handle, $line) = @_;
70 45         97 $line =~ s/\015?\012//g;
71 45         100 push @lines, $line;
72 45         3313 });
73 20         1779 });
74            
75 20     20   185 }, sub { $args->{timeout} };
  20         5470  
76            
77 20         2785 $self;
78             }
79              
80             1;
81              
82             __END__