blib/lib/LWP/Simple/Post.pm

filter: Statementstmt 56.2% 18 / 32 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 75.0% 6 / 8 Podpod 100% 2 / 2 totaltotal 41.9% 26 / 62 CC 8 SCAR 36.6
CC
8
Cov
38%
post
72.0
post_xml
2.0
BEGIN
1.0
CRAP 38.9
↑ Summary
1 package LWP::Simple::Post;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $LWP::Simple::Post::VERSION = '0.05';
4 }
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use parent "Exporter";
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 our @EXPORT_OK = qw( post post_xml );
11  
12 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 LWP::Simple::Post - Single-method POST requests
18  
19 =head1 VERSION
20  
21 version 0.05
22  
23 =head1 DESCRIPTION
24  
25 Really simple wrapper to HTTP POST requests
26  
27 =head1 SYNOPSIS
28  
29 use LWP::Simple::Post qw(post post_xml);
30  
31 my $response = post('http://production/receiver', 'some text');
32  
33 =head1 OVERVIEW
34  
35 B<DON'T USE THIS MODULE! There are very few situations in which
36 this module would be a win over using LWP::UserAgent directly.
37 It was a bad idea I implemented a long time ago.>
38  
39 This module is intended to do for HTTP POST requests what
40 LWP::Simple did for GET requests. If you want to do anything
41 complicated, this module is not for you. If you just want to
42 push data at a URL with the minimum of fuss, you're the target
43 audience.
44  
45 =head1 METHODS
46  
47 =head2 post
48  
49 my $content = post( string $url, string $data );
50  
51 Posts the data in C<$data> to the URL in C<$url>, and returns
52 what we got back. Returns C<undef> on failure.
53  
54 =cut
55  
56 # I have added all sorts of hooks here in case you need to do
57 # anything complicated, BUT, if you do, you shouldn't be using
58 # this module...
59  
60 0 ▶sub post {
Subroutine post: not called CC=8 SCAR=42.8
Pod documented
61 0 my ( $url, $data, $params ) = @_;
62  
63 # Make the top secret params argument safe to use easily
64 0 ▶ $params = {} unless $params and ref $params;
Branch: 1 branch
BranchTrueFalseTotal
unless $params and ref $params 0 0 0
Condition0%0/3
$params and ref $params
!ll&&!rl&&r
000
MC/DC0%0/2
$params and ref $params
$params ref $params
Truth table0%0/3
A and B
A= $paramsB= ref $params
ABresult
0X0
100
111
65  
66 # Prepare the request itself
67 0 my $request = HTTP::Request->new( POST => $url );
68 0 $request->content($data);
69 0 ▶ $request->header( 'Content-type' => 'text/xml' ) if $params->{'as_xml'};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"as_xml"} 0 0 0
70 0 ▶ return $request if $params->{'return_request'};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"return_request"} 0 0 0
71  
72 # Execute the request
73 0 ▶ my $ua = $params->{'ua'} || LWP::UserAgent->new();
Condition0%0/3
$params->{'ua'} || 'LWP::UserAgent'->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->{'ua'} || 'LWP::UserAgent'->new
$params->{'ua'} 'LWP::UserAgent'->new
Truth table0%0/3
A || B
A= $params->{'ua'}B= 'LWP::UserAgent'->new
ABresult
1X1
011
000
74 0 my $response = $ua->request($request);
75 0 ▶ return $response if $params->{'return_response'};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"return_response"} 0 0 0
76  
77 # Give the result to the user
78 0 my $content;
79 0 ▶ $content = $response->content if $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $response->is_success 0 0 0
80 0 return $content;
81 }
82  
83 =head2 post_xml
84  
85 my $content = post_xml( string $url, string $data );
86  
87 Having written this module, it turned out that 99% of what I needed it
88 for required a content-type of C<text/xml>. This does exactly what C<post>
89 does, only the content-type header is set to C<text/html>.
90  
91 =cut
92  
93 0 ▶sub post_xml {
Subroutine post_xml: not called CC=1 SCAR=6.9
Pod documented
94 0 my ( $url, $data ) = @_;
95 0 return post( $url, $data, { as_xml => 1 });
96 }
97  
98 =head1 AUTHOR
99  
100 Peter Sergeant - C<pete@clueball.com>
101  
102 =head1 COPYRIGHT
103  
104 Copyright 2011 Pete Sergeant.
105  
106 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
107  
108 =cut
109  
110 1 1;
↑ Summary