File Coverage

blib/lib/Business/Monzo.pm
Criterion Covered Total %
statement 34 35 97.1
branch 3 6 50.0
condition n/a
subroutine 12 12 100.0
pod 4 5 80.0
total 53 58 91.3


line stmt bran cond sub pod time code
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.11
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             # attachments
82             my $Attachment = $monzo->upload_attachment(
83             file_name => 'foo.png',
84             file_type => 'image/png',
85             );
86              
87             $Attachment->register(
88             external_id => 'my_id'
89             );
90              
91             $Attachment->deregister;
92              
93             =head1 ERROR HANDLING
94              
95             Any problems or errors will result in a Business::Monzo::Exception
96             object being thrown, so you should wrap any calls to the library in the
97             appropriate error catching code (ideally a module from CPAN):
98              
99             try {
100             ...
101             }
102             catch ( Business::Monzo::Exception $e ) {
103             # error specific to Business::Monzo
104             ...
105             say $e->message; # error message
106             say $e->code; # HTTP status code
107             say $e->response; # HTTP status message
108              
109             # ->request may not always be present
110             say $e->request->{path} if $e->request;
111             say $e->request->{params} if $e->request;
112             say $e->request->{headers} if $e->request;
113             say $e->request->{content} if $e->request;
114             }
115             catch ( $e ) {
116             # some other failure?
117             ...
118             }
119              
120             You can view some useful debugging information by setting the MONZO_DEBUG
121             env varible, this will show the calls to the Monzo endpoints as well as a
122             stack trace in the event of exceptions:
123              
124             $ENV{MONZO_DEBUG} = 1;
125              
126             =cut
127              
128 10     10   104326 use strict;
  10         32  
  10         496  
129 10     10   80 use warnings;
  10         28  
  10         758  
130              
131 10     10   807 use Moo;
  10         17386  
  10         94  
132             with 'Business::Monzo::Version';
133              
134             $Business::Monzo::VERSION = '0.11';
135              
136 10     10   7359 use Carp qw/ confess /;
  10         36  
  10         788  
137              
138 10     10   908 use Business::Monzo::Client;
  10         37  
  10         389  
139 10     10   84 use Business::Monzo::Account;
  10         32  
  10         373  
140 10     10   78 use Business::Monzo::Attachment;
  10         29  
  10         5902  
141              
142             =head1 ATTRIBUTES
143              
144             =head2 token
145              
146             Your Monzo access token, this is required
147              
148             =head2 api_url
149              
150             The Monzo url, which will default to https://api.monzo.com
151              
152             =head2 client
153              
154             A Business::Monzo::Client object, this will be constructed for you so
155             you shouldn't need to pass this
156              
157             =cut
158              
159             has [ qw/ token / ] => (
160             is => 'ro',
161             required => 1,
162             );
163              
164             has api_url => (
165             is => 'ro',
166             required => 0,
167             default => sub { $Business::Monzo::API_URL },
168             );
169              
170             has client => (
171             is => 'ro',
172             isa => sub {
173             confess( "$_[0] is not a Business::Monzo::Client" )
174             if ref $_[0] ne 'Business::Monzo::Client';
175             },
176             required => 0,
177             lazy => 1,
178             default => sub {
179             my ( $self ) = @_;
180              
181             # fix any load order issues with Resources requiring a Client
182             $Business::Monzo::Resource::client = Business::Monzo::Client->new(
183             token => $self->token,
184             api_url => $self->api_url,
185             );
186             },
187             );
188              
189             =head1 METHODS
190              
191             In the following %query_params refers to the possible query params as shown in
192             the Monzo API documentation. For example: limit=100.
193              
194             # transactions in the previous month
195             my @transactions = $monzo->transactions(
196             since => DateTime->now->subtract( months => 1 ),
197             );
198              
199             =cut
200              
201             =head2 transactions
202              
203             $monzo->transactions( %query_params );
204              
205             Get a list of transactions. Will return a list of L<Business::Monzo::Transaction>
206             objects. Note you must supply an account_id in the params hash;
207              
208             =cut
209              
210             sub transactions {
211 1     1 1 5 my ( $self,%params ) = @_;
212              
213             # transactions requires account_id, whereas transaction doesn't
214             # the Monzo API is a little inconsistent at this point...
215 1 50       10 $params{account_id} || Business::Monzo::Exception->throw({
216             message => "transactions requires params: account_id",
217             });
218              
219             return Business::Monzo::Account->new(
220             client => $self->client,
221             id => $params{account_id},
222 1         30 )->transactions( 'expand[]' => 'merchant' );
223             }
224              
225             =head2 balance
226              
227             my $Balance = $monzo->balance( account_id => $account_id );
228              
229             Get an account balance Returns a L<Business::Monzo::Balance> object.
230              
231             =cut
232              
233             sub balance {
234 1     1 1 428 my ( $self,%params ) = @_;
235              
236 1 50       6 $params{account_id} || Business::Monzo::Exception->throw({
237             message => "balance requires params: account_id",
238             });
239              
240             return Business::Monzo::Account->new(
241             client => $self->client,
242             id => $params{account_id},
243 1         33 )->balance( %params );
244             }
245              
246             =head2 transaction
247              
248             my $Transaction = $monzo->transaction(
249             id => $id,
250             expand => 'merchant'
251             );
252              
253             Get a transaction. Will return a L<Business::Monzo::Transaction> object
254              
255             =cut
256              
257             sub transaction {
258 1     1 1 1374 my ( $self,%params ) = @_;
259              
260 1 50       8 if ( my $expand = delete( $params{expand} ) ) {
261 0         0 $params{'expand[]'} = $expand;
262             }
263              
264 1         33 return $self->client->_get_transaction( \%params );
265             }
266              
267             =head2 accounts
268              
269             $monzo->accounts;
270              
271             Get a list of accounts. Will return a list of L<Business::Monzo::Account>
272             objects
273              
274             =cut
275              
276             sub accounts {
277 1     1 1 846 my ( $self ) = @_;
278 1         33 return $self->client->_get_accounts;
279             }
280              
281             sub upload_attachment {
282 1     1 0 1102 my ( $self,%params ) = @_;
283              
284 1         88 return Business::Monzo::Attachment->new(
285             client => $self->client,
286             )->upload( %params );
287             }
288              
289             =head1 EXAMPLES
290              
291             See the t/002_end_to_end.t test included with this distribution. you can run
292             this test against the Monzo emulator by running end_to_end_emulated.sh (this
293             is advised, don't run it against a live endpoint).
294              
295             =head1 SEE ALSO
296              
297             L<Business::Monzo::Account>
298              
299             L<Business::Monzo::Attachment>
300              
301             L<Business::Monzo::Balance>
302              
303             L<Business::Monzo::Transaction>
304              
305             L<Business::Monzo::Webhook>
306              
307             =head1 AUTHOR
308              
309             Lee Johnson - C<leejo@cpan.org>
310              
311             =head1 LICENSE
312              
313             This library is free software; you can redistribute it and/or modify it under
314             the same terms as Perl itself. If you would like to contribute documentation,
315             features, bug fixes, or anything else then please raise an issue / pull request:
316              
317             https://github.com/leejo/business-monzo
318              
319             =cut
320              
321             1;
322              
323             # vim: ts=4:sw=4:et