Rafb.pm

filter: Statementstmt 40.8% 20 / 49 Branchbran 37.5% 3 / 8 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 66.6% 4 / 6 Podpod 0.0% 0 / 2 totaltotal 38.6% 29 / 75 CC 8 SCAR 27.2
CC
8
Cov
40%
new
23.9
_agent
2.0
paste
2.0
CRAP 15.2
↑ Summary code.pl »
1 package WWW::Rafb;
2  
3 =pod
4  
5 =head1 NAME
6  
7 WWW::Rafb - Perl interface for rafb pasting site ( rafb.net/paste )
8  
9 =head1 SYNOPSIS
10
11 # create object with the paste information
12 my $paste = WWW::Rafb->new( 'language' => 'perl',
13 'nickname' => 'Di42lo',
14 'description' => 'my first script in perl',
15 'tabs' => 'No',
16 'file' => "~/first.pl");
17
18 # do the http request ( the paste itself )
19 $paste->paste();
20  
21 # print the url
22 print "You can get $paste->{file} at url: $paste->{URL}\n";
23  
24 =head1 DESCRIPTION
25  
26 "WWW::Rafb" provides object interface for pasting any text file/source file
27 into the the rafb site with the needed information.
28  
29 This module requires B<LWP::UserAgent> and B<use URI::Escape>.
30  
31 =head1 INTERFACE
32  
33 =cut
34  
35 #use strict;
36  
37 1 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 $VERSION = "0.02";
39  
40 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 0 ▶sub _agent {
Subroutine _agent: not called CC=1 SCAR=6.9
44 0 my $self = shift;
45  
46 # creates LWP::UserAgent object
47 0 my $agent = LWP::UserAgent->new;
48 0 $agent->timeout(10);
49
50 0 $agent->agent('Mozilla/5.0'); # fake agent
51 0 $agent->default_header('Keep-Alive' => 300);
52 0 $agent->default_header('Connection' => 'keep-alive');
53  
54 0 $self->{AGENT} = $agent;
55  
56 0 return $self;
57 }
58  
59 =pod
60  
61 new( [ARGS] )
62  
63 Used to set the pasting information about the file and create a new
64 Object of the WWW::Rafb module.
65  
66 Arguments:
67  
68 language - The file type. One of this types:
69 note: you should write the value (in the () ) and not the full name.
70  
71 C89 (c89)
72 C99 (c)
73 C++ (c++)
74 C# (c#)
75 Java (java)
76 Pascal (pascal)
77 Perl (perl)
78 PHP (php)
79 PL/I (pl/i)
80 Python (python)
81 Ruby (ruby)
82 SQL (sql)
83 Visual Basic (vb)
84 Plain Text (plain text)
85
86 nickname - the nickname of the script/file publisher
87 description' => 'my first script in perl',
88 tabs - values: No/2/4/6/8
89 file - path of the file we want to upload.
90  
91 NOTES: Returns a blessed object with the information.
92  
93 =cut
94  
95 1 my %types = (
96 "C89" => "c89",
97 "C99" => "c",
98 "C++" => "c++",
99 "C#" => "c#",
100 "Java" => "java",
101 "Pascal" => "pascal",
102 "Perl" => "perl",
103 "PHP" => "php",
104 "PL/I" => "pl/i",
105 "Python" => "python",
106 "Ruby" => "ruby)",
107 "SQL" => "sql",
108 "Visual Basic" => "vb",
109 "Plain Text" => "plain text"
110 );
111
112 1 ▶sub new {
Subroutine new: called CC=8 SCAR=31.7
Pod undocumented
113 1 my $class = shift;
114 1 my %args = @_;
115 1 my $self;
116  
117 # inserts args into class
118 1 foreach (keys %args) {
119 5 $self->{uc($_)} = $args{$_};
120 }
121  
122 # check args
123 1 ▶ $self->{LANGUAGE} = "C++" unless ($self->{LANGUAGE} && $types{"$self->{LANGUAGE}"});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"LANGUAGE"} and $types{qq[$self->{"LANGUAGE"}]} 1 0 1
Condition33%1/3
$self->{"LANGUAGE"} and $types{qq[$self->{"LANGUAGE"}]}
!ll&&!rl&&r
010
MC/DC0%0/2
$self->{"LANGUAGE"} and $types{qq[$self->{"LANGUAGE"}]}
$self->{"LANGUAGE"} $types{qq[$self->{"LANGUAGE"}]}
Truth table33%1/3
A and B
A= $self->{"LANGUAGE"}B= $types{qq[$self->{"LANGUAGE"}]}
ABresult
0X0
100
111
124
125 1 ▶ $self->{TABS} = "No" if ($self->{TABS} < 2 || $self->{TABS} > 8);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"TABS"} < 2 or $self->{"TABS"} > 8 1 0 1
Condition33%1/3
$self->{"TABS"} < 2 or $self->{"TABS"} > 8
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->{"TABS"} < 2 or $self->{"TABS"} > 8
$self->{"TABS"} < 2 $self->{"TABS"} > 8
Truth table33%1/3
A or B
A= $self->{"TABS"} < 2B= $self->{"TABS"} > 8
ABresult
1X1
011
000
126
127 1 ▶ open(FH, "< $self->{FILE}") || die ("Cant open file for pasting!\n");
Branch: 1 branch
BranchTrueFalseTotal
open FH, qq[< $self->{"FILE"}] or die "Cant open file for pasting!\n" 0 1 1
128
129 # escape our form values
130 0 ▶ $self->{SOURCE} .= $_ while (<FH>);
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline FH) and do { $self->{"SOURCE"} .= $_ } 0 0 0
131 0 close (FH);
132  
133 0 $self->{SOURCE} =~ s/ /+/g;
134 0 $self->{SOURCE} = URI::Escape::uri_escape_utf8($self->{SOURCE});
135 0 $self->{SOURCE} =~ s/%2B/+/g;
136
137 0 $self->{NICKNAME} =~ s/ /+/g;
138 0 $self->{NICKNAME} = URI::Escape::uri_escape_utf8($self->{NICKNAME});
139 0 $self->{NICKNAME} =~ s/%2B/+/g;
140
141 0 $self->{DESCRIPTION} =~ s/ /+/g;
142 0 $self->{DESCRIPTION} = URI::Escape::uri_escape_utf8($self->{DESCRIPTION});
143 0 $self->{DESCRIPTION} =~ s/%2B/+/g;
144
145 0 $self->{SITE} = "http://www.rafb.net/paste";
146 # return the object
147 0 return bless $self, $class;
148
149 }
150  
151 =pod
152  
153 paste()
154  
155 Returns the object with the URL of the file in the
156 rafb.net/paste site and the other informations.
157  
158 No Arguments.
159  
160 =cut
161  
162 0 ▶sub paste {
Subroutine paste: not called CC=1 SCAR=6.9
Pod undocumented
163 0 my $self = shift;
164
165 0 $self->_agent();
166
167 # Create and send the request
168 0 my $req = HTTP::Request->new( POST => $self->{SITE} ."/paste.php");
169 0 $req->content_type('application/x-www-form-urlencoded');
170 $req->content( "lang=" . $self->{LANGUAGE} . "&".
171 "nick=" . $self->{NICKNAME} . "&" .
172 "desc=" . $self->{DESCRIPTION} . "&" .
173 "cvt_tabs=" . $self->{TABS} . "&" .
174 0 "text=" . $self->{SOURCE});
175
176 0 my $res = $self->{AGENT}->request($req);
177
178 0 $self->{URL} = "http://www.rafb.net/" . $res->header("Location");
179 0 return $self;
180  
181 }
182  
183 =pod
184  
185 =head1 LINKS
186  
187 http://www.rafb.net/paste
188  
189 =head1 NOTES
190  
191 Don't run this script too much times, the site has protection from that.
192 to solve that - sleep(10) between the pastes.
193  
194 =head1 AUTHOR
195  
196 This module was written by
197 Amit Sides C<< <amit.sides@gmail.com> >>
198  
199 =head1 Copyright
200  
201 Copyright (c) 2006 Amit Sides. All rights reserved.
202  
203 =head1 LICENSE
204  
205 This program is free software; you can redistribute it
206 and/or modify it under the same terms as Perl itself.
207  
208 =cut
209  
210 1 1;
↑ Summary code.pl »