File Coverage

blib/lib/Message/Passing/Output/File.pm
Criterion Covered Total %
statement 9 22 40.9
branch 0 6 0.0
condition n/a
subroutine 3 6 50.0
pod 1 2 50.0
total 13 36 36.1


line stmt bran cond sub pod time code
1             package Message::Passing::Output::File;
2 1     1   1636 use Moo;
  1         3  
  1         7  
3 1     1   381 use MooX::Types::MooseLike::Base qw/ Str Bool /;
  1         2  
  1         83  
4 1     1   7 use namespace::clean -except => 'meta';
  1         6  
  1         14  
5              
6             with 'Message::Passing::Role::Output';
7              
8             has filename => (
9             isa => Str,
10             is => 'ro',
11             predicate => '_has_filename',
12             );
13              
14             has fh => (
15             is => 'ro',
16             lazy => 1,
17             builder => '_build_fh',
18             );
19              
20             has append => (
21             is => 'ro',
22             isa => Bool,
23             default => sub { 1 },
24             );
25              
26             sub _build_fh {
27 0     0     my $self = shift;
28 0 0         confess("Need a filename to output to") unless $self->_has_filename;
29 0 0         my $mode = $self->append ? '>>' : '>';
30 0 0         open(my $fh, $mode, $self->filename) or confess("Could not open ".
31             $self->filename . " for writing: $!");
32 0           $fh;
33             }
34              
35             sub BUILD {
36 0     0 0   my $self = shift;
37 0           $self->fh;
38             }
39              
40             sub consume {
41 0     0 1   my $self = shift;
42 0           my $saved = select($self->fh);
43 0           local $|=1;
44 0           print shift() . "\n";
45 0           select($saved);
46 0           return 1;
47             }
48              
49              
50             1;
51              
52             =head1 NAME
53              
54             Message::Passing::Output::File - File output
55              
56             =head1 SYNOPSIS
57              
58             message-pass --input STDIN --output File --output_options '{"filename": "/tmp/my.log"}'
59             {"foo": "bar"}
60             {"foo":"bar"}
61              
62             =head1 DESCRIPTION
63              
64             Output messages to File
65              
66             =head1 METHODS
67              
68             =head2 append
69              
70             A boolean attribute for if the output file should be re-created, or
71             appended to. Default true.
72              
73             =head2 filename
74              
75             An attribute for the file name to write to.
76              
77             =head2 consume
78              
79             Consumes a message by JSON encoding it and printing it, followed by \n
80              
81             =head1 SEE ALSO
82              
83             L
84              
85             =head1 SPONSORSHIP
86              
87             This module exists due to the wonderful people at Suretec Systems Ltd.
88             who sponsored its development for its
89             VoIP division called SureVoIP for use with
90             the SureVoIP API -
91            
92              
93             =head1 AUTHOR, COPYRIGHT AND LICENSE
94              
95             See L.
96              
97             =cut