Karmasphere/Publisher.pm

filter: Statementstmt 47.4% 37 / 78 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 75.0% 12 / 16 Podpod 0.0% 0 / 3 totaltotal 42.2% 49 / 116 CC 13 SCAR 34.5
CC
13
Cov
39%
parse
90.0
_output_file
12.0
new
6.0
CRAP 31.5
« Karmasphere/Response.pm ↑ Summary Karmasphere/Parser/Base.pm »
1 package Mail::Karmasphere::Publisher;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp qw(cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 @ISA = qw(Exporter);
18 1 @EXPORT_OK = qw();
19 1 %EXPORT_TAGS = (
20 'all' => \@EXPORT_OK,
21 'ALL' => \@EXPORT_OK,
22 );
23 }
24  
25 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
26 0 my $class = shift;
27 0 ▶ my $self = ($#_ == 0) ? { %{ (shift) } } : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
$#_ == 0 ? : 0 0 0
28 # Check for Principal, Credentials
29 0 return bless $self, $class;
30 }
31  
32 0 ▶sub _output_file {
Subroutine _output_file: not called CC=3 SCAR=24.8
33 0 my ($input, $output, $index) = @_;
34 0 ▶ return $output if ref $output; # An IO::File
Branch: 1 branch
BranchTrueFalseTotal
if ref $output 0 0 0
35 0 ▶ return new IO::File("> $output") if defined $output;
Branch: 1 branch
BranchTrueFalseTotal
if defined $output 0 0 0
36 0 my $temp = $input;
37 0 return new File::Temp(
38 TEMPLATE => "$input.$$.$index.XXXXXX",
39 SUFFIX => ".ktmp",
40 );
41 }
42  
43 # Do not fuck with this method, Karma-Syndicator calls it.
44 # we output to a temporary file and write to it.
45 # once we're done writing to it, we rename the temp file to the real filename.
46 0 ▶sub parse {
Subroutine parse: not called CC=9 SCAR=45.0
Pod undocumented
47 0 my ($self, $input, $class, $outputs, %args) = @_;
48  
49 0 eval qq{ require $class; };
50 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
51  
52 0 my $fh = new IO::File("< $input");
53 0 my $parser = $class->new(fh => $fh, %args);
54 0 my $streams = $parser->streams;
55  
56 0 ▶ $outputs ||= []; # An array of filenames.
Condition0%0/2
$outputs ||= []
l!l
00
MC/DC0%0/1
$outputs ||= []
$outputs
Truth table0%0/2
A
A= $outputs
Aresult
11
00
57  
58 # print STDERR "outputs are " . Dumper($outputs);
59 0 my @files = map { _output_file($outputs->[$_], undef, $_) } (0..$#$streams);
60 # print STDERR ">>> parse temp files are " . Dumper(\@files);
61  
62 0 for my $i (0..$#$streams) {
63 0 ▶ if (not $outputs->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($outputs->[$i]) 0 0 0
64 0 cluck ("parser $parser for input $input defines stream $i, but there is no corresponding output filename!");
65 }
66 }
67
68 # this is a ridiculously slow inner loop. can we optimize?
69 0 ▶ while (my @records = $parser->parse) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@records) = $parser->parse) 0 0 0
70 0 for my $record (@records) {
71 0 ▶ next if not defined $record;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $record 0 0 0
72 0 my $file = $files[$record->stream];
73 0 print $file $record->as_string, "\n";
74 }
75 }
76  
77 0 foreach my $i (0 .. $#$streams) {
78 0 my $fh = $files[$i];
79 0 print STDERR " >> stream $i: renaming " . $fh->filename() . " to " . $outputs->[$i] . "\n";
80 0 rename($fh->filename, $outputs->[$i]);
81 0 chmod 0644, $outputs->[$i];
82 }
83  
84 0 return 1;
85 }
86  
87 0 ▶sub publish {
Subroutine publish: not called CC=2 SCAR=17.9
Pod undocumented
88 0 my ($self, $file, $class, $params) = @_;
89  
90 0 my $ua = LWP::UserAgent->new;
91  
92 0 my $url = $params->{url};
93 0 my $feed = $params->{feed};
94  
95 my $req = POST ($url,
96 Content_Type => "form-data",
97 Content =>
98 [ feed_id => $feed,
99 login => $params->{user},
100 password => $params->{pass},
101 0 data_source => "upload",
102 data_file => [ $file ],
103 ]);
104  
105 0 ▶ if (defined $params->{htuser}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params->{"htuser"}) 0 0 0
106 $req->headers->authorization_basic($params->{htuser},
107 0 $params->{htpass});
108 }
109
110 0 my $res = $ua->request($req);
111 0 return $res;
112 }
113  
114  
115  
116 1 1;
117  
118 __END__
« Karmasphere/Response.pm ↑ Summary Karmasphere/Parser/Base.pm »