File Coverage

blib/lib/RT/Client/REST/Attachment.pm
Criterion Covered Total %
statement 26 31 83.8
branch 1 2 50.0
condition n/a
subroutine 10 11 90.9
pod 3 3 100.0
total 40 47 85.1


line stmt bran cond sub pod time code
1             #!perl
2             # vim: softtabstop=4 tabstop=4 shiftwidth=4 ft=perl expandtab smarttab
3             # PODNAME: RT::Client::REST::Attachment
4             # ABSTRACT: attachment object representation.
5              
6 3     3   111005 use strict;
  3         19  
  3         98  
7 3     3   17 use warnings;
  3         5  
  3         193  
8              
9             package RT::Client::REST::Attachment;
10             $RT::Client::REST::Attachment::VERSION = '0.71';
11 3     3   487 use parent 'RT::Client::REST::Object';
  3         314  
  3         20  
12              
13 3     3   230 use Params::Validate qw(:types);
  3         7  
  3         550  
14 3     3   24 use RT::Client::REST::Object::Exception;
  3         7  
  3         44  
15              
16             sub _attributes {{
17 3     3   85 id => {
18             validation => {
19             type => SCALAR,
20             regex => qr/^\d+$/,
21             },
22             },
23              
24             creator_id => {
25             validation => {
26             type => SCALAR,
27             regex => qr/^\d+$/,
28             },
29             rest_name => 'Creator',
30             },
31              
32             parent_id => {
33             validation => {
34             type => SCALAR,
35             regex => qr/^\d+$/,
36             },
37             },
38              
39             subject => {
40             validation => {
41             type => SCALAR,
42             },
43             },
44              
45             content_type => {
46             validation => {
47             type => SCALAR,
48             },
49             rest_name => 'ContentType',
50             },
51              
52             file_name => {
53             validation => {
54             type => SCALAR,
55             },
56             rest_name => 'Filename',
57             },
58              
59             transaction_id => {
60             validation => {
61             type => SCALAR,
62             regex => qr/^\d+$/,
63             },
64             rest_name => 'Transaction',
65             },
66              
67             message_id => {
68             validation => {
69             type => SCALAR,
70             },
71             rest_name => 'MessageId',
72             },
73              
74             created => {
75             validation => {
76             type => SCALAR,
77             },
78             is_datetime => 1,
79             },
80              
81             content => {
82             validation => {
83             type => SCALAR,
84             },
85             },
86              
87             headers => {
88             validation => {
89             type => SCALAR,
90             },
91             },
92              
93             parent => {
94             validation => {
95             type => SCALAR,
96             },
97             },
98              
99             content_encoding => {
100             validation => {
101             type => SCALAR,
102             },
103             rest_name => 'ContentEncoding',
104             },
105             }}
106              
107 1     1 1 940 sub rt_type { 'attachment' }
108              
109             sub retrieve {
110 0     0 1 0 my $self = shift;
111              
112 0         0 $self->from_form(
113             $self->rt->get_attachment(
114             parent_id => $self->parent_id,
115             id => $self->id,
116             ),
117             );
118              
119 0         0 $self->{__dirty} = {};
120              
121 0         0 return $self;
122             }
123              
124             my @unsupported = qw(store search count);
125             # Override unsupported methods.
126             for my $method (@unsupported) {
127 3     3   1248 no strict 'refs'; ## no critic (ProhibitNoStrict)
  3         16  
  3         493  
128             *$method = sub {
129 6     6   5657 my $self = shift;
130 6         48 RT::Client::REST::Object::IllegalMethodException->throw(
131             ref($self) . " does not support '$method' method",
132             );
133             };
134             }
135              
136             # FIXME this is kind of horrible, probably functions should be provided via mixin?
137             sub can {
138 17     17 1 5860 my ($self, $method) = @_;
139 17 50       35 if (grep { $_ eq $method } @unsupported) {
  51         127  
140 0         0 return;
141             }
142 17         109 return $self->SUPER::can($method);
143             }
144              
145             __PACKAGE__->_generate_methods;
146              
147             1;
148              
149             __END__
150              
151             =pod
152              
153             =encoding UTF-8
154              
155             =head1 NAME
156              
157             RT::Client::REST::Attachment - attachment object representation.
158              
159             =head1 VERSION
160              
161             version 0.71
162              
163             =head1 SYNOPSIS
164              
165             my $attachments = $ticket->attachments;
166              
167             my $count = $attachments->count;
168             print "There are $count attachments.\n";
169              
170             my $iterator = $attachments->get_iterator;
171             while (my $att = &$iterator) {
172             print "Id: ", $att->id, "; Subject: ", $att->subject, "\n";
173             }
174              
175             =head1 DESCRIPTION
176              
177             An attachment is a second-class citizen, as it does not exist (at least
178             from the current REST protocol implementation) by itself. At the moment,
179             it is always associated with a ticket (see B<parent_id> attribute).
180             Thus, you will
181             rarely retrieve an attachment by itself; instead, you should use
182             C<attachments()> method of L<RT::Client::REST::Ticket> object to get
183             an iterator for all attachments for that ticket.
184              
185             =head1 ATTRIBUTES
186              
187             =over 2
188              
189             =item B<id>
190              
191             Numeric ID of the attachment.
192              
193             =item B<creator_id>
194              
195             Numeric ID of the user who created the attachment.
196              
197             =item B<parent_id>
198              
199             Numeric ID of the object the attachment is associated with. This is not
200             a proper attribute of the attachment as specified by REST -- it is simply
201             to store the ID of the L<RT::Client::REST::Ticket> object this attachment
202             belongs to.
203              
204             =item B<subject>
205              
206             Subject of the attachment.
207              
208             =item B<content_type>
209              
210             Content type.
211              
212             =item B<file_name>
213              
214             File name (if any).
215              
216             =item B<transaction_id>
217              
218             Numeric ID of the L<RT::Client::REST::Transaction> object this attachment
219             is associated with.
220              
221             =item B<message_id>
222              
223             Message ID.
224              
225             =item B<created>
226              
227             Time when the attachment was created
228              
229             =item B<content>
230              
231             Actual content of the attachment.
232              
233             =item B<headers>
234              
235             Headers (not parsed), if any.
236              
237             =item B<parent>
238              
239             Parent (not sure what this is yet).
240              
241             =item B<content_encoding>
242              
243             Content encoding, if any.
244              
245             =back
246              
247             =head1 METHODS
248              
249             B<RT::Client::REST::Attachment> is a read-only object, so you cannot
250             C<store()> it. Also, because it is a second-class citizen, you cannot
251             C<search()> or C<count()> it -- use C<attachments()> method provided
252             by L<RT::Client::REST::Ticket>.
253              
254             =over 2
255              
256             =item retrieve
257              
258             To retrieve an attachment, attributes B<id> and B<parent_id> must
259             be set.
260              
261             =back
262              
263             =head1 INTERNAL METHODS
264              
265             =over 2
266              
267             =item B<can>
268              
269             Wraps the normal I<can()> call, to exclude unsupported methods from parent.
270              
271             =item B<rt_type>
272              
273             Returns 'attachment'.
274              
275             =back
276              
277             =head1 CREATING ATTACHMENTS
278              
279             Currently RT does not allow creating attachments via their API.
280              
281             See L<https://rt-wiki.bestpractical.com/wiki/REST#Ticket_Attachment>
282              
283             =head1 SEE ALSO
284              
285             L<RT::Client::REST::Ticket>,
286             L<RT::Client::REST::SearchResult>.
287              
288             =head1 AUTHOR
289              
290             Dean Hamstead <dean@fragfest.com.au>
291              
292             =head1 COPYRIGHT AND LICENSE
293              
294             This software is copyright (c) 2022, 2020 by Dmitri Tikhonov.
295              
296             This is free software; you can redistribute it and/or modify it under
297             the same terms as the Perl 5 programming language system itself.
298              
299             =cut