File Coverage

blib/lib/WebService/Mattermost/V4/API/Resource/Posts.pm
Criterion Covered Total %
statement 11 20 55.0
branch 1 4 25.0
condition 1 6 16.6
subroutine 3 5 60.0
pod 2 2 100.0
total 18 37 48.6


line stmt bran cond sub pod time code
1             package WebService::Mattermost::V4::API::Resource::Posts;
2:

3: # ABSTRACT: Wrapped API methods for the posts API endpoints. 4:
5: use Moo;
6:
7: extends 'WebService::Mattermost::V4::API::Resource';
8: with 'WebService::Mattermost::V4::API::Resource::Role::View::Post';
9:
10: ################################################################################
11:
12: sub create {
13: my $self = shift;
14: my $args = shift;
15:
16: $args->{message} = $self->_stringify_message($args->{message});
17:
18: return $self->_post({
19: parameters => $args,
20: required => [ qw(channel_id message) ],
21: });
22: }
23:
24: sub create_ephemeral {
25: my $self = shift;
26: my $args = shift;
27:
28: unless ($self->_validate_minimum_post_args($args->{post})) {
29: return $self->error_return('The "post" argument must contain message and channel_id keys');
30: }
31:
32: $args->{post}->{message} = $self->_stringify_message($args->{post}->{message});
33:
34: return $self->_post({
35: endpoint => 'ephemeral',
36: parameters => $args,
37: required => [ qw(user_id post) ],
38: });
39: }
40:
41: ################################################################################
42:
43: sub _stringify_message {
44: my $self = shift;
45: my $message = shift;
46:
47: # This catch is in place to ensure a message of 1 or 0 are not interpreted
48: # as a boolean value, and are instead sent as a string
49: $message .= '' if $message && $message =~ /^\d+$/;
50:
51: return $message;
52: }
53:
54: sub _validate_minimum_post_args {
55: my $self = shift;
56: my $post = shift;
57:
58: return ref $post
59: && ref $post eq 'HASH'
60: && $post->{channel_id}
61: && $post->{message};
62: }
63:
64: ################################################################################
65:
66: 1;
67:
68: __END__
69:
70: =pod
71:
72: =encoding UTF-8
73:
74: =head1 NAME
75:
76: WebService::Mattermost::V4::API::Resource::Posts - Wrapped API methods for the posts API endpoints.
77:
78: =head1 VERSION
79:
80: version 0.30
81:
82: =head1 DESCRIPTION
83:
84: =head2 USAGE
85:
86: use WebService::Mattermost;
87:
88: my $mm = WebService::Mattermost->new({
89: authenticate => 1,
90: username => 'email@address.com',
91: password => 'passwordhere',
92: base_url => 'https://my.mattermost.server.com/api/v4/',
93: });
94:
95: my $resource = $mm->api->posts;
96:
97: =head2 METHODS
98:
99: =over 4
100:
101: =item C<create()>
102:
103: L<Create a post|https://api.mattermost.com/#tag/posts%2Fpaths%2F~1posts%2Fpost>
104:
105: my $response = $resource->create({
106: # Required parameters:
107: message => '...',
108: channel_id => 'CHANNEL-ID-HERE',
109:
110: # Optional parameters:
111: root_id => 'PARENT-POST-ID-HERE',
112: file_ids => [ '...' ],
113: props => {},
114: });
115:
116: =item C<create_ephemeral()>
117:
118: L<Create a ephemeral post|https://api.mattermost.com/#tag/posts%2Fpaths%2F~1posts~1ephemeral%2Fpost>
119:
120: my $response = $resource->create_ephemeral({
121: # Required parameters:
122: user_id => 'USER-ID-HERE',
123: post => {
124: channel_id => '...',
125: message => '...',
126: },
127: });
128:
129: =back
130:
131: =head1 SEE ALSO
132:
133: =over 4
134:
135: =item L<https://api.mattermost.com/#tag/posts>
136:
137: Official "posts" API documentation.
138:
139: =back
140:
141: =head1 AUTHOR
142:
143: Mike Jones <mike@netsplit.org.uk>
144:
145: =head1 COPYRIGHT AND LICENSE
146:
147: This software is Copyright (c) 2023 by Mike Jones.
148:
149: This is free software, licensed under:
150:
151: The MIT (X11) License
152:
153: =cut
154: