blib/lib/Froody/Invoker/Remote.pm

filter: Statementstmt 97.0% 66 / 68 Branchbran 81.2% 13 / 16 Conditioncond 62.5% 5 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 14 / 14 Podpod 100% 2 / 2 totaltotal 89.3% 101 / 113 CC 13 SCAR 19.8
CC
13
Cov
91%
invoke
13.5
BEGIN
1.0
BEGIN
1.0
CRAP 7.3
« blib/lib/Froody/Server.pm ↑ Summary blib/lib/Froody/Request.pm »
1 package Froody::Invoker::Remote;
2 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 21 ▶use base qw(Froody::Invoker);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 21 __PACKAGE__->mk_accessors(qw( url ));
8  
9 21 ▶use Froody::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 21 ▶use LWP::Simple ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 21 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 21 our $VERSION = 0.01;
14  
15 21 ▶use Froody::Response::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 21 ▶use Froody::Response::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 21 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 21 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 21 ▶use Encode qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 21 my $ua = LWP::UserAgent->new;
22  
23 sub invoke
24 13 ▶{
Subroutine invoke: called CC=13 SCAR=26.1
Pod documented
25 13 my $self = shift;
26 13 my $method = shift;
27 13 my $params = shift;
28  
29 # build a URI
30 13 my $uri = URI->new($self->url);
31 13 my $multi = undef;
32 13 for my $key (keys %$params) {
33 5 my $arg_data = $method->arguments->{$key};
34 5 my $value = delete $params->{$key};
35 # HTTP::Request::Common doesn't like undef
36 5 ▶ $value = '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 5 5
37 21 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
38 5 ▶ if ($arg_data->{type}[0] eq 'multipart') {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg_data->{'type'}[0] eq 'multipart') { } 2 3 5
elsif ($arg_data->{'type'}[0] eq 'csv' and ref $value eq 'ARRAY') { } 1 4 5
39 2 ▶ if (ref($value) eq 'ARRAY' and ref($value->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY" and ref $value->[0]) 0 2 2
Condition66%2/3
ref $value eq "ARRAY" and ref $value->[0]
!ll&&!rl&&r
110
MC/DC0%0/2
ref $value eq "ARRAY" and ref $value->[0]
ref $value eq "ARRAY" ref $value->[0]
Truth table66%2/3
A and B
A= ref $value eq "ARRAY"B= ref $value->[0]
ABresult
0X0
100
111
40 0 $value = $value->[0];
41 }
42 2 ▶ $multi ||= 1;
Condition50%1/2
$multi ||= 1
l!l
02
MC/DC0%0/1
$multi ||= 1
$multi
Truth table50%1/2
A
A= $multi
Aresult
11
00
43 2 ▶ if (UNIVERSAL::isa($value, "Froody::Upload")) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($value, 'Froody::Upload')) { } 1 1 2
elsif (not ref $value) { } 0 1 1
44 1 $value = [ $value->filename, $value->client_filename ];
45 } elsif (!ref($value)) {
46 0 $value = [$value];
47 }
48 ▶ } elsif ($arg_data->{type}[0] eq 'csv' and ref($value) eq 'ARRAY') {
Condition66%2/3
$arg_data->{"type"}[0] eq "csv" and ref $value eq "ARRAY"
!ll&&!rl&&r
201
MC/DC50%1/2
$arg_data->{"type"}[0] eq "csv" and ref $value eq "ARRAY"
$arg_data->{"type"}[0] eq "csv" ref $value eq "ARRAY"
Truth table66%2/3
A and B
A= $arg_data->{"type"}[0] eq "csv"B= ref $value eq "ARRAY"
ABresult
0X0
100
111
49 1 $value = join(",", @$value);
50 }
51 5 ▶ $params->{$key} = ref $value ? $value : encode_utf8($value);
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 2 3 5
52 }
53 13 $params->{method} = $method->full_name;
54  
55 # Create a request
56 13 my $req = POST $uri,
57 Content_Type => 'form-data',
58 Content => [ %$params ];
59 13 my $res = $ua->request($req);
60  
61 13 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 3 10 13
62 3 Froody::Error->throw("froody.invoke.remote", "Bad response from remote server - ". $res->status_line);
63 }
64  
65 # return whatever we got
66 10 my $frs = Froody::Response::String->new();
67 10 $frs->set_bytes($res->content);
68 10 $frs->structure($method);
69 10 return $frs;
70 }
71  
72 1 ▶sub source {
Subroutine source: called CC=1 SCAR=0.0
Pod documented
73 1 shift->url;
74 }
75  
76 =head1 NAME
77  
78 Froody::Invoker::Remote - invoker that calls methods remotely
79  
80 =head1 SYNOPSIS
81  
82 use Froody::Invoker::Remote;
83  
84 my $remote = Froody::Invoker::Remote
85 ->new()
86 ->url("http://someserver.com/restendpoint/");
87
88 my $method = Froody::Method->new()
89 ->full_name("fred.bar.baz")
90 ->invoker($remote);
91
92 my $response = $method->call({ foo => "wibble" });
93  
94 =head1 DESCRIPTION
95  
96 An Invoker that calls a remote server to get the Froody::Response.
97  
98 =head2 Accessors
99  
100 =over
101  
102 =item url
103  
104 The URL of the REST endpoint.
105  
106 =item source
107  
108 Returns where this invoker originated
109  
110 =back
111  
112 =head1 BUGS
113  
114 None known.
115  
116 Please report any bugs you find via the CPAN RT system.
117 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Froody>
118  
119 =head1 AUTHOR
120  
121 Copyright Fotango 2005. All rights reserved.
122  
123 Please see the main L<Froody> documentation for details of who has worked
124 on this project.
125  
126 This module is free software; you can redistribute it and/or modify it under
127 the same terms as Perl itself.
128  
129 =head1 SEE ALSO
130  
131 L<Froody>, L<Froody::Invoker>
132  
133 =cut
134  
135 21 1;
« blib/lib/Froody/Server.pm ↑ Summary blib/lib/Froody/Request.pm »