blib/lib/WWW/Pastebin/PastebinCa/Retrieve.pm

filter: Statementstmt 95.4% 63 / 66 Branchbran 50.0% 13 / 26 Conditioncond 36.3% 4 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 90.0% 9 / 10 Podpod n/a totaltotal 74.1% 89 / 120 CC 18 SCAR 17.3
CC
18
Cov
78%
_get_was_successful
10.1
_format_date
5.2
_ordinal_suffix
4.8
CRAP 5.6
↑ Summary
1 package WWW::Pastebin::PastebinCa::Retrieve;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = '1.001003'; # VERSION
7  
8 3 ▶use base 'WWW::Pastebin::Base::Retrieve';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # pastebin.ca was rebuilt in 2026 behind a modern API. The old numeric
13 # ("legacy") pastes were restored and are served through two endpoints:
14 # GET /raw/<id> -> the raw paste body (text/plain)
15 # GET /api/v1/legacy/<id> -> JSON metadata about the paste
16 # There is no longer an HTML page to scrape, so we fetch the raw body via
17 # the base class and grab the remaining metadata from the JSON probe.
18  
19 3 my $Base = 'https://pastebin.ca';
20  
21 2 ▶sub _make_uri_and_id {
Subroutine _make_uri_and_id: called CC=3 SCAR=11.3
22 2 my ( $self, $id ) = @_;
23  
24 # Accept either a bare numeric id or any pastebin.ca URL pointing at one.
25 2 $id =~ s{\s+}{}g;
26 2 $id =~ s{^https?://}{}i;
27 2 $id =~ s{^(?:www\.)?pastebin\.ca/}{}i;
28 2 $id =~ s{/(?:raw|dl|edit|embed).*$}{}i;
29 2 $id =~ s{^raw/}{}i;
30  
31 2 ▶ return $self->_set_error(
Branch: 1 branch
BranchTrueFalseTotal
unless length $id and $id =~ /\A[0-9]+\z/ 0 2 2
Condition33%1/3
length $id and $id =~ /\A[0-9]+\z/
!ll&&!rl&&r
002
MC/DC0%0/2
length $id and $id =~ /\A[0-9]+\z/
length $id $id =~ /\A[0-9]+\z/
Truth table33%1/3
A and B
A= length $idB= $id =~ /\A[0-9]+\z/
ABresult
0X0
100
111
32 q|Doesn't look like a correct paste ID or URI|
33 ) unless length $id and $id =~ /\A[0-9]+\z/;
34  
35 # The URI the base class fetches is the raw-body endpoint; that response
36 # becomes the paste content in _get_was_successful().
37 2 return ( URI->new("$Base/raw/$id"), $id );
38 }
39  
40 2 ▶sub _get_was_successful {
Subroutine _get_was_successful: called CC=9 SCAR=23.1
41 2 my ( $self, $content ) = @_;
42  
43 # $content here is the raw paste body from GET /raw/<id>.
44 2 $self->content( $content );
45  
46 2 my %data = ( content => $content );
47  
48 # Pull the remaining metadata (title, language, date) from the JSON probe.
49 2 my $meta_uri = "$Base/api/v1/legacy/" . $self->id;
50 2 my $meta_res = $self->ua->get( $meta_uri );
51 2 ▶ if ( $meta_res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta_res->is_success) 2 0 2
52 2 my $json = eval { JSON::PP::decode_json( $meta_res->decoded_content ) };
53 2 ▶ my $p = ref $json eq 'HASH' ? $json->{paste} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $json eq 'HASH' ? : 2 0 2
54 2 ▶ if ( ref $p eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p eq "HASH") 2 0 2
55 2 ▶ $data{name} = defined $p->{title} ? $p->{title} : 'Unnamed';
Branch: 1 branch
BranchTrueFalseTotal
defined $p->{'title'} ? : 2 0 2
56 $data{language} = defined $p->{syntax_hint}
57 2 ▶ ? $p->{syntax_hint} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $p->{'syntax_hint'} ? : 2 0 2
58 2 $data{post_date} = _format_date( $p->{created_at} );
59 }
60 }
61  
62 # These keys always exist for a consistent return shape, even when the
63 # metadata probe is unavailable.
64 2 ▶ $data{name} = 'Unnamed' unless defined $data{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data{"name"} 0 2 2
65 2 ▶ $data{language} = '' unless defined $data{language};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data{"language"} 0 2 2
66 2 ▶ $data{post_date} = '' unless defined $data{post_date};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data{"post_date"} 0 2 2
67 # pastebin.ca no longer stores a separate paste description; kept for
68 # backwards compatibility of the return shape.
69 2 $data{desc} = '';
70  
71 2 return $self->results( \%data );
72 }
73  
74 2 ▶sub _format_date {
Subroutine _format_date: called CC=5 SCAR=16.5
75 2 my $ms = shift;
76 2 ▶ return '' unless defined $ms and $ms =~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ms and $ms =~ /\A\d+\z/ 0 2 2
Condition33%1/3
defined $ms and $ms =~ /\A\d+\z/
!ll&&!rl&&r
002
MC/DC0%0/2
defined $ms and $ms =~ /\A\d+\z/
defined $ms $ms =~ /\A\d+\z/
Truth table33%1/3
A and B
A= defined $msB= $ms =~ /\A\d+\z/
ABresult
0X0
100
111
77 2 my $epoch = int( $ms / 1000 );
78 2 my @t = gmtime $epoch;
79 # e.g. "Thursday, March 6th, 2008 at 3:57:44pm UTC"
80 2 my $day = POSIX::strftime( '%A', @t );
81 2 my $month = POSIX::strftime( '%B', @t );
82 2 my $mday = $t[3];
83 2 my $year = $t[5] + 1900;
84 2 my $suffix = _ordinal_suffix( $mday );
85 2 my $hour24 = $t[2];
86 2 ▶ my $ampm = $hour24 >= 12 ? 'pm' : 'am';
Branch: 1 branch
BranchTrueFalseTotal
$hour24 >= 12 ? : 2 0 2
87 2 ▶ my $hour12 = $hour24 % 12; $hour12 = 12 unless $hour12;
Branch: 1 branch
BranchTrueFalseTotal
unless $hour12 0 2 2
88 2 my $time = sprintf '%d:%02d:%02d%s', $hour12, $t[1], $t[0], $ampm;
89 2 return "$day, $month ${mday}${suffix}, $year at $time UTC";
90 }
91  
92 2 ▶sub _ordinal_suffix {
Subroutine _ordinal_suffix: called CC=4 SCAR=15.6
93 2 my $n = shift;
94 2 ▶ return 'th' if $n % 100 >= 11 and $n % 100 <= 13;
Branch: 1 branch
BranchTrueFalseTotal
if $n % 100 >= 11 and $n % 100 <= 13 0 2 2
Condition33%1/3
$n % 100 >= 11 and $n % 100 <= 13
!ll&&!rl&&r
200
MC/DC0%0/2
$n % 100 >= 11 and $n % 100 <= 13
$n % 100 >= 11 $n % 100 <= 13
Truth table33%1/3
A and B
A= $n % 100 >= 11B= $n % 100 <= 13
ABresult
0X0
100
111
95 2 my %s = ( 1 => 'st', 2 => 'nd', 3 => 'rd' );
96 2 ▶ return $s{ $n % 10 } || 'th';
Condition50%1/2
$s{$n % 10} || 'th'
l!l
02
MC/DC0%0/1
$s{$n % 10} || 'th'
$s{$n % 10}
Truth table50%1/2
A
A= $s{$n % 10}
Aresult
11
00
97 }
98  
99 0 ▶sub _parse {
Subroutine _parse: not called CC=1 SCAR=6.9
100 # Not used any more: the raw endpoint returns the body directly, and
101 # _get_was_successful() assembles the result. Kept as a courtesy for the
102 # base-class contract in case a subclass calls it.
103 0 my ( $self, $content ) = @_;
104 0 $self->content( $content );
105 0 return { content => $content, name => 'Unnamed',
106 language => '', post_date => '', desc => '' };
107 }
108  
109 3 1;
110 __END__
↑ Summary