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