| 1 | package WWW::Pastebin::Sprunge::Create; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | # ABSTRACT: create new pastes on sprunge.us | ||
| 5 | our $VERSION = '0.009'; # VERSION | ||
| 6 | use Carp; | ||
| 7 | use URI; | ||
| 8 | use LWP::UserAgent; | ||
| 9 | use HTTP::Request::Common; | ||
| 10 | use Encode; | ||
| 11 | use base 'Class::Data::Accessor'; | ||
| 12 | __PACKAGE__->mk_classaccessors(qw( | ||
| 13 | ua | ||
| 14 | paste_uri | ||
| 15 | error | ||
| 16 | )); | ||
| 17 | |||
| 18 | use overload q|""| => sub { shift->paste_uri; }; | ||
| 19 | |||
| 20 | |||
| 21 | sub new { | ||
| 22 | my $class = shift; | ||
| 23 | croak 'Must have even number of arguments to new()' | ||
| 24 | if @_ & 1; | ||
| 25 | |||
| 26 | my %args = @_; | ||
| 27 | $args{ +lc } = delete $args{ $_ } for keys %args; | ||
| 28 | |||
| 29 | $args{timeout} ||= 30; | ||
| 30 | $args{ua} ||= LWP::UserAgent->new( | ||
| 31 | timeout => $args{timeout}, | ||
| 32 | agent => 'WWW::Pastebin::Sprunge (+http://p3rl.org/WWW::Pastebin::Sprunge)', | ||
| 33 | ); | ||
| 34 | |||
| 35 | my $self = bless {}, $class; | ||
| 36 | $self->ua( $args{ua} ); | ||
| 37 | |||
| 38 | return $self; | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | sub paste { | ||
| 43 | my ( $self, $text ) = splice @_, 0, 2; | ||
| 44 | |||
| 45 | $self->$_(undef) for qw(paste_uri error); | ||
| 46 | |||
| 47 | defined $text or carp 'Undefined paste content' and return; | ||
| 48 | |||
| 49 | croak 'Must have even number of optional arguments to paste()' | ||
| 50 | if @_ & 1; | ||
| 51 | |||
| 52 | my %args = @_; | ||
| 53 | %args = ( | ||
| 54 | sprunge => $text, | ||
| 55 | %args, | ||
| 56 | ); | ||
| 57 | |||
| 58 | $args{lang} = lc $args{lang} if $args{lang}; | ||
| 59 | |||
| 60 | $args{file} | ||
| 61 | and not -e $args{sprunge} | ||
| 62 | and return $self->_set_error("File $args{sprunge} does not seem to exist"); | ||
| 63 | |||
| 64 | my $ua = $self->ua; | ||
| 65 | $ua->requests_redirectable( [ ] ); | ||
| 66 | |||
| 67 | my @post_request = ( | ||
| 68 | 'http://sprunge.us', | ||
| 69 | Content_Type => 'form-data', | ||
| 70 | Content => [ | ||
| 71 | $args{file} | ||
| 72 | ? (sprunge => [ $args{sprunge}, '' ]) | ||
| 73 | : (sprunge => encode_utf8($args{sprunge})) | ||
| 74 | ], | ||
| 75 | ); | ||
| 76 | my $response = do { | ||
| 77 | local $HTTP::Request::Common::DYNAMIC_FILE_UPLOAD = 1; | ||
| 78 | $self->ua->post( @post_request ); | ||
| 79 | }; | ||
| 80 | if ( $response->is_success() ) { | ||
| 81 | my $uri = URI->new($response->{_content}); | ||
| 82 | return $self->_set_error(q{Request was successful but I don't see a link to the paste } | ||
| 83 | . $response->code | ||
| 84 | . $response->content | ||
| 85 | ) unless $uri; | ||
| 86 | $uri->query($args{lang}) if $args{lang}; | ||
| 87 | return $self->paste_uri($uri); | ||
| 88 | } | ||
| 89 | else { | ||
| 90 | return $self->_set_error($response, 'net'); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | sub _set_error { | ||
| 96 | my ( $self, $error, $type ) = @_; | ||
| 97 | |||
| 98 | if ( defined $type and $type eq 'net' ) { | ||
| 99 | $self->error( 'Network error: ' . $error->status_line ); | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | $self->error( $error ); | ||
| 103 | } | ||
| 104 | return; | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | 1; | ||
| 109 | |||
| 110 | __END__ |