AnyEvent-Lingr
view release on metacpan or search on metacpan
lib/AnyEvent/Lingr.pm view on Meta::CPAN
else {
debugf "no on_event callback";
}
}
$self->_clear_polling_guard;
$self->_polling;
}
else {
$self->_on_error($res, $hdr);
}
};
Scalar::Util::weaken($self);
$self->_polling_guard( $guard );
}
sub say {
my ($self, $room, $msg, $cb) = @_;
$self->post('room/say', { session => $self->session, room => $room, text => $msg }, sub {
my ($res, $hdr) = @_;
return unless $self;
if ($res and $res->{status} eq 'ok') {
$cb->($res) if $cb;
}
else {
$self->_on_error($res, $hdr);
}
});
Scalar::Util::weaken($self);
}
1;
__END__
=head1 NAME
AnyEvent::Lingr - Asynchronous Lingr client.
=head1 SYNOPSIS
use AnyEvent;
use AnyEvent::Lingr;
my $lingr = AnyEvent::Lingr->new(
user => 'your lingr username',
password => 'your lingr password',
api_key => 'your lingr api_key', # optional
);
# error handler
$lingr->on_error(sub {
my ($msg) = @_;
warn 'Lingr error: ', $msg;
# reconnect after 5 seconds,
my $t; $t = AnyEvent->timer(
after => 5,
cb => sub {
$lingr->start_session;
undef $t;
},
);
});
# room info handler
$lingr->on_room_info(sub {
my ($rooms) = @_;
print "Joined rooms:\n";
for my $room (@$rooms) {
print " $room->{id}\n";
}
});
# event handler
$lingr->on_event(sub {
my ($event) = @_;
# print message
if (my $msg = $event->{message}) {
print sprintf "[%s] %s: %s\n",
$msg->{room}, $msg->{nickname}, $msg->{text};
}
});
# start lingr session
$lingr->start_session;
=head1 DESCRIPTION
AnyEvent::Lingr is asynchronous client interface for L<Lingr|http://lingr.com/>.
=head1 METHODS
=head2 new(%options)
Create AnyEvent::Lingr object. Available %options are:
=over
=item * user => 'Str' (required)
Lingr username
=item * password => 'Str' (required)
Lingr password
=item * api_key => 'Str' (optional)
Lingr api_key.
=item * session => 'Str' (optional)
Lingr session key. If this parameter is passed, this module try to reuse this key for calling session/verify api, otherwise create new session.
lib/AnyEvent/Lingr.pm view on Meta::CPAN
Update joined room info, and fire on_room_info callback.
This method also update subscription rooms which is target room for on_event callback.
=head2 say($room, $message [, $cb ])
Say something to lingr room.
$lingr->say('perl_jp', 'hi!');
If you want response data, you can speficy callback.
The callback is invoked when the API call was successful.
$lingr->say('perl_jp', 'hi there!', sub {
my $res = shift;
warn $res->{message}->{id};
});
=head1 CALLBACKS
This module supports following three callbacks:
=over
=item * on_error->($msg)
=item * on_room_info->($rooms)
=item * on_event->($event)
=back
All callbacks can be set by accessor:
$lingr->on_error(sub { ... });
Or by constructor:
my $lingr = AnyEvent::Lingr->new(
...
on_error => sub { ... },
);
=head2 on_error->($msg)
Error callbacks.
C<$msg> is error message. If this message is form of "\d\d\d: message" like:
595: Invalid argument
This is http level or connection level error. Otherwise C<$msg> is error message returned from lingr api server.
Both case, lingr session was closed before this callback, so you can restart session in this callback:
$lingr->on_error(sub {
my ($msg) = @_;
warn 'Lingr error: ', $msg;
# reconnect after 5 seconds,
my $t; $t = AnyEvent->timer(
after => 5,
cb => sub {
$lingr->start_session;
undef $t;
},
);
});
=head2 on_room_info->($rooms)
Fired when after start_session or after update_room_info method.
C<$rooms> is ArrayRef of room information you joined.
=head2 on_event->($event)
Fired when some events is occurred in your subscribed rooms.
=head1 AUTHOR
Daisuke Murase <typester@cpan.org>
=head1 COPYRIGHT AND LICENSE
Copyright (c) 2013 Daisuke Murase All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
=cut
( run in 1.320 second using v1.01-cache-2.11-cpan-39bf76dae61 )