Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
FriendFeed に AnyEvent 文脈で いろいろポストする
package MyApp::AnyEvent::FriendFeed;
use strict;
use warnings;
use Carp;
use AnyEvent;
use AnyEvent::HTTP;
use MIME::Base64;
use HTTP::Request::Common;
use HTTP::Request;
use JSON;
our $VERSION = '0.01';
my $api = 'http://friendfeed-api.com/v2/entry';
sub new {
my $class = shift;
my %args = @_;
$args{'username'} || Carp::croak qq(! failed: "username" not found\n);
$args{'remotekey'} || Carp::croak qq(! failed: "remotekey" not found\n);
chomp(my $auth = MIME::Base64::encode( join ':', $args{username}, $args{remotekey}));
bless {
authorization => "Basic ${auth}",
}, $class;
}
sub post {
my $self = shift;
my $body = shift || Carp::croak qq(! failed: "body" parameter not found\n);
my $cb = pop || Carp::croak qq(! failed: "callback" not found\n);
my %opts = @_;
my $on_error = delete $opts{on_error} || sub { die @_; return; };
my $on_header = delete $opts{on_header} || sub {
my $headers = shift;
unless ($headers->{Status} =~ /^2/) {
$on_error->(qq(! failed: $headers->{Status} $! "$headers->{Reason}"\n));
return;
}
return 1;
};
$opts{body} = $body;
my @request_params = ($api,
Authorization => $self->{authorization},
Content => \%opts
);
push @request_params, qw/Content_Type form-data/ if $opts{file};
my $request = HTTP::Request::Common::POST(@request_params);
my $p; $p = http_request('POST' => $api,
headers => $request->headers,
body => $request->content,
on_header => $on_header,
sub {
undef $p;
my($body, $headers) = @_;
my $res = JSON::decode_json $body;
$cb->($res, $headers);
}
);
}
1;
__END__
=head1 NAME
MyApp::AnyEvent::FriendFeed - Post to FriendFeed API based AnyEvent
=head1 SYNOPSIS
use MyApp::AnyEvent::FriendFeed;
my $client = MyApp::AnyEvent::FriendFeed->new(
username => 'your Friendfeed username',
remotekey => 'your Friendfeed remotekey',
);
my $AE::cv;
$client->post('any message...',
link => 'http://....', # option
comment => "any comment", # option
file => [ "abc.jpg", "def.jpg", "ghi.jpg" ], # option
on_error => sub {
warn @_;
},
sub {
my($response_json, $headers) = @_;
if ($response_json->{errorCode}){
warn $response_json->{errorCode};
} else {
print join ' ',
$response_json->{date},
$response_json->{url},
$response_json->{id},
$response_json->{body};
}
$cv->send;
}
);
$cv->recv;
=head1 DESCRIPTION
Post any entry to friendfeed.com on base AnyEvent.
Note: this module is "username" and "remotekey" required.
=head1 METHOD
post($message, %options, callback)
B<$message> required. this parameter as same as the parameter "body" at frinedfeed API.
detail of B<%options> see L<http://friendfeed.com/api/documentation#write>.
B<$callback> required. the parameters of this callback function are responseBody(hashref) form frinedfeedAPI and responseHeaders(hashhref).
=ehad1 SEE ALSO
L<AnyEvent::HTTP>, L<http://friendfeed.com/api/documentation>
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment