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   227829 use strict;
  6         40  
  6         158  
4 6     6   26 use warnings;
  6         11  
  6         154  
5 6     6   1929 use AnyEvent::Socket qw( tcp_connect );
  6         112415  
  6         342  
6 6     6   2409 use AnyEvent::Handle;
  6         26834  
  6         187  
7 6     6   34 use Carp qw( carp );
  6         10  
  6         3301  
8              
9             # ABSTRACT: Simple asynchronous finger client
10             our $VERSION = '0.14'; # VERSION
11              
12              
13             sub new
14             {
15 8     8 0 1624 my $class = shift;
16 8 50       38 my $args = ref $_[0] eq 'HASH' ? (\%{$_[0]}) : ({@_});
  0         0  
17 8         17 my $port = $args->{port};
18 8 100       21 $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     102 }, $class;
      50        
      100        
25             }
26              
27              
28             sub finger
29             {
30 20     20 1 27045 my $self = shift;
31 20         28 my $request = shift;
32 20 50       43 $request = '' unless defined $request;
33 20   50 0   40 my $callback = shift || sub {};
34 20 100       51 my $args = ref $_[0] eq 'HASH' ? (\%{$_[0]}) : ({@_});
  7         15  
35              
36 20         37 for(qw( hostname port timeout on_error ))
37             {
38 80 100       142 next if defined $args->{$_};
39 71         138 $args->{$_} = $self->{$_};
40             }
41              
42             tcp_connect $args->{hostname}, $args->{port}, sub {
43              
44 20     20   1529 my($fh) = @_;
45 20 50       51 return $args->{on_error}->("unable to connect: $!") unless $fh;
46              
47 20         28 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         1194 $handle->destroy;
59 20         372 $callback->(\@lines);
60             },
61 20         158 );
62              
63 20 50 33     1627 if(ref $request && $request->isa('AnyEvent::Finger::Request'))
64 0         0 { $request = $request->{raw} }
65 20         80 $handle->push_write("$request\015\012");
66              
67             $handle->on_read(sub {
68             $handle->push_read( line => sub {
69 45         1615 my($handle, $line) = @_;
70 45         66 $line =~ s/\015?\012//g;
71 45         84 push @lines, $line;
72 45         2619 });
73 20         1563 });
74              
75 20     20   131 }, sub { $args->{timeout} };
  20         4603  
76              
77 20         1930 $self;
78             }
79              
80             1;
81              
82             __END__