Permalink
Browse files

Campfire: handle POST requests

This will be necessary for room-entering (and eventually
other things, if this script keeps growing).

The code is very similar to the GET method, but not quite
enough to make refactoring worthwhile.
  • Loading branch information...
1 parent 0abd1a9 commit e2715c6e2ed490d7b472ca76c1bf546f0222bcfd @peff committed Jan 14, 2011
Showing with 29 additions and 0 deletions.
  1. +29 −0 Campfire.pm
View
@@ -5,6 +5,7 @@ use Campfire::User; # DEPEND
use XML::Smart;
use WWW::Curl::Easy;
use WWW::Curl::Multi;
+use WWW::Curl::Form;
use URI;
use Memoize;
@@ -47,6 +48,7 @@ sub _get {
my $body;
$self->{curl}->setopt(CURLOPT_URL, $url);
+ $self->{curl}->setopt(CURLOPT_POST, 0);
$self->{curl}->setopt(CURLOPT_WRITEDATA, \$body);
my $r = $self->{curl}->perform;
@@ -60,6 +62,33 @@ sub _get {
return XML::Smart->new($body);
}
+sub _post {
+ my $self = shift;
+ my $req = shift;
+
+ my $url = $self->{url}->clone;
+ $url->path("$req.xml");
+
+ my $form = WWW::Curl::Form->new;
+ while (@_) {
+ my $k = shift; my $v = shift;
+ $form->formadd($k => $v);
+ }
+
+ my $body;
+ $self->{curl}->setopt(CURLOPT_URL, $url);
+ $self->{curl}->setopt(CURLOPT_HTTPPOST, $form);
+ $self->{curl}->setopt(CURLOPT_WRITEDATA, \$body);
+
+ my $r = $self->{curl}->perform;
+ $r == 0
+ or die join(' ',
+ "unable to post to $url:",
+ $self->{curl}->strerror($r),
+ $self->{curl}->errbuf
+ );
+}
+
sub _stream {
my $self = shift;
my $req = shift;

0 comments on commit e2715c6

Please sign in to comment.