| 1 | package Discord::Client::Model; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use v5.26; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use JSON::PP (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Future; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Discord::Client::Error; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 5 | our $VERSION = '0.002'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 31 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 31 | my ( $class, %args ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # Require a resource identity before constructing a detached JSON snapshot. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | croak 'Model data requires an ID' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | unless ref( $args{data} ) eq 'HASH' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | && defined( $args{data}{id} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | && !ref( $args{data}{id} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 31 | ▶ | && $args{data}{id} =~ /\A\d{1,20}\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%6/9
ref $args{"data"} eq "HASH" and defined $args{"data"}{"id"} and not ref $args{"data"}{"id"} and $args{"data"}{"id"} =~ /\A\d{1,20}\z/u
ref $args{"data"} eq "HASH" and defined $args{"data"}{"id"} and not ref $args{"data"}{"id"}
ref $args{"data"} eq "HASH" and defined $args{"data"}{"id"}
MC/DC25%1/4
ref $args{"data"} eq "HASH" and defined $args{"data"}{"id"} and not ref $args{"data"}{"id"} and $args{"data"}{"id"} =~ /\A\d{1,20}\z/u
ref $args{"data"} eq "HASH"
defined $args{"data"}{"id"}
not ref $args{"data"}{"id"}
$args{"data"}{"id"} =~ /\A\d{1,20}\z/u
Truth table40%2/5
A and B and not(C) and D
A= ref $args{"data"} eq "HASH"B= defined $args{"data"}{"id"}C= not ref $args{"data"}{"id"}D= $args{"data"}{"id"} =~ /\A\d{1,20}\z/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 30 | my $data = eval { _clone( $args{data} ) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 30 | ▶ | croak 'Model data must be JSON encodable' if $@; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 30 | $data->{id} = "$data->{id}"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 30 | return bless { data => $data, rest => $args{rest} }, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 64 | ▶ | sub _clone { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_clone: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 64 | my ($data) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # Support scalar field values as well as nested resource data. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 64 | my $json = JSON::PP->new->allow_nonref; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 64 | return $json->decode( $json->encode($data) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 20 | ▶ | sub id { $_[0]->{data}{id} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | ▶ | sub raw { _clone( $_[0]->{data} ) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
raw: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 33 | ▶ | sub get { _clone( $_[0]->{data}{ $_[1] } ) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 3 | ▶ | sub rest { $_[0]->{rest} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rest: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 31 | ▶ | sub wrap { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 31 | my ( $class, $type, $data, %args ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # Load only known model classes; caller input never becomes an arbitrary module path. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 31 | my %classes = ( message => 'Message', channel => 'Channel', guild => 'Guild', user => 'User' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 31 | ▶ | croak 'Unknown model type' unless defined($type) && !ref($type) && $classes{$type}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $type and not ref $type and $classes{$type}
defined $type and not ref $type
MC/DC0%0/3
defined $type and not ref $type and $classes{$type}
defined $type
not ref $type
$classes{$type}
Truth table25%1/4
A and not(B) and C
A= defined $typeB= not ref $typeC= $classes{$type}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 31 | my $package = 'Discord::Client::Model::' . $classes{$type}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 31 | ( my $file = "$package.pm" ) =~ s{::}{/}g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 31 | require $file; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 31 | return $package->new( data => $data, %args ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 12 | ▶ | sub _call { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_call: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 12 | my ( $self, $method, @args ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # Read-only snapshots fail asynchronous operations without constructing a hidden client. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 12 | ▶ | return $self->_failed( 'Model operation requires a REST client', 'state' ) unless $self->{rest}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 11 | return $self->{rest}->$method(@args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 9 | ▶ | sub _model_future { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_model_future: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 9 | my ( $self, $type, $future ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | # Wrap successful responses without mutating the original resource snapshot. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | return $future->then( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 7 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 7 | my ($data) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 7 | my $model = eval { __PACKAGE__->wrap( $type, $data, rest => $self->{rest} ) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 7 | ▶ | return $self->_failed( 'Discord returned invalid model data', 'protocol' ) unless $model; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 6 | return Future->done($model); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 9 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 2 | ▶ | sub _failed { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_failed: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | my ( $self, $message, $category ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 2 | my $error = Discord::Client::Error->new( message => $message, category => $category ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 2 | return Future->fail( $message, $category, $error ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||