File Coverage

blib/lib/Business/Mondo/Attachment.pm
Criterion Covered Total %
statement 33 34 97.0
branch 4 6 66.6
condition 9 15 60.0
subroutine 9 9 100.0
pod 3 3 100.0
total 58 67 86.5


line stmt bran cond sub pod time code
1             package Business::Mondo::Attachment;
2              
3             =head1 NAME
4              
5             Business::Mondo::Attachment
6              
7             =head1 DESCRIPTION
8              
9             A class for a Mondo attachment, extends L<Business::Mondo::Resource>
10              
11             =cut
12              
13 10     10   33 use strict;
  10         12  
  10         230  
14 10     10   31 use warnings;
  10         12  
  10         195  
15              
16 10     10   32 use Moo;
  10         9  
  10         69  
17             extends 'Business::Mondo::Resource';
18             with 'Business::Mondo::Utils';
19              
20 10     10   2588 use Types::Standard qw/ :all /;
  10         20  
  10         69  
21 10     10   246249 use DateTime::Format::DateParse;
  10         2644338  
  10         319  
22 10     10   67 use Business::Mondo::Exception;
  10         13  
  10         3038  
23              
24             =head1 ATTRIBUTES
25              
26             The Attachment class has the following attributes (with their type).
27              
28             id (Str)
29             user_id (Str)
30             external_id (Str)
31             upload_url (Str)
32             file_name (Str)
33             file_url (Str)
34             file_type (Str)
35             created (DateTime)
36              
37             Note that when when a Str is passed to ->created this will be coerced
38             to a DateTime object.
39              
40             =cut
41              
42             has [ qw/
43             id
44             user_id
45             external_id
46             upload_url
47             file_name
48             file_url
49             file_type
50             / ] => (
51             is => 'ro',
52             isa => Str,
53             );
54              
55             has created => (
56             is => 'ro',
57             isa => Maybe[InstanceOf['DateTime']],
58             coerce => sub {
59             my ( $args ) = @_;
60              
61             if ( ! ref( $args ) ) {
62             $args = DateTime::Format::DateParse->parse_datetime( $args );
63             }
64              
65             return $args;
66             },
67             );
68              
69             =head1 Operations on an attachment
70              
71             =head2 upload
72              
73             Gets the upload_url and file_url for the given file_name and file_type.
74             Returns a new L<Business::Mondo::Attachment> object with the attributes
75             file_name, file_type, file_url, and upload_url populated. Note the
76             required parameters:
77              
78             my $file_details = $Attachment->upload(
79             file_name => 'foo.png', # REQUIRED
80             file_type => 'image/png', # REQUIRED
81             );
82              
83             TODO: should probably make this easier - upload should actually upload a given
84             file (file handle?)
85              
86             =cut
87              
88             sub upload {
89 3     3 1 1674 my ( $self,%params ) = @_;
90              
91             $params{file_name} && $params{file_type} ||
92 3 100 66     90 Business::Mondo::Exception->throw({
93             message => "upload requires params: file_name, file_type",
94             });
95              
96             my $data = $self->client->api_post( 'attachment/upload',{
97             file_name => $params{file_name},
98             file_type => $params{file_type},
99 2         18 } );
100              
101             return $self->new(
102             client => $self->client,
103             file_name => $params{file_name},
104             file_type => $params{file_type},
105 2         187 %{ $data },
  2         47  
106             );
107             }
108              
109             =head2 register
110              
111             Registers an attachment against an entity (transaction, etc). Returns
112             a new Business::Mondo::Attachment object with the details populated
113              
114             my $file_details = $Attachment->webhooks(
115             # the following are REQUIRED if not set on $Attachment
116             file_url => 'http://www.example.com/foo.png', # REQUIRED
117             file_type => 'image/png', # REQUIRED
118              
119             # one of the following REQUIRED:
120             external_id => $id,
121             entity => $object # Business::Mondo:: - Transaction, Account, etc
122             );
123              
124             =cut
125              
126             sub register {
127 2     2 1 2867 my ( $self,%params ) = @_;
128              
129 2 50       7 if ( $params{entity} ) {
130 0         0 $params{external_id} = $params{entity}->id;
131             }
132              
133 2   66     9 $params{file_url} //= $self->file_url;
134 2   66     7 $params{file_type} //= $self->file_type;
135              
136             $params{external_id} && $params{file_url} && $params{file_type} ||
137 2 50 66     14 Business::Mondo::Exception->throw({
      33        
138             message => "register requires params: external_id, file_name, file_type",
139             });
140              
141             my $data = $self->client->api_post( 'attachment/register',{
142             external_id => $params{external_id},
143             file_url => $params{file_url},
144             file_type => $params{file_type},
145 1         5 } );
146              
147             return $self->new(
148             client => $self->client,
149             file_name => $self->file_name,
150             upload_url => $self->upload_url,
151 1         11 %{ $data->{attachment} },
  1         23  
152             );
153             }
154              
155             =head2 deregister
156              
157             Removes an attachment
158              
159             $attachment->deregister;
160              
161             =cut
162              
163             sub deregister {
164 1     1 1 383 my ( $self ) = @_;
165              
166 1         7 return $self->client->api_post( 'attachment/deregister',{
167             id => $self->id
168             } );
169             }
170              
171             =head1 SEE ALSO
172              
173             L<Business::Mondo>
174              
175             L<Business::Mondo::Resource>
176              
177             =head1 AUTHOR
178              
179             Lee Johnson - C<leejo@cpan.org>
180              
181             =head1 LICENSE
182              
183             This library is free software; you can redistribute it and/or modify it under
184             the same terms as Perl itself. If you would like to contribute documentation,
185             features, bug fixes, or anything else then please raise an issue / pull request:
186              
187             https://github.com/leejo/business-mondo
188              
189             =cut
190              
191             1;
192              
193             # vim: ts=4:sw=4:et