File Coverage

blib/lib/WWW/Shorten/SCK.pm
Criterion Covered Total %
statement 43 45 95.5
branch 7 14 50.0
condition 2 6 33.3
subroutine 11 11 100.0
pod 2 2 100.0
total 65 78 83.3


line stmt bran cond sub pod time code
1             #
2             # This file is part of WWW-Shorten-SCK
3             #
4             # This software is copyright (c) 2013 by celogeek .
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9             package WWW::Shorten::SCK;
10 1     1   101018 use strict;
  1         5  
  1         24  
11 1     1   4 use warnings;
  1         2  
  1         22  
12 1     1   391 use LWP::Protocol::https;
  1         81910  
  1         41  
13 1     1   8 use URI::Escape qw/uri_escape_utf8/;
  1         1  
  1         71  
14 1     1   625 use JSON;
  1         8202  
  1         4  
15             our $VERSION = '0.7'; # VERSION
16              
17             # ABSTRACT: Perl interface to sck.pm
18              
19 1     1   170 use 5.006;
  1         4  
20              
21 1     1   5 use parent qw( WWW::Shorten::generic Exporter );
  1         2  
  1         7  
22 1     1   60 use vars qw(@EXPORT_OK %EXPORT_TAGS);
  1         14  
  1         111  
23             @EXPORT_OK = qw( makeashorterlink makealongerlink );
24             %EXPORT_TAGS = ( all => [@EXPORT_OK] );
25              
26 1     1   5 use Carp;
  1         2  
  1         232  
27              
28             sub makeashorterlink {
29 2 50   2 1 151 my $url = shift or croak 'No URL passed to makeashorterlink';
30 2         21 my $ua = __PACKAGE__->ua();
31 2         16575 my $sck_url = 'https://api.sck.pm';
32 2         13 my $resp = $ua->get( $sck_url . '/shorten?' . $url, );
33 2 50       1342215 return unless $resp->is_success;
34 2         39 my $content = decode_json( $resp->content );
35 2 50 33     495 if ( ref $content && $content->{status} eq 'OK' ) {
36 2         76 return $content->{short_url};
37             }
38 0         0 return;
39             }
40              
41             sub makealongerlink {
42 2 50   2 1 14 my $sck_url = shift
43             or croak 'No SCK key / URL passed to makealongerlink';
44 2         26 my $ua = __PACKAGE__->ua();
45              
46             #call api to get long url from the short
47 2 50       35 if ( $sck_url =~ /^https?:\/\/sck.pm\/(.*)$/x ) {
48 2         10 $sck_url = $1;
49             }
50              
51 2         13 my $resp = $ua->get( "https://api.sck.pm/expand?" . $sck_url );
52 2 50       296871 return unless $resp->is_success;
53 2         45 my $content = decode_json( $resp->content );
54 2 50 33     71 if ( ref $content && $content->{status} eq 'OK' ) {
55 2         51 return $content->{url};
56             }
57 0           return;
58              
59             }
60              
61             1;
62              
63             __END__