AnyEvent-Lingr

 view release on metacpan or  search on metacpan

lib/AnyEvent/Lingr.pm  view on Meta::CPAN

package AnyEvent::Lingr;
use Mouse;

our $VERSION = '0.07';

use AnyEvent::HTTP;

use Carp;
use JSON;
use Log::Minimal;
use Scalar::Util ();
use Try::Tiny;
use URI;

has ['user', 'password'] => (
    is       => 'ro',
    required => 1,
);

has 'api_key' => (
    is => 'ro',
);

has 'endpoint' => (
    is      => 'ro',
    default => 'http://lingr.com/api/',
);

has 'session' => (
    is => 'rw',
);

has ['on_error', 'on_room_info', 'on_event'] => (
    is  => 'rw',
    isa => 'CodeRef',
);

has 'counter' => (
    is  => 'rw',
    isa => 'Int',
);

has '_polling_guard' => (
    is      => 'rw',
    clearer => '_clear_polling_guard',
);

no Mouse;

sub request {
    my ($self, $http_method, $method, $params, $cb) = @_;

    my $uri = URI->new($self->endpoint . $method);
    $uri->query_form($params);

    my $cb_wrap = sub {
        my ($body, $hdr) = @_;

        my $json = try { decode_json $body };
        $cb->($json, $hdr);
    };

    if ($http_method eq 'GET') {
        http_get $uri, $cb_wrap;
    } elsif ($http_method eq 'POST') {
        my $body = $uri->query;
        $uri->query(undef);
        http_post $uri, $body, $cb_wrap;
    } else {
        croak "unsupported http method: $http_method"
    }



( run in 1.258 second using v1.01-cache-2.11-cpan-5b529ec07f3 )