AnyEvent-WebService-Notifo

 view release on metacpan or  search on metacpan

lib/AnyEvent/WebService/Notifo.pm  view on Meta::CPAN

package AnyEvent::WebService::Notifo;
BEGIN {
  $AnyEvent::WebService::Notifo::VERSION = '0.001';
}

# ABSTRACT: AnyEvent-powered client for the notifo.com API

use strict;
use warnings;
use parent 'Protocol::Notifo';
use AnyEvent::HTTP;

sub send_notification {
  my ($self, %args) = @_;

  my $cb = delete $args{cb};
  confess("Missing required parameter 'cb', ") unless $cb;

  ## Accept both coderef's and condvar's
  unless (ref($cb) eq 'CODE') {
    my $cv = $cb;
    $cb = sub { $cv->send(@_) };
  }

  my $req = $self->SUPER::send_notification(%args);
  return $self->_do_request($cb, $req);
}

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

  my ($meth, $url, $body, $hdrs) = @$req{qw(method url body headers)};

  return http_request(
    $meth   => $url,
    body    => $body,
    headers => {@$hdrs},
    sub { $self->_do_response($cb, @_) }
  );
}

sub _do_response {
  my ($self, $cb, $data, $h) = @_;

  my $res = $self->parse_response(
    http_response_code => $h->{Status},
    http_body          => $data,
    http_status_line   => "$h->{Status} $h->{Reason}",
  );

  $cb->($res);
}

1;


__END__
=pod

=head1 NAME

AnyEvent::WebService::Notifo - AnyEvent-powered client for the notifo.com API

=head1 VERSION

version 0.001

=head1 SYNOPSIS

    use AnyEvent;
    use AnyEvent::WebService::Notifo;
    
    # Uses the default values obtained from configuration file
    my $awn = AnyEvent::WebService::Notifo->new;
    
    # ... or just pass them in
    my $awn = AnyEvent::WebService::Notifo->new(
        api_key => 'api_key_value',
        user    => 'api_user',
    );
    
    # a coderef as a callback is one possibility...
    $awn->send_notification(msg => 'my nottification text', cb => sub {
      my ($res) = @_;
      # $res is our response 



( run in 1.673 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )