blib/lib/Net/Airbrake/V2.pm

filter: Statementstmt 100% 77 / 77 Branchbran 93.7% 15 / 16 Conditioncond 52.3% 11 / 21 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 100% 19 / 19 Podpod 75.0% 3 / 4 totaltotal 85.2% 127 / 149 SCAR 29.1
CC
18
Cov
90%
convert_request
8.5
ua
3.5
_change_content_type
3.0
CRAP 18.3
↑ 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-4-g4971881
15  
16 2 our $AUTHORITY = 'cpan:RWSTAUNER';
17 2 $Net::Airbrake::V2::VERSION = '0.03';
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 3 return ($parent => [
53 {
54 $self->_var_content($vars),
55 }
56 ]);
57 }
58  
59 sub _var_content {
60 13 ▶ my ($self, $v) = @_;
Subroutine _var_content: called CC=3 SCAR=11.0
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 sub stringify_ref {
80 2 ▶ my ($self, $value) = @_;
Subroutine stringify_ref: called CC=2 SCAR=7.0
Pod documented
81 2 ▶ ($Dumper ||= do {
Condition66%2/3
$Net::Airbrake::V2::Dumper ||= do {
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 sub convert_request {
95 3 ▶ my ($self, $req, $opts) = @_;
Subroutine convert_request: called CC=8 SCAR=21.4
Pod documented
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} || [] }
Branch: 1 branch
BranchTrueFalseTotal
$req->{"errors"} or [] 1 2 3
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 sub _xml_encode {
160 2 ▶ my ($self, $notice) = @_;
Subroutine _xml_encode: called CC=1 SCAR=0.0
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 sub convert_response {
178 3 ▶ my ($self, $res) = @_;
Subroutine convert_response: called CC=2 SCAR=6.9
Pod documented
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 sub new {
191 2 ▶ bless $_[1], $_[0];
Subroutine new: called CC=1 SCAR=0.0
192 }
193  
194 sub ua {
195 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
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 sub _convert_request {
205 2 ▶ my ($self, $msg) = @_;
Subroutine _convert_request: called CC=1 SCAR=0.0
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 sub _convert_response {
211 2 ▶ my ($self, $msg) = @_;
Subroutine _convert_response: called CC=1 SCAR=0.0
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 sub _change_content_type {
218 4 ▶ my ($self, $msg, $val) = @_;
Subroutine _change_content_type: called CC=3 SCAR=11.1
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 sub request {
227 2 ▶ my ($self, $method, $url, $req) = @_;
Subroutine request: called CC=1 SCAR=0.0
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__
245  
246 =pod
247  
248 =encoding UTF-8
249  
250 =for :stopwords Randy Stauner ACKNOWLEDGEMENTS Errbit cpan testmatrix url annocpan anno
251 bugtracker rt cpants kwalitee diff irc mailto metadata placeholders
252 metacpan
253  
254 =head1 NAME
255  
256 Net::Airbrake::V2 - Airbrake Notifier API V2 Client
257  
258 =head1 VERSION
259  
260 version 0.03
261  
262 =head1 SYNOPSIS
263  
264 use Net::Airbrake::V2;
265  
266 my $airbrake = Net::Airbrake::V2->new(
267 api_key => 'xxxxxxx',
268 # project_id is not used.
269 );
270  
271 eval { die 'Oops' };
272 $airbrake->notify($@);
273  
274 =head1 DESCRIPTION
275  
276 API Compatible with L<Net::Airbrake> but converts v3 requests to v2 and then converts the response back.
277  
278 This makes it usable with L<Errbit|https://errbit.github.io/errbit/> C<< <= v0.3 >>.
279  
280 B<Note>: This is currently based heavily on the internals of L<Net::Airbrake> (as of C<0.02>).
281 This enables laziness at the cost of fragility.
282 As such the implementation is subject to change.
283  
284 See L<Net::Airbrake> for descriptions of methods and arguments.
285  
286 =head1 METHODS
287  
288 =head2 stringify_ref
289  
290 The values of the the "var" hashes ("params", "session", and "environment")
291 that are references (other than hashes) will stringified.
292  
293 This is currently done with C<Data::Dumper>
294 which is similar to the way the ruby gem dumps structures.
295 The format is subject to change.
296  
297 =head2 convert_request
298  
299 $client->convert_request(\%v3_request);
300 Net::Airbrake::V2->convert_request(\%v3_request, \%config);
301  
302 Convert a v3 request (JSON) to v2 (XML).
303 This rearranges the data structure as best it can.
304  
305 This can also be called as a class method
306 if a config hash is passed, containing:
307  
308 =over 4
309  
310 =item *
311  
312 api_key
313  
314 =back
315  
316 =head2 convert_response
317  
318 Convert v2 response (XML) to v3 response (JSON).
319  
320 =for Pod::Coverage BUILD
321  
322 =head1 VERSION DIFFERENCES
323  
324 Some data may be lost converting from v3 to v2.
325 Specifically v2 does not have explicit places for:
326  
327 errors/{i}/backtrace/{i}/column
328 context/os
329 context/language
330 context/userAgent
331 context/userId
332 context/userName
333 context/userEmail
334  
335 =head1 SEE ALSO
336  
337 =over 4
338  
339 =item *
340  
341 L<Net::Airbrake>
342  
343 =item *
344  
345 L<Airbrake|https://airbrake.io>
346  
347 =item *
348  
349 L<Airbrake Notifier API v2|https://help.airbrake.io/kb/api-2/notifier-api-v23>
350  
351 =item *
352  
353 L<Errbit|https://errbit.github.io/errbit/>
354  
355 =back
356  
357 =head1 SUPPORT
358  
359 =head2 Perldoc
360  
361 You can find documentation for this module with the perldoc command.
362  
363 perldoc Net::Airbrake::V2
364  
365 =head2 Websites
366  
367 The following websites have more information about this module, and may be of help to you. As always,
368 in addition to those websites please use your favorite search engine to discover more resources.
369  
370 =over 4
371  
372 =item *
373  
374 MetaCPAN
375  
376 A modern, open-source CPAN search engine, useful to view POD in HTML format.
377  
378 L<http://metacpan.org/release/Net-Airbrake-V2>
379  
380 =back
381  
382 =head2 Bugs / Feature Requests
383  
384 Please report any bugs or feature requests by email to C<bug-net-airbrake-v2 at rt.cpan.org>, or through
385 the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=Net-Airbrake-V2>. You will be automatically notified of any
386 progress on the request by the system.
387  
388 =head2 Source Code
389  
390  
391 L<https://github.com/rwstauner/Net-Airbrake-V2>
392  
393 git clone https://github.com/rwstauner/Net-Airbrake-V2.git
394  
395 =head1 AUTHOR
396  
397 Randy Stauner <rwstauner@cpan.org>
398  
399 =head1 COPYRIGHT AND LICENSE
400  
401 This software is copyright (c) 2015 by Randy Stauner.
402  
403 This is free software; you can redistribute it and/or modify it under
404 the same terms as the Perl 5 programming language system itself.
405  
406 =cut
↑ Summary