blib/lib/CGI/Untaint/Twitter.pm

filter: Statementstmt 46.1% 24 / 52 Branchbran 5.0% 1 / 20 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 77.7% 7 / 9 Podpod 100% 1 / 1 totaltotal 34.7% 33 / 95 SCAR 43.7
CC
14
Cov
31%
is_valid
72.0
_init
42.0
import
2.3
CRAP 78.8
↑ Summary
1 package CGI::Untaint::Twitter;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use base 'CGI::Untaint::object';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Net::Twitter::Lite::WithAPIv1_1;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 CGI::Untaint::Twitter - Validate a Twitter ID in a CGI script
13  
14 =head1 VERSION
15  
16 Version 0.03
17  
18 =cut
19  
20 3 our $VERSION = '0.03';
21 3 our $consumer_key;
22 3 our $consumer_secret;
23 3 our $access_token;
24 3 our $access_token_secret;
25  
26 =head1 SYNOPSIS
27  
28 CGI::Untaint::Twitter is a subclass of CGI::Untaint used to
29 validate if the given Twitter ID is valid.
30  
31 use CGI::Info;
32 use CGI::Untaint;
33 use CGI::Untaint::Twitter;
34 # ...
35 my $info = CGI::Info->new();
36 my $params = $info->params();
37 # ...
38 my $u = CGI::Untaint->new($params);
39 my $tid = $u->extract(-as_Twitter => 'twitter');
40 # $tid will be lower case
41  
42 =head1 SUBROUTINES/METHODS
43  
44 =head2 is_valid
45  
46 Validates the data.
47 Returns a boolean if $self->value is a valid twitter ID.
48  
49 =cut
50  
51 sub _untaint_re {
52 # Only allow letters and digits
53 # Remove the leading @ if any - leading spaces and so on will be
54 # ignored
55 1 ▶ return qr/\@?([a-zA-z0-9]+)/;
Subroutine _untaint_re: called CC=1 SCAR=0.0
56 }
57  
58 sub is_valid {
59 0 ▶ my $self = shift;
Subroutine is_valid: not called CC=8 SCAR=42.8
Pod documented
60  
61 0 my $value = $self->value;
62  
63 0 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
64 0 return 0;
65 }
66 0 ▶ unless($consumer_key && $consumer_secret && $access_token && $access_token_secret) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($CGI::Untaint::Twitter::consumer_key and $CGI::Untaint::Twitter::consumer_secret and $CGI::Untaint::Twitter::access_token and $CGI::Untaint::Twitter::access_token_secret) 0 0 0
Condition0%0/9
$CGI::Untaint::Twitter::consumer_key and $CGI::Untaint::Twitter::consumer_secret and $CGI::Untaint::Twitter::access_token and $CGI::Untaint::Twitter::access_token_secret
!ll&&!rl&&r
000
$CGI::Untaint::Twitter::consumer_key and $CGI::Untaint::Twitter::consumer_secret and $CGI::Untaint::Twitter::access_token
!ll&&!rl&&r
000
$CGI::Untaint::Twitter::consumer_key and $CGI::Untaint::Twitter::consumer_secret
!ll&&!rl&&r
000
MC/DC0%0/4
$CGI::Untaint::Twitter::consumer_key and $CGI::Untaint::Twitter::consumer_secret and $CGI::Untaint::Twitter::access_token and $CGI::Untaint::Twitter::access_token_secret
$CGI::Untaint::Twitter::consumer_key $CGI::Untaint::Twitter::consumer_secret $CGI::Untaint::Twitter::access_token $CGI::Untaint::Twitter::access_token_secret
Truth table0%0/5
A and B and C and D
A= $CGI::Untaint::Twitter::consumer_keyB= $CGI::Untaint::Twitter::consumer_secretC= $CGI::Untaint::Twitter::access_tokenD= $CGI::Untaint::Twitter::access_token_secret
ABCDresult
0XXX0
10XX0
110X0
11100
11111
67 0 carp 'Access tokens are required';
68 0 return 0;
69 }
70  
71 # Ignore leading and trailing spaces
72 0 $value =~ s/\s+$//;
73 0 $value =~ s/^\s+//;
74  
75 0 my $known_user = 0;
76  
77 0 eval {
78 0 my $nt = Net::Twitter::Lite::WithAPIv1_1->new(
79 consumer_key => $consumer_key,
80 consumer_secret => $consumer_secret,
81 legacy_lists_api => 0,
82 access_token => $access_token,
83 access_token_secret => $access_token_secret
84 );
85 0 ▶ if($nt->show_user({ screen_name => $value })) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nt->show_user({"screen_name", $value})) 0 0 0
86 0 $known_user = 1;
87 }
88 };
89 0 ▶ if($@ =~ /exceeded/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /exceeded/) 0 0 0
90 # Rate limit exceeded. Clients may not make more than 150 requests per hour.
91 # Fall back assume it would have worked so as not to
92 # incovenience the user
93 0 return 1;
94 }
95 0 return $known_user;
96 }
97  
98 =head2 init
99  
100 Set various options and override default values.
101  
102 use CGI::Info;
103 use CGI::Untaint;
104 use CGI::Untaint::Twitter {
105 access_token => 'xxxxxx', access_token_secret => 'yyyyy',
106 consumer_key => 'xyzzy', consumer_secret => 'plugh',
107 };
108  
109 =cut
110  
111 sub _init {
112 0 ▶ my %params = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;
Subroutine _init: not called CC=6 SCAR=37.4
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
113  
114 # Safe options - can be called at any time
115 0 ▶ if(defined($params{access_token})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"access_token"}) 0 0 0
116 0 $access_token = $params{access_token};
117 }
118 0 ▶ if(defined($params{access_token_secret})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"access_token_secret"}) 0 0 0
119 0 $access_token_secret = $params{access_token_secret};
120 }
121 0 ▶ if(defined($params{consumer_key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"consumer_key"}) 0 0 0
122 0 $consumer_key = $params{consumer_key};
123 }
124 0 ▶ if(defined($params{consumer_secret})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"consumer_secret"}) 0 0 0
125 0 $consumer_secret = $params{consumer_secret};
126 }
127 }
128  
129 sub import {
130 # my $class = shift;
131 1 ▶ shift;
Subroutine import: called CC=2 SCAR=8.1
132  
133 1 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 0 1
134  
135 0 _init(@_);
136 }
137  
138 =head1 AUTHOR
139  
140 Nigel Horne, C<< <njh at bandsman.co.uk> >>
141  
142 =head1 BUGS
143  
144 Twitter only allows 150 requests per hour. If you exceed that,
145 C<CGI::Untaint::Twitter> won't validate and will assume all ID's are valid.
146  
147 Please report any bugs or feature requests to C<bug-cgi-untaint-twitter at rt.cpan.org>, or through
148 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CGI-Untaint-Twitter>. I will be notified, and then you'll
149 automatically be notified of progress on your bug as I make changes.
150  
151  
152 =head1 SEE ALSO
153  
154 CGI::Untaint
155  
156  
157 =head1 SUPPORT
158  
159 You can find documentation for this module with the perldoc command.
160  
161 perldoc CGI::Untaint::Twitter
162  
163  
164 You can also look for information at:
165  
166 =over 4
167  
168 =item * RT: CPAN's request tracker
169  
170 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=CGI-Untaint-Twitter>
171  
172 =item * AnnoCPAN: Annotated CPAN documentation
173  
174 L<http://annocpan.org/dist/CGI-Untaint-Twitter>
175  
176 =item * CPAN Ratings
177  
178 L<http://cpanratings.perl.org/d/CGI-Untaint-Twitter>
179  
180 =item * Search CPAN
181  
182 L<http://search.cpan.org/dist/CGI-Untaint-Twitter>
183  
184 =back
185  
186  
187 =head1 ACKNOWLEDGEMENTS
188  
189  
190 =head1 LICENSE AND COPYRIGHT
191  
192 Copyright 2012,2013 Nigel Horne.
193  
194 This program is released under the following licence: GPL
195  
196  
197 =cut
198  
199 3 1; # End of CGI::Untaint::Twitter
↑ Summary