PageRenderTime 31ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/perl/site/lib/WWW/Pastebin/PastebinCom/Create.pm

http://github.com/dwimperl/perl-5.14.2.1-32bit-windows
Perl | 532 lines | 481 code | 50 blank | 1 comment | 8 complexity | e38fdd347f89055f531af5eae2980b0c MD5 | raw file
Possible License(s): LGPL-3.0, Unlicense, GPL-2.0, LGPL-2.0, BSD-3-Clause, LGPL-2.1, AGPL-1.0, GPL-3.0
  1. package WWW::Pastebin::PastebinCom::Create;
  2. use warnings;
  3. use strict;
  4. our $VERSION = '0.004';
  5. use Carp;
  6. use URI;
  7. use LWP::UserAgent;
  8. use overload q|""| => sub { shift->paste_uri };
  9. sub new {
  10. my $class = shift;
  11. croak "Must have even number of arguments to the constructor"
  12. if @_ & 1;
  13. my %args = @_;
  14. unless ( $args{timeout} ) {
  15. $args{timeout} = 30;
  16. }
  17. unless ( $args{ua} ) {
  18. $args{ua} = LWP::UserAgent->new(
  19. timeout => $args{timeout},
  20. agent => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US;'
  21. . ' rv:1.8.1.12) Gecko/20080207 Ubuntu/7.10 (gutsy)'
  22. . ' Firefox/2.0.0.12',
  23. );
  24. }
  25. return bless \%args, $class;
  26. }
  27. sub paste {
  28. my $self = shift;
  29. croak "Must have even number of arguments to paste() method"
  30. if @_ & 1;
  31. my %args = @_;
  32. $args{ +lc } = delete $args{ $_ } for keys %args;
  33. unless ( defined $args{text} ) {
  34. $self->error( 'Missing or undefined `text` argument' );
  35. return;
  36. }
  37. # handle uri (deprecated argument)
  38. if ( exists $args{uri} )
  39. {
  40. ($args{subdomain} ) = $args{uri} =~ m{http://(.+)\.pastebin\.com}
  41. or croak( "can't parse URI parameter: $args{uri}\n" );
  42. delete $args{uri};
  43. }
  44. $self->paste_uri( undef );
  45. $self->error( undef );
  46. %args = (
  47. format => 'text',
  48. expiry => 'd',
  49. poster => '',
  50. email => '',
  51. %args,
  52. );
  53. my $valid_formats = $self->get_valid_formats;
  54. unless ( exists $valid_formats->{ $args{format} } ) {
  55. croak "Invalid syntax-highlight format was specified\n"
  56. . "Use ->get_valid_formats() method to get full list"
  57. . " of valid values";
  58. }
  59. # map onto expiration
  60. my %expire = ( f => 'n',
  61. d => '1d',
  62. m => '1m' );
  63. croak "Invalid `expiry` argument. Must be either 'f', 'd' or 'm'"
  64. if !exists $expire{$args{expiry}};
  65. $args{expiry} = $expire{ $args{expiry} };
  66. # map onto API parameters
  67. my %API = (
  68. poster => 'paste_name',
  69. text => 'paste_code',
  70. email => 'paste_email',
  71. subdomain => 'paste_subdomain',
  72. private => 'paste_private',
  73. expiry => 'paste_expire_date',
  74. format => 'paste_format',
  75. );
  76. $args{$API{$_}} = delete $args{$_}
  77. foreach grep { defined $API{$_}} keys %args;
  78. my $uri = URI->new( 'http://pastebin.com/api_public.php' );
  79. my $response = $self->{ua}->post( $uri, \%args );
  80. if ( $response->is_success or $response->is_redirect ) {
  81. return $self->paste_uri( $response->content );
  82. }
  83. else {
  84. $self->error( $response->status_line );
  85. return;
  86. }
  87. }
  88. sub error {
  89. my $self = shift;
  90. if ( @_ ) {
  91. $self->{ ERROR } = shift;
  92. }
  93. return $self->{ ERROR };
  94. }
  95. sub paste_uri {
  96. my $self = shift;
  97. if ( @_ ) {
  98. $self->{ PASTE_URI } = shift;
  99. }
  100. return $self->{ PASTE_URI };
  101. }
  102. sub get_valid_formats {
  103. return {
  104. abap => 'ABAP',
  105. actionscript => 'ActionScript',
  106. actionscript3 => 'ActionScript 3',
  107. ada => 'Ada',
  108. apache => 'Apache Log',
  109. applescript => 'AppleScript',
  110. apt_sources => 'APT Sources',
  111. asm => 'ASM (NASM)',
  112. asp => 'ASP',
  113. autoit => 'AutoIt',
  114. avisynth => 'Avisynth',
  115. bash => 'Bash',
  116. basic4gl => 'Basic4GL',
  117. bibtex => 'BibTeX',
  118. blitzbasic => 'Blitz Basic',
  119. bnf => 'BNF',
  120. boo => 'BOO',
  121. bf => 'BrainFuck',
  122. c => 'C',
  123. c_mac => 'C for Macs',
  124. cill => 'C Intermediate Language',
  125. csharp => 'C#',
  126. cpp => 'C++',
  127. caddcl => 'CAD DCL',
  128. cadlisp => 'CAD Lisp',
  129. cfdg => 'CFDG',
  130. klonec => 'Clone C',
  131. klonecpp => 'Clone C++',
  132. cmake => 'CMake',
  133. cobol => 'COBOL',
  134. cfm => 'ColdFusion',
  135. css => 'CSS',
  136. d => 'D',
  137. dcs => 'DCS',
  138. delphi => 'Delphi',
  139. dff => 'Diff',
  140. div => 'DIV',
  141. dos => 'DOS',
  142. dot => 'DOT',
  143. eiffel => 'Eiffel',
  144. email => 'Email',
  145. erlang => 'Erlang',
  146. fo => 'FO Language',
  147. fortran => 'Fortran',
  148. freebasic => 'FreeBasic',
  149. gml => 'Game Maker',
  150. genero => 'Genero',
  151. gettext => 'GetText',
  152. groovy => 'Groovy',
  153. haskell => 'Haskell',
  154. hq9plus => 'HQ9 Plus',
  155. html4strict => 'HTML',
  156. idl => 'IDL',
  157. ini => 'INI file',
  158. inno => 'Inno Script',
  159. intercal => 'INTERCAL',
  160. io => 'IO',
  161. java => 'Java',
  162. java5 => 'Java 5',
  163. javascript => 'JavaScript',
  164. kixtart => 'KiXtart',
  165. latex => 'Latex',
  166. lsl2 => 'Linden Scripting',
  167. lisp => 'Lisp',
  168. locobasic => 'Loco Basic',
  169. lolcode => 'LOL Code',
  170. lotusformulas => 'Lotus Formulas',
  171. lotusscript => 'Lotus Script',
  172. lscript => 'LScript',
  173. lua => 'Lua',
  174. m68k => 'M68000 Assembler',
  175. make => 'Make',
  176. matlab => 'MatLab',
  177. matlab => 'MatLab',
  178. mirc => 'mIRC',
  179. modula3 => 'Modula 3',
  180. mpasm => 'MPASM',
  181. mxml => 'MXML',
  182. mysql => 'MySQL',
  183. text => 'None',
  184. nsis => 'NullSoft Installer',
  185. oberon2 => 'Oberon 2',
  186. objc => 'Objective C',
  187. 'ocaml-brief' => 'OCalm Brief',
  188. ocaml => 'OCaml',
  189. glsl => 'OpenGL Shading',
  190. oobas => 'Openoffice BASIC',
  191. oracle11 => 'Oracle 11',
  192. oracle8 => 'Oracle 8',
  193. pascal => 'Pascal',
  194. pawn => 'PAWN',
  195. per => 'Per',
  196. perl => 'Perl',
  197. php => 'PHP',
  198. 'php-brief' => 'PHP Brief',
  199. pic16 => 'Pic 16',
  200. pixelbender => 'Pixel Bender',
  201. plsql => 'PL/SQL',
  202. povray => 'POV-Ray',
  203. powershell => 'Power Shell',
  204. progress => 'Progress',
  205. prolog => 'Prolog',
  206. properties => 'Properties',
  207. providex => 'ProvideX',
  208. python => 'Python',
  209. qbasic => 'QBasic',
  210. rails => 'Rails',
  211. rebol => 'REBOL',
  212. reg => 'REG',
  213. robots => 'Robots',
  214. ruby => 'Ruby',
  215. gnuplot => 'Ruby Gnuplot',
  216. sas => 'SAS',
  217. scala => 'Scala',
  218. scheme => 'Scheme',
  219. scilab => 'Scilab',
  220. sdlbasic => 'SdlBasic',
  221. smalltalk => 'Smalltalk',
  222. smarty => 'Smarty',
  223. sql => 'SQL',
  224. tsql => 'T-SQL',
  225. tcl => 'TCL',
  226. tcl => 'TCL',
  227. teraterm => 'Tera Term',
  228. thinbasic => 'thinBasic',
  229. typoscript => 'TypoScript',
  230. unreal => 'unrealScript',
  231. vbnet => 'VB.NET',
  232. verilog => 'VeriLog',
  233. vhdl => 'VHDL',
  234. vim => 'VIM',
  235. visualprolog => 'Visual Pro Log',
  236. vb => 'VisualBasic',
  237. visualfoxpro => 'VisualFoxPro',
  238. whitespace => 'WhiteSpace',
  239. whois => 'WHOIS',
  240. winbatch => 'Win Batch',
  241. xml => 'XML',
  242. xorg_conf => 'Xorg Config',
  243. xpp => 'XPP',
  244. z80 => 'Z80 Assembler',
  245. };
  246. }
  247. 1;
  248. __END__
  249. =head1 NAME
  250. WWW::Pastebin::PastebinCom::Create - paste to L<http://pastebin.com> from Perl.
  251. =head1 SYNOPSIS
  252. use strict;
  253. use warnings;
  254. use WWW::Pastebin::PastebinCom::Create;
  255. my $paste = WWW::Pastebin::PastebinCom::Create->new;
  256. $paste->paste( text => 'lots and lost of text to paste' )
  257. or die "Error: " . $paste->error;
  258. print "Your paste can be found on $paste\n";
  259. =head1 DESCRIPTION
  260. The module provides means of pasting large texts into
  261. L<http://pastebin.com> pastebin site.
  262. =head1 CONSTRUCTOR
  263. =head2 new
  264. my $paste = WWW::Pastebin::PastebinCom::Create->new;
  265. my $paste = WWW::Pastebin::PastebinCom::Create->new(
  266. timeout => 10,
  267. );
  268. my $paste = WWW::Pastebin::PastebinCom::Create->new(
  269. ua => LWP::UserAgent->new(
  270. timeout => 10,
  271. agent => 'PasterUA',
  272. ),
  273. );
  274. Constructs and returns a brand new yummy juicy WWW::Pastebin::PastebinCom::Create
  275. object. Takes two arguments, both are I<optional>. Possible arguments are
  276. as follows:
  277. =head3 timeout
  278. ->new( timeout => 10 );
  279. B<Optional>. Specifies the C<timeout> argument of L<LWP::UserAgent>'s
  280. constructor, which is used for pasting. B<Defaults to:> C<30> seconds.
  281. =head3 ua
  282. ->new( ua => LWP::UserAgent->new( agent => 'Foos!' ) );
  283. B<Optional>. If the C<timeout> argument is not enough for your needs
  284. of mutilating the L<LWP::UserAgent> object used for pasting, feel free
  285. to specify the C<ua> argument which takes an L<LWP::UserAgent> object
  286. as a value. B<Note:> the C<timeout> argument to the constructor will
  287. not do anything if you specify the C<ua> argument as well. B<Defaults to:>
  288. plain boring default L<LWP::UserAgent> object with C<timeout> argument
  289. set to whatever C<WWW::Pastebin::PastebinCom::Create>'s C<timeout> argument is
  290. set to as well as C<agent> argument is set to mimic Firefox.
  291. =head1 METHODS
  292. =head2 paste
  293. $paste->paste( text => 'long long text' )
  294. or die "Failed to paste: " . $paste->error;
  295. my $paste_uri = $paste->paste(
  296. text => 'long long text',
  297. format => 'perl',
  298. poster => 'Zoffix',
  299. expiry => 'm',
  300. subdomain => 'subdomain',
  301. private => 0,
  302. ) or die "Failed to paste: " . $paste->error;
  303. Instructs the object to pastebin some text. If pasting succeeded returns
  304. a URI pointing to your paste, otherwise returns either C<undef> or
  305. an empty list (depending on the context) and the reason for the failure
  306. will be avalable via C<error()> method (see below).
  307. Note: you don't have to store the return value. There is a C<paste_uri()>
  308. method as well as overloaded construct; see C<paste_uri()> method's
  309. description below.
  310. Takes one mandatory and
  311. three optional arguments which are as follows:
  312. =head3 text
  313. ->paste( text => 'long long long long text to paste' );
  314. B<Mandatory>. The C<text> argument must contain the text to paste. If
  315. C<text>'s value is undefined the C<paste()> method will return either
  316. C<undef> or an empty list (depending on the context) and the C<error()>
  317. method will contain a message about undefined C<text>.
  318. =head3 format
  319. ->paste( text => 'foo', format => 'perl' );
  320. B<Optional>. Specifies the format of the paste to enable specific syntax
  321. highlights on L<http://pastebin.com>. The list of possible values is
  322. very long, see C<get_valid_formats()> method below for information
  323. on how to obtain possible valid values for the C<format> argument.
  324. B<Defaults to:> C<text> (plain text paste).
  325. =head3 poster
  326. ->paste( text => 'foo', poster => 'Zoffix Znet' );
  327. B<Optional>. Specifies the name of the person pasting the text.
  328. B<Defaults to:> empty string, which leads to C<Anonymous> apearing on
  329. L<http://pastebin.com>
  330. =head3 expiry
  331. ->paste( text => 'foo', expiry => 'f' );
  332. B<Optional>. Specifies when the paste should expire.
  333. B<Defaults to:> C<d> (expire the paste in one day). Takes three possible
  334. values:
  335. =over 5
  336. =item d
  337. When C<expiry> is set to value C<d>, the paste will expire in one day.
  338. =item m
  339. When C<expiry> is set to value C<m>, the paste will expire in one month.
  340. =item f
  341. When C<expiry> is set to value C<f>, the paste will (should) stick around
  342. "forever".
  343. =back
  344. =head3 C<subdomain>
  345. subdomain => 'private_domain'
  346. B<Optional>. Allows one to paste into a so called "private" pastebin with a personal domain name. Takes the domain name.
  347. =head3 C<uri>
  348. uri => 'http://private_domain.pastebin.com/'
  349. B<DEPRECATED>. use C<subdomain>.
  350. =head2 error
  351. $paste->paste( text => 'foos' )
  352. or die "Error: " . $paste->error;
  353. If the C<paste()> method failed to paste your text for any reason
  354. (including your text being undefined) it will return either C<undef>
  355. or an empty list depending on the context. When that happens you will
  356. be able to find out the reason of the error via C<error()> method.
  357. Returns a scalar containing human readable message describing the error.
  358. Takes no arguments.
  359. =head2 paste_uri (and overloads)
  360. print "You can find your pasted text on " . $paste->paste_uri . "\n";
  361. # or by interpolating the WWW::Pastebin::PastebinCom::Create object directly:
  362. print "You can find your pasted text on $paste\n";
  363. Takes no arguments. Returns a URI pointing to the L<http://pastebin.com>
  364. page containing the text you have pasted. If you call this method before
  365. pasting anything or if C<paste()> method failed the C<paste_uri> will
  366. return either C<undef> or an empty list depending on the context.
  367. B<Note:> the WWW::Pastebin::PastebinCom::Create object is overloaded so instead
  368. of calling C<paste_uri> method you could simply interpolate the
  369. WWW::Pastebin::PastebinCom::Create object. For example:
  370. my $paster = WWW::Pastebin::PastebinCom::Create->new;
  371. $paster->paste( text => 'long text' )
  372. or die "Failed to paste: " . $paster->error;
  373. print "Your paste is located on $paster\n";
  374. =head2 get_valid_formats
  375. my $valid_formats_hashref = $paste->get_valid_formats;
  376. Takes no arguments. Returns a hashref, keys of which will be valid
  377. values of the C<format> argument to C<paste()> method and values of which
  378. will be explanation of semi-cryptic codes.
  379. =head1 AUTHOR
  380. Zoffix Znet, C<< <zoffix at cpan.org> >>
  381. (L<http://zoffix.com>, L<http://haslayout.net>, L<http://mind-power-book.com/>)
  382. Patches by Diab Jerius (DJERIUS)
  383. =head1 BUGS
  384. Please report any bugs or feature requests to C<bug-www-pastebin-pastebincom-create at rt.cpan.org>, or through
  385. the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-Pastebin-PastebinCom-Create>. I will be notified, and then you'll
  386. automatically be notified of progress on your bug as I make changes.
  387. =head1 SUPPORT
  388. You can find documentation for this module with the perldoc command.
  389. perldoc WWW::Pastebin::PastebinCom::Create
  390. You can also look for information at:
  391. =over 4
  392. =item * RT: CPAN's request tracker
  393. L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Pastebin-PastebinCom-Create>
  394. =item * AnnoCPAN: Annotated CPAN documentation
  395. L<http://annocpan.org/dist/WWW-Pastebin-PastebinCom-Create>
  396. =item * CPAN Ratings
  397. L<http://cpanratings.perl.org/d/WWW-Pastebin-PastebinCom-Create>
  398. =item * Search CPAN
  399. L<http://search.cpan.org/dist/WWW-Pastebin-PastebinCom-Create>
  400. =back
  401. =head1 COPYRIGHT & LICENSE
  402. Copyright 2008 Zoffix Znet, all rights reserved.
  403. This program is free software; you can redistribute it and/or modify it
  404. under the same terms as Perl itself.
  405. =cut