File Coverage

blib/lib/Business/Monzo/Account.pm
Criterion Covered Total %
statement 50 50 100.0
branch 4 4 100.0
condition 4 7 57.1
subroutine 15 15 100.0
pod 6 7 85.7
total 79 83 95.1


line stmt bran cond sub pod time code
1             package Business::Monzo::Account;
2              
3             =head1 NAME
4              
5             Business::Monzo::Account
6              
7             =head1 DESCRIPTION
8              
9             A class for a Monzo account, extends L<Business::Monzo::Resource>
10              
11             =cut
12              
13 10     10   68 use strict;
  10         21  
  10         298  
14 10     10   58 use warnings;
  10         21  
  10         260  
15              
16 10     10   52 use Moo;
  10         23  
  10         69  
17             extends 'Business::Monzo::Resource';
18             with 'Business::Monzo::Utils';
19              
20 10     10   4078 use Types::Standard qw/ :all /;
  10         26  
  10         90  
21 10     10   440060 use Business::Monzo::Address;
  10         41  
  10         360  
22 10     10   4787 use Business::Monzo::Balance;
  10         34  
  10         389  
23 10     10   4418 use Business::Monzo::Webhook;
  10         38  
  10         434  
24 10     10   78 use Business::Monzo::Exception;
  10         22  
  10         6435  
25              
26             =head1 ATTRIBUTES
27              
28             The Account class has the following attributes (with their type).
29              
30             id (Str)
31             description (Str)
32             created (DateTime)
33              
34             Note that when a Str is passed to ->created this will be coerced
35             to a DateTime object.
36              
37             =cut
38              
39             has [ qw/ id / ] => (
40             is => 'ro',
41             isa => Str,
42             required => 1,
43             );
44              
45             has [ qw/ description / ] => (
46             is => 'ro',
47             isa => Str,
48             );
49              
50             has created => (
51             is => 'ro',
52             isa => Maybe[InstanceOf['DateTime']],
53             coerce => sub {
54             my ( $args ) = @_;
55              
56             if ( ! ref( $args ) ) {
57             $args = DateTime::Format::DateParse->parse_datetime( $args );
58             }
59              
60             return $args;
61             },
62             );
63              
64             =head1 Operations on an account
65              
66             =cut
67              
68             sub url {
69 1     1 0 890 Business::Monzo::Exception->throw({
70             message => "Monzo API does not currently support getting account data",
71             });
72             }
73              
74             sub get {
75 1     1 1 965 Business::Monzo::Exception->throw({
76             message => "Monzo API does not currently support getting account data",
77             });
78             }
79              
80             =head2 add_feed_item
81              
82             Adds a feed item to the Account. Returns true on success, otherwise will throw
83             an error. Note the required parameters:
84              
85             $Account->add_feed_item(
86             account_id => $id, # defaults to $Account->id,
87             type => $type, # defaults to 'basic'
88             url => $url, # optional, the URL to open when the item is tapped
89             params => {
90             title => $title, # REQUIRED
91             image_url => $url, # REQUIRED
92             body => $body_text, # optional
93             background_color => $hex_value, # optional
94             title_color => $hex_value, # optional
95             body_color => $hex_value, # optional
96             }
97             );
98              
99             =cut
100              
101             sub add_feed_item {
102 2     2 1 1506 my ( $self,%params ) = @_;
103              
104             $params{params}{title} && $params{params}{image_url} ||
105 2 100 66     22 Business::Monzo::Exception->throw({
106             message => "add_feed_item requires params: title, image_url",
107             });
108              
109 1         5 $params{account_id} = $self->id;
110 1   50     7 $params{type} //= 'basic';
111              
112             # title -> params[title] (params, params, params, params, params... what a mess)
113 1         9 $params{params} = { $self->_params_as_array_string( 'params',$params{params} ) };
114              
115             my %post_params = (
116 1         3 %{ delete $params{params} },
  1         7  
117             %params,
118             );
119              
120 1         13 return $self->client->api_post( 'feed',\%post_params );
121             }
122              
123             =head2 register_webhook
124              
125             Registers a webhook against the Account. Returns a Business::Monzo::Webhook
126             object. Note the required parameters:
127              
128             my $Webhook = $Account->webhooks(
129             callback_url => 'https://www.example.com/monzo/callback' # REQUIRED
130             );
131              
132             =cut
133              
134             sub register_webhook {
135 2     2 1 1029 my ( $self,%params ) = @_;
136              
137 2 100       10 $params{callback_url} || Business::Monzo::Exception->throw({
138             message => "register_webhook requires params: callback_url",
139             });
140              
141             my %post_params = (
142             url => $params{callback_url},
143 1         7 account_id => $self->id,
144             );
145              
146 1         6 my $data = $self->client->api_post( 'webhooks',\%post_params );
147              
148             return Business::Monzo::Webhook->new(
149             client => $self->client,
150             id => $data->{webhook}{id},
151             callback_url => $data->{webhook}{url},
152 1         19 account => $self,
153             );
154             }
155              
156             =head2 webhooks
157              
158             Returns a list of Business::Monzo::Webhook objects linked to the Account
159              
160             my @webhooks = $Account->webhooks
161              
162             =cut
163              
164             sub webhooks {
165 1     1 1 1742 my ( $self ) = @_;
166              
167 1         10 my $data = $self->client->api_get(
168             'webhooks',{ account_id => $self->id }
169             );
170              
171 1         11 my @webhooks;
172              
173 1   50     2 foreach my $webhook ( @{ $data->{webhooks} // [] } ) {
  1         6  
174              
175             push(
176             @webhooks,
177             Business::Monzo::Webhook->new(
178             client => $self->client,
179             id => $webhook->{id},
180             callback_url => $webhook->{url},
181 2         61 account => $self,
182             )
183             );
184             }
185              
186 1         28 return @webhooks;
187             }
188              
189             =head2 transactions
190              
191             Returns a list of L<Business::Monzo::Transaction> objects for the
192             account
193              
194             my @transactions = $Account->transactions( %query_params );
195              
196             =cut
197              
198             sub transactions {
199 1     1 1 38 my ( $self,%params ) = @_;
200              
201 1         16 return $self->client->_get_transactions({
202             %params,
203             account_id => $self->id,
204             });
205             }
206              
207             =head2 balance
208              
209             Returns a L<Business::Monzo::Balance> object for the account with the
210             attributes populated having called the Monzo API
211              
212             $Balance = $Account->balance;
213              
214             =cut
215              
216             sub balance {
217 2     2 1 32 my ( $self ) = @_;
218              
219 2         34 return Business::Monzo::Balance->new(
220             client => $self->client,
221             account_id => $self->id,
222             )->get;
223             }
224              
225             =head1 SEE ALSO
226              
227             L<Business::Monzo>
228              
229             L<Business::Monzo::Resource>
230              
231             L<Business::Monzo::Balance>
232              
233             L<Business::Monzo::Transaction>
234              
235             =head1 AUTHOR
236              
237             Lee Johnson - C<leejo@cpan.org>
238              
239             =head1 LICENSE
240              
241             This library is free software; you can redistribute it and/or modify it under
242             the same terms as Perl itself. If you would like to contribute documentation,
243             features, bug fixes, or anything else then please raise an issue / pull request:
244              
245             https://github.com/leejo/business-monzo
246              
247             =cut
248              
249             1;
250              
251             # vim: ts=4:sw=4:et