Mapper/SiteMap.pm

filter: Statementstmt 50.0% 30 / 60 Branchbran 7.1% 2 / 28 Conditioncond 15.3% 2 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 87.5% 7 / 8 Podpod 100% 3 / 3 totaltotal 36.6% 44 / 120 CC 23 SCAR 36.0
CC
23
Cov
34%
_visit
56.0
url_to_service
51.7
profile
32.1
CRAP 36.5
↑ Summary Mapper/Persona.pm »
1 package Net::Social::Mapper::SiteMap;
2  
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use JSON::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Net::Social::Mapper::SiteMap - information about know services around the internet
9  
10 =head1 SYNOPSIS
11  
12 my $sitemap = Net::Social::Mapper::SiteMap;
13 my $info = $sitemap->profile($user, $service);
14 my ($user, $service) = $sitemap->url_to_service($url);
15
16  
17 =head1 DESCRIPTION
18  
19 This is designed to be overriden and replaced with a more scalable
20 database if necessary.
21  
22 Alternatively the information could come from Google's C<sgnodemapper>
23  
24 http://code.google.com/p/google-sgnodemapper/
25  
26 =head1 METHODS
27  
28 =head2 new
29  
30 Instantiate a new sitemap.
31  
32 =cut
33 10 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
34 10 my $class = shift;
35 10 my %opts = @_;
36 10 my $self = bless \%opts, $class;
37 10 $self->_init;
38 10 return $self;
39 }
40  
41 10 ▶sub _init { }
Subroutine _init: called CC=1 SCAR=0.0
42  
43  
44 10 ▶sub _get_sitemap {
Subroutine _get_sitemap: called CC=3 SCAR=11.2
45 10 my $self = shift;
46 10 ▶ return $self->{_sitemap} if $self->{_sitemap};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_sitemap"} 0 10 10
47 10 my $fh = *DATA;
48 10 my $off = tell($fh);
49 10 my $json = JSON::Any->new;
50 10 my $data = do { local $/; <$fh> };
51 10 seek($fh, $off, 0);
52 10 ▶ return $self->{_sitemap} ||= $json->decode($data);
Condition33%1/3
$self->{"_sitemap"} ||= $json->decode($data)
l!l&&r!l&&!r
0010
MC/DC0%0/2
$self->{"_sitemap"} ||= $json->decode($data)
$self->{"_sitemap"} $json->decode($data)
Truth table33%1/3
A ||= B
A= $self->{"_sitemap"}B= $json->decode($data)
ABresult
1X1
011
000
53 }
54  
55 =head2 profile <service> [user]
56  
57 Returns a hash ref with all the known information about C<service>.
58  
59 If C<user> is passed in then any urls will be updated to include that information;
60  
61 Returns undef if the service isn't known.
62  
63 =cut
64 6 ▶sub profile {
Subroutine profile: called CC=7 SCAR=34.7
Pod documented
65 6 my $self = shift;
66 6 ▶ my $service = shift || return;
Condition50%1/2
shift() || (return)
l!l
60
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
67 6 my $user = shift;
68 6 my $site = $self->_get_sitemap;
69 0 my $val = $service;
70 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
ref $val ne "" or do { do { $val = $site->{$val}; return unless defined $val and not $val =~ /^\s*$/; $service = $val unless ref $val ne "" } } 0 0 0
71 0 $val = $site->{$val};
72 0 ▶ return unless defined $val && $val !~ m!^\s*$!;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and not $val =~ /^\s*$/ 0 0 0
Condition0%0/3
defined $val and not $val =~ /^\s*$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $val and not $val =~ /^\s*$/
defined $val not $val =~ /^\s*$/
Truth table0%0/3
A and not(B)
A= defined $valB= not $val =~ /^\s*$/
ABresult
0X0
100
111
73 0 ▶ $service = $val unless ref($val) ne "";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val ne "" 0 0 0
74 } until (ref($val) ne "");
75 0 ▶ $val = $self->_visit($val, $user) if defined $user;
Branch: 1 branch
BranchTrueFalseTotal
if defined $user 0 0 0
76 0 $val->{service} = $service;
77 0 return $val;
78 }
79  
80 0 ▶sub _visit {
Subroutine _visit: not called CC=7 SCAR=40.3
81 0 my $self = shift;
82 0 my $struct = shift;
83 0 my $user = shift;
84 0 my $ref = ref($struct);
85  
86 0 ▶ if (!defined $ref || $ref eq "") {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $ref or $ref eq '') { } 0 0 0
elsif ($ref eq 'SCALAR') { } 0 0 0
elsif ($ref eq 'HASH') { } 0 0 0
elsif ($ref eq 'ARRAY') { } 0 0 0
Condition0%0/3
not defined $ref or $ref eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $ref or $ref eq ""
not defined $ref $ref eq ""
Truth table0%0/3
not(A) or B
A= not defined $refB= $ref eq ""
ABresult
1X1
011
000
87 0 $struct =~ s!%user!$user!msg;
88 } elsif ($ref eq 'SCALAR') {
89 0 $$struct = $self->_visit($$struct, $user);
90 } elsif ($ref eq 'HASH') {
91 0 foreach my $key (keys %$struct) {
92 0 $struct->{$key} = $self->_visit($struct->{$key}, $user);
93 }
94 } elsif ($ref eq 'ARRAY') {
95 0 $struct = [ map { $self->_visit($_, $user) } @$struct ];
96 }
97 0 return $struct;
98 }
99  
100 =head2 url_to_service <url>
101  
102 Take a url and work out what username and service it is
103  
104 =cut
105 4 ▶sub url_to_service {
Subroutine url_to_service: called CC=9 SCAR=39.5
Pod documented
106 4 my $self = shift;
107 4 my $url = shift;
108 4 ▶ $url = "http://$url" unless $url =~ m![a-z]+:\/\/!i;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[[a-z]+:\/\/]i 0 4 4
109  
110 4 my $sitemap = $self->_get_sitemap;
111  
112 0 foreach my $service (keys %$sitemap) {
113 0 ▶ next unless ref($sitemap->{$service});
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sitemap->{$service} 0 0 0
114 0 ▶ my $regex = $sitemap->{$service}->{regex} || next;
Condition0%0/2
$sitemap->{$service}{'regex'} || (next)
l!l
00
MC/DC0%0/1
$sitemap->{$service}{'regex'} || (next)
$sitemap->{$service}{'regex'}
Truth table0%0/2
A
A= $sitemap->{$service}{'regex'}
Aresult
11
00
115 0 ▶ my @regexes = (ref($regex)) ? @$regex : ($regex);
Branch: 1 branch
BranchTrueFalseTotal
ref $regex ? : 0 0 0
116 0 foreach my $test (@regexes) {
117 0 $test =~ s!\?!\\?!g;
118 0 ▶ next unless $url =~ m!$test!;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ /$test/ 0 0 0
119 0 ▶ next unless defined $1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $1 0 0 0
120 0 return ($1, $service);
121 }
122 }
123  
124 # The default
125 0 return ($url, 'website');
126 }
127  
128 ###
129 # Needed
130 ##
131  
132 # Catser
133 # ColourLovers
134 # Corkd
135 # Dogster
136 # Gametap
137 # Goodreads
138 # Google
139 # Hi5
140 # Iconbuffet
141 # Icq
142 # Iminta
143 # IStockPhoto
144 # IUseThis
145 # IWatchThis
146 # Mog
147 # Multiply
148 # Netflix
149 # NetVibes
150 # NewsVine
151 # Ning
152 # Ohloh
153 # Opera
154 # p0pulist
155 # Skype
156 # Sonicliving
157 # Spin.de
158 # Stumbleupon
159 # Tabblo
160 # Technorati
161 # Threadless
162 # Uncrate
163 # Viddler
164 # Virb
165 # Wakoopa
166 # Wists
167 14 1;
168 __DATA__
↑ Summary Mapper/Persona.pm »