Mapper.pm

filter: Statementstmt 60.7% 51 / 84 Branchbran 40.9% 9 / 22 Conditioncond 30.4% 7 / 23 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 73.3% 11 / 15 Podpod 100% 5 / 5 totaltotal 52.4% 85 / 162 CC 23 SCAR 21.7
CC
23
Cov
52%
_get
20.0
_get_feeds
20.0
get
12.0
CRAP 8.8
« Mapper/Persona/Myspace.pm ↑ Summary Mapper/Persona/Generic.pm »
1 package Net::Social::Mapper;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use Email::Valid;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Encode qw(decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Net::Social::Mapper::SiteMap;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 13 our $VERSION = '0.5';
10  
11 =head1 NAME
12  
13 Net::Social::Mapper - utilities for dealing with internet persona
14  
15 =head1 SYNOPSIS
16  
17  
18 my $mapper = Net::Social::Mapper->new;
19 my $persona = $mapper->persona('daveman692', 'livejournal');
20  
21 print $persona->user; # daveman692
22 print $persona->service; # livejournal
23 print $persona->domain; # livejournal.com
24 print $persona->name; # LiveJournal
25  
26 # Print out any feeds available (if any exist)
27 print "Feeds : ".join(", ", @feeds);
28  
29 # What type are the feed items
30 print "Feeds contain : ".join(", ", $persona->types);
31  
32 # These other options may or may not be available
33 print "Home page : ".$persona->homepage;
34 print "Profile url : ".$persona->profile;
35 print "Full Name : ".$persona->full_name;
36 print "Service id : ".$persona->id;
37 print "Photo url : ".$persona->photo;
38 print "FOAF url : ".$persona->foaf;
39  
40 # If you have network access then you can query
41 # who they are elsewhere on the web
42 my @personas = $persona->elsewhere;
43  
44 # Other examples ...
45  
46 my $persona = $mapper->persona('daveman692', 'flickr');
47 print $persona->user; # daveman692
48 print $persona->id; # 36381329@N00
49  
50 my $persona = $mapper->persona('http://davidrecordon.com');
51 print $persona->user; # http://davidrecordon.com
52 print $persona->service; # website
53 print $persona->domain; # davidrecordon.com
54  
55 my $persona = $mapper->persona('test@example.com');
56 print $persona->user; # test@example.com
57 print $persona->service; # email
58 print $persona->id; # test
59 print $person->domain; # example.com
60  
61 # Moreover Net::Social::Mapper tries to work out the service from the url so that ...
62 my $persona = $mapper->persona('http://daveman692.livejournal.com');
63 my $persona = $mapper->persona('http://daveman692.livejournal.com/data/rss');
64 my $persona = $mapper->persona('http://daveman692.livejournal.com/data/atom');
65 my $persona = $mapper->persona('http://www.livejournal.com/userinfo.bml?user=daveman692');
66 # ... all return
67 print $persona->user; # daveman692
68 print $persona->service; # livejournal
69 print $persona->domain; # livejournal.com
70 print $persona->name; # LiveJournal
71  
72 =head1 METHODS
73  
74 =cut
75  
76 =head2 new
77  
78 Get a new C<Net::Social::Mapper> object.
79  
80 =cut
81 9 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
82 9 my $class = shift;
83 9 my %opts = @_;
84 9 my $self = bless \%opts, $class;
85 9 $self->_init;
86 9 return $self;
87 }
88  
89 9 ▶sub _init { }
Subroutine _init: called CC=1 SCAR=0.0
90  
91 =head2 persona <user> [service]
92  
93 Return a C<Net::Social::Mapper::Persona> object representing the user.
94  
95 Returns undef if it doesn't know anything about the service.
96  
97 =cut
98 8 ▶sub persona {
Subroutine persona: called CC=6 SCAR=18.9
Pod documented
99 8 my $self = shift;
100 8 my $user = shift;
101 8 my $service = shift;
102  
103 8 ▶ unless (defined $service) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $service) 5 3 8
104 5 ▶ if ($user =~ m!@! && $user !~ m!/!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user =~ /@/ and not $user =~ m[/]) 2 3 5
Condition66%2/3
$user =~ /@/ and not $user =~ m[/]
!ll&&!rl&&r
302
MC/DC50%1/2
$user =~ /@/ and not $user =~ m[/]
$user =~ /@/ not $user =~ m[/]
Truth table66%2/3
A and not(B)
A= $user =~ /@/B= not $user =~ m[/]
ABresult
0X0
100
111
105 2 my $original = $user;
106 2 my @pieces = split '@', $user;
107 2 $service = pop @pieces;
108 2 $user = join '@', @pieces;
109 2 my $persona = $self->_load_persona($user, $service);
110 0 ▶ return $persona if $persona;
Branch: 1 branch
BranchTrueFalseTotal
if $persona 0 0 0
111 0 $user = $original;
112 }
113 3 ▶ if (Email::Valid->address($user)) {
Branch: 1 branch
BranchTrueFalseTotal
if ('Email::Valid'->address($user)) { } 0 3 3
114 0 $service = 'email';
115 } else {
116 3 $service = 'website';
117 }
118 }
119 6 return $self->_load_persona($user, $service);
120 }
121  
122 8 ▶sub _load_persona {
Subroutine _load_persona: called CC=5 SCAR=16.2
123 8 my $self = shift;
124 8 ▶ my $user = shift || return;
Condition50%1/2
shift() || (return)
l!l
80
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
125 8 my $service = shift;
126 8 my %classmap = $self->classmap;
127  
128 8 ▶ ($user, $service) = $self->sitemap->url_to_service($user) if $service eq 'website';
Branch: 1 branch
BranchTrueFalseTotal
if $service eq "website" 3 5 8
129  
130 5 ▶ my $class = $classmap{lc($service)} || 'Net::Social::Mapper::Persona::Generic';
Condition100%2/2
$classmap{lc $service} || 'Net::Social::Mapper::Persona::Generic'
l!l
14
MC/DC100%1/1
$classmap{lc $service} || 'Net::Social::Mapper::Persona::Generic'
$classmap{lc $service}
Truth table100%2/2
A
A= $classmap{lc $service}
Aresult
11
00
131 5 eval "require $class";
132 5 ▶ return undef if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
133 5 return $class->new($user, lc($service), _mapper => $self);
134 }
135  
136 =head2 sitemap
137  
138 The C<Net::Social::Mapper::SiteMap> object containing everything we know about various services.
139  
140 =cut
141 9 ▶sub sitemap {
Subroutine sitemap: called CC=2 SCAR=8.1
Pod documented
142 9 my $self = shift;
143 9 ▶ return $self->{_sitemap} || Net::Social::Mapper::SiteMap->new;
Condition33%1/3
$self->{'_sitemap'} || 'Net::Social::Mapper::SiteMap'->new
l!l&&r!l&&!r
090
MC/DC0%0/2
$self->{'_sitemap'} || 'Net::Social::Mapper::SiteMap'->new
$self->{'_sitemap'} 'Net::Social::Mapper::SiteMap'->new
Truth table33%1/3
A || B
A= $self->{'_sitemap'}B= 'Net::Social::Mapper::SiteMap'->new
ABresult
1X1
011
000
144 }
145  
146 =head2 classmap [key value]
147  
148 Return a hash of (lowercase) service names to classes;
149  
150 Alternatively if you pass in a key, value pair then that
151 will be added to the map.
152  
153 Passing in C<undef> as the value will delete the key.
154  
155 =cut
156 8 ▶sub classmap {
Subroutine classmap: called CC=5 SCAR=23.1
Pod documented
157 8 my $self = shift;
158 $self->{_class_map} ||= {
159 8 ▶ email => 'Net::Social::Mapper::Persona::Email',
Condition50%1/2
$self->{"_class_map"} ||= {"email", "Net::Social::Mapper::Persona::Email", "website", "Net::Social::Mapper::Persona::Website", "flickr", "Net::Social::Mapper::Persona::Flickr", "myspace", "Net::Social::Mapper::Persona::Myspace"}
l!l
08
MC/DC0%0/1
$self->{"_class_map"} ||= {"email", "Net::Social::Mapper::Persona::Email", "website", "Net::Social::Mapper::Persona::Website", "flickr", "Net::Social::Mapper::Persona::Flickr", "myspace", "Net::Social::Mapper::Persona::Myspace"}
$self->{"_class_map"}
Truth table50%1/2
A
A= $self->{"_class_map"}
Aresult
11
00
160 website => 'Net::Social::Mapper::Persona::Website',
161 flickr => 'Net::Social::Mapper::Persona::Flickr',
162 myspace => 'Net::Social::Mapper::Persona::Myspace',
163 };
164 8 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 8 8
165 0 my %tmp = @_;
166 0 foreach my $key (keys %tmp) {
167 0 my $value = $tmp{$key};
168 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
169 0 $self->{_class_map}->{$key} = $value;
170 } else {
171 0 delete $self->{_class_map}->{$key};
172 }
173 }
174 }
175 8 return %{$self->{_class_map}};
176 }
177  
178 =head2 get <url>
179  
180 Get the contents of the url or undef on failure;
181  
182 =cut
183 0 ▶sub get {
Subroutine get: not called CC=3 SCAR=24.8
Pod documented
184 0 my $self = shift;
185 0 ▶ my $url = shift || return;
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
186 0 ▶ my $r = $self->_get($url) || return;
Condition0%0/2
$self->_get($url) || (return)
l!l
00
MC/DC0%0/1
$self->_get($url) || (return)
$self->_get($url)
Truth table0%0/2
A
A= $self->_get($url)
Aresult
11
00
187 0 return $r->decoded_content;
188 }
189  
190 0 ▶sub _get {
Subroutine _get: not called CC=4 SCAR=30.0
191 0 my $self = shift;
192 0 ▶ my $url = shift || return;
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
193 0 ▶ my $ua = $self->{_ua} ||= LWP::UserAgent->new(parse_head => 0);
Condition0%0/3
$self->{"_ua"} ||= "LWP::UserAgent"->new("parse_head", 0)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_ua"} ||= "LWP::UserAgent"->new("parse_head", 0)
$self->{"_ua"} "LWP::UserAgent"->new("parse_head", 0)
Truth table0%0/3
A ||= B
A= $self->{"_ua"}B= "LWP::UserAgent"->new("parse_head", 0)
ABresult
1X1
011
000
194 0 $self->{_ua}->env_proxy(1);
195 0 my $r = $self->{_ua}->get("$url");
196 0 ▶ return unless $r->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $r->is_success 0 0 0
197 0 return $r;
198 }
199  
200  
201 # a list of all content types that are feeds
202 0 ▶sub _feed_types {(
Subroutine _feed_types: not called CC=1 SCAR=6.9
203 0 "text/xml" => 1,
204 "application/xml" => 1,
205 "application/rdf+xml" => 1,
206 "application/rss+xml" => 1,
207 "application/atom+xml" => 1,
208 "application/x.atom+xml" => 1,
209 )};
210  
211 0 ▶sub _get_feeds {
Subroutine _get_feeds: not called CC=4 SCAR=30.0
212 0 my $self = shift;
213 0 my $url = shift;
214 0 my %types = $self->_feed_types;
215 0 my $r = $self->_get($url);
216 0 ▶ my $mime = $r->header('Content-Type') || "";
Condition0%0/2
$r->header('Content-Type') || ''
l!l
00
MC/DC0%0/1
$r->header('Content-Type') || ''
$r->header('Content-Type')
Truth table0%0/2
A
A= $r->header('Content-Type')
Aresult
11
00
217 0 $mime =~ s!;.*$!!;
218 0 ▶ return ($url) if $types{$mime};
Branch: 1 branch
BranchTrueFalseTotal
if $types{$mime} 0 0 0
219 0 my $page = $r->decoded_content;
220  
221 0 my $tmp = eval { decode_utf8($page) };
222 0 ▶ $tmp = $page unless defined $tmp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tmp 0 0 0
223  
224 0 return Feed::Find->find_in_html(\$tmp, $url);
225 }
226  
227 =head1 AUTHOR
228  
229 Simon Wistow <swistow@sixapart.com>
230  
231 =head1 COPYRIGHT
232  
233 Copyright 2008, Six Apart Ltd.
234  
235 Released under the same terms as Perl itself.
236  
237 =cut
238  
239  
240 13 1;
« Mapper/Persona/Myspace.pm ↑ Summary Mapper/Persona/Generic.pm »