Monzo/Attachment.pm

filter: Statementstmt 97.6% 42 / 43 Branchbran 75.0% 6 / 8 Conditioncond 66.6% 10 / 15 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 82.9% 73 / 88 CC 10 SCAR 13.6
CC
10
Cov
88%
register
7.7
upload
3.0
__ANON__
2.0
CRAP 3.9
« Monzo/Client.pm ↑ Summary Monzo/Resource.pm »
1 package Business::Monzo::Attachment;
2  
3 =head1 NAME
4  
5 Business::Monzo::Attachment
6  
7 =head1 DESCRIPTION
8  
9 A class for a Monzo attachment, extends L<Business::Monzo::Resource>
10  
11 =cut
12  
13 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 11 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 11 extends 'Business::Monzo::Resource';
18 11 with 'Business::Monzo::Utils';
19  
20 11 ▶use Types::Standard qw/ :all /;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 11 ▶use DateTime::Format::DateParse;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 11 ▶use Business::Monzo::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 11 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 6 ▶ coerce => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
59 6 my ( $args ) = @_;
60  
61 6 ▶ if ( ! ref( $args ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $args) 6 0 6
62 6 $args = DateTime::Format::DateParse->parse_datetime( $args );
63 }
64  
65 6 return $args;
66 },
67 11 );
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::Monzo::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 3 ▶sub upload {
Subroutine upload: called CC=3 SCAR=11.0
Pod documented
89 3 my ( $self,%params ) = @_;
90  
91 $params{file_name} && $params{file_type} ||
92 3 ▶ Business::Monzo::Exception->throw({
Branch: 1 branch
BranchTrueFalseTotal
$params{"file_name"} and $params{"file_type"} or "Business::Monzo::Exception"->throw({"message", "upload requires params: file_name, file_type"}) 2 1 3
Condition66%2/3
$params{"file_name"} and $params{"file_type"}
!ll&&!rl&&r
102
MC/DC50%1/2
$params{"file_name"} and $params{"file_type"}
$params{"file_name"} $params{"file_type"}
Truth table66%2/3
A and B
A= $params{"file_name"}B= $params{"file_type"}
ABresult
0X0
100
111
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 } );
100  
101 return $self->new(
102 client => $self->client,
103 file_name => $params{file_name},
104 file_type => $params{file_type},
105 2 %{ $data },
106 );
107 }
108  
109 =head2 register
110  
111 Registers an attachment against an entity (transaction, etc). Returns
112 a new Business::Monzo::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::Monzo:: - Transaction, Account, etc
122 );
123  
124 =cut
125  
126 2 ▶sub register {
Subroutine register: called CC=7 SCAR=20.4
Pod documented
127 2 my ( $self,%params ) = @_;
128  
129 2 ▶ if ( $params{entity} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"entity"}) 0 2 2
130 0 $params{external_id} = $params{entity}->id;
131 }
132  
133 2 ▶ $params{file_url} //= $self->file_url;
Condition66%2/3
$params{"file_url"} //= $self->file_url
l!l&&r!l&&!r
110
MC/DC0%0/2
$params{"file_url"} //= $self->file_url
$params{"file_url"} $self->file_url
Truth table66%2/3
A //= B
A= $params{"file_url"}B= $self->file_url
ABresult
1X1
011
000
134 2 ▶ $params{file_type} //= $self->file_type;
Condition66%2/3
$params{"file_type"} //= $self->file_type
l!l&&r!l&&!r
110
MC/DC0%0/2
$params{"file_type"} //= $self->file_type
$params{"file_type"} $self->file_type
Truth table66%2/3
A //= B
A= $params{"file_type"}B= $self->file_type
ABresult
1X1
011
000
135  
136 $params{external_id} && $params{file_url} && $params{file_type} ||
137 2 ▶ Business::Monzo::Exception->throw({
Branch: 1 branch
BranchTrueFalseTotal
$params{"external_id"} and $params{"file_url"} and $params{"file_type"} or "Business::Monzo::Exception"->throw({"message", "register requires params: external_id, file_name, file_type"}) 1 1 2
Condition66%4/6
$params{"external_id"} and $params{"file_url"} and $params{"file_type"}
!ll&&!rl&&r
101
$params{"external_id"} and $params{"file_url"}
!ll&&!rl&&r
101
MC/DC33%1/3
$params{"external_id"} and $params{"file_url"} and $params{"file_type"}
$params{"external_id"} $params{"file_url"} $params{"file_type"}
Truth table50%2/4
A and B and C
A= $params{"external_id"}B= $params{"file_url"}C= $params{"file_type"}
ABCresult
0XX0
10X0
1100
1111
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 } );
146  
147 return $self->new(
148 client => $self->client,
149 file_name => $self->file_name,
150 upload_url => $self->upload_url,
151 1 %{ $data->{attachment} },
152 );
153 }
154  
155 =head2 deregister
156  
157 Removes an attachment
158  
159 $attachment->deregister;
160  
161 =cut
162  
163 1 ▶sub deregister {
Subroutine deregister: called CC=1 SCAR=0.0
Pod documented
164 1 my ( $self ) = @_;
165  
166 1 return $self->client->api_post( 'attachment/deregister',{
167 id => $self->id
168 } );
169 }
170  
171 =head1 SEE ALSO
172  
173 L<Business::Monzo>
174  
175 L<Business::Monzo::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-monzo
188  
189 =cut
190  
191 11 1;
192  
193 # vim: ts=4:sw=4:et
« Monzo/Client.pm ↑ Summary Monzo/Resource.pm »