File Coverage

blib/lib/WebService/PagerDuty/Response.pm
Criterion Covered Total %
statement 33 39 84.6
branch 8 14 57.1
condition 1 2 50.0
subroutine 7 8 87.5
pod 1 1 100.0
total 50 64 78.1


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl -w
2              
3             ## workaround for PkgVersion
4             ## no critic
5             package WebService::PagerDuty::Response;
6             {
7             $WebService::PagerDuty::Response::VERSION = '1.20131219.1627';
8             }
9             ## use critic
10 2     2   12 use strict;
  2         4  
  2         68  
11 2     2   11 use warnings;
  2         4  
  2         63  
12              
13 2     2   27 use base qw/ WebService::PagerDuty::Base /;
  2         5  
  2         219  
14 2     2   12 use JSON;
  2         5  
  2         12  
15 2     2   2300 use Error qw/ :try /;
  2         8996  
  2         12  
16              
17             my @all_options = qw/
18             code status message error
19             incident_key
20             total limit offset
21             entries
22             data
23             /;
24              
25             __PACKAGE__->mk_ro_accessors(@all_options);
26              
27             sub new {
28 1     1 1 4 my ( $self, $response, $options ) = @_;
29 1   50     9 $options ||= {};
30              
31 1 50       4 if ($response) {
32 1         5 $options->{code} = $response->code();
33 1         16 $options->{status} = $response->status_line();
34 1         15 $options->{message} = $response->message();
35 1         12 $options->{errors} = undef;
36              
37             try {
38 1 50   1   29 $options->{data} = from_json( $response->content() ) if $response->content();
39             }
40             otherwise {
41 0     0   0 my $error = shift;
42             ## the only error that could happen and we care of - it's when $response->content can't
43             ## be parsed as json (no difference why - because of bad request or something else)
44 0         0 $options->{data} = {
45             status => 'invalid',
46             message => $_,
47             };
48 1         14 };
49              
50 1         96 for my $option (@all_options) {
51 10 100       37 $options->{$option} = delete $options->{data}{$option} if exists $options->{data}{$option};
52             }
53              
54             # one extra-case
55 1 50       6 $options->{entries} = delete $options->{data}{incidents} if exists $options->{data}{incidents};
56              
57             # translate HTTP codes to human-readable status
58 1 50       8 if ( $options->{status} =~ /^(\d+)/ ) {
59 1 50       5 if ( $1 eq '200' ) {
60 1         4 $options->{status} = 'success';
61             }
62             else {
63 0         0 $options->{status} = 'invalid';
64             }
65             }
66              
67             # eliminate uneeded fields
68 1 50       2 delete $options->{data} unless %{ $options->{data} };
  1         11  
69             }
70             else {
71 0         0 $options->{code} = 599;
72 0         0 $options->{status} = 'invalid';
73 0         0 $options->{message} = $options->{error} = 'WebService::PagerDuty::Response was created incorrectly';
74             }
75              
76 1         15 $self->SUPER::new(%$options);
77             }
78              
79             1;
80              
81             __END__