Message.pm

filter: Statementstmt 100% 52 / 52 Branchbran 100% 14 / 14 Conditioncond 62.5% 5 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 16 / 16 Podpod 100% 7 / 7 totaltotal 93.1% 95 / 102 CC 11 SCAR 10.8
CC
11
Cov
96%
new
7.0
headers
2.1
_error
2.0
CRAP 2.9
↑ Summary Response.pm »
1 # vim: set ts=2 sts=2 sw=2 expandtab smarttab:
2 #
3 # This file is part of AnyEvent-HTTP-Message
4 #
5 # This software is copyright (c) 2012 by Randy Stauner.
6 #
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 #
10 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 package AnyEvent::HTTP::Message;
14 {
15 6 $AnyEvent::HTTP::Message::VERSION = '0.302';
16 }
17 # git description: v0.301-3-ge92f3a7
18  
19 6 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 $AnyEvent::HTTP::Message::AUTHORITY = 'cpan:RWSTAUNER';
21 }
22 # ABSTRACT: Lightweight objects for AnyEvent::HTTP Request/Response
23  
24 6 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27  
28 21 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
29 21 my $class = shift;
30  
31 21 my $self;
32 21 ▶ if( ref($_[0]) eq 'HASH' ){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 5 16 21
elsif (builtin::blessed($_[0]) and $_[0]->isa('HTTP::Message')) { } 6 10 16
33 # if passed a single hashref take a shallow copy
34 5 $self = { %{ $_[0] } };
35 }
36 ▶ elsif( Scalar::Util::blessed($_[0]) && $_[0]->isa('HTTP::Message') ){
Condition66%2/3
builtin::blessed($_[0]) and $_[0]->isa("HTTP::Message")
!ll&&!rl&&r
1006
MC/DC50%1/2
builtin::blessed($_[0]) and $_[0]->isa("HTTP::Message")
builtin::blessed($_[0]) $_[0]->isa("HTTP::Message")
Truth table66%2/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa("HTTP::Message")
ABresult
0X0
100
111
37 # allow an optional second hashref for extra params
38 6 $self = $class->from_http_message(@_);
39 }
40 else {
41 # otherwise it's the argument list for http_request()
42 10 $self = $class->parse_args(@_);
43 }
44  
45 # accept 'content' as an alias for 'body', but store as 'body'
46 $self->{body} = delete $self->{content}
47 18 ▶ if exists $self->{content};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"content"} 1 17 18
48  
49 $self->{body} = ''
50 18 ▶ if !defined $self->{body};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"body"} 2 16 18
51  
52 $self->{headers} = $self->{headers}
53 ? $class->_normalize_headers($self->{headers})
54 18 ▶ : {};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'headers'} ? : 16 2 18
55  
56 18 bless $self, $class;
57 }
58  
59 10 ▶sub _error {
Subroutine _error: called CC=2 SCAR=7.0
60 10 my $self = shift;
61 10 ▶ @_ = join ' ', (ref($self) || $self), 'error:', @_;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
190
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
62 10 goto &Carp::croak;
63 }
64  
65  
66 1 ▶sub parse_args {
Subroutine parse_args: called CC=1 SCAR=0.0
Pod documented
67 1 $_[0]->_error('parse_args() is not defined');
68 }
69  
70  
71 1 ▶sub from_http_message {
Subroutine from_http_message: called CC=1 SCAR=0.0
Pod documented
72 1 $_[0]->_error('from_http_message() is not defined');
73 }
74  
75 # turn HTTP::Headers into a hashref
76 4 ▶sub _hash_http_headers {
Subroutine _hash_http_headers: called CC=1 SCAR=0.0
77 4 my ($self, $headers) = @_;
78 4 my $aeh = {};
79 9 ▶ $headers->scan(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
80 9 my ($k, $v) = @_;
81 9 my $l = lc $k;
82 9 ▶ $aeh->{$l} = exists($aeh->{$l}) ? $aeh->{$l} . ',' . $v : $v;
Branch: 1 branch
BranchTrueFalseTotal
exists $aeh->{$l} ? : 3 6 9
83 4 });
84 4 return $aeh;
85 }
86  
87  
88 # stubs for read-only accessors
89 26 ▶sub body { $_[0]->{body} }
Subroutine body: called CC=1 SCAR=0.0
Pod documented
90 34 ▶sub headers { $_[0]->{headers} ||= {} }
Subroutine headers: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
$_[0]{"headers"} ||= {}
l!l
340
MC/DC0%0/1
$_[0]{"headers"} ||= {}
$_[0]{"headers"}
Truth table50%1/2
A
A= $_[0]{"headers"}
Aresult
11
00
91  
92 # alias
93 8 ▶sub content { $_[0]->body }
Subroutine content: called CC=1 SCAR=0.0
Pod documented
94  
95  
96 12 ▶sub header {
Subroutine header: called CC=1 SCAR=0.0
Pod documented
97 12 my ($self, $h) = @_;
98 12 $h =~ tr/_/-/;
99 12 return $self->headers->{ lc $h };
100 }
101  
102 # ensure keys are stored with dashes (not underscores) and lower-cased
103 16 ▶sub _normalize_headers {
Subroutine _normalize_headers: called CC=2 SCAR=6.9
104 16 my ($self, $headers) = @_;
105 16 my $norm = {};
106 16 ▶ while( my ($k, $v) = each %$headers ){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$headers) 19 16 35
107 19 my $n = $k;
108 19 $n =~ tr/_/-/;
109 19 $norm->{ lc $n } = $v;
110 }
111 16 return $norm;
112 }
113  
114 6 1;
115  
116 __END__
↑ Summary Response.pm »