| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package WWW::Shorten::Naver; |
|
2
|
1
|
|
|
1
|
|
985
|
use strict; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
40
|
|
|
3
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
45
|
|
|
4
|
1
|
|
|
1
|
|
18
|
use Carp (); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
24
|
|
|
5
|
1
|
|
|
1
|
|
671
|
use JSON::MaybeXS; |
|
|
1
|
|
|
|
|
6808
|
|
|
|
1
|
|
|
|
|
77
|
|
|
6
|
1
|
|
|
1
|
|
736
|
use URI (); |
|
|
1
|
|
|
|
|
5427
|
|
|
|
1
|
|
|
|
|
40
|
|
|
7
|
1
|
|
|
1
|
|
10
|
use Scalar::Util qw(blessed); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
127
|
|
|
8
|
1
|
|
|
1
|
|
850
|
use parent qw( WWW::Shorten::generic Exporter ); |
|
|
1
|
|
|
|
|
384
|
|
|
|
1
|
|
|
|
|
8
|
|
|
9
|
|
|
|
|
|
|
our @EXPORT = qw(new VERSION); |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
|
12
|
|
|
|
|
|
|
$VERSION = eval $VERSION; |
|
13
|
|
|
|
|
|
|
|
|
14
|
1
|
|
50
|
1
|
|
53078
|
use constant NAVER_SHORTEN_API_ENDPOINT => $ENV{NAVER_SHORTEN_API_URL} || 'https://openapi.naver.com/v1/util/shorturl'; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
1762
|
|
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub _attr { |
|
17
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
18
|
0
|
|
0
|
|
|
|
my $attr = lc(_trim(shift) || ''); |
|
19
|
|
|
|
|
|
|
# attribute list is small enough to just grep each time. meh. |
|
20
|
0
|
0
|
|
|
|
|
Carp::croak("Invalid attribute") unless grep {$attr eq $_} @{_attrs()}; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
21
|
0
|
0
|
|
|
|
|
return $self->{$attr} unless @_; |
|
22
|
|
|
|
|
|
|
|
|
23
|
0
|
|
|
|
|
|
my $val = shift; |
|
24
|
0
|
0
|
|
|
|
|
unless (defined($val)) { |
|
25
|
0
|
|
|
|
|
|
$self->{$attr} = undef; |
|
26
|
0
|
|
|
|
|
|
return $self; |
|
27
|
|
|
|
|
|
|
} |
|
28
|
0
|
|
|
|
|
|
$self->{$attr} = $val; |
|
29
|
0
|
|
|
|
|
|
return $self; |
|
30
|
|
|
|
|
|
|
} |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# _attrs (static, private) |
|
33
|
|
|
|
|
|
|
{ |
|
34
|
|
|
|
|
|
|
my $attrs; # mimic the state keyword |
|
35
|
|
|
|
|
|
|
sub _attrs { |
|
36
|
0
|
0
|
|
0
|
|
|
return [@{$attrs}] if $attrs; |
|
|
0
|
|
|
|
|
|
|
|
37
|
0
|
|
|
|
|
|
$attrs = [ |
|
38
|
|
|
|
|
|
|
qw(username password access_token client_id client_secret), |
|
39
|
|
|
|
|
|
|
]; |
|
40
|
0
|
|
|
|
|
|
return [@{$attrs}]; |
|
|
0
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
} |
|
42
|
|
|
|
|
|
|
} |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub _request { |
|
45
|
0
|
|
|
0
|
|
|
my ($self, $url) = @_; |
|
46
|
|
|
|
|
|
|
|
|
47
|
0
|
0
|
0
|
|
|
|
Carp::croak("Invalid URI object") unless $url && blessed($url) && $url->isa('URI'); |
|
|
|
|
0
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
my $ua = __PACKAGE__->ua(); |
|
50
|
0
|
|
|
|
|
|
$ua->default_header( 'X-Naver-Client-ID' => $self->client_id ); |
|
51
|
0
|
|
|
|
|
|
$ua->default_header( 'X-Naver-Client-Secret' => $self->client_secret ); |
|
52
|
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
my $res = $ua->get($url); |
|
54
|
0
|
0
|
|
|
|
|
Carp::croak("Invalid response") unless $res; |
|
55
|
0
|
0
|
|
|
|
|
unless ($res->is_success) { |
|
56
|
0
|
|
|
|
|
|
Carp::croak($res->status_line); |
|
57
|
|
|
|
|
|
|
} |
|
58
|
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
my $content_type = $res->header('Content-Type'); |
|
60
|
0
|
|
|
|
|
|
my $content = $res->decoded_content(); |
|
61
|
0
|
0
|
0
|
|
|
|
unless ($content_type && $content_type =~ m{application/json}) { |
|
62
|
0
|
|
|
|
|
|
Carp::croak("Unexpected response: $content"); |
|
63
|
|
|
|
|
|
|
} |
|
64
|
0
|
|
|
|
|
|
my $json = decode_json($content); |
|
65
|
0
|
0
|
|
|
|
|
Carp::croak("Invalid data returned: $content") unless $json; |
|
66
|
0
|
|
|
|
|
|
return $json->{result}; |
|
67
|
|
|
|
|
|
|
} |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# _parse_args (static, private) |
|
70
|
|
|
|
|
|
|
sub _parse_args { |
|
71
|
0
|
|
|
0
|
|
|
my $args; |
|
72
|
0
|
0
|
0
|
|
|
|
if ( @_ == 1 && ref $_[0] ) { |
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
my %copy = eval { %{ $_[0] } }; # try shallow copy |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
74
|
0
|
0
|
|
|
|
|
Carp::croak("Argument to method could not be dereferenced as a hash") if $@; |
|
75
|
0
|
|
|
|
|
|
$args = \%copy; |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
elsif (@_==1 && !ref($_[0])) { |
|
78
|
0
|
|
|
|
|
|
$args = {single_arg => $_[0]}; |
|
79
|
|
|
|
|
|
|
} |
|
80
|
|
|
|
|
|
|
elsif ( @_ % 2 == 0 ) { |
|
81
|
0
|
|
|
|
|
|
$args = {@_}; |
|
82
|
|
|
|
|
|
|
} |
|
83
|
|
|
|
|
|
|
else { |
|
84
|
0
|
|
|
|
|
|
Carp::croak("Method got an odd number of elements"); |
|
85
|
|
|
|
|
|
|
} |
|
86
|
0
|
|
|
|
|
|
return $args; |
|
87
|
|
|
|
|
|
|
} |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# _trim (static, private) |
|
90
|
|
|
|
|
|
|
sub _trim { |
|
91
|
0
|
|
|
0
|
|
|
my $input = shift; |
|
92
|
0
|
0
|
0
|
|
|
|
return $input unless defined $input && !ref($input) && length($input); |
|
|
|
|
0
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
$input =~ s/\A\s*//; |
|
94
|
0
|
|
|
|
|
|
$input =~ s/\s*\z//; |
|
95
|
0
|
|
|
|
|
|
return $input; |
|
96
|
|
|
|
|
|
|
} |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub new { |
|
99
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
|
100
|
0
|
|
|
|
|
|
my $args; |
|
101
|
0
|
0
|
0
|
|
|
|
if ( @_ == 1 && ref $_[0] ) { |
|
|
|
0
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
my %copy = eval { %{ $_[0] } }; # try shallow copy |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
103
|
0
|
0
|
|
|
|
|
Carp::croak("Argument to $class->new() could not be dereferenced as a hash") if $@; |
|
104
|
0
|
|
|
|
|
|
$args = \%copy; |
|
105
|
|
|
|
|
|
|
} |
|
106
|
|
|
|
|
|
|
elsif ( @_ % 2 == 0 ) { |
|
107
|
0
|
|
|
|
|
|
$args = {@_}; |
|
108
|
|
|
|
|
|
|
} |
|
109
|
|
|
|
|
|
|
else { |
|
110
|
0
|
|
|
|
|
|
Carp::croak("$class->new() got an odd number of elements"); |
|
111
|
|
|
|
|
|
|
} |
|
112
|
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
my $attrs = _attrs(); |
|
114
|
0
|
|
|
|
|
|
my $href = {}; |
|
115
|
0
|
|
|
|
|
|
for my $key (%{$args}) { |
|
|
0
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
$href->{$key} = $args->{$key}; |
|
117
|
|
|
|
|
|
|
} |
|
118
|
0
|
|
|
|
|
|
return bless $href, $class; |
|
119
|
|
|
|
|
|
|
} |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
|
|
122
|
0
|
|
|
0
|
0
|
|
sub client_id { return shift->_attr('client_id', @_); } |
|
123
|
|
|
|
|
|
|
|
|
124
|
0
|
|
|
0
|
0
|
|
sub client_secret { return shift->_attr('client_secret', @_); } |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub makeashorterlink { |
|
127
|
0
|
|
|
0
|
1
|
|
my $self; |
|
128
|
0
|
0
|
0
|
|
|
|
if ($_[0] && blessed($_[0]) && $_[0]->isa('WWW::Shorten::Naver')) { |
|
|
|
|
0
|
|
|
|
|
|
129
|
0
|
|
|
|
|
|
$self = shift; |
|
130
|
|
|
|
|
|
|
} |
|
131
|
0
|
0
|
|
|
|
|
my $url = shift or Carp::croak('No URL passed to makeashorterlink'); |
|
132
|
0
|
|
0
|
|
|
|
$self ||= __PACKAGE__->new(@_); |
|
133
|
0
|
|
|
|
|
|
my $res = $self->shorten( url => $url, @_); |
|
134
|
0
|
|
|
|
|
|
return $res->{url}; |
|
135
|
|
|
|
|
|
|
} |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub makealongerlink { |
|
138
|
0
|
|
|
0
|
0
|
|
my $self; |
|
139
|
0
|
0
|
0
|
|
|
|
if ($_[0] && blessed($_[0]) && $_[0]->isa('WWW::Shorten::Bitly')) { |
|
|
|
|
0
|
|
|
|
|
|
140
|
0
|
|
|
|
|
|
$self = shift; |
|
141
|
|
|
|
|
|
|
} |
|
142
|
0
|
0
|
|
|
|
|
my $url = shift or Carp::croak('No URL passed to makealongerlink'); |
|
143
|
0
|
|
0
|
|
|
|
$self ||= __PACKAGE__->new(@_); |
|
144
|
0
|
|
|
|
|
|
my $longer_url = $self->expand( url => $url ); |
|
145
|
0
|
|
|
|
|
|
return $longer_url; |
|
146
|
|
|
|
|
|
|
} |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub expand { |
|
149
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
|
150
|
0
|
|
|
|
|
|
my $args = _parse_args(@_); |
|
151
|
0
|
|
|
|
|
|
my $short_url = $args->{url}; |
|
152
|
0
|
0
|
|
|
|
|
unless ($short_url) { |
|
153
|
0
|
|
|
|
|
|
Carp::croak("A shortUrl parameter is required.\n"); |
|
154
|
|
|
|
|
|
|
} |
|
155
|
|
|
|
|
|
|
|
|
156
|
0
|
|
|
|
|
|
my $url = URI->new($short_url); |
|
157
|
0
|
|
|
|
|
|
my $ua = __PACKAGE__->ua(); |
|
158
|
0
|
|
|
|
|
|
my $res = $ua->get($url); |
|
159
|
0
|
|
|
|
|
|
my $longer_url = $res->header('Location'); |
|
160
|
0
|
|
|
|
|
|
return $longer_url; |
|
161
|
|
|
|
|
|
|
} |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub shorten { |
|
164
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
165
|
|
|
|
|
|
|
|
|
166
|
0
|
|
|
|
|
|
my $args = _parse_args(@_); |
|
167
|
|
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
|
my $long_url = $args->{url}; |
|
169
|
0
|
0
|
|
|
|
|
unless ($long_url) { |
|
170
|
0
|
|
|
|
|
|
Carp::croak("A longUrl parameter is required.\n"); |
|
171
|
|
|
|
|
|
|
} |
|
172
|
|
|
|
|
|
|
|
|
173
|
0
|
|
|
|
|
|
my $url = URI->new(NAVER_SHORTEN_API_ENDPOINT); |
|
174
|
0
|
|
|
|
|
|
$url->query_form( |
|
175
|
|
|
|
|
|
|
url => $long_url, |
|
176
|
|
|
|
|
|
|
); |
|
177
|
0
|
|
|
|
|
|
return $self->_request($url, $args); |
|
178
|
|
|
|
|
|
|
} |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
1; # End of WWW::Shorten::Naver |
|
181
|
|
|
|
|
|
|
__END__ |