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   328 use strict;
  1         2  
  1         20  
2 1     1   3 use warnings;
  1         1  
  1         34  
3             package Exception::Reporter::Summarizer::Text;
4             # ABSTRACT: a summarizer for plain text strings
5             $Exception::Reporter::Summarizer::Text::VERSION = '0.013';
6 1     1   2 use parent 'Exception::Reporter::Summarizer';
  1         2  
  1         3  
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   42 use Encode ();
  1         2  
  1         9  
22 1     1   3 use Try::Tiny;
  1         1  
  1         209  
23              
24             sub can_summarize {
25 8     8 0 6 my ($self, $entry) = @_;
26 8         9 my $value = $entry->[1];
27 8 50       13 return unless defined $value;
28 8 100       17 return if ref $value;
29 5 50       11 return if ref \$value ne 'SCALAR';
30 5         11 return 1;
31             }
32              
33             sub summarize {
34 5     5 0 5 my ($self, $entry, $internal_arg) = @_;
35 5         6 my ($name, $value, $arg) = @$entry;
36              
37 5         13 my $fn_base = $self->sanitize_filename($name);
38              
39             my $octets = try {
40 5     5   126 encode('utf-8', $value, Encode::FB_CROAK);
41             } catch {
42 5     5   37 $value;
43 5         19 };
44              
45 5         12 my $ident = $value;
46 5         8 $ident =~ s/\A\n+//;
47 5         11 ($ident) = split /\n/, $ident;
48              
49             return {
50 5         29 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__