AnyEvent-Superfeedr

 view release on metacpan or  search on metacpan

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

}

sub subscribe {
    my $superfeedr = shift;
    $superfeedr->pubsub_method('subscribe_nodes', @_);
}

sub unsubscribe {
    my $superfeedr = shift;
    $superfeedr->pubsub_method('unsubscribe_nodes', @_);
}

sub pubsub_method {
    my $superfeedr = shift;
    my($method, @feed_uris) = @_;
    my $cb = ref $feed_uris[-1] eq 'CODE' ? pop @feed_uris : sub { };

    my $pubsub = $superfeedr->xmpp_pubsub;
    unless ($pubsub) {
        $superfeedr->event(error => "no pubsub extension available");
        return;
    }
    my $con = $superfeedr->xmpp_connection;
    unless ($con) {
        $superfeedr->event(error => "Wait to be connected");
        return;
    }

    my @chunk = splice @feed_uris, 0, SUBSCRIBE_CHUNK_SIZE;

    my $res_cb;

    my $chunk_cb = sub {
        my ($chunk, $res_cb) = @_;
        my @xmpp_uris = map { xmpp_node_uri($_) } @$chunk;
        $pubsub->$method($con, \@xmpp_uris, $res_cb);
    };

    $res_cb = sub {
        my $err = shift;
        if ($err) {
            $superfeedr->event(error => $err);
            undef @chunk;
            undef @feed_uris;
        } else {
            $cb->($_) for @chunk;
            if (@feed_uris) {
                @chunk = splice @feed_uris, 0, SUBSCRIBE_CHUNK_SIZE;
                $chunk_cb->(\@chunk, $res_cb);
            }
            else {
                undef $chunk_cb;
                undef $res_cb;
            }
        }
    };
    $chunk_cb->(\@chunk, $res_cb);
}

sub xmpp_node_uri {
    my $enc_feed = URI::Escape::uri_escape_utf8(shift, "\x00-\x1f\x7f-\xff");
    # work around what I think is a but in AnyEvent::XMPP
    #return "xmpp:$SERVICE?;node=$enc_feed";
    return "xmpp:$SERVICE?sub;node=$enc_feed";
}

sub xmpp_pubsub {
    my $superfeedr = shift;
    return $superfeedr->{xmpp_pubsub};
}

sub xmpp_connection {
    my $superfeedr = shift;
    my $con = $superfeedr->{xmpp_connection};
    return $con if $con;

    my $client = $superfeedr->{xmpp_client} or return;
    my $jid = $superfeedr->{jid};
    my $account = $client->get_account($jid) or return;
    $con = $account->connection;
    $superfeedr->{xmpp_connection} = $con;
    return $con;
}

1;
__END__

=encoding utf-8

=for stopwords

=head1 NAME

AnyEvent::Superfeedr - XMPP interface to Superfeedr service.

=head1 SYNOPSIS

  use AnyEvent::Superfeedr;

  my $callback = sub {
      my AnyEvent::Superfeedr::Notification $notification = shift;
      my $feed_uri    = $notification->feed_uri;
      my $http_status = $notification->http_status;
      my $next_fetch  = $notification->next_fetch;
      printf "status %s for %s. next: %s\n",
              $http_status, $feed_uri, $next_fetch;
      for my XML::Atom::Entry $entry ($notification->entries) {
          printf "Got: %s\n" $entry->title;
      }
  };

  my $superfeedr = AnyEvent::Superfeedr->new(
      jid => $jid,
      password => $password
      on_notification => $callback,
  );
  $superfeedr->connect;
  AnyEvent->condvar->recv;

  # Subsribe upon connection
  my $superfeedr = AnyEvent::Superfeedr->new(



( run in 0.547 second using v1.01-cache-2.11-cpan-0d23b851a93 )