API/Base.pm

filter: Statementstmt 17.8% 28 / 157 Branchbran 0.0% 0 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 57.1% 8 / 14 Podpod 100% 5 / 5 totaltotal 19.3% 41 / 212 CC 28 SCAR 45.4
CC
28
Cov
15%
insert
156.0
update
156.0
_load_item_type
12.0
CRAP 93.3
↑ Summary API/Account/ProgrammaticLogin.pm »
1 package WWW::Google::API::Base;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 WWW::Google::API::Base - Perl client to the Google Base API C<< <http://code.google.com/apis/base/> >>
9  
10 =head1 VERSION
11  
12 version 0.001
13  
14 $Id$
15  
16 =head1 SYNOPSIS
17  
18 use WWW::Google::API::Base;
19  
20 my $file_conf = LoadFile($ENV{HOME}.'/.gapi');
21  
22 my $api_key = $ENV{gapi_key};
23 my $api_user = $ENV{gapi_user};
24 my $api_pass = $ENV{gapi_pass};
25  
26 my $gbase = WWW::Google::API::Base->new( { auth_type => 'ProgrammaticLogin',
27 api_key => $api_key,
28 api_user => $api_user,
29 api_pass => $api_pass },
30 { } );
31  
32 =head1 METHODS
33  
34 =cut
35  
36 1 our $VERSION = '0.001';
37  
38 1 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use WWW::Google::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use XML::Atom::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use XML::Atom::Util qw( nodelist );
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 1 __PACKAGE__->mk_ro_accessors(qw(namespaces));
47 1 __PACKAGE__->mk_accessors(qw(client));
48  
49 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
50 0 my $class = shift;
51
52 0 my $client;
53 0 eval {
54 0 $client = WWW::Google::API->new('gbase', @_);
55 };
56 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
57 0 my $e = $@;
58 0 warn $e;
59 }
60 0 my $self = { client => $client,
61 namespaces => {
62 gm => XML::Atom::Namespace->new( gm => 'http://base.google.com/ns-metadata/1.0'),
63 g => XML::Atom::Namespace->new( g => 'http://base.google.com/ns/1.0' ),
64 batch => XML::Atom::Namespace->new( batch => 'http://schemas.google.com/gdata/batch' ),
65 }
66 };
67  
68 0 bless($self, $class);
69 0 return $self;
70 }
71  
72 0 ▶sub _load_item_type {
Subroutine _load_item_type: not called CC=3 SCAR=24.8
73 0 my $self = shift;
74 0 my $type = shift;
75  
76
77 0 my $ua = LWP::UserAgent->new( agent => 'WWW::Google::API' );
78
79 0 my $response = $ua->get($type);
80
81 0 ▶ die $response->status_line unless $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 0 0
82
83 0 my $entry = XML::Atom::Entry->new(\$response->content);
84
85 0 $type = $entry->get($self->{namespaces}{gm}, 'item_type');
86 0 my @attributes = nodelist($entry->elem, $self->{namespaces}{gm}{uri}, 'attribute');
87 0 my $attribute_types;
88 0 foreach my $attribute (@attributes) {
89 0 my $name = $attribute->getAttribute('name');
90 0 my $type = $attribute->getAttribute('type');
91 0 $name =~ s/\s/_/g;
92 0 $attribute_types->{$name} = $type;
93 }
94 0 $attribute_types->{'label'} = 'text';
95 0 return $type, $attribute_types;
96 }
97  
98 =head2 insert
99  
100 $insert_entry = $gbase->insert(
101 'http://www.google.com/base/feeds/itemtypes/en_US/Recipes',
102 { -title => 'He Jingxian\'s chicken',
103 -content => "<div xmlns='http://www.w3.org/1999/xhtml'>Delectable Sichuan specialty</div>",
104 -link => [
105 { rel => 'alternate',
106 type => 'text/html',
107 href => 'http://localhost/uniqueid'
108 },
109 ],
110 cooking_time => 30,
111 label => [qw(foo bar baz)],
112 main_ingredient => [qw(chicken chili peanuts)],
113 servings => 5,
114 },
115 );
116  
117 $new_id = $insert_entry->id;
118  
119 =cut
120  
121 0 ▶sub insert {
Subroutine insert: not called CC=12 SCAR=50.5
Pod documented
122 0 my $self = shift;
123 0 my $item_type = shift;
124 0 my $item_parts = shift;
125  
126 0 my ($type, $gpart_types) = $self->_load_item_type($item_type);
127  
128 0 $self->client->ua->default_header('content-type', 'application/atom+xml');
129  
130 0 my $xml = <<EOF;
131 <?xml version='1.0'?>
132 <entry xmlns='http://www.w3.org/2005/Atom'
133 xmlns:g='http://base.google.com/ns/1.0'>
134 <category scheme='http://base.google.com/categories/itemtypes' term='Recipes'/>
135 <g:item_type>$type</g:item_type>
136 EOF
137  
138 0 for my $key (keys %$item_parts) {
139 0 ▶ if ($key =~ /^-/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^-/) { } 0 0 0
140 0 ▶ if ($key eq '-content') {
Branch: 3 branches
BranchTrueFalseTotal
if ($key eq '-content') { } 0 0 0
elsif ($key eq '-link') { } 0 0 0
elsif (ref $item_parts->{$key} eq 'ARRAY') { } 0 0 0
141 0 $xml .= "<content type='xhtml'>\n";
142 0 $xml .= "$item_parts->{$key}\n";
143 0 $xml .= "</content>\n";
144 } elsif ($key eq '-link') {
145 0 ▶ if (ref $item_parts->{$key} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item_parts->{$key} eq 'ARRAY') { } 0 0 0
146 0 foreach (@{$item_parts->{$key}}) {
147 0 $xml .= "<link rel='$_->{rel}' type='$_->{type}' href='$_->{href}'/>\n";
148 }
149 } else {
150 0 $xml .= "<link rel='$item_parts->{$key}{rel}' type='$item_parts->{$key}{type}' href='$item_parts->{$key}{href}'/>\n";
151 }
152 } elsif (ref $item_parts->{$key} eq 'ARRAY') {
153 0 for my $item (@{$item_parts->{$key}}) {
154 0 $key =~ s/^-//;
155 0 $xml .= "<$key type='text'>$item</$key>\n";
156 }
157 } else {
158 0 $key =~ s/^-//;
159 0 $xml .= "<$key type='text'>".$item_parts->{"-$key"}."</$key>\n";
160 }
161 } else {
162 0 ▶ if (ref $item_parts->{$key} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item_parts->{$key} eq 'ARRAY') { } 0 0 0
163 0 for my $item (@{$item_parts->{$key}}) {
164 0 $xml .= "<g:$key type='$gpart_types->{$key}'>$item</g:$key>\n";
165 }
166 } else {
167 0 $xml .= "<g:$key type='$gpart_types->{$key}'>$item_parts->{$key}</g:$key>\n";
168 }
169 }
170 }
171 0 $xml .= "</entry>\n";
172
173 0 my $insert_request = HTTP::Request->new( POST => 'http://www.google.com/base/feeds/items',
174 $self->client->ua->default_headers,
175 $xml);
176 0 my $response;
177 0 eval {
178 0 $response = $self->client->do($insert_request);
179 };
180 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
181 0 my $error = $@;
182 0 die $error;
183 }
184  
185 0 my $atom = $response->content;
186
187 0 my $entry = XML::Atom::Entry->new(\$atom);
188  
189 0 return $entry
190 }
191  
192 =head2 update
193
194 $update_entry = $gbase->update(
195 $new_id,
196 { -title => 'He Jingxian\'s chicken',
197 -content => "<div xmlns='http://www.w3.org/1999/xhtml'>Delectable Sichuan specialty</div>",
198 -link => [
199 { rel => 'alternate',
200 type => 'text/html',
201 href => 'http://localhost/uniqueid'
202 },
203 ],
204 cooking_time => 60,
205 label => [qw(fio bir biz)],
206 main_ingredient => [qw(chicken chili peanuts)],
207 servings => 15,
208 },
209 );
210  
211 =cut
212  
213 0 ▶sub update {
Subroutine update: not called CC=12 SCAR=50.5
Pod documented
214 0 my $self = shift;
215 0 my $item_id = shift;
216 0 my $item_parts = shift;
217  
218 0 my $item = $self->select($item_id);
219  
220 0 my $item_type = 'http://www.google.com/base/feeds/itemtypes/en_US/';
221 0 $item_type .= $item->get($self->{namespaces}{g}, 'item_type');
222  
223 0 my ($type, $gpart_types) = $self->_load_item_type($item_type);
224  
225 0 $self->client->ua->default_header('content-type', 'application/atom+xml');
226  
227 0 my $xml = <<EOF;
228 <?xml version='1.0'?>
229 <entry xmlns='http://www.w3.org/2005/Atom'
230 xmlns:g='http://base.google.com/ns/1.0'>
231 <category scheme='http://base.google.com/categories/itemtypes' term='Recipes'/>
232 <g:item_type>$type</g:item_type>
233 EOF
234  
235 0 for my $key (keys %$item_parts) {
236 0 ▶ if ($key =~ /^-/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^-/) { } 0 0 0
237 0 ▶ if ($key eq '-content') {
Branch: 3 branches
BranchTrueFalseTotal
if ($key eq '-content') { } 0 0 0
elsif ($key eq '-link') { } 0 0 0
elsif (ref $item_parts->{$key} eq 'ARRAY') { } 0 0 0
238 0 $xml .= "<content type='xhtml'>\n";
239 0 $xml .= "$item_parts->{$key}\n";
240 0 $xml .= "</content>\n";
241 } elsif ($key eq '-link') {
242 0 ▶ if (ref $item_parts->{$key} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item_parts->{$key} eq 'ARRAY') { } 0 0 0
243 0 foreach (@{$item_parts->{$key}}) {
244 0 $xml .= "<link rel='$_->{rel}' type='$_->{type}' href='$_->{href}'/>\n";
245 }
246 } else {
247 0 $xml .= "<link rel='$item_parts->{$key}{rel}' type='$item_parts->{$key}{type}' href='$item_parts->{$key}{href}'/>\n";
248 }
249 } elsif (ref $item_parts->{$key} eq 'ARRAY') {
250 0 for my $item (@{$item_parts->{$key}}) {
251 0 $key =~ s/^-//;
252 0 $xml .= "<$key type='text'>$item</$key>\n";
253 }
254 } else {
255 0 $key =~ s/^-//;
256 0 $xml .= "<$key type='text'>".$item_parts->{"-$key"}."</$key>\n";
257 }
258  
259 } else {
260 0 ▶ if (ref $item_parts->{$key} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item_parts->{$key} eq 'ARRAY') { } 0 0 0
261 0 for my $item (@{$item_parts->{$key}}) {
262 0 $xml .= "<g:$key type='$gpart_types->{$key}'>$item</g:$key>\n";
263 }
264 } else {
265 0 $xml .= "<g:$key type='$gpart_types->{$key}'>$item_parts->{$key}</g:$key>\n";
266 }
267 }
268 }
269 0 $xml .= "</entry>\n";
270  
271 0 my $update_request = HTTP::Request->new( PUT => $item_id,
272 $self->client->ua->default_headers,
273 $xml);
274 0 my $response;
275 0 eval {
276 0 $response = $self->client->do($update_request);
277 };
278 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
279 0 my $error = $@;
280 0 die $error;
281 }
282  
283 0 my $atom = $response->content;
284
285 0 my $entry = XML::Atom::Entry->new(\$atom);
286  
287 0 return $entry;
288 }
289  
290 =head2 delete
291  
292 my $delete_response;
293 eval {
294 $delete_response =$gbase->delete($new_id);
295 };
296 if ($@) {
297 my $e = $@;
298 die $e->status_line; # HTTP::Response
299 }
300  
301 die "Successfully deleted if $delete_response->code == 200; # HTTP::Response
302  
303 =cut
304  
305 0 ▶sub delete {
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
306 0 my $self = shift;
307 0 my $item_id = shift;
308 0 my $delete_request = HTTP::Request->new( DELETE => $item_id,
309 $self->client->ua->default_headers );
310 0 my $response;
311 0 eval {
312 0 $response = $self->client->do($delete_request);
313 };
314 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
315 0 my $error = $@;
316 0 die $error;
317 }
318 0 return $response;
319 }
320  
321 =head2 select
322  
323 Currently only supports querying by id
324  
325 my $select_inserted_entry;
326 eval {
327 $select_inserted_entry =$gbase->select($new_id);
328 };
329 if ($@) {
330 my $e = $@;
331 die $e->status_line; # HTTP::Response
332 }
333  
334 =cut
335  
336 0 ▶sub select {
Subroutine select: not called CC=2 SCAR=17.9
Pod documented
337 0 my $self = shift;
338 0 my $item_id = shift;
339
340 0 my $select_request = HTTP::Request->new( GET => $item_id,
341 $self->client->ua->default_headers );
342 0 my $response;
343 0 eval {
344 0 $response = $self->client->do($select_request);
345 };
346 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
347 0 my $error = $@;
348 0 die $error;
349 }
350  
351 0 my $atom = $response->content;
352
353 0 my $entry = XML::Atom::Entry->new(\$atom);
354  
355 0 return $entry;
356 }
357  
358 1 1;
↑ Summary API/Account/ProgrammaticLogin.pm »