blib/lib/Net/Airbrake/V2.pm

filter: Statementstmt 100% 69 / 69 Branchbran 91.6% 11 / 12 Conditioncond 50.0% 9 / 18 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 17 / 17 Podpod 66.6% 2 / 3 totaltotal 85.2% 110 / 129 SCAR 27.2
CC
15
Cov
90%
convert_request
8.5
ua
3.5
_change_content_type
3.0
CRAP 15.2
↑ 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.01-2-g1348790
15  
16 2 our $AUTHORITY = 'cpan:RWSTAUNER';
17 2 $Net::Airbrake::V2::VERSION = '0.02';
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 sub BUILD {
28 2 ▶ my $self = shift;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
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 sub _url {
41 2 ▶ my ($self) = @_;
Subroutine _url: called CC=1 SCAR=0.0
42 2 $self->base_url . '/notifier_api/v2/notices';
43 }
44  
45 # helper
46 sub _make_vars {
47 9 ▶ my ($self, $parent, $vars) = @_;
Subroutine _make_vars: called CC=3 SCAR=11.0
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 return ($parent => [
53 {
54 var => [
55 map {
56 {
57 key => $_,
58 4 content => $vars->{$_},
59 }
60 3 } keys %{ $vars }
61 ]
62 }
63 ]);
64 }
65  
66  
67 sub convert_request {
68 3 ▶ my ($self, $req, $opts) = @_;
Subroutine convert_request: called CC=8 SCAR=21.4
Pod documented
69  
70 3 my $string = !ref $req;
71 3 ▶ $req = decode_json($req) if $string;
Branch: 1 branch
BranchTrueFalseTotal
if $string 2 1 3
72  
73 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
74  
75 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
76 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
77  
78 my $notice = {
79 notice => {
80 version => '2.3',
81 'api-key' => [ $api_key ],
82 notifier => [
83 {
84 name => [ $mod ],
85 version => [ $mod->VERSION ],
86 url => [ "https://metacpan.org/pod/$mod" ],
87 },
88 ],
89 error => [
90 map {
91 {
92 class => [ $_->{type} ],
93 message => [ $_->{message} ],
94 backtrace => {
95 line => [
96 map {
97 +{
98 number => $_->{line},
99 file => $_->{file},
100 method => $_->{function},
101 }
102 1 }
103 2 @{ $_->{backtrace} }
104 ]
105 }
106 }
107 }
108 3 ▶ @{ $req->{errors} || [] }
Branch: 1 branch
BranchTrueFalseTotal
$req->{"errors"} or [] 1 2 3
109 ],
110 request => {
111 url => [ $req->{context}{url} ],
112 component => [ $req->{context}{component} ],
113 action => [ $req->{context}{action} ],
114 $self->_make_vars(params => $req->{params}),
115 $self->_make_vars(session => $req->{session}),
116 $self->_make_vars('cgi-data' => $req->{environment}),
117 },
118 'server-environment' => {
119 # Errbit accepts 'hostname' however it's not listed in the spec.
120 'project-root' => [ $req->{context}{rootDirectory} ],
121 'app-version' => [ $req->{context}{version} ],
122 3 'environment-name' => [ $req->{context}{environment} ],
123 },
124 # Errbit accepts 'framework'.
125 }
126 };
127  
128 3 ▶ return $notice if !$string;
Branch: 1 branch
BranchTrueFalseTotal
unless $string 1 2 3
129 2 return $self->_xml_encode($notice);
130 }
131  
132 sub _xml_encode {
133 2 ▶ my ($self, $notice) = @_;
Subroutine _xml_encode: called CC=1 SCAR=0.0
134  
135 2 my $xml = xml_out($notice,
136 RootName => undef,
137 NoIndent => 1,
138 SuppressEmpty => 1,
139 XMLDecl => q[<?xml version="1.0" encoding="utf-8"?>],
140 );
141  
142 2 return $xml;
143 }
144  
145  
146 sub convert_response {
147 3 ▶ my ($self, $res) = @_;
Subroutine convert_response: called CC=2 SCAR=6.9
Pod documented
148  
149 # For consistnecy with convert_request.
150 3 ▶ return $res if ref $res;
Branch: 1 branch
BranchTrueFalseTotal
if ref $res 1 2 3
151  
152 2 return encode_json( xml_in($res) );
153 }
154  
155 {
156 package # no_index
157 Net::Airbrake::V2::UserAgent;
158  
159 sub new {
160 2 ▶ bless $_[1], $_[0];
Subroutine new: called CC=1 SCAR=0.0
161 }
162  
163 sub ua {
164 2 ▶ $_[0]->{ua} ||= do {
Subroutine ua: called CC=3 SCAR=12.5
Condition33%1/3
$_[0]{"ua"} ||= do {
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
165 2 my $mod = $_[0]->{mod};
166 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
167 agent => join('/', $mod, $mod->VERSION || 0),
168 timeout => 5,
169 );
170 };
171 }
172  
173 sub _convert_request {
174 2 ▶ my ($self, $msg) = @_;
Subroutine _convert_request: called CC=1 SCAR=0.0
175 2 $msg->{content} = $self->{mod}->convert_request($msg->{content}, { api_key => $self->{api_key} });
176 2 $self->_change_content_type($msg, 'application/xml');
177 }
178  
179 sub _convert_response {
180 2 ▶ my ($self, $msg) = @_;
Subroutine _convert_response: called CC=1 SCAR=0.0
181 2 $msg->{content} = $self->{mod}->convert_response($msg->{content});
182 2 $self->_change_content_type($msg, 'application/json');
183 }
184  
185 # Find and update the content type regardless of capitalization/punctuation.
186 sub _change_content_type {
187 4 ▶ my ($self, $msg, $val) = @_;
Subroutine _change_content_type: called CC=3 SCAR=11.1
188 4 my $headers = $msg->{headers};
189 4 foreach my $h ( keys %$headers ){
190 4 ▶ $headers->{ $h } = $val
Branch: 1 branch
BranchTrueFalseTotal
if $h =~ /^content.type$/i 4 0 4
191 if $h =~ /^content.type$/i;
192 }
193 }
194  
195 sub request {
196 2 ▶ my ($self, $method, $url, $req) = @_;
Subroutine request: called CC=1 SCAR=0.0
197  
198 2 $self->_convert_request($req);
199  
200 2 my $res = $self->ua->request($method, $url, $req);
201  
202 # TODO: catch xml parse error.
203 # TODO: check content type?
204  
205 2 $self->_convert_response($res);
206  
207 2 return $res;
208 }
209 }
210  
211 2 1;
212  
213 __END__
214  
215 =pod
216  
217 =encoding UTF-8
218  
219 =for :stopwords Randy Stauner ACKNOWLEDGEMENTS Errbit cpan testmatrix url annocpan anno
220 bugtracker rt cpants kwalitee diff irc mailto metadata placeholders
221 metacpan
222  
223 =head1 NAME
224  
225 Net::Airbrake::V2 - Airbrake Notifier API V2 Client
226  
227 =head1 VERSION
228  
229 version 0.02
230  
231 =head1 SYNOPSIS
232  
233 use Net::Airbrake::V2;
234  
235 my $airbrake = Net::Airbrake::V2->new(
236 api_key => 'xxxxxxx',
237 # project_id is not used.
238 );
239  
240 eval { die 'Oops' };
241 $airbrake->notify($@);
242  
243 =head1 DESCRIPTION
244  
245 API Compatible with L<Net::Airbrake> but converts v3 requests to v2 and then converts the response back.
246  
247 This makes it usable with L<Errbit|https://errbit.github.io/errbit/> C<< <= v0.3 >>.
248  
249 B<Note>: This is currently based heavily on the internals of L<Net::Airbrake> (as of C<0.02>).
250 This enables laziness at the cost of fragility.
251 As such the implementation is subject to change.
252  
253 See L<Net::Airbrake> for descriptions of methods and arguments.
254  
255 =head1 METHODS
256  
257 =head2 convert_request
258  
259 $client->convert_request(\%v3_request);
260 Net::Airbrake::V2->convert_request(\%v3_request, \%config);
261  
262 Convert a v3 request (JSON) to v2 (XML).
263 This rearranges the data structure as best it can.
264  
265 This can also be called as a class method
266 if a config hash is passed, containing:
267  
268 =over 4
269  
270 =item *
271  
272 api_key
273  
274 =back
275  
276 =head2 convert_response
277  
278 Convert v2 response (XML) to v3 response (JSON).
279  
280 =for Pod::Coverage BUILD
281  
282 =head1 VERSION DIFFERENCES
283  
284 Some data may be lost converting from v3 to v2.
285 Specifically v2 does not have explicit places for:
286  
287 errors/{i}/backtrace/{i}/column
288 context/os
289 context/language
290 context/userAgent
291 context/userId
292 context/userName
293 context/userEmail
294  
295 =head1 SEE ALSO
296  
297 =over 4
298  
299 =item *
300  
301 L<Net::Airbrake>
302  
303 =item *
304  
305 L<Airbrake|https://airbrake.io>
306  
307 =item *
308  
309 L<Airbrake Notifier API v2|https://help.airbrake.io/kb/api-2/notifier-api-v23>
310  
311 =item *
312  
313 L<Errbit|https://errbit.github.io/errbit/>
314  
315 =back
316  
317 =head1 SUPPORT
318  
319 =head2 Perldoc
320  
321 You can find documentation for this module with the perldoc command.
322  
323 perldoc Net::Airbrake::V2
324  
325 =head2 Websites
326  
327 The following websites have more information about this module, and may be of help to you. As always,
328 in addition to those websites please use your favorite search engine to discover more resources.
329  
330 =over 4
331  
332 =item *
333  
334 MetaCPAN
335  
336 A modern, open-source CPAN search engine, useful to view POD in HTML format.
337  
338 L<http://metacpan.org/release/Net-Airbrake-V2>
339  
340 =back
341  
342 =head2 Bugs / Feature Requests
343  
344 Please report any bugs or feature requests by email to C<bug-net-airbrake-v2 at rt.cpan.org>, or through
345 the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=Net-Airbrake-V2>. You will be automatically notified of any
346 progress on the request by the system.
347  
348 =head2 Source Code
349  
350  
351 L<https://github.com/rwstauner/Net-Airbrake-V2>
352  
353 git clone https://github.com/rwstauner/Net-Airbrake-V2.git
354  
355 =head1 AUTHOR
356  
357 Randy Stauner <rwstauner@cpan.org>
358  
359 =head1 COPYRIGHT AND LICENSE
360  
361 This software is copyright (c) 2015 by Randy Stauner.
362  
363 This is free software; you can redistribute it and/or modify it under
364 the same terms as the Perl 5 programming language system itself.
365  
366 =cut
↑ Summary