Finance-Bank-Postbank_de

 view release on metacpan or  search on metacpan

lib/HAL/Resource.pm  view on Meta::CPAN

package HAL::Resource;
use Moo;
use JSON 'decode_json';
use Filter::signatures;
no warnings 'experimental::signatures';
use feature 'signatures';
use Future;

use Carp qw(croak);

our $VERSION = '0.57';

=head1 NAME

HAL::Resource - wrap a HAL resource

=head1 SYNOPSIS

    my $ua = WWW::Mechanize->new();
    my $res = $ua->get('https://api.example.com/');
    my $r = HAL::Resource->new(
        ua => $ua,
        %{ decode_json( $res->decoded_content ) },
    );

=head1 ABOUT

This module is just a very thin wrapper for HAL resources. If you find this
module useful, I'm very happy to spin it off into its own distribution.

=head1 SEE ALSO

L<Data::HAL> - similar to this module, but lacks a HTTP transfer facility and
currently fails its test suite

L<HAL::Tiny> - a module to generate HAL JSON

L<WebAPI::DBIC::Resource::HAL> - an adapter to export DBIx::Class structures
as HAL

Hypertext Application Language - L<https://en.wikipedia.org/wiki/Hypertext_Application_Language>

=cut

has ua => (
    weaken => 1,
    is => 'ro',
);

has _links => (
    is => 'ro',
);

has _external => (
    is => 'ro',
);

has _embedded => (
    is => 'ro',
);

sub resource_url( $self, $name ) {
    my $l = $self->_links;
    if( exists $l->{$name} ) {
        $l->{$name}->{href}
    }
}

sub resources( $self ) {
    sort keys %{ $self->_links }
}

sub fetch_resource_future( $self, $name, %options ) {
    my $class = $options{ class } || ref $self;
    my $ua = $self->ua;
    my $url = $self->resource_url( $name )
        or croak "Couldn't find resource '$name' in " . join ",", sort keys %{$self->_links};
    Future->done( $ua->get( $url ))->then( sub( $res ) {
        Future->done( bless { ua => $ua, %{ decode_json( $res->content )} } => $class );
    });
}

sub fetch_resource( $self, $name, %options ) {



( run in 0.567 second using v1.01-cache-2.11-cpan-5a3173703d6 )