Net-Fluidinfo

 view release on metacpan or  search on metacpan

lib/Net/Fluidinfo/Object.pm  view on Meta::CPAN

package Net::Fluidinfo::Object;
use Moose;
extends 'Net::Fluidinfo::Base';

use Carp;
use Scalar::Util qw(blessed);
use Net::Fluidinfo::Tag;
use Net::Fluidinfo::Value;
use Net::Fluidinfo::Value::Native;
use Net::Fluidinfo::Value::NonNative;
use Net::Fluidinfo::Value::Null;
use Net::Fluidinfo::Value::Boolean;
use Net::Fluidinfo::Value::Integer;
use Net::Fluidinfo::Value::Float;
use Net::Fluidinfo::Value::String;
use Net::Fluidinfo::Value::ListOfStrings;

has id        => (is => 'ro', isa => 'Str', writer => '_set_id', predicate => 'has_id');
has about     => (is => 'rw', isa => 'Str', predicate => 'has_about');
has tag_paths => (is => 'ro', isa => 'ArrayRef[Str]', writer => '_set_tag_paths', default => sub { [] });

sub create {
    my $self = shift;

    my $payload = $self->has_about ? $self->json->encode({about => $self->about}) : undef;
    $self->fin->post(
        path       => $self->abs_path('objects'),
        headers    => $self->fin->headers_for_json,
        payload    => $payload,
        on_success => sub {
            my $response = shift;
            my $h = $self->json->decode($response->content);
            $self->_set_id($h->{id});
            # Unset tag paths to force fetching the about tag.
            $self->_set_tag_paths(['fluiddb/about']) if $self->has_about;
            1;
        }
    );
}

sub get {
    print STDERR "get has been deprecated and will be removed, please use get_by_id instead\n";
    &get_by_id;
}

sub get_by_id {
    my ($class, $fin, $id, %opts) = @_;

    $opts{showAbout} = $class->true if delete $opts{about};
    $fin->get(
        path       => $class->abs_path('objects', $id),
        query      => \%opts,
        headers    => $fin->accept_header_for_json,
        on_success => sub {
            my $response = shift;
            my $h = $class->json->decode($response->content);
            my $o = $class->new(fin => $fin, %$h);
            $o->_set_id($id);
            $o->_set_tag_paths($h->{tagPaths});
            $o;
        }
    );
}

sub get_by_about {
    my ($class, $fin, $about) = @_;
    $fin->get(
        path       => $class->abs_path('/about', $about),
        headers    => $fin->accept_header_for_json,
        on_success => sub {
            my $response = shift;
            my $h = $class->json->decode($response->content);
            my $o = $class->new(fin => $fin);
            $o->_set_id($h->{id});
            $o->_set_tag_paths($h->{tagPaths});
            $o->about($about);
            $o;
        }
    );
}



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