lib/App/Standby/Service/HTTP.pm

filter: Statementstmt 44.8% 39 / 87 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 57.8% 11 / 19 Podpod n/a totaltotal 42.0% 50 / 119 CC 7 SCAR 21.2
CC
7
Cov
40%
_update
30.0
__ANON__
12.0
_init_json
2.0
CRAP 8.4
« lib/App/Standby/Group.pm ↑ Summary lib/App/Standby/DB.pm »
1 package App::Standby::Service::HTTP;
2 2 $App::Standby::Service::HTTP::VERSION = '0.04';
3 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 $App::Standby::Service::HTTP::AUTHORITY = 'cpan:TEX';
5 }
6 # ABSTRACT: Baseclass for any simple HTTP service
7  
8 2 ▶use 5.010_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # use IO::Handle;
16 # use autodie;
17 # use MooseX::Params::Validate;
18 # use Carp;
19 # use English qw( -no_match_vars );
20 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # extends ...
26 2 extends 'App::Standby::Service';
27 # has ...
28 2 has '_ua' => (
29 'is' => 'rw',
30 'isa' => 'LWP::UserAgent',
31 'lazy' => 1,
32 'builder' => '_init_ua',
33 );
34  
35 2 has '_json' => (
36 'is' => 'rw',
37 'isa' => 'JSON',
38 'lazy' => 1,
39 'builder' => '_init_json',
40 );
41  
42 2 has 'username' => (
43 'is' => 'rw',
44 'isa' => 'Str',
45 'required' => 0,
46 );
47  
48 2 has 'password' => (
49 'is' => 'rw',
50 'isa' => 'Str',
51 'required' => 0,
52 );
53  
54 2 has 'endpoints' => (
55 'is' => 'rw',
56 'isa' => 'ArrayRef',
57 'lazy' => 1,
58 'builder' => '_init_endpoints',
59 );
60 # with ...
61 # initializers ...
62 0 ▶sub _init_json {
Subroutine _init_json: not called CC=1 SCAR=6.9
63 0 my $self = shift;
64  
65 0 my $JSON = JSON::->new->utf8();
66  
67 0 return $JSON;
68 }
69  
70 0 ▶sub _init_ua {
Subroutine _init_ua: not called CC=1 SCAR=6.9
71 0 my $self = shift;
72  
73 0 my $UA = LWP::UserAgent::->new();
74 0 $UA->agent('App::Standby::Service::HTTP/0.01');
75  
76 0 return $UA;
77 }
78  
79 # your code here ...
80 0 ▶sub _build_payload {
Subroutine _build_payload: not called CC=1 SCAR=6.9
81 0 my $self = shift;
82 0 my $user_ref = shift;
83  
84 0 my $content = $self->_json()->encode($user_ref);
85 0 $content = URI::Escape::uri_escape($content);
86 #$content =~ s/%3D/=/g;
87 0 $content = 'queue='.$content;
88 0 $content .= '&group_id='.$self->_config_value($self->name().'_group_id');
89  
90 0 return $content;
91 }
92  
93 0 ▶sub _update {
Subroutine _update: not called CC=5 SCAR=34.0
94 0 my $self = shift;
95 0 my $user_ref = shift;
96  
97 0 my $count = 0;
98  
99 0 foreach my $endpoint (@{$self->endpoints()}) {
100 0 $self->logger()->log( message => "Updating endpoint: ".$endpoint, level => 'debug', );
101 0 my $req = HTTP::Request::->new( POST => $endpoint );
102 0 $req->content_type('application/x-www-form-urlencoded');
103 0 my $payload = $self->_build_payload($user_ref);
104 0 $req->content($payload);
105 0 $self->logger()->log( message => "Payload: ".$payload, level => 'debug', );
106  
107 0 ▶ if($self->username() && $self->password()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->username and $self->password) 0 0 0
Condition0%0/3
$self->username and $self->password
!ll&&!rl&&r
000
MC/DC0%0/2
$self->username and $self->password
$self->username $self->password
Truth table0%0/3
A and B
A= $self->usernameB= $self->password
ABresult
0X0
100
111
108 0 $req->authorization_basic( $self->username(), $self->password() );
109 }
110  
111 0 my $content;
112 my $response;
113 0 my $prev_alarm = 0;
114  
115 0 ▶ my $success = try {
Subroutine __ANON__: not called CC=3 SCAR=24.8
116 0 ▶ local $SIG{ALRM} = sub { die "alarm-standby-service\n"; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
117 0 $prev_alarm = alarm 10;
118 0 $response = $self->_ua()->request($req);
119 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
120 0 my $msg = "ERROR Request to $endpoint failed: " . $response->code() . ' - ' . $response->message();
121 0 $self->logger()->log( message => $msg, level => 'error', );
122 0 die( $msg );
123 }
124 0 $content = $response->content;
125 0 ▶ if ( !$content ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($content) 0 0 0
126 0 my $msg = "ERROR No content at $endpoint : " . $response->code() . ' - ' . $response->message();
127 0 $self->logger()->log( message => $msg, level => 'error', );
128 0 die( $msg );
129 }
130 0 1; # make sure $success has a true value ...
131 }
132 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
133 0 $self->logger()->log( message => "Request failed: ".$_, level => 'debug', );
134 }
135 0 ▶ finally {
Subroutine __ANON__: not called CC=1 SCAR=6.9
136  
137 # make sure the alarm is off
138 0 alarm $prev_alarm;
139 0 };
140  
141 0 ▶ $count++ if $success;
Branch: 1 branch
BranchTrueFalseTotal
if $success 0 0 0
142 }
143  
144 0 return $count;
145 }
146  
147 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
148 2 __PACKAGE__->meta->make_immutable;
149  
150 2 1;
151  
152 __END__
153  
154 =pod
155  
156 =encoding utf-8
157  
158 =head1 NAME
159  
160 App::Standby::Service::HTTP - Baseclass for any simple HTTP service
161  
162 =head1 NAME
163  
164 App::Standby::Service::HTTP - Baseclass for any simple HTTP service
165  
166 =head1 AUTHOR
167  
168 Dominik Schulz <dominik.schulz@gauner.org>
169  
170 =head1 COPYRIGHT AND LICENSE
171  
172 This software is copyright (c) 2012 by Dominik Schulz.
173  
174 This is free software; you can redistribute it and/or modify it under
175 the same terms as the Perl 5 programming language system itself.
176  
177 =cut
« lib/App/Standby/Group.pm ↑ Summary lib/App/Standby/DB.pm »