blib/lib/Net/Airbrake/V2.pm

filter: Statementstmt 100% 77 / 77 Branchbran 92.8% 13 / 14 Conditioncond 56.5% 13 / 23 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 100% 19 / 19 Podpod 75.0% 3 / 4 totaltotal 85.3% 128 / 150 CC 18 SCAR 12.5
CC
18
Cov
90%
convert_request
8.5
ua
3.5
_change_content_type
3.0
CRAP 3.5
↑ Summary
1 # vim: set ts=2 sts=2 sw=2 expandtab smarttab:
2 #
3 # This file is part of Net-Airbrake-V2
4 #
5 # This software is copyright (c) 2015 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 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 package Net::Airbrake::V2;
14 # git description: v0.02-6-g6136d47
15  
16 2 our $AUTHORITY = 'cpan:RWSTAUNER';
17 2 $Net::Airbrake::V2::VERSION = '0.04';
18 # ABSTRACT: Airbrake Notifier API V2 Client
19  
20 2 ▶use Net::Airbrake 0.02;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use JSON::MaybeXS qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use XML::Simple qw(xml_in xml_out);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 2 our @ISA = 'Net::Airbrake';
25  
26 # Net::Airbrake uses Class::Tiny.
27 2 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
28 2 my $self = shift;
29  
30 # Pass simple args to avoid circular references.
31 2 $self->{_ua} = Net::Airbrake::V2::UserAgent->new({
32 mod => ref $self,
33 api_key => $self->api_key,
34 });
35  
36 2 return;
37 }
38  
39 # Change uri suffix for v2.
40 2 ▶sub _url {
Subroutine _url: called CC=1 SCAR=0.0
41 2 my ($self) = @_;
42 2 $self->base_url . '/notifier_api/v2/notices';
43 }
44  
45 # helper
46 9 ▶sub _make_vars {
Subroutine _make_vars: called CC=3 SCAR=11.0
47 9 my ($self, $parent, $vars) = @_;
48 9 ▶ $vars ||= {};
Condition100%2/2
$vars ||= {}
l!l
63
MC/DC100%1/1
$vars ||= {}
$vars
Truth table100%2/2
A
A= $vars
Aresult
11
00
49  
50 9 ▶ return () if !keys %$vars;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$vars 6 3 9
51  
52 3 return ($parent => [
53 {
54 $self->_var_content($vars),
55 }
56 ]);
57 }
58  
59 13 ▶sub _var_content {
Subroutine _var_content: called CC=3 SCAR=11.0
60 13 my ($self, $v) = @_;
61  
62 13 ▶ return (content => ref($v) ? $self->stringify_ref($v) : $v)
Branch: 2 branches
BranchTrueFalseTotal
unless ref $v eq "HASH" 8 5 13
ref $v ? : 2 6 8
63 unless ref($v) eq 'HASH';
64  
65 return (
66 var => [
67 map {
68 5 {
69 key => $_,
70 10 $self->_var_content($v->{$_}),
71 }
72 } keys %$v
73 ]
74 );
75 }
76  
77  
78 2 our $Dumper;
79 2 ▶sub stringify_ref {
Subroutine stringify_ref: called CC=2 SCAR=7.0
Pod documented
80 2 my ($self, $value) = @_;
81 2 ▶ ($Dumper ||= do {
Condition66%2/3
$Net::Airbrake::V2::Dumper ||= do { require Data::Dumper; "Data::Dumper"->new([])->Indent(0)->Useqq(1)->Terse(1)->Sortkeys(1) }
l!l&&r!l&&!r
110
MC/DC0%0/2
$Net::Airbrake::V2::Dumper ||= do { require Data::Dumper; "Data::Dumper"->new([])->Indent(0)->Useqq(1)->Terse(1)->Sortkeys(1) }
$Net::Airbrake::V2::Dumper do { require Data::Dumper; "Data::Dumper"->new([])->Indent(0)->Useqq(1)->Terse(1)->Sortkeys(1) }
Truth table66%2/3
A ||= B
A= $Net::Airbrake::V2::DumperB= do { require Data::Dumper; "Data::Dumper"->new([])->Indent(0)->Useqq(1)->Terse(1)->Sortkeys(1) }
ABresult
1X1
011
000
82 1 require Data::Dumper;
83 1 Data::Dumper->new([])
84 ->Indent(0)
85 ->Useqq(1)
86 ->Terse(1)
87 ->Sortkeys(1)
88 })
89 ->Values([$value])
90 ->Dump;
91 }
92  
93  
94 3 ▶sub convert_request {
Subroutine convert_request: called CC=8 SCAR=21.4
Pod documented
95 3 my ($self, $req, $opts) = @_;
96  
97 3 my $string = !ref $req;
98 3 ▶ $req = decode_json($req) if $string;
Branch: 1 branch
BranchTrueFalseTotal
if $string 2 1 3
99  
100 3 ▶ $opts ||= {};
Condition50%1/2
$opts ||= {}
l!l
30
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
101  
102 3 ▶ my $mod = $opts->{mod} || ref($self) || $self;
Condition33%2/6
$opts->{'mod'} || ref $self || $self
l!l&&r!l&&!r
030
$opts->{"mod"} or ref $self
l!l&&r!l&&!r
003
MC/DC0%0/3
$opts->{'mod'} || ref $self || $self
$opts->{"mod"} ref $self $self
Truth table25%1/4
A or B || C
A= $opts->{"mod"}B= ref $selfC= $self
ABCresult
1XX1
01X1
0011
0000
103 3 ▶ my $api_key = $opts->{api_key} || $self->api_key;
Condition66%2/3
$opts->{'api_key'} || $self->api_key
l!l&&r!l&&!r
102
MC/DC50%1/2
$opts->{'api_key'} || $self->api_key
$opts->{'api_key'} $self->api_key
Truth table66%2/3
A || B
A= $opts->{'api_key'}B= $self->api_key
ABresult
1X1
011
000
104  
105 my $notice = {
106 notice => {
107 version => '2.3',
108 'api-key' => [ $api_key ],
109 notifier => [
110 {
111 name => [ $mod ],
112 version => [ $mod->VERSION ],
113 url => [ "https://metacpan.org/pod/$mod" ],
114 },
115 ],
116 error => [
117 map {
118 {
119 class => [ $_->{type} ],
120 message => [ $_->{message} ],
121 backtrace => {
122 line => [
123 map {
124 +{
125 number => $_->{line},
126 file => $_->{file},
127 method => $_->{function},
128 }
129 1 }
130 2 @{ $_->{backtrace} }
131 ]
132 }
133 }
134 }
135 3 ▶ @{ $req->{errors} || [] }
Condition100%2/2
$req->{'errors'} || []
l!l
21
MC/DC100%1/1
$req->{'errors'} || []
$req->{'errors'}
Truth table100%2/2
A
A= $req->{'errors'}
Aresult
11
00
136 ],
137 request => {
138 url => [ $req->{context}{url} ],
139 component => [ $req->{context}{component} ],
140 action => [ $req->{context}{action} ],
141 $self->_make_vars(params => $req->{params}),
142 $self->_make_vars(session => $req->{session}),
143 $self->_make_vars('cgi-data' => $req->{environment}),
144 },
145 'server-environment' => {
146 # Errbit accepts 'hostname' however it's not listed in the spec.
147 'project-root' => [ $req->{context}{rootDirectory} ],
148 'app-version' => [ $req->{context}{version} ],
149 3 'environment-name' => [ $req->{context}{environment} ],
150 },
151 # Errbit accepts 'framework'.
152 }
153 };
154  
155 3 ▶ return $notice if !$string;
Branch: 1 branch
BranchTrueFalseTotal
unless $string 1 2 3
156 2 return $self->_xml_encode($notice);
157 }
158  
159 2 ▶sub _xml_encode {
Subroutine _xml_encode: called CC=1 SCAR=0.0
160 2 my ($self, $notice) = @_;
161  
162 2 my $xml = xml_out($notice,
163 RootName => undef,
164 NoIndent => 1,
165 SuppressEmpty => 1,
166 XMLDecl => q[<?xml version="1.0" encoding="utf-8"?>],
167 );
168  
169 # decode_json should return a structure with character strings,
170 # and XML::Simple doesn't encode until writing to a file handle.
171 2 utf8::encode($xml);
172  
173 2 return $xml;
174 }
175  
176  
177 3 ▶sub convert_response {
Subroutine convert_response: called CC=2 SCAR=6.9
Pod documented
178 3 my ($self, $res) = @_;
179  
180 # For consistnecy with convert_request.
181 3 ▶ return $res if ref $res;
Branch: 1 branch
BranchTrueFalseTotal
if ref $res 1 2 3
182  
183 2 return encode_json( xml_in($res) );
184 }
185  
186 {
187 package # no_index
188 Net::Airbrake::V2::UserAgent;
189  
190 2 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
191 2 bless $_[1], $_[0];
192 }
193  
194 2 ▶ sub ua {
Subroutine ua: called CC=3 SCAR=12.5
195 2 ▶ $_[0]->{ua} ||= do {
Condition33%1/3
$_[0]{"ua"} ||= do { my $mod = $_[0]{"mod"}; "HTTP::Tiny"->new("agent", join("/", $mod, $mod->VERSION || 0), "timeout", 5) }
l!l&&r!l&&!r
020
MC/DC0%0/2
$_[0]{"ua"} ||= do { my $mod = $_[0]{"mod"}; "HTTP::Tiny"->new("agent", join("/", $mod, $mod->VERSION || 0), "timeout", 5) }
$_[0]{"ua"} do { my $mod = $_[0]{"mod"}; "HTTP::Tiny"->new("agent", join("/", $mod, $mod->VERSION || 0), "timeout", 5) }
Truth table33%1/3
A ||= B
A= $_[0]{"ua"}B= do { my $mod = $_[0]{"mod"}; "HTTP::Tiny"->new("agent", join("/", $mod, $mod->VERSION || 0), "timeout", 5) }
ABresult
1X1
011
000
196 2 my $mod = $_[0]->{mod};
197 2 ▶ HTTP::Tiny->new(
Condition50%1/2
$mod->VERSION || 0
l!l
20
MC/DC0%0/1
$mod->VERSION || 0
$mod->VERSION
Truth table50%1/2
A
A= $mod->VERSION
Aresult
11
00
198 agent => join('/', $mod, $mod->VERSION || 0),
199 timeout => 5,
200 );
201 };
202 }
203  
204 2 ▶ sub _convert_request {
Subroutine _convert_request: called CC=1 SCAR=0.0
205 2 my ($self, $msg) = @_;
206 2 $msg->{content} = $self->{mod}->convert_request($msg->{content}, { api_key => $self->{api_key} });
207 2 $self->_change_content_type($msg, 'application/xml');
208 }
209  
210 2 ▶ sub _convert_response {
Subroutine _convert_response: called CC=1 SCAR=0.0
211 2 my ($self, $msg) = @_;
212 2 $msg->{content} = $self->{mod}->convert_response($msg->{content});
213 2 $self->_change_content_type($msg, 'application/json');
214 }
215  
216 # Find and update the content type regardless of capitalization/punctuation.
217 4 ▶ sub _change_content_type {
Subroutine _change_content_type: called CC=3 SCAR=11.1
218 4 my ($self, $msg, $val) = @_;
219 4 my $headers = $msg->{headers};
220 4 foreach my $h ( keys %$headers ){
221 4 ▶ $headers->{ $h } = $val
Branch: 1 branch
BranchTrueFalseTotal
if $h =~ /^content.type$/i 4 0 4
222 if $h =~ /^content.type$/i;
223 }
224 }
225  
226 2 ▶ sub request {
Subroutine request: called CC=1 SCAR=0.0
227 2 my ($self, $method, $url, $req) = @_;
228  
229 2 $self->_convert_request($req);
230  
231 2 my $res = $self->ua->request($method, $url, $req);
232  
233 # TODO: catch xml parse error.
234 # TODO: check content type?
235  
236 2 $self->_convert_response($res);
237  
238 2 return $res;
239 }
240 }
241  
242 2 1;
243  
244 __END__
↑ Summary