MojoX-Session

 view release on metacpan or  search on metacpan

lib/MojoX/Session/Store/Couchdb.pm  view on Meta::CPAN

    $data = $json->encode($data);
    unless ($data && !$json->error) {
        $self->error("Can't parse incoming data");
        return;
    }

    return $data;
}

sub _decode_json {
    my $self = shift;
    my $body = shift;

    my $json = Mojo::JSON->new;

    $body = $json->decode($body);

    unless ($body && !$json->error) {
        $self->error("Can't parse response");
        return;
    }

    return $body;
}

sub create {
    my ($self, $sid, $expires, $data, $cb) = @_;

    $self->error('');

    $data = $self->_encode_json({data => $data, expires => $expires});
    return $cb->($self) unless $data;

    my $url = $self->_build_url($sid);

    my $tx = $self->client->put(
        $url => $data
    );

    unless ($tx->success) {
        $self->error($tx->error);
        return $cb->($self);
    }

    unless ($tx->res->code == 201) {
        $self->error('Wrong response');
        return $cb->($self);
    }

    my $body = $self->_decode_json($tx->res->body);
    if ($body->{error}) {
        $self->error($body->{error});
        return $cb->($self);
    }

    $self->_rev($body->{_rev});

    return $cb->($self);
}

sub update {
    my ($self, $sid, $expires, $data, $cb) = @_;

    $self->error('');

    $data =
      $self->_encode_json({data => $data, expires => $expires, _rev => $self->_rev});
    return $cb->($self) unless $data;

    my $url = $self->_build_url($sid);

    my $tx = $self->client->put(
        $url => $data
    );

    if ($tx->error) {
        $self->error($tx->error);
        return $cb->($self);
    }

    unless ($tx->res->code == 201) {
        $self->error('Wrong response');
        return $cb->($self);
    }

    my $body = $self->_decode_json($tx->res->body);
    if ($body->{error}) {
        $self->error($body->{error});
        return $cb->($self);
    }

    $self->_rev($body->{_rev});

    return $cb->($self);
}

sub load {
    my ($self, $sid, $cb) = @_;

    $self->error('');

    my $url = $self->_build_url($sid);

    my $tx = $self->client->get($url);

    if ($tx->error) {
        $self->error($tx->error);
        return $cb->($self);
    }

    # Session not found
    if ($tx->res->code == 404) {
        return $cb->($self);
    }

    # Wrong response status
    unless ($tx->res->code == 200) {
        $self->error('Wrong response');
        return $cb->($self);
    }



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