NG/HTTP/Client.pm

filter: Statementstmt 51.1% 22 / 43 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 70.0% 7 / 10 Podpod 0.0% 0 / 2 totaltotal 47.5% 29 / 61 CC 5 SCAR 19.8
CC
5
Cov
45%
web_get
20.0
__ANON__
6.0
new
2.0
CRAP 7.2
« NG/DB.pm ↑ Summary NG/HTTP/DOM.pm »
1 package NG::HTTP::Client;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base qw(NG::Object);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use NG::SHashtable;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use NG::HTTP::DOM;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use AnyEvent::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
11 0 my $pkg = shift;
12 0 return bless {}, $pkg;
13 }
14  
15 =head1 web_get
16 my $url = 'http://www.baidu.com/';
17 my $content = web_get($url);
18 say "Scalar: " . $content->xml;
19  
20 my @urls = qw(http://www.baidu.com http://www.sina.com.cn);
21 web_get(@urls, sub {
22 #web_get($url, sub {
23 my ($content, $code, $res_headers) = @_; # $res_headers 是 SHashtable 类型
24 say "HTTP: " . $code;
25 say "Body: " . $content->text;
26 $res_headers->each(
27 sub {
28 my ($header_k, $header_v) = @_;
29 say "$header_k: $header_v";
30 }
31 );
32 });
33 =cut
34  
35 0 ▶sub web_get {
Subroutine web_get: not called CC=4 SCAR=30.0
Pod undocumented
36 0 my $cb;
37 0 ▶ if ( ref( $_[-1] ) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[-1] eq "CODE") 0 0 0
38 0 $cb = pop @_;
39 }
40 0 my @urls = @_;
41 0 my $content;
42 0 my $w = AnyEvent->condvar;
43 0 for my $url (@urls) {
44 0 $w->begin;
45 0 ▶ http_get $url, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
46 0 my ( $data, $headers ) = @_;
47 0 my $code = delete $headers->{Status};
48 0 my $res_headers = NG::SHashtable->new(%$headers);
49 0 $content = NG::HTTP::DOM->new($data);
50 0 $w->end;
51 0 ▶ if ( defined $cb ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cb) 0 0 0
52 0 $cb->( $content, $code, $res_headers );
53 };
54 0 };
55 }
56 0 $w->recv;
57 0 ▶ if ( !defined $cb ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cb) 0 0 0
58 0 return $content;
59 }
60 }
61  
62 1 1;
« NG/DB.pm ↑ Summary NG/HTTP/DOM.pm »