Mapper/Persona.pm

filter: Statementstmt 29.2% 26 / 89 Branchbran 11.1% 2 / 18 Conditioncond 8.0% 2 / 25 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 33.3% 9 / 27 Podpod 100% 18 / 18 totaltotal 29.8% 57 / 191 CC 24 SCAR 31.7
CC
24
Cov
23%
_attributes_to_persona
90.0
elsewhere
42.0
_do_array
12.0
CRAP 23.9
« Mapper/SiteMap.pm ↑ Summary Mapper/Persona/Flickr.pm »
1 package Net::Social::Mapper::Persona;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use JSON::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Net::Social::Mapper::Persona - an object representing an internet persona
9  
10 =head1 SYNOPSIS
11  
12 See C<Net::Social::Mapper>
13  
14 =head1 METHODS
15  
16 =cut
17  
18 =head2 new <user> <service> [opt[s]]
19  
20 Create a new persona.
21  
22 =cut
23 8 ▶sub new {
Subroutine new: called CC=4 SCAR=14.2
Pod documented
24 8 my $class = shift;
25 8 ▶ my $user = shift || return undef;
Condition50%1/2
shift() || (return undef)
l!l
80
MC/DC0%0/1
shift() || (return undef)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
26 8 ▶ my $service = shift || return undef;
Condition50%1/2
shift() || (return undef)
l!l
80
MC/DC0%0/1
shift() || (return undef)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
27 8 my %opts = @_;
28  
29 8 $opts{user} = $user;
30 8 $opts{service} = $service;
31 8 my $self = bless \%opts, $class;
32  
33 8 ▶ $self->_init || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_init or return 3 0 3
34 3 return $self;
35 }
36  
37 0 ▶sub _init { 1 }
Subroutine _init: not called CC=1 SCAR=6.9
38  
39 =head2 user
40  
41 The user name of this persona
42  
43 =cut
44 3 ▶sub user { shift->_do('user', @_) }
Subroutine user: called CC=1 SCAR=0.0
Pod documented
45  
46 =head2 service
47  
48 The service for this persona
49  
50 =cut
51 3 ▶sub service { shift->_do('service', @_) }
Subroutine service: called CC=1 SCAR=0.0
Pod documented
52  
53 =head2 domain
54  
55 The domain for this service
56  
57 =cut
58 3 ▶sub domain { shift->_do('domain', @_) }
Subroutine domain: called CC=1 SCAR=0.0
Pod documented
59  
60  
61 =head2 favicon
62  
63 The url to the favicon for this service
64  
65 =cut
66 0 ▶sub favicon {
Subroutine favicon: not called CC=2 SCAR=17.9
Pod documented
67 0 my $self = shift;
68 0 ▶ return $self->_do('favicon') || "http://".$self->domain."/favicon.ico";
Condition0%0/2
$self->_do('favicon') || 'http://' . $self->domain . '/favicon.ico'
l!l
00
MC/DC0%0/1
$self->_do('favicon') || 'http://' . $self->domain . '/favicon.ico'
$self->_do('favicon')
Truth table0%0/2
A
A= $self->_do('favicon')
Aresult
11
00
69 }
70  
71 =head2 name
72  
73 The canonical name of the service.
74  
75 =cut
76 0 ▶sub name { shift->_do('name', @_) }
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
77  
78 =head2 feeds
79  
80 Returns a hash of feeds (which might be empty)
81  
82 =cut
83 0 ▶sub feeds { shift->_do_array('feeds', @_) }
Subroutine feeds: not called CC=1 SCAR=6.9
Pod documented
84  
85 10 ▶sub _do {
Subroutine _do: called CC=2 SCAR=7.0
86 10 my $self = shift;
87 10 my $what = shift;
88 10 ▶ $self->{$what} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 10 10
89 10 return $self->{$what};
90 }
91  
92 0 ▶sub _do_array {
Subroutine _do_array: not called CC=3 SCAR=24.8
93 0 my $self = shift;
94 0 my $what = shift;
95 0 ▶ $self->{$what} = [@_] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
96 0 ▶ return @{$self->{$what}||[]};
Condition0%0/2
$self->{$what} || []
l!l
00
MC/DC0%0/1
$self->{$what} || []
$self->{$what}
Truth table0%0/2
A
A= $self->{$what}
Aresult
11
00
97 }
98  
99 0 ▶sub _do_array_with_defaults {
Subroutine _do_array_with_defaults: not called CC=2 SCAR=17.9
100 0 my $self = shift;
101 0 my $what = shift;
102 0 my $default = shift;
103 0 my @return = $self->_do_array($what, @_);
104 0 ▶ @return = ($default) unless @return;
Branch: 1 branch
BranchTrueFalseTotal
unless @return 0 0 0
105 0 return @return;
106 }
107  
108 =head2 types
109  
110 Return what type(s) feed objects are.
111  
112 Will almost certainly be one of - posts (default), notes, photos, videos
113  
114 =cut
115 0 ▶sub types { shift->_do_array_with_defaults('types', 'posts', @_) }
Subroutine types: not called CC=1 SCAR=6.9
Pod documented
116  
117 =head2 verbs
118  
119 Return what verb(s) feed objects are.
120  
121 Will almost certainly be one of - post (default), favorite
122  
123 =cut
124 0 ▶sub verbs { shift->_do_array_with_defaults('verbs', 'post', @_) }
Subroutine verbs: not called CC=1 SCAR=6.9
Pod documented
125  
126 =head2 persona_name
127  
128 A canonical short name for this persona. Generally C<user>@C<service>
129  
130 =cut
131 0 ▶sub persona_name {
Subroutine persona_name: not called CC=1 SCAR=6.9
Pod documented
132 0 my $self = shift;
133 0 return $self->user.'@'.$self->service;
134 }
135  
136 =head2 elsewhere
137  
138 Get other personas for this user
139  
140 =cut
141 0 ▶sub elsewhere {
Subroutine elsewhere: not called CC=6 SCAR=37.4
Pod documented
142 0 my $self = shift;
143 0 my $url = URI->new("http://socialgraph.apis.google.com/otherme");
144 0 $url->query_form( q => $self->_elsewhere_param );
145 0 ▶ my $page = $self->mapper->get("$url") || return ();
Condition0%0/2
$self->mapper->get("$url") || (return ())
l!l
00
MC/DC0%0/1
$self->mapper->get("$url") || (return ())
$self->mapper->get("$url")
Truth table0%0/2
A
A= $self->mapper->get("$url")
Aresult
11
00
146 0 ▶ my $info = eval { $self->_json->decode($page) } || return ();
Condition0%0/2
eval { do { $self->_json->decode($page) } } || (return ())
l!l
00
MC/DC0%0/1
eval { do { $self->_json->decode($page) } } || (return ())
eval { do { $self->_json->decode($page) } }
Truth table0%0/2
A
A= eval { do { $self->_json->decode($page) } }
Aresult
11
00
147 0 my @personas;
148 0 foreach my $url (keys %$info) {
149 0 my $attributes = $info->{$url}->{attributes};
150 0 ▶ next unless keys %$attributes;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$attributes 0 0 0
151 0 ▶ my $persona = $self->_attributes_to_persona($url, $attributes) || next;
Condition0%0/2
$self->_attributes_to_persona($url, $attributes) || (next)
l!l
00
MC/DC0%0/1
$self->_attributes_to_persona($url, $attributes) || (next)
$self->_attributes_to_persona($url, $attributes)
Truth table0%0/2
A
A= $self->_attributes_to_persona($url, $attributes)
Aresult
11
00
152 0 push @personas, $persona;
153 }
154 0 return @personas;
155 }
156  
157 =head2 mapper
158  
159 Return the C<Net::Social::Mapper> object for this persona.
160  
161 =cut
162 5 ▶sub mapper { shift->{_mapper} }
Subroutine mapper: called CC=1 SCAR=0.0
Pod documented
163  
164 =head1 METHODS WHICH MIGHT RETURN UNDEF
165  
166 =cut
167  
168 =head2 homepage
169  
170 The url of their homepage on this service
171  
172 =cut
173 0 ▶sub homepage { shift->_do('homepage', @_) }
Subroutine homepage: not called CC=1 SCAR=6.9
Pod documented
174  
175 =head2 profile
176  
177 The url of their profile on this service.
178  
179 =cut
180 0 ▶sub profile { shift->_do('profile', @_) }
Subroutine profile: not called CC=1 SCAR=6.9
Pod documented
181  
182 =head2 foaf
183  
184 The url of their foaf feed on this service.
185  
186 =cut
187 0 ▶sub foaf { shift->_do('foaf', @_) }
Subroutine foaf: not called CC=1 SCAR=6.9
Pod documented
188  
189 =head2 full_name
190  
191 Returns the full name of the persona if available
192  
193 =cut
194 0 ▶sub full_name { shift->_do('full_name', @_) }
Subroutine full_name: not called CC=1 SCAR=6.9
Pod documented
195  
196 =head2 id
197  
198 Returns the id of the persona on the service if applicable
199  
200 =cut
201 1 ▶sub id { shift->_do('id', @_) }
Subroutine id: called CC=1 SCAR=0.0
Pod documented
202  
203 =head2 photo
204  
205 Returns the profile picture of the person on the service if available
206  
207 =cut
208 0 ▶sub photo { shift->_do('photo', @_) }
Subroutine photo: not called CC=1 SCAR=6.9
Pod documented
209  
210 11 my %_attribute_map = (
211 fn => "fullname",
212 url => "homepage",
213 profile => "profile",
214 photo => "photo",
215 foaf => "foaf",
216 feed => "feed",
217 );
218  
219  
220 0 ▶sub _attributes_to_persona {
Subroutine _attributes_to_persona: not called CC=9 SCAR=45.0
221 0 my $self = shift;
222 0 my $url = shift;
223 0 my $attributes = shift;
224 0 my $mapper = $self->mapper;
225
226 # work out what persona this is
227 0 my ($user, $service) = $mapper->sitemap->url_to_service($url);
228 # and instantiate it
229 0 my $persona = $mapper->persona($user, $service);
230  
231 # collapse the atom and rss feeds down
232 0 foreach my $feed (qw(atom rss)) {
233 0 ▶ push @{$attributes->{feeds}}, delete $attributes->{$feed} if exists $attributes->{$feed};
Branch: 1 branch
BranchTrueFalseTotal
if exists $attributes->{$feed} 0 0 0
234 }
235  
236 # Now go through and add an additional data in
237 0 foreach my $key (keys %$attributes) {
238 # Skip what we're not interested in
239 0 ▶ my $name = $_attribute_map{$key} || next;
Condition0%0/2
$_attribute_map{$key} || (next)
l!l
00
MC/DC0%0/1
$_attribute_map{$key} || (next)
$_attribute_map{$key}
Truth table0%0/2
A
A= $_attribute_map{$key}
Aresult
11
00
240  
241 # If either is an array already then combine the values
242 0 ▶ if (ref $persona->{$name} eq 'ARRAY' || ref $attributes->{$key} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $persona->{$name} eq "ARRAY" or ref $attributes->{$key} eq "ARRAY") 0 0 0
Condition0%0/3
ref $persona->{$name} eq "ARRAY" or ref $attributes->{$key} eq "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $persona->{$name} eq "ARRAY" or ref $attributes->{$key} eq "ARRAY"
ref $persona->{$name} eq "ARRAY" ref $attributes->{$key} eq "ARRAY"
Truth table0%0/3
A or B
A= ref $persona->{$name} eq "ARRAY"B= ref $attributes->{$key} eq "ARRAY"
ABresult
1X1
011
000
243 0 ▶ my @to = ref($persona->{$name}) ? @{$persona->{$name}} : ($persona->{$name});
Branch: 1 branch
BranchTrueFalseTotal
ref $persona->{$name} ? : 0 0 0
244 0 ▶ my @from = ref($attributes->{$key}) ? @{$attributes->{$key}} : ($attributes->{$key});
Branch: 1 branch
BranchTrueFalseTotal
ref $attributes->{$key} ? : 0 0 0
245 0 my %tmp = map { $_ => 1 } (@to, @from);
246 0 $attributes->{$key} = [ keys %tmp ];
247 }
248
249 # Now merge the values. This assumes Google knows more than we do. Which may be wrong.
250 0 $persona->{$name} = $attributes->{$key};
251 }
252 0 return $persona;
253 }
254  
255 0 ▶sub _json {
Subroutine _json: not called CC=2 SCAR=17.9
256 0 my $self = shift;
257 0 ▶ return $self->{_json} ||= JSON::Any->new;
Condition0%0/3
$self->{"_json"} ||= "JSON::Any"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_json"} ||= "JSON::Any"->new
$self->{"_json"} "JSON::Any"->new
Truth table0%0/3
A ||= B
A= $self->{"_json"}B= "JSON::Any"->new
ABresult
1X1
011
000
258 }
259  
260 0 ▶sub _elsewhere_param {
Subroutine _elsewhere_param: not called CC=2 SCAR=17.9
261 0 my $self = shift;
262 0 ▶ return $self->homepage || $self->user;
Condition0%0/3
$self->homepage || $self->user
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->homepage || $self->user
$self->homepage $self->user
Truth table0%0/3
A || B
A= $self->homepageB= $self->user
ABresult
1X1
011
000
263 }
264  
265 11 1;
266  
« Mapper/SiteMap.pm ↑ Summary Mapper/Persona/Flickr.pm »