APNS-Agent

 view release on metacpan or  search on metacpan

Build.PL  view on Meta::CPAN

# =========================================================================
# THIS FILE IS AUTOMATICALLY GENERATED BY MINILLA.
# DO NOT EDIT DIRECTLY.
# =========================================================================

use 5.008_001;

use strict;
use warnings;
use utf8;

use Module::Build;
use File::Basename;
use File::Spec;
use CPAN::Meta;
use CPAN::Meta::Prereqs;

my %args = (
    license              => 'perl',
    dynamic_config       => 0,

lib/APNS/Agent.pm  view on Meta::CPAN

package APNS::Agent;
use 5.010;
use strict;
use warnings;

our $VERSION = "0.06";

use AnyEvent::APNS;
use Cache::LRU;
use Encode qw/decode_utf8/;
use JSON::XS;
use Log::Minimal;
use Plack::Request;
use Router::Boom::Method;

use Class::Accessor::Lite::Lazy 0.03 (
    new => 1,
    ro => [qw/
        certificate
        private_key

lib/APNS/Agent.pm  view on Meta::CPAN

    };
}

sub _do_main {
    my ($self, $req) = @_;

    my $token = $req->param('token') or return [400, [], ['Bad Request']];

    my $payload;
    if (my $payload_json = $req->param('payload') ) {
        state $json_driver = JSON::XS->new->utf8;
        local $@;
        $payload = eval { $json_driver->decode($payload_json) };
        return [400, [], ['BAD REQUEST']] if $@;
    }
    elsif (my $alert = $req->param('alert')) {
        $payload = +{
            alert => decode_utf8($alert),
        };
    }
    return [400, [], ['BAD REQUEST']] unless $payload;

    my @payloads = map {[$_, $payload]} split /,/, $token;
    push @{$self->_queue}, @payloads;

    infof "event:payload queued\ttoken:%s", $token;
    if ($self->__apns->connected) {
        $self->_sending;

lib/APNS/Agent.pm  view on Meta::CPAN

sub _do_monitor {
    my ($self, $req) = @_;

    my $result = {
        sent   => $self->_sent,
        queued => scalar( @{ $self->_queue } ),
    };
    my $body = encode_json($result);

    return [200, [
        'Content-Type'   => 'application/json; charset=utf-8',
        'Content-Length' => length($body),
    ], [$body]];
}

sub _build_apns {
    my $self = shift;

    AnyEvent::APNS->new(
        certificate => $self->certificate,
        private_key => $self->private_key,

lib/APNS/Agent.pm  view on Meta::CPAN

    if (!$args{listen} && !$args{port} && !$ENV{SERVER_STARTER_PORT}) {
        $args{port} = 4905;
    }
    require Plack::Loader;
    Plack::Loader->load(Twiggy => %args)->run($self->to_app);
}

1;
__END__

=encoding utf-8

=head1 NAME

APNS::Agent - agent server for APNS

=head1 SYNOPSIS

    use APNS::Agent;
    my $agent = APNS::Agent->new(
        certificate => '/path/to/certificate',

t/01_basic.t  view on Meta::CPAN

use strict;
use warnings;
use utf8;
use Test::More 0.98;
use Test::TCP;
use Plack::Test;
use HTTP::Request::Common;

use AnyEvent;
use AnyEvent::Socket;

use JSON::XS;
use APNS::Agent;



( run in 0.895 second using v1.01-cache-2.11-cpan-fd5d4e115d8 )