blib/lib/Net/Discident.pm

filter: Statementstmt 80.6% 54 / 67 Branchbran 35.7% 5 / 14 Conditioncond 45.4% 5 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 86.6% 13 / 15 Podpod 66.6% 4 / 6 totaltotal 68.3% 82 / 120 CC 12 SCAR 15.2
CC
12
Cov
70%
query
12.0
__ANON__
6.0
fingerprint
4.0
CRAP 4.6
↑ Summary
1 package Net::Discident;
2  
3 2 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Digest::MD5 qw( md5_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use HTTP::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 our $VERSION = qv( 1.0.1 );
12  
13 2 ▶use constant BASE_URI => 'http://discident.com/v1';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16  
17 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
18 2 my $class = shift;
19 2 my $path = shift;
20
21 2 my $self = {};
22 2 bless $self, $class;
23
24 2 $self->fingerprint( $path );
25
26 2 return $self;
27 }
28  
29 3 ▶sub fingerprint {
Subroutine fingerprint: called CC=4 SCAR=13.9
Pod documented
30 3 my $self = shift;
31 3 my $path = shift;
32 3 my $fingerprint = shift;
33
34 3 ▶ return $self->ident()
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fingerprint or defined $path 2 1 3
Condition66%2/3
defined $fingerprint or defined $path
l!l&&r!l&&!r
102
MC/DC50%1/2
defined $fingerprint or defined $path
defined $fingerprint defined $path
Truth table66%2/3
A or B
A= defined $fingerprintB= defined $path
ABresult
1X1
011
000
35 if !defined $fingerprint && !defined $path;
36
37 1 ▶ $fingerprint = $self->fingerprint_files( $path )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fingerprint 0 1 1
38 if !defined $fingerprint;
39
40 # discident fingerprints are uppercase and hyphenated hex md5s
41 1 my $md5 = uc md5_hex( $fingerprint );
42 1 $md5 =~ s{(.{8})(.{4})(.{4})(.{4})(.*)}{$1-$2-$3-$4-$5};
43
44 1 $self->{'ident'} = $md5;
45
46 1 return $md5;
47 }
48 3 ▶sub ident {
Subroutine ident: called CC=2 SCAR=7.0
Pod documented
49 3 my $self = shift;
50 3 my $ident = shift;
51
52 3 ▶ $self->{'ident'} = $ident
Branch: 1 branch
BranchTrueFalseTotal
if defined $ident 0 3 3
53 if defined $ident;
54
55 3 return $self->{'ident'};
56 }
57 1 ▶sub query {
Subroutine query: called CC=6 SCAR=24.8
Pod documented
58 1 my $self = shift;
59 1 ▶ my $ident = shift // $self->ident();
Condition33%1/3
shift() // $self->ident
l!l&&r!l&&!r
010
MC/DC0%0/2
shift() // $self->ident
shift() $self->ident
Truth table33%1/3
A // B
A= shift()B= $self->ident
ABresult
1X1
011
000
60 1 ▶ my $raw = shift // 0;
Condition50%1/2
shift() // 0
l!l
01
MC/DC0%0/1
shift() // 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
61
62 1 my $uri = $self->query_url( $ident );
63 1 my $http = HTTP::Lite->new();
64 1 ▶ my $code = $http->request( $uri )
Branch: 1 branch
BranchTrueFalseTotal
my $code = $http->request($uri) or die "Unable to fetch ident: $!" 0 1 1
65 or die "Unable to fetch ident: $!";
66
67 0 ▶ die "Unable to fetch ident: HTTP $code"
Branch: 1 branch
BranchTrueFalseTotal
unless 200 == $code 0 0 0
68 unless 200 == $code;
69
70 0 ▶ return $http->body()
Branch: 1 branch
BranchTrueFalseTotal
if $raw 0 0 0
71 if $raw;
72
73 0 return from_json $http->body()
74 }
75 1 ▶sub query_url {
Subroutine query_url: called CC=2 SCAR=7.6
Pod documented
76 1 my $self = shift;
77 1 ▶ my $ident = shift // $self->ident();
Condition33%1/3
shift() // $self->ident
l!l&&r!l&&!r
100
MC/DC0%0/2
shift() // $self->ident
shift() $self->ident
Truth table33%1/3
A // B
A= shift()B= $self->ident
ABresult
1X1
011
000
78
79 1 return sprintf "%s/%s/", BASE_URI, $ident;
80 }
81  
82 0 ▶sub fingerprint_files {
Subroutine fingerprint_files: not called CC=1 SCAR=6.9
Pod undocumented
83 0 my $self = shift;
84 0 my $path = shift;
85
86 0 my $long_fingerprint;
87
88 0 ▶ my $stat_file = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
89 0 ▶ return if -d $_;
Branch: 1 branch
BranchTrueFalseTotal
if -d $_ 0 0 0
90  
91 0 my $stat = stat $_;
92 0 substr $_, 0, length( $path ), '';
93
94 0 $long_fingerprint .= sprintf(
95 ":%s:%lld",
96 $_,
97 $stat->size,
98 );
99 0 };
100
101 0 find(
102 {
103 wanted => $stat_file,
104 no_chdir => 1,
105 },
106 $path,
107 );
108
109 0 return $long_fingerprint;
110 }
111  
112 2 1;
113  
114 __END__
↑ Summary