| 1 | package HAL::Resource; | ||||||||||||||||||||
| 2 | 10 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 10 | ▶ | use JSON 'decode_json'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 10 | ▶ | use Filter::signatures; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 10 | ▶ | no warnings 'experimental::signatures'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 10 | ▶ | use feature 'signatures'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 10 | ▶ | use Future; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 10 | ▶ | use Carp qw(croak); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 10 | our $VERSION = '0.57'; | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | HAL::Resource - wrap a HAL resource | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | my $ua = WWW::Mechanize->new(); | ||||||||||||||||||||
| 20 | my $res = $ua->get('https://api.example.com/'); | ||||||||||||||||||||
| 21 | my $r = HAL::Resource->new( | ||||||||||||||||||||
| 22 | ua => $ua, | ||||||||||||||||||||
| 23 | %{ decode_json( $res->decoded_content ) }, | ||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | =head1 ABOUT | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | This module is just a very thin wrapper for HAL resources. If you find this | ||||||||||||||||||||
| 29 | module useful, I'm very happy to spin it off into its own distribution. | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | =head1 SEE ALSO | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | L<Data::HAL> - similar to this module, but lacks a HTTP transfer facility and | ||||||||||||||||||||
| 34 | currently fails its test suite | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | L<HAL::Tiny> - a module to generate HAL JSON | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | L<WebAPI::DBIC::Resource::HAL> - an adapter to export DBIx::Class structures | ||||||||||||||||||||
| 39 | as HAL | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | Hypertext Application Language - L<https://en.wikipedia.org/wiki/Hypertext_Application_Language> | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | =cut | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 10 | has ua => ( | |||||||||||||||||||
| 46 | weaken => 1, | ||||||||||||||||||||
| 47 | is => 'ro', | ||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 10 | has _links => ( | |||||||||||||||||||
| 51 | is => 'ro', | ||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 10 | has _external => ( | |||||||||||||||||||
| 55 | is => 'ro', | ||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 10 | has _embedded => ( | |||||||||||||||||||
| 59 | is => 'ro', | ||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 0 | ▶ | sub resource_url( $self, $name ) { | ||||||||||||||||||
|
Subroutine
resource_url: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 63 | 0 | my $l = $self->_links; | |||||||||||||||||||
| 64 | 0 | ▶ | if( exists $l->{$name} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | $l->{$name}->{href} | ||||||||||||||||||||
| 66 | 0 | } | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 0 | ▶ | sub resources( $self ) { | ||||||||||||||||||
|
Subroutine
resources: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 70 | 0 | sort keys %{ $self->_links } | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 0 | ▶ | sub fetch_resource_future( $self, $name, %options ) { | ||||||||||||||||||
|
Subroutine
fetch_resource_future: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 74 | 0 | ▶ | my $class = $options{ class } || ref $self; | ||||||||||||||||||
|
Condition0%0/3
$options{'class'} || ref $self
MC/DC0%0/2
$options{'class'} || ref $self
$options{'class'}
ref $self
Truth table0%0/3
A || B
A= $options{'class'}B= ref $self
| |||||||||||||||||||||
| 75 | 0 | my $ua = $self->ua; | |||||||||||||||||||
| 76 | my $url = $self->resource_url( $name ) | ||||||||||||||||||||
| 77 | 0 | ▶ | or croak "Couldn't find resource '$name' in " . join ",", sort keys %{$self->_links}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | 0 | ▶ | Future->done( $ua->get( $url ))->then( sub( $res ) { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 79 | 0 | Future->done( bless { ua => $ua, %{ decode_json( $res->content )} } => $class ); | |||||||||||||||||||
| 80 | 0 | }); | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 0 | ▶ | sub fetch_resource( $self, $name, %options ) { | ||||||||||||||||||
|
Subroutine
fetch_resource: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 84 | 0 | $self->fetch_resource_future( $name, %options )->get | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 0 | ▶ | sub navigate_future( $self, %options ) { | ||||||||||||||||||
|
Subroutine
navigate_future: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||
| 88 | 0 | ▶ | $options{ class } ||= ref $self; | ||||||||||||||||||
|
Condition0%0/3
$options{"class"} ||= ref $self
MC/DC0%0/2
$options{"class"} ||= ref $self
$options{"class"}
ref $self
Truth table0%0/3
A ||= B
A= $options{"class"}B= ref $self
| |||||||||||||||||||||
| 89 | 0 | ▶ | my $path = delete $options{ path } || []; | ||||||||||||||||||
|
Condition0%0/2
delete $options{'path'} || []
MC/DC0%0/1
delete $options{'path'} || []
delete $options{'path'}
Truth table0%0/2
A
A= delete $options{'path'}
| |||||||||||||||||||||
| 90 | 0 | my $resource = Future->done( $self ); | |||||||||||||||||||
| 91 | 0 | for my $item (@$path) { | |||||||||||||||||||
| 92 | 0 | my $i = $item; | |||||||||||||||||||
| 93 | 0 | ▶ | $resource = $resource->then( sub( $r ) { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 94 | 0 | $r->fetch_resource_future( $i, %options ); | |||||||||||||||||||
| 95 | 0 | }); | |||||||||||||||||||
| 96 | }; | ||||||||||||||||||||
| 97 | 0 | $resource | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 0 | ▶ | sub navigate( $self, %options ) { | ||||||||||||||||||
|
Subroutine
navigate: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 101 | 0 | $self->navigate_future( %options )->get | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 0 | ▶ | sub inflate_list( $self, $class, $list ) { | ||||||||||||||||||
|
Subroutine
inflate_list: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 105 | 0 | my $ua = $self->ua; | |||||||||||||||||||
| 106 | map { | ||||||||||||||||||||
| 107 | 0 | $class->new( ua => $ua, %$_ ) | |||||||||||||||||||
| 108 | 0 | } @{ $list }; | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 10 | 1; | |||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | =head1 AUTHOR | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | Max Maischein, E<lt>corion@cpan.orgE<gt> | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | =head1 SEE ALSO | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | L<perl>, L<WWW::Mechanize>. | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | =head1 REPOSITORY | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | The public repository of this module is | ||||||||||||||||||||
| 124 | L<https://github.com/Corion/Finance-Bank-Postbank_de>. | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | =head1 SUPPORT | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | The public support forum of this module is | ||||||||||||||||||||
| 129 | L<https://perlmonks.org/>. | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | =head1 BUG TRACKER | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | Please report bugs in this module via the RT CPAN bug queue at | ||||||||||||||||||||
| 134 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=Finance-Bank-Postbank_de> | ||||||||||||||||||||
| 135 | or via mail to L<finance-bank-postbank_de-Bugs@rt.cpan.org>. | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | =head1 COPYRIGHT (c) | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | Copyright 2003-2019 by Max Maischein C<corion@cpan.org>. | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | =head1 LICENSE | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | This module is released under the same terms as Perl itself. | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =cut | ||||||||||||||||||||