AnyEvent-MyPeopleBot-Client
view release on metacpan or search on metacpan
lib/AnyEvent/MyPeopleBot/Client.pm view on Meta::CPAN
package AnyEvent::MyPeopleBot::Client;
{
$AnyEvent::MyPeopleBot::Client::VERSION = '0.0.2';
}
# Abstract: MyPeopleBot API in an event loop
use Moose;
use namespace::autoclean;
use AnyEvent;
use AnyEvent::HTTP::ScopedClient;
has apikey => (
is => 'ro',
isa => 'Str',
required => 1,
);
sub profile {
my ($self, $buddyId, $cb) = @_;
my $client = AnyEvent::HTTP::ScopedClient->new("https://apis.daum.net/mypeople/profile/buddy.json?apikey=" . $self->apikey);
$client->header('Accept', 'application/json')
->post(
{ buddyId => $buddyId },
sub {
my ($body, $hdr) = @_;
return if ( !$body || $hdr->{Status} !~ /^2/ );
print "$body\n" if $ENV{DEBUG};
$cb->($body) if $cb;
}
);
}
sub buddys {
my ($self, $groupId, $cb) = @_;
my $client = AnyEvent::HTTP::ScopedClient->new("https://apis.daum.net/mypeople/group/members.json?apikey=" . $self->apikey);
$client->header('Accept', 'application/json')
->post(
{ groupId => $groupId },
sub {
my ($body, $hdr) = @_;
return if ( !$body || $hdr->{Status} !~ /^2/ );
print "$body\n" if $ENV{DEBUG};
$cb->($body) if $cb;
}
);
}
sub send {
my ($self, $id, $msg, $cb) = @_;
my $which = $id =~ /^B/ ? 'buddy' : 'group';
my %params = (
$which . 'Id' => $id,
content => $msg,
);
my $client = AnyEvent::HTTP::ScopedClient->new("https://apis.daum.net/mypeople/$which/send.json?apikey=" . $self->apikey);
$client->header('Accept', 'application/json')
->post(
\%params,
sub {
my ($body, $hdr) = @_;
( run in 0.519 second using v1.01-cache-2.11-cpan-39bf76dae61 )