blib/lib/Net/Laconica.pm

filter: Statementstmt 52.0% 50 / 96 Branchbran 20.0% 10 / 50 Conditioncond 47.6% 10 / 21 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 56.2% 9 / 16 Podpod 100% 3 / 3 totaltotal 43.6% 86 / 197 CC 33 SCAR 30.3
CC
33
Cov
42%
__ANON__
56.0
send
30.0
_start_handler
30.0
CRAP 20.6
↑ Summary
1 package Net::Laconica;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use HTML::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Data::Validate qw(is_alphanumeric);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Data::Validate::URI qw(is_http_uri);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = '0.08';
12  
13 2 my $ua = LWP::UserAgent->new;
14 2 $ua->agent('Mozilla');
15 2 $ua->cookie_jar({ file => 'cookies.txt' });
16  
17  
18 20 ▶sub new {
Subroutine new: called CC=9 SCAR=22.1
Pod documented
19 20 my $class = shift;
20 20 my $self = { login => 1, @_ };
21  
22 20 ▶ unless( exists $self->{uri} && exists $self->{username} && exists $self->{password}
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"uri"} and exists $self->{"username"} and exists $self->{"password"} or exists $self->{"uri"} and exists $self->{"username"}) 13 7 20
Condition83%10/12
exists $self->{"uri"} and exists $self->{"username"} and exists $self->{"password"} or exists $self->{"uri"} and exists $self->{"username"}
l!l&&r!l&&!r
0713
exists $self->{"uri"} and exists $self->{"username"} and exists $self->{"password"}
!ll&&!rl&&r
1370
exists $self->{"uri"} and exists $self->{"username"}
!ll&&!rl&&r
767
exists $self->{"uri"} and exists $self->{"username"}
!ll&&!rl&&r
767
MC/DC80%4/5
exists $self->{"uri"} and exists $self->{"username"} and exists $self->{"password"} or exists $self->{"uri"} and exists $self->{"username"}
exists $self->{"uri"} exists $self->{"username"} exists $self->{"password"} exists $self->{"uri"} exists $self->{"username"}
Truth table30%3/10
A and B and C or D and E
A= exists $self->{"uri"}B= exists $self->{"username"}C= exists $self->{"password"}D= exists $self->{"uri"}E= exists $self->{"username"}
ABCDEresult
111XX1
0XX111
10X111
110111
0XX0X0
0XX100
10X0X0
10X100
1100X0
110100
23 or exists $self->{uri} && exists $self->{username} ) {
24 13 croak 'Invalid arguments';
25 }
26  
27 # Sanitise arguments and check for validity
28 7 ▶ is_http_uri($self->{uri}) || croak 'Invalid URI';
Branch: 1 branch
BranchTrueFalseTotal
is_http_uri($self->{"uri"}) or croak("Invalid URI") 5 2 7
29 5 ▶ is_alphanumeric($self->{username}) || croak 'Invalid username';
Branch: 1 branch
BranchTrueFalseTotal
is_alphanumeric($self->{"username"}) or croak("Invalid username") 3 2 5
30  
31 # Append a slash at the end of uri if it does not end with one
32 3 ▶ if( substr($self->{uri}, (length $self->{uri}) - 1, 1) ne '/' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($self->{"uri"}, length($self->{"uri"}) - 1, 1) ne "/") 3 0 3
33 3 $self->{uri} .= '/';
34 }
35  
36 # Convert the username to lowercase and return the blessed reference
37 3 $self->{username} = lc $self->{username};
38 3 bless $self, $class;
39 }
40  
41  
42 1 ▶sub fetch {
Subroutine fetch: called CC=5 SCAR=22.2
Pod documented
43 1 my $self = shift;
44 1 undef $self->{contents};
45 1 my $number;
46  
47 # Get/set the number of messages to be fetched
48 1 ▶ if( @_ == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 0 1 1
elsif (@_ == 0) { } 1 0 1
49 0 $number = shift;
50 0 ▶ if($number > 20) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number > 20) 0 0 0
51 0 $number = 20;
52 }
53 } elsif( @_ == 0 ) {
54 1 $number = 10;
55 } else {
56 0 croak 'Invalid arguments';
57 }
58  
59 # Start fetching messages
60 1 my $p = HTML::Parser->new(api_version => 3);
61 0 ▶ $p->handler(start => sub { $self->_start_handler(@_) }, 'self,tagname,attr');
Subroutine __ANON__: not called CC=1 SCAR=1.2
62 0 ▶ $p->handler(end => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
63 0 ▶ return unless defined $self->{value};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"value"} 0 0 0
64 0 ▶ return if $self->{value} eq 'content' && shift eq 'a';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"value"} eq "content" and shift() eq "a" 0 0 0
Condition0%0/3
$self->{"value"} eq "content" and shift() eq "a"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"value"} eq "content" and shift() eq "a"
$self->{"value"} eq "content" shift() eq "a"
Truth table0%0/3
A and B
A= $self->{"value"} eq "content"B= shift() eq "a"
ABresult
0X0
100
111
65 0 $self->{value} = undef;
66 1 }, 'tagname');
67 1 $p->utf8_mode(1);
68  
69 1 my $response = $ua->get($self->{uri} . $self->{username} . '/all');
70 1 $p->parse($response->content);
71  
72 1 ▶ unless( $self->{login} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"login"}) 0 1 1
73 0 croak 'Incorrect username';
74 }
75  
76 # Ignore the first array element which is undef, and return the rest of the elements
77 1 splice @{$self->{contents}}, 1, $number;
78 }
79  
80  
81 0 ▶sub send {
Subroutine send: not called CC=5 SCAR=34.0
Pod documented
82 0 my $self = shift;
83 0 my $message;
84  
85 0 ▶ unless( exists $self->{password} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"password"}) 0 0 0
86 0 return $self->{login} = 0;
87 }
88  
89 0 ▶ if( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
90 # Strip the message to 140 characters if the message is longer
91 0 $message = shift;
92 0 ▶ if(length $message > 140) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $message > 140) 0 0 0
93 0 $message = substr $message, 0, 140;
94 }
95 } else {
96 0 croak 'Invalid arguments';
97 }
98  
99 # Start sending messages
100 0 my $p = HTML::Parser->new(api_version => 3);
101 0 ▶ $p->handler(start => sub { $self->_start_handler(@_) }, 'self,tagname,attr');
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 0 ▶ $p->handler(end => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
103 0 ▶ return unless defined $self->{value};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"value"} 0 0 0
104 0 ▶ return if $self->{value} eq 'content' && shift eq 'a';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"value"} eq "content" and shift() eq "a" 0 0 0
Condition0%0/3
$self->{"value"} eq "content" and shift() eq "a"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"value"} eq "content" and shift() eq "a"
$self->{"value"} eq "content" shift() eq "a"
Truth table0%0/3
A and B
A= $self->{"value"} eq "content"B= shift() eq "a"
ABresult
0X0
100
111
105 0 $self->{value} = undef;
106 0 }, 'tagname');
107 0 $p->utf8_mode(1);
108  
109 0 my $response = $ua->post($self->{uri} . 'main/login', [nickname => $self->{username}, password => $self->{password}]);
110 0 $p->parse($response->content);
111  
112 # Return 0 if not logged in
113 0 ▶ return 0 unless $self->{login};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"login"} 0 0 0
114 0 $response = $ua->post($self->{uri} . 'notice/new', [status_textarea => $message, returnto => 'all']);
115 }
116  
117  
118 0 ▶sub _start_handler {
Subroutine _start_handler: not called CC=5 SCAR=34.0
119 0 my $class = shift;
120 0 my $self = shift;
121  
122 0 ▶ return unless exists $_[1]->{class};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_[1]{"class"} 0 0 0
123  
124 0 ▶ if( $_[1]->{class} eq 'nickname' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($_[1]{'class'} eq 'nickname') { } 0 0 0
elsif ($_[1]{'class'} eq 'content') { } 0 0 0
elsif ($_[1]{'class'} eq 'error') { } 0 0 0
125 0 $class->{value} = 'nickname';
126 0 $class->{counter}++;
127 } elsif( $_[1]->{class} eq 'content' ) {
128 0 $class->{value} = 'content';
129 } elsif( $_[1]->{class} eq 'error' ) {
130 0 $class->{value} = 'error';
131 }
132  
133 0 ▶ $self->handler(text => sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
134 0 ▶ return unless defined $class->{value};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class->{"value"} 0 0 0
135 0 ▶ if( $class->{value} eq 'content' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($class->{'value'} eq 'content') { } 0 0 0
elsif ($class->{'value'} eq 'nickname') { } 0 0 0
elsif ($class->{'value'} eq 'error') { } 0 0 0
136 0 $class->{contents}[$class->{counter}] .= shift;
137 } elsif( $class->{value} eq 'nickname' ) {
138 0 $class->{contents}[$class->{counter}] .= shift(@_) . ': ';
139 } elsif( $class->{value} eq 'error' ) {
140 0 my $error = shift;
141 0 ▶ if( $error eq 'Incorrect username or password.' || $error eq 'No such user.' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error eq "Incorrect username or password." or $error eq "No such user.") 0 0 0
Condition0%0/3
$error eq "Incorrect username or password." or $error eq "No such user."
l!l&&r!l&&!r
000
MC/DC0%0/2
$error eq "Incorrect username or password." or $error eq "No such user."
$error eq "Incorrect username or password." $error eq "No such user."
Truth table0%0/3
A or B
A= $error eq "Incorrect username or password."B= $error eq "No such user."
ABresult
1X1
011
000
142 0 $class->{login} = 0;
143 }
144 }
145 0 }, 'dtext');
146 }
147  
148 2 1;
149  
150 __END__
↑ Summary