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 CC 14 SCAR 36.4
CC
14
Cov
31%
is_valid
72.0
_init
42.0
import
2.3
CRAP 38.1
↑ 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.05
17  
18 =cut
19  
20 3 our $VERSION = '0.05';
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 1 ▶sub _untaint_re {
Subroutine _untaint_re: called CC=1 SCAR=0.0
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]+)$/;
56 }
57  
58 0 ▶sub is_valid {
Subroutine is_valid: not called CC=8 SCAR=42.8
Pod documented
59 0 my $self = shift;
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 ssl => 1,
85 );
86 0 ▶ if($nt->show_user({ screen_name => $value })) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nt->show_user({"screen_name", $value})) 0 0 0
87 0 $known_user = 1;
88 }
89 };
90 0 ▶ if($@ =~ /exceeded/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /exceeded/) 0 0 0
91 # Rate limit exceeded. Clients may not make more than 150 requests per hour.
92 # Fall back assume it would have worked so as not to
93 # incovenience the user
94 0 return 1;
95 }
96 0 return $known_user;
97 }
98  
99 =head2 init
100  
101 Set various options and override default values.
102  
103 use CGI::Info;
104 use CGI::Untaint;
105 use CGI::Untaint::Twitter {
106 access_token => 'xxxxxx', access_token_secret => 'yyyyy',
107 consumer_key => 'xyzzy', consumer_secret => 'plugh',
108 };
109  
110 =cut
111  
112 0 ▶sub _init {
Subroutine _init: not called CC=6 SCAR=37.4
113 0 ▶ my %params = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
114  
115 # Safe options - can be called at any time
116 0 ▶ if(defined($params{access_token})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"access_token"}) 0 0 0
117 0 $access_token = $params{access_token};
118 }
119 0 ▶ if(defined($params{access_token_secret})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"access_token_secret"}) 0 0 0
120 0 $access_token_secret = $params{access_token_secret};
121 }
122 0 ▶ if(defined($params{consumer_key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"consumer_key"}) 0 0 0
123 0 $consumer_key = $params{consumer_key};
124 }
125 0 ▶ if(defined($params{consumer_secret})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"consumer_secret"}) 0 0 0
126 0 $consumer_secret = $params{consumer_secret};
127 }
128 }
129  
130 1 ▶sub import {
Subroutine import: called CC=2 SCAR=8.1
131 # my $class = shift;
132 1 shift;
133  
134 1 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 0 1
135  
136 0 _init(@_);
137 }
138  
139 =head1 AUTHOR
140  
141 Nigel Horne, C<< <njh at bandsman.co.uk> >>
142  
143 =head1 BUGS
144  
145 Twitter only allows 150 requests per hour. If you exceed that,
146 C<CGI::Untaint::Twitter> won't validate and will assume all ID's are valid.
147  
148 Please report any bugs or feature requests to C<bug-cgi-untaint-twitter at rt.cpan.org>, or through
149 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CGI-Untaint-Twitter>. I will be notified, and then you'll
150 automatically be notified of progress on your bug as I make changes.
151  
152  
153 =head1 SEE ALSO
154  
155 CGI::Untaint
156  
157  
158 =head1 SUPPORT
159  
160 You can find documentation for this module with the perldoc command.
161  
162 perldoc CGI::Untaint::Twitter
163  
164  
165 You can also look for information at:
166  
167 =over 4
168  
169 =item * RT: CPAN's request tracker
170  
171 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=CGI-Untaint-Twitter>
172  
173 =item * CPAN Ratings
174  
175 L<http://cpanratings.perl.org/d/CGI-Untaint-Twitter>
176  
177 =item * Search CPAN
178  
179 L<http://search.cpan.org/dist/CGI-Untaint-Twitter>
180  
181 =back
182  
183  
184 =head1 ACKNOWLEDGEMENTS
185  
186  
187 =head1 LICENSE AND COPYRIGHT
188  
189 Copyright 2012-2019 Nigel Horne.
190  
191 This program is released under the following licence: GPL
192  
193  
194 =cut
195  
196 3 1; # End of CGI::Untaint::Twitter
↑ Summary