Mattermost/V4/API/Object/Role/APIMethods.pm

filter: Statementstmt 59.2% 16 / 27 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 66.6% 4 / 6 Podpod 100% 3 / 3 totaltotal 46.9% 23 / 49 CC 5 SCAR 22.6
CC
5
Cov
43%
method_is_valid
20.0
call
6.0
BEGIN
1.0
CRAP 9.6
« Mattermost/V4/API/Resource/Channels.pm ↑ Summary Mattermost/V4/API/Resource/Posts.pm »
1 package WebService::Mattermost::V4::API::Object::Role::APIMethods;
2  
3 # ABSTRACT: Allows a result object to call the API.
4  
5 12 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Types::Standard qw(HashRef Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 ################################################################################
9  
10 2 ▶has available_api_methods => (is => 'rw', isa => HashRef, default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
11 12 has api_resource_name => (is => 'rw', isa => Str);
12  
13 ################################################################################
14  
15 sub call {
16 0 ▶ my $self = shift;
Subroutine call: not called CC=2 SCAR=17.9
Pod documented
17 0 my $method = shift;
18 0 my $args = shift;
19  
20 0 ▶ if (my $where = $self->method_is_valid($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $where = $self->method_is_valid($method)) 0 0 0
21 0 return $where->$method($self->id, $args);
22 }
23  
24 0 return $self->error_return("'${method}' is not available");
25 }
26  
27 sub set_available_api_methods {
28 2 ▶ my $self = shift;
Subroutine set_available_api_methods: called CC=1 SCAR=0.0
Pod documented
29 2 my $methods = shift;
30  
31 2 my %available = map { $_ => 1 } @{$methods};
32  
33 2 return $self->available_api_methods(\%available);
34 }
35  
36 sub method_is_valid {
37 0 ▶ my $self = shift;
Subroutine method_is_valid: not called CC=4 SCAR=30.0
Pod documented
38 0 my $method = shift;
39  
40 0 my $where = $self->api_resource_name;
41 0 ▶ my $expected = $where ? $self->api->$where : $self;
Branch: 1 branch
BranchTrueFalseTotal
$where ? : 0 0 0
42  
43 0 ▶ return $self->available_api_methods->{$method} && $expected->can($method) && $expected;
Condition0%0/6
$self->available_api_methods->{$method} && $expected->can($method) && $expected
!ll&&!rl&&r
000
$self->available_api_methods->{$method} and $expected->can($method)
!ll&&!rl&&r
000
MC/DC0%0/3
$self->available_api_methods->{$method} && $expected->can($method) && $expected
$self->available_api_methods->{$method} $expected->can($method) $expected
Truth table0%0/4
A and B && C
A= $self->available_api_methods->{$method}B= $expected->can($method)C= $expected
ABCresult
0XX0
10X0
1100
1111
44 }
45  
46 ################################################################################
47  
48 12 1;
49  
50 __END__
51  
52 =pod
53  
54 =encoding UTF-8
55  
56 =head1 NAME
57  
58 WebService::Mattermost::V4::API::Object::Role::APIMethods - Allows a result object to call the API.
59  
60 =head1 VERSION
61  
62 version 0.28
63  
64 =head1 DESCRIPTION
65  
66 Mark methods as available for use from a result object.
67  
68 =head2 USAGE
69  
70 package SomeResultObj;
71  
72 use Moo;
73  
74 extends 'WebService::Mattermost::V4::API::Object';
75 with 'WebService::Mattermost::V4::API::Object::Role::APIMethods';
76  
77 sub BUILD {
78 my $self = shift;
79  
80 # e.g. user
81 $self->api_resource_name('lower case name of the API resource');
82  
83 $self->set_available_api_methods([ qw(
84 method_name
85 another_method_name
86 yet_another_method_name
87 ) ]);
88 }
89  
90 1;
91  
92 =head1 METHODS
93  
94 =over 4
95  
96 =item C<call()>
97  
98 Call an API method which is available to this class.
99  
100 $object->call('method_name', {
101 some => 'arguments',
102 });
103  
104 =item C<set_available_api_methods()>
105  
106 $self->set_available_api_methods([ qw(foo bar baz) ]);
107  
108 =item C<method_is_valid()>
109  
110 Checks whether the given method is set as permitted.
111  
112 my $valid = $self->method_is_valid('foo'); # 1 or 0
113  
114 =back
115  
116 =head1 AUTHOR
117  
118 Mike Jones <mike@netsplit.org.uk>
119  
120 =head1 COPYRIGHT AND LICENSE
121  
122 This software is Copyright (c) 2020 by Mike Jones.
123  
124 This is free software, licensed under:
125  
126 The MIT (X11) License
127  
128 =cut
« Mattermost/V4/API/Resource/Channels.pm ↑ Summary Mattermost/V4/API/Resource/Posts.pm »