File Coverage

blib/lib/Exception/Reporter/Summarizer/Text.pm
Criterion Covered Total %
statement 31 31 100.0
branch 4 6 66.6
condition n/a
subroutine 9 9 100.0
pod 0 2 0.0
total 44 48 91.6


line stmt bran cond sub pod time code
1 1     1   428 use strict;
  1         2  
  1         28  
2 1     1   5 use warnings;
  1         2  
  1         40  
3             package Exception::Reporter::Summarizer::Text 0.015;
4             # ABSTRACT: a summarizer for plain text strings
5              
6 1     1   5 use parent 'Exception::Reporter::Summarizer';
  1         2  
  1         6  
7              
8             #pod =head1 OVERVIEW
9             #pod
10             #pod This summarizer will summarize simple, non-empty strings by accepting them
11             #pod verbatim. They are assumed to be text, and will be encoded to UTF-8. If that
12             #pod fails, they will be used verbatim, possibly with strange results.
13             #pod
14             #pod The summary's C will be the first non-blank line of the string.
15             #pod
16             #pod =cut
17              
18             # Maybe in the future we can have options to allow empty strings. -- rjbs,
19             # 2013-02-06
20              
21 1     1   55 use Encode ();
  1         2  
  1         13  
22 1     1   15 use Try::Tiny;
  1         4  
  1         332  
23              
24             sub can_summarize {
25 8     8 0 17 my ($self, $entry) = @_;
26 8         18 my $value = $entry->[1];
27 8 50       35 return unless defined $value;
28 8 100       28 return if ref $value;
29 5 50       15 return if ref \$value ne 'SCALAR';
30 5         17 return 1;
31             }
32              
33             sub summarize {
34 5     5 0 24 my ($self, $entry, $internal_arg) = @_;
35 5         12 my ($name, $value, $arg) = @$entry;
36              
37 5         33 my $fn_base = $self->sanitize_filename($name);
38              
39             my $octets = try {
40 5     5   271 encode('utf-8', $value, Encode::FB_CROAK);
41             } catch {
42 5     5   106 $value;
43 5         55 };
44              
45 5         26 my $ident = $value;
46 5         15 $ident =~ s/\A\n+//;
47 5         17 ($ident) = split /\n/, $ident;
48              
49             return {
50 5         38 filename => "$fn_base.txt",
51             ident => $ident,
52             mimetype => 'text/plain',
53             charset => 'utf-8', # possibly a lie if the try failed
54             body => "$value",
55             };
56             }
57              
58             1;
59              
60             __END__