Monzo.pm

filter: Statementstmt 94.0% 47 / 50 Branchbran 50.0% 4 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 94.1% 16 / 17 Podpod 83.3% 5 / 6 totaltotal 88.8% 72 / 81 CC 5 SCAR 3.8
CC
5
Cov
88%
__ANON__
2.1
transaction
2.1
transactions
2.0
CRAP 1.5
« Monzo/Currency.pm ↑ Summary Monzo/Merchant.pm »
1 package Business::Monzo;
2  
3 =head1 NAME
4  
5 Business::Monzo - Perl library for interacting with the Monzo API
6 (https://api.monzo.com)
7  
8 =for html
9 <a href='https://travis-ci.org/leejo/business-monzo?branch=master'><img src='https://travis-ci.org/leejo/business-monzo.svg?branch=master' alt='Build Status' /></a>
10 <a href='https://coveralls.io/r/leejo/business-monzo?branch=master'><img src='https://coveralls.io/repos/leejo/business-monzo/badge.png?branch=master' alt='Coverage Status' /></a>
11  
12 =head1 VERSION
13  
14 0.13
15  
16 =head1 DESCRIPTION
17  
18 Business::Monzo is a library for easy interface to the Monzo banking API,
19 it implements all of the functionality currently found in the service's API
20 documentation: L<https://monzo.com/docs>
21  
22 B<You should refer to the official Monzo API documentation in conjunction>
23 B<with this perldoc>, as the official API documentation explains in more depth
24 some of the functionality including required / optional parameters for certain
25 methods.
26  
27 Please note this library is very much a work in progress, as is the Monzo API.
28 Also note the Monzo were previously called Mondo, so if you see any references
29 to Mondo they are either typos or historical references that have not yet been
30 updated to reflect the changes.
31  
32 All objects within the Business::Monzo namespace are immutable. Calls to methods
33 will, for the most part, return new instances of objects.
34  
35 =head1 SYNOPSIS
36  
37 my $monzo = Business::Monzo->new(
38 token => $token, # REQUIRED
39 api_url => $url, # optional
40 );
41  
42 # transaction related information
43 my @transactions = $monzo->transactions( account_id => $account_id );
44  
45 my $Transaction = $monzo->transaction( id => 1 );
46  
47 $Transaction->annotate(
48 foo => 'bar',
49 baz => 'boz,
50 );
51  
52 my $annotations = $Transaction->annotations;
53  
54 # account related information
55 my @accounts = $monzo->accounts;
56  
57 foreach my $Account ( @accounts ) {
58  
59 my @transactions = $Account->transactions;
60  
61 $Account->add_feed_item(
62 params => {
63 title => 'My Feed Item',
64 image_url => 'http://...',
65 }
66 );
67  
68 # balance information
69 my $Balance = $Account->balance;
70  
71 # webhooks
72 my @webhooks = $Account->webhooks;
73  
74 my $Webhook = $Account->register_webhook(
75 callback_url => 'http://www.foo.com',
76 );
77  
78 $Webhook->delete
79 }
80  
81 # pots
82 my @pots = $monzo->pots();
83  
84 # attachments
85 my $Attachment = $monzo->upload_attachment(
86 file_name => 'foo.png',
87 file_type => 'image/png',
88 );
89  
90 $Attachment->register(
91 external_id => 'my_id'
92 );
93  
94 $Attachment->deregister;
95  
96 =head1 ERROR HANDLING
97  
98 Any problems or errors will result in a Business::Monzo::Exception
99 object being thrown, so you should wrap any calls to the library in the
100 appropriate error catching code (ideally a module from CPAN):
101  
102 try {
103 ...
104 }
105 catch ( Business::Monzo::Exception $e ) {
106 # error specific to Business::Monzo
107 ...
108 say $e->message; # error message
109 say $e->code; # HTTP status code
110 say $e->response; # HTTP status message
111  
112 # ->request may not always be present
113 say $e->request->{path} if $e->request;
114 say $e->request->{params} if $e->request;
115 say $e->request->{headers} if $e->request;
116 say $e->request->{content} if $e->request;
117 }
118 catch ( $e ) {
119 # some other failure?
120 ...
121 }
122  
123 You can view some useful debugging information by setting the MONZO_DEBUG
124 env varible, this will show the calls to the Monzo endpoints as well as a
125 stack trace in the event of exceptions:
126  
127 $ENV{MONZO_DEBUG} = 1;
128  
129 =cut
130  
131 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
132 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
133  
134 11 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
135 11 with 'Business::Monzo::Version';
136  
137 11 $Business::Monzo::VERSION = '0.13';
138  
139 11 ▶use Carp qw/ confess /;
Subroutine BEGIN: called CC=1 SCAR=0.0
140  
141 11 ▶use Business::Monzo::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
142 11 ▶use Business::Monzo::Account;
Subroutine BEGIN: called CC=1 SCAR=0.0
143 11 ▶use Business::Monzo::Pot;
Subroutine BEGIN: called CC=1 SCAR=0.0
144 11 ▶use Business::Monzo::Attachment;
Subroutine BEGIN: called CC=1 SCAR=0.0
145  
146 =head1 ATTRIBUTES
147  
148 =head2 token
149  
150 Your Monzo access token, this is required
151  
152 =head2 api_url
153  
154 The Monzo url, which will default to https://api.monzo.com
155  
156 =head2 client
157  
158 A Business::Monzo::Client object, this will be constructed for you so
159 you shouldn't need to pass this
160  
161 =cut
162  
163 11 has [ qw/ token / ] => (
164 is => 'ro',
165 required => 1,
166 );
167  
168 has api_url => (
169 is => 'ro',
170 required => 0,
171 1 ▶ default => sub { $Business::Monzo::API_URL },
Subroutine __ANON__: called CC=1 SCAR=0.0
172 11 );
173  
174 has client => (
175 is => 'ro',
176 1 ▶ isa => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
177 1 ▶ confess( "$_[0] is not a Business::Monzo::Client" )
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] ne "Business::Monzo::Client" 0 1 1
178 if ref $_[0] ne 'Business::Monzo::Client';
179 },
180 required => 0,
181 lazy => 1,
182 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
183 1 my ( $self ) = @_;
184  
185 # fix any load order issues with Resources requiring a Client
186 1 $Business::Monzo::Resource::client = Business::Monzo::Client->new(
187 token => $self->token,
188 api_url => $self->api_url,
189 );
190 },
191 11 );
192  
193 =head1 METHODS
194  
195 In the following %query_params refers to the possible query params as shown in
196 the Monzo API documentation. For example: limit=100.
197  
198 # transactions in the previous month
199 my @transactions = $monzo->transactions(
200 since => DateTime->now->subtract( months => 1 ),
201 );
202  
203 =cut
204  
205 =head2 transactions
206  
207 $monzo->transactions( %query_params );
208  
209 Get a list of transactions. Will return a list of L<Business::Monzo::Transaction>
210 objects. Note you must supply an account_id in the params hash;
211  
212 =cut
213  
214 1 ▶sub transactions {
Subroutine transactions: called CC=2 SCAR=7.1
Pod documented
215 1 my ( $self,%params ) = @_;
216  
217 # transactions requires account_id, whereas transaction doesn't
218 # the Monzo API is a little inconsistent at this point...
219 1 ▶ $params{account_id} || Business::Monzo::Exception->throw({
Branch: 1 branch
BranchTrueFalseTotal
$params{"account_id"} or "Business::Monzo::Exception"->throw({"message", "transactions requires params: account_id"}) 1 0 1
220 message => "transactions requires params: account_id",
221 });
222  
223 return Business::Monzo::Account->new(
224 client => $self->client,
225 id => $params{account_id},
226 1 )->transactions( 'expand[]' => 'merchant',%params );
227 }
228  
229 =head2 balance
230  
231 my $Balance = $monzo->balance( account_id => $account_id );
232  
233 Get an account balance Returns a L<Business::Monzo::Balance> object.
234  
235 =cut
236  
237 1 ▶sub balance {
Subroutine balance: called CC=2 SCAR=7.1
Pod documented
238 1 my ( $self,%params ) = @_;
239  
240 1 ▶ $params{account_id} || Business::Monzo::Exception->throw({
Branch: 1 branch
BranchTrueFalseTotal
$params{"account_id"} or "Business::Monzo::Exception"->throw({"message", "balance requires params: account_id"}) 1 0 1
241 message => "balance requires params: account_id",
242 });
243  
244 return Business::Monzo::Account->new(
245 client => $self->client,
246 id => $params{account_id},
247 1 )->balance( %params );
248 }
249  
250 =head2 transaction
251  
252 my $Transaction = $monzo->transaction(
253 id => $id,
254 expand => 'merchant'
255 );
256  
257 Get a transaction. Will return a L<Business::Monzo::Transaction> object
258  
259 =cut
260  
261 1 ▶sub transaction {
Subroutine transaction: called CC=2 SCAR=7.6
Pod documented
262 1 my ( $self,%params ) = @_;
263  
264 1 ▶ if ( my $expand = delete( $params{expand} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $expand = delete $params{"expand"}) 0 1 1
265 0 $params{'expand[]'} = $expand;
266 }
267  
268 1 return $self->client->_get_transaction( \%params );
269 }
270  
271 =head2 accounts
272  
273 $monzo->accounts; # all accounts
274 $monzo->accounts( account_type => "uk_prepaid" ); # prepaid accounts
275 $monzo->accounts( account_type => "uk_retail" ); # current accounts
276  
277 Get a list of accounts. Will return a list of L<Business::Monzo::Account>
278 objects
279  
280 =cut
281  
282 1 ▶sub accounts {
Subroutine accounts: called CC=1 SCAR=0.0
Pod documented
283 1 my ( $self,%params ) = @_;
284 1 return $self->client->_get_accounts( \%params );
285 }
286  
287 =head2 pots
288  
289 $monzo->pots;
290  
291 Get a list of pots. Will return a list of L<Business::Monzo::Pot>
292 objects
293  
294 =cut
295  
296 0 ▶sub pots {
Subroutine pots: not called CC=1 SCAR=6.9
Pod documented
297 0 my ( $self ) = @_;
298 0 return $self->client->_get_pots;
299 }
300  
301 1 ▶sub upload_attachment {
Subroutine upload_attachment: called CC=1 SCAR=0.0
Pod undocumented
302 1 my ( $self,%params ) = @_;
303  
304 1 return Business::Monzo::Attachment->new(
305 client => $self->client,
306 )->upload( %params );
307 }
308  
309 =head1 PAGINATION
310  
311 As per the Monzo docs: L<https://monzo.com/docs/#pagination> - you can pass
312 through arguments to the methods (e.g. C<transactions>) to limit the return
313 data or set date ranges, etc:
314  
315 # last three months transactions, but only show 5
316 my $since = DateTime->now->subtract( months => 3 )->iso8601 . "Z";
317 my $limit = 5;
318  
319 foreach my $transaction (
320 $monzo->transactions(
321 account_id => $account_id,
322 limit => $limit,
323 since => $since,
324 )
325 {
326 ...
327 }
328  
329 The supported pagination keys are C<limit>, C<since>, and C<before> - where
330 C<since> can be an RFC 3339-encoded timestamp or an object id, and C<before>
331 can be an RFC 3339-encoded timestamp. C<limit> should always be an integer.
332  
333 =head1 EXAMPLES
334  
335 See the t/002_end_to_end.t test included with this distribution. you can run
336 this test against the Monzo emulator by running end_to_end_emulated.sh (this
337 is advised, don't run it against a live endpoint).
338  
339 You can also see the scripts in the bin/ directory included in this dist for
340 more examples.
341  
342 =head1 SEE ALSO
343  
344 L<Business::Monzo::Account>
345  
346 L<Business::Monzo::Attachment>
347  
348 L<Business::Monzo::Balance>
349  
350 L<Business::Monzo::Transaction>
351  
352 L<Business::Monzo::Webhook>
353  
354 =head1 AUTHOR
355  
356 Lee Johnson - C<leejo@cpan.org>
357  
358 With contributions from:
359  
360 Chris Merry
361 Aaron Moses
362 Dave Cross
363  
364 =head1 LICENSE
365  
366 This library is free software; you can redistribute it and/or modify it under
367 the same terms as Perl itself. If you would like to contribute documentation,
368 features, bug fixes, or anything else then please raise an issue / pull request:
369  
370 https://github.com/leejo/business-monzo
371  
372 =cut
373  
374 11 1;
375  
376 # vim: ts=4:sw=4:et
« Monzo/Currency.pm ↑ Summary Monzo/Merchant.pm »