blib/lib/WWW/Shorten/Akari.pm

filter: Statementstmt 67.8% 59 / 87 Branchbran 37.5% 6 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 64.0% 16 / 25 Podpod 41.6% 5 / 12 totaltotal 57.3% 86 / 150 CC 10 SCAR 13.4
CC
10
Cov
60%
makeashorterlink
12.0
makealongerlink
12.0
reduce
5.7
CRAP 3.8
↑ Summary
1 2 ▶use v5.8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package WWW::Shorten::Akari;
7 # ABSTRACT: Reduces the presence of URLs using http://waa.ai
8 2 our $VERSION = 'v1.2.1'; # VERSION
9  
10  
11 2 ▶use parent qw{Exporter WWW::Shorten::generic};
Subroutine BEGIN: called CC=1 SCAR=0.0
12 # must be exported by default; part of WWW::Shorten API
13 2 our @EXPORT = qw{makeashorterlink makealongerlink};
14 2 our @EXPORT_OK = qw{short_link long_link};
15 2 our %EXPORT_TAGS = (
16 # not redundant; ":default" is part of the WWW::Shorten::generic import API
17 default => [@EXPORT],
18 short => [@EXPORT_OK],
19 );
20  
21 # Workaround for `use WWW::Shorten 'Akari'`.
22 # Similar to the hack used within WWW::Shorten::generic with their
23 # custom-implemented `import` method that doesn't respect empty
24 # import lists.
25 2 ▶sub import {
Subroutine import: called CC=2 SCAR=7.2
26 2 my ($package) = caller;
27 2 ▶ __PACKAGE__->export_to_level(
Branch: 1 branch
BranchTrueFalseTotal
$package eq 'WWW::Shorten' ? : 0 2 2
28 $package eq 'WWW::Shorten' ? 2 : 1,
29 @_);
30 }
31  
32 2 ▶use constant API_URL => q{http://api.waa.ai/};
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use Encode qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use Scalar::Util qw{blessed};
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 3 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
39 3 my $class = shift;
40 3 my $self = {};
41 3 bless $self, $class;
42  
43 3 $self->_init(@_);
44 3 return $self;
45 }
46  
47 3 ▶sub _init {
Subroutine _init: called CC=1 SCAR=0.0
48 3 my ($self) = @_;
49  
50 3 $self->{ua} = __PACKAGE__->ua;
51 3 $self->{utf8} = Encode::find_encoding("UTF-8");
52 }
53  
54 2 ▶sub reduce {
Subroutine reduce: called CC=3 SCAR=17.3
Pod documented
55 2 my ($self, $url) = @_;
56 2 ▶ unless ($url) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url) 2 0 2
57 2 carp "No URL given";
58 2 return;
59 }
60  
61 #$url = $self->{utf8}->encode($url) if Encode::is_utf8($url);
62  
63 0 my $uri = URI->new(API_URL);
64 0 $uri->query_form(url => $url);
65  
66 0 my $res = $self->{ua}->get($uri->as_string);
67  
68 0 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
69 0 carp "HTTP error ". $res->status_line ." when shortening $url";
70 0 return;
71 }
72  
73 0 return $res->decoded_content;
74 }
75  
76 0 ▶sub shorten {
Subroutine shorten: not called CC=1 SCAR=6.9
Pod undocumented
77 0 my ($self, @args) = @_;
78 0 return $self->reduce(@args);
79 }
80  
81 5 ▶sub increase {
Subroutine increase: called CC=3 SCAR=11.3
Pod documented
82 5 my ($self, $url) = @_;
83 5 ▶ unless ($url) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url) 2 3 5
84 2 carp "No URL given";
85 2 return;
86 }
87  
88 3 ▶ unless ($self->_check_url($url)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_check_url($url)) 3 0 3
89 3 carp "URL $url wasn't shortened by Akari";
90 3 return;
91 }
92  
93 0 my $res = $self->{ua}->head($url);
94 0 return $res->header("Location");
95 }
96  
97 3 ▶sub _check_url {
Subroutine _check_url: called CC=1 SCAR=0.0
98 3 my ($self, $url) = @_;
99 3 return scalar $url =~ m{^http://waa\.ai/[^.]+$};
100 }
101  
102 0 ▶sub unshorten {
Subroutine unshorten: not called CC=1 SCAR=6.9
Pod undocumented
103 0 my ($self, @args) = @_;
104 0 return $self->increase(@args);
105 }
106  
107 0 ▶sub lengthen {
Subroutine lengthen: not called CC=1 SCAR=6.9
Pod undocumented
108 0 my ($self, @args) = @_;
109 0 return $self->increase(@args);
110 }
111  
112 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
113 2 ▶croak "Remove the deprecated 'lenghten'"
Branch: 1 branch
BranchTrueFalseTotal
if $WWW::Shorten::Akari::VERSION >= qv("v2.0.0") 0 2 2
114 if $WWW::Shorten::Akari::VERSION >= qv('v2.0.0');
115 0 ▶sub lenghten {
Subroutine lenghten: not called CC=1 SCAR=6.9
Pod undocumented
116 0 carp "Don't use lenghten, use lengthen";
117 0 my ($self, @args) = @_;
118 0 return $self->increase(@args);
119 }
120  
121 0 ▶sub extract {
Subroutine extract: not called CC=1 SCAR=6.9
Pod undocumented
122 0 my ($self, @args) = @_;
123 0 return $self->increase(@args);
124 }
125  
126 # Used by the functions when called as functions
127 2 my $presence = WWW::Shorten::Akari->new;
128  
129 # Aliases to reduce when called as method; calls reduce on $presence when called as function
130 0 ▶sub makeashorterlink($) {
Subroutine makeashorterlink: not called CC=3 SCAR=24.8
Pod documented
131 0 my $self = shift;
132 0 ▶ return $self->reduce(shift) if blessed($self) && $self->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($self) and $self->isa(__PACKAGE__) 0 0 0
Condition0%0/3
builtin::blessed($self) and $self->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($self) and $self->isa(__PACKAGE__)
builtin::blessed($self) $self->isa(__PACKAGE__)
Truth table0%0/3
A and B
A= builtin::blessed($self)B= $self->isa(__PACKAGE__)
ABresult
0X0
100
111
133 0 return $presence->reduce($self);
134 }
135  
136 # Aliases to increase when called as method; calls increase on $presence when called as function
137 0 ▶sub makealongerlink($) {
Subroutine makealongerlink: not called CC=3 SCAR=24.8
Pod documented
138 0 my $self = shift;
139 0 ▶ return $self->increase(shift) if blessed($self) && $self->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($self) and $self->isa(__PACKAGE__) 0 0 0
Condition0%0/3
builtin::blessed($self) and $self->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($self) and $self->isa(__PACKAGE__)
builtin::blessed($self) $self->isa(__PACKAGE__)
Truth table0%0/3
A and B
A= builtin::blessed($self)B= $self->isa(__PACKAGE__)
ABresult
0X0
100
111
140 0 return $presence->increase($self);
141 }
142  
143 0 ▶sub short_link($) { # merely redirects call, thus ignores prototypes
Subroutine short_link: not called CC=1 SCAR=6.9
Pod undocumented
144 0 return &makeashorterlink(@_);
145 }
146  
147 0 ▶sub long_link($) { # merely redirects call, thus ignores prototypes
Subroutine long_link: not called CC=1 SCAR=6.9
Pod undocumented
148 0 return &makealongerlink(@_);
149 }
150  
151 2 1;
152  
153 __END__
↑ Summary