blib/lib/LWP/Simple/REST.pm

filter: Statementstmt 80.1% 85 / 106 Branchbran 50.0% 7 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 84.2% 16 / 19 Podpod 60.0% 6 / 10 totaltotal 76.5% 114 / 149 CC 8 SCAR 14.6
CC
8
Cov
77%
answer
20.0
json_get
2.0
http_get
2.0
CRAP 4.3
↑ Summary
1 package LWP::Simple::REST;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Data::Structure::Util qw( unbless );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 8 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 our @EXPORT_OK = qw/
11 http_get
12 http_post
13 http_put
14 http_delete
15 http_head
16 http_upload
17 json_get
18 json_post
19 json_put
20 json_head
21 /;
22  
23 8 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 8 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 8 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 8 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 8 our $VERSION = '0.091';
29  
30 8 my $user_agent = "LWP::Simple::REST";
31  
32 sub http_get {
33 1 ▶ my ( $url, $arguments ) = @_;
Subroutine http_get: called CC=2 SCAR=6.9
Pod documented
34  
35 1 my $ua = LWP::UserAgent->new;
36 1 $ua->agent($user_agent);
37  
38 # Pass a url sanitizer
39 1 my @parameters;
40 1 ▶ while ( my ( $key, $value ) = each %{ $arguments } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$arguments;}) 1 1 2
41 1 push @parameters, "$key=$value";
42 }
43 1 my $parameters_for_url = join "&", @parameters;
44 1 my $response = $ua->get( $url . "?$parameters_for_url" );
45  
46 1 return $response->content;
47 }
48  
49  
50 sub http_post {
51 1 ▶ my ( $url, $arguments ) = @_;
Subroutine http_post: called CC=1 SCAR=0.0
Pod documented
52  
53 1 my $ua = LWP::UserAgent->new;
54 1 $ua->agent($user_agent);
55  
56 1 my $response = $ua->post( $url,
57 $arguments,
58 );
59  
60 1 return $response->content;
61 }
62  
63 sub http_put {
64 1 ▶ my ( $url, $arguments ) = @_;
Subroutine http_put: called CC=1 SCAR=0.0
Pod undocumented
65  
66 1 my $ua = LWP::UserAgent->new;
67 1 $ua->agent($user_agent);
68  
69 1 my $response = $ua->put( $url,
70 $arguments,
71 );
72  
73 1 return $response->content;
74 }
75  
76 sub upload_post {
77 0 ▶ my ( $url, $json, $filename ) = @_;
Subroutine upload_post: not called CC=1 SCAR=6.9
Pod undocumented
78  
79 0 my $ua = LWP::UserAgent->new;
80 0 $ua->agent('RESTClient');
81  
82 0 my $response = $ua->post(
83 $url,
84 [
85 file => [ $filename ],
86 ],
87 'Content_Type' => 'form-data',
88 );
89  
90 0 return answer( $response );
91 }
92  
93 sub http_delete {
94 1 ▶ my ( $url, $arguments ) = @_;
Subroutine http_delete: called CC=2 SCAR=6.9
Pod documented
95  
96 1 my $ua = LWP::UserAgent->new;
97 1 $ua->agent('RESTClient');
98  
99 1 my @parameters;
100 1 ▶ while ( my ( $key, $value ) = each %{ $arguments } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$arguments;}) 1 1 2
101 1 push @parameters, "$key=$value";
102 }
103  
104 1 my $parameters_for_url = join "&", @parameters;
105  
106 1 my $response = $ua->delete( $url . "?$parameters_for_url" );
107  
108 1 return $response->content;
109  
110 }
111  
112 sub http_head {
113 1 ▶ my ( $url, $arguments ) = @_;
Subroutine http_head: called CC=2 SCAR=6.9
Pod documented
114  
115 1 my $ua = LWP::UserAgent->new;
116 1 $ua->agent($user_agent);
117  
118 1 my @parameters;
119 1 ▶ while ( my ( $key, $value ) = each %{ $arguments } ){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$arguments;}) 1 1 2
120 1 push @parameters, "$key=$value";
121 }
122 1 my $parameters_for_url = join "&", @parameters;
123 1 my $response = $ua->head( $url . "?$parameters_for_url" );
124  
125 1 return $response->headers;
126  
127 }
128  
129 sub json_post {
130 1 ▶ my ( $url, $arguments ) = @_;
Subroutine json_post: called CC=1 SCAR=0.0
Pod documented
131  
132 1 my $ua = LWP::UserAgent->new;
133 1 $ua->agent($user_agent);
134  
135 1 my $response = $ua->post( $url,
136 $arguments,
137 );
138  
139 1 return decode_json $response->content;
140 }
141  
142 sub json_put {
143 0 ▶ my ( $url, $arguments ) = @_;
Subroutine json_put: not called CC=1 SCAR=6.9
Pod undocumented
144  
145 0 my $ua = LWP::UserAgent->new;
146 0 $ua->agent($user_agent);
147  
148 0 my $response = $ua->put( $url,
149 $arguments,
150 );
151  
152 0 return decode_json $response->content;
153 }
154  
155 sub json_get {
156 1 ▶ my ( $url, $arguments ) = @_;
Subroutine json_get: called CC=2 SCAR=7.0
Pod documented
157  
158 1 my $ua = LWP::UserAgent->new;
159 1 $ua->agent($user_agent);
160  
161 # Pass a url sanitazier
162 1 my @parameters;
163 1 ▶ while ( my ( $key, $value ) = each %{ $arguments } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$arguments;}) 0 1 1
164 0 push @parameters, "$key=$value";
165 }
166 1 my $parameters_for_url = join "&", @parameters;
167 1 my $response = $ua->get( $url . "?$parameters_for_url" );
168  
169 1 return decode_json $response->content;
170 }
171  
172 sub answer {
173 0 ▶ my ( $response ) = @_;
Subroutine answer: not called CC=4 SCAR=30.0
Pod undocumented
174  
175 0 my $http_code = $response->code();
176 0 my $return = $response->decoded_content;
177  
178 0 ▶ if ( $response->is_success ){
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) 0 0 0
179 0 my $answer;
180 0 ▶ if ( $http_code =~ /(2\d\d)/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($http_code =~ /(2\d\d)/) 0 0 0
181 0 ▶ if ( $1 == 204 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($1 == 204) { } 0 0 0
182 0 return $return;
183 }else{
184 0 return decode_json( $return );
185 }
186 }
187 }
188 0 my $status = $response->status_line;
189 }
190  
191 =head1 NAME
192  
193 LWP::Simple::REST - A simple procedural interface do http verbs
194  
195 =head1 VERSION
196  
197 Version 0.091
198  
199 =head1 SYNOPSIS
200  
201 This module is a simple wrapper for simple http requests. It has two groups
202 of wrappers, http_ and json_. The first are to use with plain answers, the
203 second one assumes a json answer and already decode it.
204  
205 This is a classical example, to post a information to a server.
206  
207 use LWP::Simple::REST qw/http_post/;
208  
209 my $foo = http_post( "http://example.org", { example => "1", show => "all" } );
210 ...
211  
212 =head1 SUBROUTINES/METHODS
213  
214 All methods receive an url and a hashref with parameters. Now you can only send
215 normal parameters, in future is possible to send json encoded parameters on the
216 body.
217  
218 Also there is a method to upload files to the server, really simple, just on
219 hands for small files.
220  
221 =head2 http_get
222  
223 Sends a http get and returns the content of this request
224  
225 =head2 http_post
226  
227 Sends a http post and returns the content of this request
228  
229 =head2 http_delete
230  
231 Sends a delete request for the url
232  
233 =head2 http_head
234  
235 Sends a head request for the url, and unblesses the headers's object allowing access the header
236  
237 =head2 http_upload
238  
239 Sends an Upload to url
240  
241 =head2 json_get
242  
243 Sends a get request, expects a json response
244  
245 =head2 json_post
246  
247 Sends a post request, expects a json response
248  
249 =head1 AUTHOR
250  
251 GONCALES, C<< <italo.goncales at gmail.com> >>
252  
253 RECSKY, C<< <recsky at cpan.org> >>
254  
255 =head1 BUGS
256  
257 Please report any bugs or feature requests to C<bug-lwp-simple-rest at rt.cpan.org>, or through
258 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=LWP-Simple-REST>. I will be notified, and then you'll
259 automatically be notified of progress on your bug as I make changes.
260  
261  
262 =head1 SUPPORT
263  
264 You can find documentation for this module with the perldoc command.
265  
266 perldoc LWP::Simple::REST
267  
268  
269 Usually we are on irc on irc.perl.org.
270  
271 #sao-paulo.pm
272  
273 =over 4
274  
275 =item * RT: CPAN's request tracker (report bugs here)
276  
277 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=LWP-Simple-REST>
278  
279 =item * AnnoCPAN: Annotated CPAN documentation
280  
281 L<http://annocpan.org/dist/LWP-Simple-REST>
282  
283 =item * CPAN Ratings
284  
285 L<http://cpanratings.perl.org/d/LWP-Simple-REST>
286  
287 =back
288  
289 =head1 LICENSE AND COPYRIGHT
290  
291 Copyright 2014 GONCALES
292 Copyright 2014 RECSKY
293  
294 This program is free software; you can redistribute it and/or modify it
295 under the terms of the the Artistic License (2.0). You may obtain a
296 copy of the full license at:
297  
298 L<http://www.perlfoundation.org/artistic_license_2_0>
299  
300 =cut
301  
302 8 1; # End of LWP::Simple::REST
↑ Summary