| 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
Condition66%2/3
defined $fingerprint or defined $path
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
| |||||||||||||||||||||||||||||
| 35 | if !defined $fingerprint && !defined $path; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 1 | ▶ | $fingerprint = $self->fingerprint_files( $path ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
shift() // $self->ident
shift()
$self->ident
Truth table33%1/3
A // B
A= shift()B= $self->ident
| |||||||||||||||||||||||||||||
| 60 | 1 | ▶ | my $raw = shift // 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() // 0
MC/DC0%0/1
shift() // 0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 65 | or die "Unable to fetch ident: $!"; | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | die "Unable to fetch ident: HTTP $code" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | unless 200 == $code; | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | return $http->body() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
shift() // $self->ident
shift()
$self->ident
Truth table33%1/3
A // B
A= shift()B= $self->ident
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||