DataAPI/Role/Service.pm

filter: Statementstmt 100% 104 / 104 Branchbran 100% 40 / 40 Conditioncond 100% 14 / 14 MC/DCmcdc 100% 9 / 9 Subroutinesub 100% 21 / 21 Podpod 0.0% 0 / 8 totaltotal 95.9% 188 / 196 CC 27 SCAR 19.9
CC
27
Cov
100%
prepare_request
13.0
request
12.0
ns
3.0
CRAP 7.3
↑ Summary DataAPI/Auth/ClientLogin/Multiple.pm »
1 package Net::Google::DataAPI::Role::Service;
2 15 ▶use Any::Moose '::Role';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use XML::Atom;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use XML::Atom::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use XML::Atom::Feed;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Net::Google::DataAPI::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Net::Google::DataAPI::Auth::Null;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 our $VERSION = '0.04';
12  
13 15 $XML::Atom::ForceUnicode = 1;
14 15 $XML::Atom::DefaultVersion = 1;
15  
16 15 has gdata_version => (
17 isa => 'Str',
18 is => 'ro',
19 required => 1,
20 default => '2.0',
21 );
22  
23 15 has ua => (
24 isa => 'LWP::UserAgent',
25 is => 'ro',
26 required => 1,
27 lazy_build => 1,
28 );
29  
30 15 has service => (
31 does => 'Net::Google::DataAPI::Role::Service',
32 is => 'ro',
33 required => 1,
34 lazy_build => 1,
35 );
36  
37 15 has source => (
38 isa => 'Str',
39 is => 'ro',
40 required => 1,
41 default => __PACKAGE__,
42 );
43  
44 15 has auth => (
45 is => 'ro',
46 does => 'Net::Google::DataAPI::Types::Auth',
47 required => 1,
48 lazy_build => 1,
49 handles => ['sign_request'],
50 coerce => 1,
51 );
52  
53 15 has namespaces => (
54 isa => 'HashRef[Str]',
55 is => 'ro',
56 );
57  
58 sub ns {
59 50 ▶ my ($self, $name) = @_;
Subroutine ns: called CC=3 SCAR=11.0
Pod undocumented
60  
61 50 ▶ if ($name eq 'gd') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "gd") 20 30 50
62 20 return XML::Atom::Namespace->new('gd', 'http://schemas.google.com/g/2005')
63 }
64 30 ▶ $self->namespaces->{$name} or confess "Namespace '$name' is not defined!";
Branch: 1 branch
BranchTrueFalseTotal
$self->namespaces->{$name} or confess("Namespace '${name}' is not defined!") 1 29 30
65 29 return XML::Atom::Namespace->new($name, $self->namespaces->{$name});
66 };
67  
68 sub _build_ua {
69 14 ▶ my $self = shift;
Subroutine _build_ua: called CC=1 SCAR=0.0
70 14 my $ua = LWP::UserAgent->new(
71 agent => $self->source,
72 requests_redirectable => [],
73 env_proxy => 1,
74 );
75 14 $ua->default_headers(
76 HTTP::Headers->new(
77 GData_Version => $self->gdata_version,
78 )
79 );
80 14 return $ua;
81 }
82  
83 11 ▶sub _build_auth { Net::Google::DataAPI::Auth::Null->new }
Subroutine _build_auth: called CC=1 SCAR=0.0
84  
85 11 ▶sub _build_service {return $_[0]}
Subroutine _build_service: called CC=1 SCAR=0.0
86  
87 sub request {
88 56 ▶ my ($self, $args) = @_;
Subroutine request: called CC=12 SCAR=24.8
Pod undocumented
89 56 my $req = $self->prepare_request($args);
90 55 my $uri = $req->uri;
91 55 my $res = eval {$self->ua->request($req)};
92 55 ▶ if ($ENV{GOOGLE_DATAAPI_DEBUG} && $res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"GOOGLE_DATAAPI_DEBUG"} and $res) 2 53 55
Condition100%3/3
$ENV{"GOOGLE_DATAAPI_DEBUG"} and $res
!ll&&!rl&&r
5212
MC/DC100%2/2
$ENV{"GOOGLE_DATAAPI_DEBUG"} and $res
$ENV{"GOOGLE_DATAAPI_DEBUG"} $res
Truth table100%3/3
A and B
A= $ENV{"GOOGLE_DATAAPI_DEBUG"}B= $res
ABresult
0X0
100
111
93 2 ▶ warn $res->request ? $res->request->as_string : $req->as_string;
Branch: 1 branch
BranchTrueFalseTotal
$res->request ? : 1 1 2
94 2 warn $res->as_string;
95 }
96 55 ▶ if ($@ || $res->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or $res->is_error) 4 51 55
Condition100%3/3
$@ or $res->is_error
l!l&&r!l&&!r
2251
MC/DC100%2/2
$@ or $res->is_error
$@ $res->is_error
Truth table100%3/3
A or B
A= $@B= $res->is_error
ABresult
1X1
011
000
97 4 ▶ confess sprintf(
Branch: 2 branches
BranchTrueFalseTotal
$res ? : 2 2 4
$res ? : 2 2 4
98 "request for '%s' failed:\n\t%s\n\t%s\n\t",
99 $uri,
100 ($res ? $res->status_line : $@),
101 ($res ? $res->content : $!),
102 );
103 }
104 51 ▶ if (my $res_obj = $args->{response_object}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $res_obj = $args->{"response_object"}) 41 10 51
105 41 my $type = $res->content_type;
106 41 ▶ if ($res->content_length && $type !~ m{^application/atom\+xml}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->content_length and not $type =~ m[^application/atom\+xml]) 1 40 41
Condition100%3/3
$res->content_length and not $type =~ m[^application/atom\+xml]
!ll&&!rl&&r
3821
MC/DC100%2/2
$res->content_length and not $type =~ m[^application/atom\+xml]
$res->content_length not $type =~ m[^application/atom\+xml]
Truth table100%3/3
A and not(B)
A= $res->content_lengthB= not $type =~ m[^application/atom\+xml]
ABresult
0X0
100
111
107 1 confess sprintf(
108 "Content-Type of response for '%s' is not 'application/atom+xml': %s",
109 $uri,
110 $type
111 );
112 }
113 40 my $obj = eval {$res_obj->new(\($res->content))};
114 40 ▶ confess sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 39 40
115 "response for '%s' is broken: %s",
116 $uri,
117 $@
118 ) if $@;
119 39 return $obj;
120 }
121 10 return $res;
122 }
123  
124 sub prepare_request {
125 57 ▶ my ($self, $args) = @_;
Subroutine prepare_request: called CC=13 SCAR=25.6
Pod undocumented
126 57 ▶ if (ref($args) eq 'HTTP::Request') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq "HTTP::Request") 1 56 57
127 1 return $args;
128 }
129 56 my $method = delete $args->{method};
130 56 ▶ $method = $args->{content} || $args->{parts} ? 'POST' : 'GET' unless $method;
Branch: 2 branches
BranchTrueFalseTotal
unless $method 41 15 56
$args->{'content'} || $args->{'parts'} ? : 14 27 41
Condition100%3/3
$args->{"content"} or $args->{"parts"}
l!l&&r!l&&!r
12227
MC/DC100%2/2
$args->{"content"} or $args->{"parts"}
$args->{"content"} $args->{"parts"}
Truth table100%3/3
A or B
A= $args->{"content"}B= $args->{"parts"}
ABresult
1X1
011
000
131 56 my $uri = URI->new($args->{uri});
132 56 my @existing_query = $uri->query_form;
133 $uri->query_form(
134 {
135 @existing_query,
136 20 %{$args->{query}}
137 }
138 56 ▶ ) if $args->{query};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"query"} 20 36 56
139 56 my $req = HTTP::Request->new($method => "$uri");
140 56 ▶ if (my $parts = $args->{parts}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parts = $args->{"parts"}) 2 54 56
141 2 $req->header('Content-Type' => 'multipart/related');
142 2 for my $part (@$parts) {
143 3 ▶ ref $part eq 'HTTP::Message'
Branch: 1 branch
BranchTrueFalseTotal
ref $part eq "HTTP::Message" or confess("part argument should be a HTTP::Message object") 1 2 3
144 or confess "part argument should be a HTTP::Message object";
145 2 $req->add_part($part);
146 }
147 }
148 55 ▶ $req->content($args->{content}) if $args->{content};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"content"} 21 34 55
149 55 ▶ $req->header('Content-Type' => $args->{content_type}) if $args->{content_type};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"content_type"} 21 34 55
150 55 ▶ if ($args->{header}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"header"}) 17 38 55
151 17 ▶ while (my @pair = each %{$args->{header}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@pair) = each %{$$args{"header"};}) 17 17 34
152 17 $req->header(@pair);
153 }
154 }
155 55 $self->sign_request($req, $args->{sign_host});
156 55 return $req;
157 }
158  
159 sub get_feed {
160 20 ▶ my ($self, $url, $query) = @_;
Subroutine get_feed: called CC=1 SCAR=0.0
Pod undocumented
161 20 return $self->request(
162 {
163 uri => $url,
164 query => $query,
165 response_object => 'XML::Atom::Feed',
166 }
167 );
168 }
169  
170 sub get_entry {
171 6 ▶ my ($self, $url) = @_;
Subroutine get_entry: called CC=1 SCAR=0.0
Pod undocumented
172 6 return $self->request(
173 {
174 uri => $url,
175 response_object => 'XML::Atom::Entry',
176 }
177 );
178 }
179  
180 sub post {
181 10 ▶ my ($self, $url, $entry, $header) = @_;
Subroutine post: called CC=2 SCAR=6.9
Pod undocumented
182 10 ▶ return $self->request(
Condition100%2/2
$header || undef
l!l
19
MC/DC100%1/1
$header || undef
$header
Truth table100%2/2
A
A= $header
Aresult
11
00
183 {
184 uri => $url,
185 content => $entry->as_xml,
186 header => $header || undef,
187 content_type => 'application/atom+xml',
188 response_object => ref $entry,
189 }
190 );
191 }
192  
193 sub put {
194 8 ▶ my ($self, $args) = @_;
Subroutine put: called CC=1 SCAR=0.0
Pod undocumented
195 return $self->request(
196 {
197 method => 'PUT',
198 uri => $args->{self}->editurl,
199 content => $args->{entry}->as_xml,
200 header => {'If-Match' => $args->{self}->etag },
201 8 content_type => 'application/atom+xml',
202 response_object => 'XML::Atom::Entry',
203 }
204 );
205 }
206  
207 sub delete {
208 4 ▶ my ($self, $args) = @_;
Subroutine delete: called CC=1 SCAR=0.0
Pod undocumented
209 my $res = $self->request(
210 {
211 uri => $args->{self}->editurl,
212 method => 'DELETE',
213 header => {'If-Match' => $args->{self}->etag},
214 }
215 4 );
216 3 return $res;
217 }
218  
219 15 ▶no Any::Moose '::Role';
Subroutine BEGIN: called CC=1 SCAR=0.0
220  
221 15 1;
222  
223 __END__
224  
225 =pod
226  
227 =head1 NAME
228  
229 Net::Google::DataAPI::Role::Service - provides base functionalities for Google Data API service
230  
231 =head1 SYNOPSIS
232  
233 package MyService;
234 use Any::Moose;
235 use Net::Google::DataAPI;
236 with 'Net::Google::DataAPI::Role::Service' => {
237 service => 'wise',
238 source => __PACKAGE__,
239 ns => {
240 foobar => 'http://example.com/schema#foobar',
241 },
242 }
243  
244 feedurl hoge => (
245 is => 'ro',
246 isa => 'Str',
247 entry_class => 'MyService::Hoge',
248 default => 'http://example.com/feed/hoge',
249 );
250  
251 1;
252  
253 =head1 DESCRIPTION
254  
255 =head1 AUTHOR
256  
257 Nobuo Danjou E<lt>danjou@soffritto.orgE<gt>
258  
259 =head1 SEE ALSO
260  
261 L<Net::Google::AuthSub>
262  
263 L<Net::Google::DataAPI>
264  
265 =cut
↑ Summary DataAPI/Auth/ClientLogin/Multiple.pm »