Simple/HTTP.pm

filter: Statementstmt 64.1% 25 / 39 Branchbran 0.0% 0 / 12 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 75.0% 6 / 8 Podpod 100% 2 / 2 totaltotal 52.2% 35 / 67 CC 9 SCAR 23.9
CC
9
Cov
49%
log
20.0
__ANON__
20.0
new
3.1
CRAP 10.9
« Simple/Twitter.pm ↑ Summary Simple/UDP.pm »
1 package App::Sysadmin::Log::Simple::HTTP;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 # ABSTRACT: a HTTP (maybe RESTful?) logger for App::Sysadmin::Log::Simple
5 4 our $VERSION = '0.009'; # VERSION
6  
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use URI::Escape qw(uri_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 our $HTTP_TIMEOUT = 10;
12  
13  
14 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
15 3 my $class = shift;
16 3 my %opts = @_;
17 3 my $app = $opts{app};
18  
19 3 ▶ $app->{http}->{uri} ||= 'http://localhost';
Condition50%1/2
$app->{"http"}{"uri"} ||= "http://localhost"
l!l
03
MC/DC0%0/1
$app->{"http"}{"uri"} ||= "http://localhost"
$app->{"http"}{"uri"}
Truth table50%1/2
A
A= $app->{"http"}{"uri"}
Aresult
11
00
20 3 ▶ $app->{http}->{method} ||= 'post';
Condition50%1/2
$app->{"http"}{"method"} ||= "post"
l!l
03
MC/DC0%0/1
$app->{"http"}{"method"} ||= "post"
$app->{"http"}{"method"}
Truth table50%1/2
A
A= $app->{"http"}{"method"}
Aresult
11
00
21 3 $app->{http}->{method} = uc $app->{http}->{method};
22  
23 return bless {
24 do_http => $app->{do_http},
25 http => $app->{http},
26 user => $app->{user},
27 3 }, $class;
28 }
29  
30  
31 0 ▶sub log {
Subroutine log: not called CC=4 SCAR=30.0
Pod documented
32 0 my $self = shift;
33 0 my $logentry = shift;
34  
35 0 ▶ return unless $self->{do_http};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"do_http"} 0 0 0
36  
37 0 ▶ my $ua = HTTP::Tiny->new(
Branch: 1 branch
BranchTrueFalseTotal
__PACKAGE__->VERSION ? : 0 0 0
38 timeout => $HTTP_TIMEOUT,
39 agent => __PACKAGE__ . '/' . (__PACKAGE__->VERSION ? __PACKAGE__->VERSION : 'dev'),
40 );
41 0 ▶ my $res = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
42 0 ▶ if ( $self->{http}->{method} eq 'GET' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'http'}{'method'} eq 'GET') { } 0 0 0
elsif ($self->{'http'}{'method'} eq 'POST') { } 0 0 0
elsif ($self->{'http'}{'method'} eq 'PUT') { } 0 0 0
43 my $params = $ua->www_form_urlencode({
44 user => $self->{user},
45 0 log => $logentry,
46 });
47 0 my $uri = $self->{http}->{uri} . "?$params";
48  
49 0 return $ua->get($uri);
50 }
51 elsif ( $self->{http}->{method} eq 'POST' ) {
52 return $ua->post_form($self->{http}->{uri}, {
53 user => $self->{user},
54 0 log => $logentry,
55 });
56 }
57 elsif ( $self->{http}->{method} eq 'PUT' ) {
58 return $ua->put($self->{http}->{uri}, {
59 user => $self->{user},
60 0 log => $logentry,
61 });
62 }
63 else {
64 0 croak 'This shouldnt happen, as the method is populated internally. Something bad has happened'
65 }
66 0 }->();
67  
68 carp sprintf('Failed to http log via %s to %s with code %d and error %s',
69 $self->{http}->{method},
70 $self->{http}->{uri},
71 $res->{status},
72 $res->{reason},
73 0 ▶ ) unless $res->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"success"} 0 0 0
74  
75 0 return "Logged to $self->{http}->{uri} via $self->{http}->{method}"
76 }
77  
78 4 1;
79  
80 __END__
« Simple/Twitter.pm ↑ Summary Simple/UDP.pm »