File Coverage

blib/lib/WebService/Mattermost/V4/API/Object/Role/APIMethods.pm
Criterion Covered Total %
statement 12 23 52.1
branch 0 4 0.0
condition 0 3 0.0
subroutine 3 5 60.0
pod 3 3 100.0
total 18 38 47.3


line stmt bran cond sub pod time code
1             package WebService::Mattermost::V4::API::Object::Role::APIMethods;
2:

3: # ABSTRACT: Allows a result object to call the API. 4:
5: use Moo::Role;
6: use Types::Standard qw(HashRef Str);
7:
8: ################################################################################
9:
10: has available_api_methods => (is => 'rw', isa => HashRef, default => sub { {} });
11: has api_resource_name => (is => 'rw', isa => Str);
12:
13: ################################################################################
14:
15: sub call {
16: my $self = shift;
17: my $method = shift;
18: my $args = shift;
19:
20: if (my $where = $self->method_is_valid($method)) {
21: return $where->$method($self->id, $args);
22: }
23:
24: return $self->error_return("'${method}' is not available");
25: }
26:
27: sub set_available_api_methods {
28: my $self = shift;
29: my $methods = shift;
30:
31: my %available = map { $_ => 1 } @{$methods};
32:
33: return $self->available_api_methods(\%available);
34: }
35:
36: sub method_is_valid {
37: my $self = shift;
38: my $method = shift;
39:
40: my $where = $self->api_resource_name;
41: my $expected = $where ? $self->api->$where : $self;
42:
43: return $self->available_api_methods->{$method} && $expected->can($method) && $expected;
44: }
45:
46: ################################################################################
47:
48: 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.30
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) 2023 by Mike Jones.
123:
124: This is free software, licensed under:
125:
126: The MIT (X11) License
127:
128: =cut
129: