File Coverage

blib/lib/WWW/Baidu/ZhanZhang.pm
Criterion Covered Total %
statement 11 28 39.2
branch 0 14 0.0
condition 0 2 0.0
subroutine 4 6 66.6
pod 0 2 0.0
total 15 52 28.8


line stmt bran cond sub pod time code
1             package WWW::Baidu::ZhanZhang;
2              
3 1     1   13739 use strict;
  1         3  
  1         22  
4 1     1   19 use 5.008_005;
  1         2  
5             our $VERSION = '0.01';
6 1     1   7 use Carp qw/croak/;
  1         2  
  1         69  
7 1     1   473 use Mojo::UserAgent;
  1         174713  
  1         9  
8              
9             sub new {
10 0     0 0   my $class = shift;
11 0 0         my $args = scalar @_ % 2 ? shift : { @_ };
12              
13             # validate
14 0 0         $args->{site} or croak 'site is required';
15 0 0         $args->{token} or croak 'token is required';
16              
17 0 0         unless ( $args->{ua} ) {
18 0   0       my $ua_args = delete $args->{ua_args} || {};
19 0           $args->{ua} = Mojo::UserAgent->new(%$ua_args);
20             }
21              
22 0           bless $args, $class;
23             }
24              
25             sub post_urls {
26 0     0 0   my ($self, @urls) = @_;
27              
28 0           my $url = "http://data.zz.baidu.com/urls?site=" . $self->{site} . "&token=" . $self->{token};
29 0 0         $url .= "&type=" . $self->{type} if $self->{type};
30              
31 0           my $tx = $self->{ua}->post($url => { 'Content-Type' => 'text/plain' } => join("\n", @urls));
32 0 0         if (my $res = $tx->success) {
33 0           return $res->json;
34             } else {
35 0           my $err = $tx->error;
36 0 0         croak "$err->{code} response: $err->{message}" if $err->{code};
37 0           croak "Connection error: $err->{message}";
38             }
39             }
40              
41             1;
42             __END__