CalDAV-Simple

 view release on metacpan or  search on metacpan

lib/CalDAV/Simple.pm  view on Meta::CPAN

package CalDAV::Simple;

use 5.006;
use strict;
use warnings;
use Moo 1.006;
use Carp            qw/ croak /;
use CalDAV::Simple::Task;

our $VERSION = '0.01';

has ua => (
    is      => 'ro',
    default => sub {
                   require HTTP::Tiny;
                   require IO::Socket::SSL;
                   return HTTP::Tiny->new(agent => __PACKAGE__.'/'.$VERSION);
               },
);

has calendar         => (is => 'ro');
has username         => (is => 'ro'); 
has password         => (is => 'ro'); 
has croak_on_failure => (is => 'ro', default => sub { 1 });
has _url             => (is => 'lazy');

sub _build__url
{
    my $self = shift;
    
    # This is a hack for doing basic auth
    if ($self->calendar =~ m!^(https?://)(.*)$!) {
        return $1.$self->username.':'.$self->password.'@'.$2;
    }
    else {
        # This is probably my fault :-)
        croak sprintf("unexpected format calendar '%s'\n",
                      $self->calendar);
    }
}

my $request = sub {
    my $self  = shift;
    my $param = shift;

    return $self->ua->request($param->{verb}, $param->{url},
                              {
                               headers => $param->{headers},
                               content => $param->{content},
                              });
};

sub tasks
{
    my $self = shift;
    my $body = '<?xml version="1.0" encoding="utf-8"?><c:calendar-query xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav"><d:prop><d:getetag/><c:calendar-data/></d:prop><c:filter><c:comp-filter name="VCALENDAR"><c:comp-filter name="VTODO"/></c:c...
    my $response = $self->$request({
        verb    => 'REPORT',
        url     => $self->_url,
        content => $body,
        headers => {
                    'Depth'        => 1,
                    'Prefer'       => 'return-minimal',
                    'Content-Type' => 'application/xml; charset=utf-8',
                   },
    });
    if ($response->{success}) {
        my @tasks;
        while ($response->{content} =~ m!<d:response>(.*?)</d:response>!msg) {
            push(@tasks, CalDAV::Simple::Task->new(vcal_string => $1));
        }
        return @tasks;
    }
    else {
        return undef unless $self->croak_on_failure;



( run in 0.900 second using v1.01-cache-2.11-cpan-39bf76dae61 )