File: | lib/WWW/Google/Contacts/Contact.pm |
Coverage: | 35.1% |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package WWW::Google::Contacts::Contact; | ||||||
2 | |||||||
3 | 11 11 11 | 82 36 96 | use Moose; | ||||
4 | 11 11 11 | 189 47 183 | use MooseX::Types::Moose qw( Str ); | ||||
5 | 11 | 240 | use WWW::Google::Contacts::Types qw( | ||||
6 | Category | ||||||
7 | Name | ||||||
8 | PhoneNumber ArrayRefOfPhoneNumber | ||||||
9 | Email ArrayRefOfEmail | ||||||
10 | IM ArrayRefOfIM | ||||||
11 | Organization ArrayRefOfOrganization | ||||||
12 | PostalAddress ArrayRefOfPostalAddress | ||||||
13 | CalendarLink ArrayRefOfCalendarLink | ||||||
14 | Birthday | ||||||
15 | ContactEvent ArrayRefOfContactEvent | ||||||
16 | ExternalId ArrayRefOfExternalId | ||||||
17 | Gender | ||||||
18 | GroupMembership ArrayRefOfGroupMembership | ||||||
19 | Hobby ArrayRefOfHobby | ||||||
20 | Jot ArrayRefOfJot | ||||||
21 | Language ArrayRefOfLanguage | ||||||
22 | Priority | ||||||
23 | Sensitivity | ||||||
24 | Relation ArrayRefOfRelation | ||||||
25 | UserDefined ArrayRefOfUserDefined | ||||||
26 | Website ArrayRefOfWebsite | ||||||
27 | Photo | ||||||
28 | 11 11 | 202 30 | ); | ||||
29 | 11 11 11 | 153 39 96 | use WWW::Google::Contacts::Meta::Attribute::Trait::XmlField; | ||||
30 | |||||||
31 | 0 | 0 | 0 | sub create_url { 'http://www.google.com/m8/feeds/contacts/default/full' } | |||
32 | |||||||
33 | extends 'WWW::Google::Contacts::Base'; | ||||||
34 | |||||||
35 | with 'WWW::Google::Contacts::Roles::CRUD'; | ||||||
36 | |||||||
37 | has id => ( | ||||||
38 | isa => Str, | ||||||
39 | is => 'ro', | ||||||
40 | writer => '_set_id', | ||||||
41 | predicate => 'has_id', | ||||||
42 | traits => [ 'XmlField' ], | ||||||
43 | xml_key => 'id', | ||||||
44 | ); | ||||||
45 | |||||||
46 | has etag => ( | ||||||
47 | isa => Str, | ||||||
48 | is => 'ro', | ||||||
49 | writer => '_set_etag', | ||||||
50 | predicate => 'has_etag', | ||||||
51 | traits => [ 'XmlField' ], | ||||||
52 | xml_key => 'gd:etag', | ||||||
53 | include_in_xml => 0, # This is set in HTTP headers | ||||||
54 | ); | ||||||
55 | |||||||
56 | has link => ( | ||||||
57 | is => 'rw', | ||||||
58 | trigger => \&_set_link, | ||||||
59 | traits => [ 'XmlField' ], | ||||||
60 | xml_key => 'link', | ||||||
61 | include_in_xml => 0, | ||||||
62 | ); | ||||||
63 | |||||||
64 | # What to do with different link types | ||||||
65 | my $link_map = { | ||||||
66 | 'self' | ||||||
67 | => sub { my ($self,$link) = @_; $self->_set_id( $link->{ href } ) }, | ||||||
68 | 'http://schemas.google.com/contacts/2008/rel#photo' | ||||||
69 | => sub { my ($self,$link) = @_; $self->photo( { %$link, server => $self->server } ) }, | ||||||
70 | }; | ||||||
71 | |||||||
72 | sub _set_link { | ||||||
73 | 0 | 0 | my ($self, $links) = @_; | ||||
74 | 0 0 | 0 0 | foreach my $link ( @{ $links } ) { | ||||
75 | 0 | 0 | next unless ( defined $link_map->{ $link->{ rel } } ); | ||||
76 | 0 | 0 | my $code = $link_map->{ $link->{ rel } }; | ||||
77 | 0 | 0 | $self->$code( $link ); | ||||
78 | } | ||||||
79 | } | ||||||
80 | |||||||
81 | has photo => ( | ||||||
82 | isa => Photo, | ||||||
83 | is => 'rw', | ||||||
84 | coerce => 1, | ||||||
85 | ); | ||||||
86 | |||||||
87 | has category => ( | ||||||
88 | isa => Category, | ||||||
89 | is => 'rw', | ||||||
90 | predicate => 'has_category', | ||||||
91 | traits => [ 'XmlField' ], | ||||||
92 | xml_key => 'category', | ||||||
93 | default => sub { undef }, | ||||||
94 | coerce => 1, | ||||||
95 | ); | ||||||
96 | |||||||
97 | has notes => ( | ||||||
98 | isa => Str, | ||||||
99 | is => 'rw', | ||||||
100 | predicate => 'has_notes', | ||||||
101 | traits => [ 'XmlField' ], | ||||||
102 | xml_key => 'content', | ||||||
103 | is_element => 1, | ||||||
104 | ); | ||||||
105 | |||||||
106 | has name => ( | ||||||
107 | isa => Name, | ||||||
108 | is => 'rw', | ||||||
109 | predicate => 'has_name', | ||||||
110 | traits => [ 'XmlField' ], | ||||||
111 | xml_key => 'gd:name', | ||||||
112 | handles => [qw( given_name additional_name family_name | ||||||
113 | name_prefix name_suffix full_name )], | ||||||
114 | default => sub { undef }, # empty Name object, so handles will work | ||||||
115 | coerce => 1, | ||||||
116 | ); | ||||||
117 | |||||||
118 | has phone_number => ( | ||||||
119 | isa => ArrayRefOfPhoneNumber, | ||||||
120 | is => 'rw', | ||||||
121 | predicate => 'has_phone_number', | ||||||
122 | traits => [ 'XmlField' ], | ||||||
123 | xml_key => 'gd:phoneNumber', | ||||||
124 | coerce => 1, | ||||||
125 | ); | ||||||
126 | |||||||
127 | has email => ( | ||||||
128 | isa => ArrayRefOfEmail, | ||||||
129 | is => 'rw', | ||||||
130 | predicate => 'has_email', | ||||||
131 | traits => [ 'XmlField' ], | ||||||
132 | xml_key => 'gd:email', | ||||||
133 | coerce => 1, | ||||||
134 | ); | ||||||
135 | |||||||
136 | has im => ( | ||||||
137 | isa => ArrayRefOfIM, | ||||||
138 | is => 'rw', | ||||||
139 | predicate => 'has_im', | ||||||
140 | traits => [ 'XmlField' ], | ||||||
141 | xml_key => 'gd:im', | ||||||
142 | coerce => 1, | ||||||
143 | ); | ||||||
144 | |||||||
145 | has organization => ( | ||||||
146 | isa => ArrayRefOfOrganization, | ||||||
147 | is => 'rw', | ||||||
148 | predicate => 'has_organization', | ||||||
149 | traits => [ 'XmlField' ], | ||||||
150 | xml_key => 'gd:organization', | ||||||
151 | coerce => 1, | ||||||
152 | ); | ||||||
153 | |||||||
154 | has postal_address => ( | ||||||
155 | isa => ArrayRefOfPostalAddress, | ||||||
156 | is => 'rw', | ||||||
157 | predicate => 'has_postal_address', | ||||||
158 | traits => [ 'XmlField' ], | ||||||
159 | xml_key => 'gd:structuredPostalAddress', | ||||||
160 | coerce => 1, | ||||||
161 | ); | ||||||
162 | |||||||
163 | has billing_information => ( | ||||||
164 | isa => Str, | ||||||
165 | is => 'rw', | ||||||
166 | predicate => 'has_billing_information', | ||||||
167 | traits => [ 'XmlField' ], | ||||||
168 | xml_key => 'gContact:billingInformation', | ||||||
169 | is_element => 1, | ||||||
170 | ); | ||||||
171 | |||||||
172 | has birthday => ( | ||||||
173 | isa => Birthday, | ||||||
174 | is => 'rw', | ||||||
175 | predicate => 'has_birthday', | ||||||
176 | traits => [ 'XmlField' ], | ||||||
177 | xml_key => 'gContact:birthday', | ||||||
178 | is_element => 1, | ||||||
179 | coerce => 1, | ||||||
180 | ); | ||||||
181 | |||||||
182 | has calendar_link => ( | ||||||
183 | isa => ArrayRefOfCalendarLink, | ||||||
184 | is => 'rw', | ||||||
185 | predicate => 'has_calendar_link', | ||||||
186 | traits => [ 'XmlField' ], | ||||||
187 | xml_key => 'gContact:calendarLink', | ||||||
188 | coerce => 1, | ||||||
189 | ); | ||||||
190 | |||||||
191 | has directory_server => ( | ||||||
192 | isa => Str, | ||||||
193 | is => 'rw', | ||||||
194 | predicate => 'has_directory_server', | ||||||
195 | traits => [ 'XmlField' ], | ||||||
196 | xml_key => 'gContact:directoryServer', | ||||||
197 | is_element => 1, | ||||||
198 | ); | ||||||
199 | |||||||
200 | has event => ( | ||||||
201 | isa => ArrayRefOfContactEvent, | ||||||
202 | is => 'rw', | ||||||
203 | predicate => 'has_event', | ||||||
204 | traits => [ 'XmlField' ], | ||||||
205 | xml_key => 'gContact:event', | ||||||
206 | coerce => 1, | ||||||
207 | ); | ||||||
208 | |||||||
209 | has external_id => ( | ||||||
210 | isa => ArrayRefOfExternalId, | ||||||
211 | is => 'rw', | ||||||
212 | predicate => 'has_external_id', | ||||||
213 | traits => [ 'XmlField' ], | ||||||
214 | xml_key => 'gContact:excternalId', | ||||||
215 | coerce => 1, | ||||||
216 | ); | ||||||
217 | |||||||
218 | has gender => ( | ||||||
219 | isa => Gender, | ||||||
220 | is => 'rw', | ||||||
221 | predicate => 'has_gender', | ||||||
222 | traits => [ 'XmlField' ], | ||||||
223 | xml_key => 'gContact:gender', | ||||||
224 | coerce => 1, | ||||||
225 | ); | ||||||
226 | |||||||
227 | has group_membership => ( | ||||||
228 | isa => ArrayRefOfGroupMembership, | ||||||
229 | is => 'rw', | ||||||
230 | predicate => 'has_group_membership', | ||||||
231 | traits => [ 'XmlField' ], | ||||||
232 | xml_key => 'gContact:groupMembershipInfo', | ||||||
233 | coerce => 1, | ||||||
234 | ); | ||||||
235 | |||||||
236 | has hobby => ( | ||||||
237 | isa => ArrayRefOfHobby, | ||||||
238 | is => 'rw', | ||||||
239 | predicate => 'has_hobby', | ||||||
240 | traits => [ 'XmlField' ], | ||||||
241 | xml_key => 'gContact:hobby', | ||||||
242 | coerce => 1, | ||||||
243 | ); | ||||||
244 | |||||||
245 | has initials => ( | ||||||
246 | isa => Str, | ||||||
247 | is => 'rw', | ||||||
248 | predicate => 'has_initials', | ||||||
249 | traits => [ 'XmlField' ], | ||||||
250 | xml_key => 'gContact:initials', | ||||||
251 | is_element => 1, | ||||||
252 | ); | ||||||
253 | |||||||
254 | has jot => ( | ||||||
255 | isa => ArrayRefOfJot, | ||||||
256 | is => 'rw', | ||||||
257 | predicate => 'has_jot', | ||||||
258 | traits => [ 'XmlField' ], | ||||||
259 | xml_key => 'gContact:jot', | ||||||
260 | coerce => 1, | ||||||
261 | ); | ||||||
262 | |||||||
263 | has language => ( | ||||||
264 | isa => ArrayRefOfLanguage, | ||||||
265 | is => 'rw', | ||||||
266 | predicate => 'has_language', | ||||||
267 | traits => [ 'XmlField' ], | ||||||
268 | xml_key => 'gContact:language', | ||||||
269 | coerce => 1, | ||||||
270 | ); | ||||||
271 | |||||||
272 | has maiden_name => ( | ||||||
273 | isa => Str, | ||||||
274 | is => 'rw', | ||||||
275 | predicate => 'has_maiden_name', | ||||||
276 | traits => [ 'XmlField' ], | ||||||
277 | xml_key => 'gContact:maidenName', | ||||||
278 | is_element => 1, | ||||||
279 | ); | ||||||
280 | |||||||
281 | has mileage => ( | ||||||
282 | isa => Str, | ||||||
283 | is => 'rw', | ||||||
284 | predicate => 'has_mileage', | ||||||
285 | traits => [ 'XmlField' ], | ||||||
286 | xml_key => 'gContact:mileage', | ||||||
287 | is_element => 1, | ||||||
288 | ); | ||||||
289 | |||||||
290 | has nickname => ( | ||||||
291 | isa => Str, | ||||||
292 | is => 'rw', | ||||||
293 | predicate => 'has_nickname', | ||||||
294 | traits => [ 'XmlField' ], | ||||||
295 | xml_key => 'gContact:nickname', | ||||||
296 | is_element => 1, | ||||||
297 | ); | ||||||
298 | |||||||
299 | has occupation => ( | ||||||
300 | isa => Str, | ||||||
301 | is => 'rw', | ||||||
302 | predicate => 'has_occupation', | ||||||
303 | traits => [ 'XmlField' ], | ||||||
304 | xml_key => 'gContact:occupation', | ||||||
305 | is_element => 1, | ||||||
306 | ); | ||||||
307 | |||||||
308 | has priority => ( | ||||||
309 | isa => Priority, | ||||||
310 | is => 'rw', | ||||||
311 | predicate => 'has_priority', | ||||||
312 | traits => [ 'XmlField' ], | ||||||
313 | xml_key => 'gContact:priority', | ||||||
314 | coerce => 1, | ||||||
315 | ); | ||||||
316 | |||||||
317 | has relation => ( | ||||||
318 | isa => ArrayRefOfRelation, | ||||||
319 | is => 'rw', | ||||||
320 | predicate => 'has_relation', | ||||||
321 | traits => [ 'XmlField' ], | ||||||
322 | xml_key => 'gContact:relation', | ||||||
323 | coerce => 1, | ||||||
324 | ); | ||||||
325 | |||||||
326 | has sensitivity => ( | ||||||
327 | isa => Sensitivity, | ||||||
328 | is => 'rw', | ||||||
329 | predicate => 'has_sensitivity', | ||||||
330 | traits => [ 'XmlField' ], | ||||||
331 | xml_key => 'gContact:sensitivity', | ||||||
332 | is_element => 1, | ||||||
333 | ); | ||||||
334 | |||||||
335 | has shortname => ( | ||||||
336 | isa => Str, | ||||||
337 | is => 'rw', | ||||||
338 | predicate => 'has_shortname', | ||||||
339 | traits => [ 'XmlField' ], | ||||||
340 | xml_key => 'gContact:shortname', | ||||||
341 | is_element => 1, | ||||||
342 | ); | ||||||
343 | |||||||
344 | has subject => ( | ||||||
345 | isa => Str, | ||||||
346 | is => 'rw', | ||||||
347 | predicate => 'has_subject', | ||||||
348 | traits => [ 'XmlField' ], | ||||||
349 | xml_key => 'gContact:subject', | ||||||
350 | is_element => 1, | ||||||
351 | ); | ||||||
352 | |||||||
353 | has user_defined => ( | ||||||
354 | isa => ArrayRefOfUserDefined, | ||||||
355 | is => 'rw', | ||||||
356 | predicate => 'has_user_defined', | ||||||
357 | traits => [ 'XmlField' ], | ||||||
358 | xml_key => 'gContact:userDefinedField', | ||||||
359 | coerce => 1, | ||||||
360 | ); | ||||||
361 | |||||||
362 | has website => ( | ||||||
363 | isa => ArrayRefOfWebsite, | ||||||
364 | is => 'rw', | ||||||
365 | predicate => 'has_website', | ||||||
366 | traits => [ 'XmlField' ], | ||||||
367 | xml_key => 'gContact:website', | ||||||
368 | coerce => 1, | ||||||
369 | ); | ||||||
370 | |||||||
371 | # Stolen from Meta/Attribute/Native/MethodProvider/Array.pm, need coercion | ||||||
372 | sub add_phone_number { | ||||||
373 | 0 | 1 | 0 | my ($self,$phone) = @_; | |||
374 | 0 0 | 0 0 | push @{ $self->phone_number }, to_PhoneNumber( $phone ); | ||||
375 | } | ||||||
376 | sub add_email { | ||||||
377 | 1 | 1 | 6 | my ($self,$email) = @_; | |||
378 | 1 1 | 4 8 | push @{ $self->email }, to_Email( $email ); | ||||
379 | } | ||||||
380 | sub add_user_defined { | ||||||
381 | 0 | 0 | my ($self,$user_def) = @_; | ||||
382 | 0 0 | push @{ $self->user_defined }, to_UserDefined( $user_def ); | |||||
383 | } | ||||||
384 | sub add_group_membership { | ||||||
385 | 0 | 0 | my ($self,$group) = @_; | ||||
386 | 0 0 | push @{ $self->group_membership }, to_GroupMembership( $group ); | |||||
387 | } | ||||||
388 | sub add_event { | ||||||
389 | 0 | 0 | my ($self,$event) = @_; | ||||
390 | 0 | unless ( $self->has_event ) { | |||||
391 | 0 | $self->event([]); | |||||
392 | } | ||||||
393 | 0 0 | push @{ $self->event }, to_ContactEvent( $event ); | |||||
394 | } | ||||||
395 | |||||||
396 | sub groups { | ||||||
397 | 0 | 0 | my $self = shift; | ||||
398 | |||||||
399 | 0 | my $to_ret = []; | |||||
400 | 0 | my $membership = $self->group_membership; | |||||
401 | 0 0 | foreach my $member (@{ $membership }) { | |||||
402 | 0 0 | push @{ $to_ret }, WWW::Google::Contacts::Group->new( id => $member->href,server => $self->server )->retrieve; | |||||
403 | } | ||||||
404 | 0 0 | return wantarray ? @{ $to_ret } : $to_ret; | |||||
405 | } | ||||||
406 | |||||||
407 | 11 11 11 | 120 44 99 | no Moose; | ||||
408 | __PACKAGE__->meta->make_immutable; | ||||||
409 | 1; |