summaryrefslogtreecommitdiffstats
path: root/lib/WWW/YNAB.pm
blob: 113662a7a2425d271f67c448a3e66728a11aa5a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package WWW::YNAB;

use Moose;
# ABSTRACT: Wrapper for the YNAB API

use WWW::YNAB::Account;
use WWW::YNAB::Budget;
use WWW::YNAB::CategoryGroup;
use WWW::YNAB::Category;
use WWW::YNAB::Month;
use WWW::YNAB::Payee;
use WWW::YNAB::ScheduledSubTransaction;
use WWW::YNAB::ScheduledTransaction;
use WWW::YNAB::SubTransaction;
use WWW::YNAB::Transaction;
use WWW::YNAB::UA;
use WWW::YNAB::User;

with 'WWW::YNAB::ModelHelpers';

=head1 SYNOPSIS

  use WWW::YNAB;

  my $ynab = WWW::YNAB->new(access_token => 'SECRET');
  my @budgets = $ynab->budgets;

=head1 DESCRIPTION

This module is a wrapper around the V1 YNAB API. It follows the API structure
quite closely, so the API documentation should be used for information about
the data that this module returns. You can find the API documentation at L<https://api.youneedabudget.com/>.

=cut

=attr access_token

Your personal access token. Information about generating a personal access
token can be found at
L<https://api.youneedabudget.com/#personal-access-tokens>. Required.

=cut

has access_token => (
    is       => 'ro',
    isa      => 'Str',
    required => 1,
);

=attr base_uri

The base uri for all API requests. Defaults to
C<https://api.youneedabudget.com/v1/>. It's unlikely you'll need to change
this.

=cut

has base_uri => (
    is      => 'ro',
    isa     => 'Str',
    default => 'https://api.youneedabudget.com/v1/',
);

=attr ua

The HTTP user agent to use. Must be compatible with L<HTTP::Tiny>.

=cut

has ua => (
    is      => 'ro',
    isa     => 'HTTP::Tiny',
    lazy    => 1,
    default => sub { HTTP::Tiny->new },
);

has _ua => (
    is      => 'ro',
    isa     => 'WWW::YNAB::UA',
    lazy    => 1,
    default => sub {
        my $self = shift;
        WWW::YNAB::UA->new(
            access_token => $self->access_token,
            base_uri     => $self->base_uri,
            ua           => $self->ua,
        )
    },
);

=method user

=cut

sub user {
    my $self = shift;

    my $data = $self->_ua->get('/user');
    my $user = $data->{data}{user};
    $self->model_from_data('WWW::YNAB::User', $user);
}

=method budgets

=cut

sub budgets {
    my $self = shift;

    my $data = $self->_ua->get('/budgets');
    map {
        $self->model_from_data('WWW::YNAB::Budget', $_)
    } @{ $data->{data}{budgets} };
}

=method budget($id, $server_knowledge=undef)

Returns the budget with id C<$id>. The returned budget object will have a
C<server_knowledge> method which represents the state of the server when that
object was returned. If the C<$server_knowledge> parameter is passed here with
a value that came from an object previously returned by this method, this
method will only return sub-objects (transactions, accounts, etc.) which have
changed since that previous object was generated.

=cut

sub budget {
    my $self = shift;
    my ($id, $server_knowledge) = @_;

    my $params;
    if (defined $server_knowledge) {
        $params = {
            last_knowledge_of_server => $server_knowledge,
        }
    }

    my $data = $self->_ua->get("/budgets/$id", $params);
    my $budget = $data->{data}{budget};
    my %budget = %$budget;

    my @accounts = map {
        $self->model_from_data('WWW::YNAB::Account', $_)
    } @{ $budget{accounts} };
    $budget{accounts} = \@accounts;

    my @payees = map {
        $self->model_from_data('WWW::YNAB::Payee', $_)
    } @{ $budget{payees} };
    $budget{payees} = \@payees;

    my @category_groups = map {
        my %category_group = %$_;
        $category_group{categories} = [
            map {
                $self->model_from_data('WWW::YNAB::Category', $_)
            } grep {
                $_->{category_group_id} eq $category_group{id}
            } @{ $budget{categories} }
        ];
        $self->model_from_data('WWW::YNAB::CategoryGroup', \%category_group)
    } @{ $budget{category_groups} };
    $budget{category_groups} = \@category_groups;

    my @months = map {
        my %month = %$_;
        $month{categories} = [
            map {
                $self->model_from_data('WWW::YNAB::Category', $_)
            } @{ $month{categories} }
        ];
        $self->model_from_data('WWW::YNAB::Month', \%month)
    } @{ $budget{months} };
    $budget{months} = \@months;

    my @transactions = map {
        my %transaction = %$_;
        if ($transaction{account_id}) {
            ($transaction{account_name}) = map {
                $_->{name}
            } grep {
                $_->{id} eq $transaction{account_id}
            } @{ $budget{accounts} };
        }
        if ($transaction{payee_id}) {
            ($transaction{payee_name}) = map {
                $_->{name}
            } grep {
                $_->{id} eq $transaction{payee_id}
            } @{ $budget{payees} };
        }
        if ($transaction{category_id}) {
            ($transaction{category_name}) = map {
                $_->{name}
            } grep {
                $_->{id} eq $transaction{category_id}
            } @{ $budget{categories} };
        }
        $transaction{subtransactions} = [
            map {
                $self->model_from_data('WWW::YNAB::SubTransaction', $_)
            } grep {
                $_->{transaction_id} eq $transaction{id}
            } @{ $budget{subtransactions} }
        ];
        $self->model_from_data('WWW::YNAB::Transaction', \%transaction)
    } @{ $budget{transactions} };
    $budget{transactions} = \@transactions;

    my @scheduled_transactions = map {
        my %transaction = %$_;
        if ($transaction{account_id}) {
            ($transaction{account_name}) = map {
                $_->{name}
            } grep {
                $_->{id} eq $transaction{account_id}
            } @{ $budget{accounts} };
        }
        if ($transaction{payee_id}) {
            ($transaction{payee_name}) = map {
                $_->{name}
            } grep {
                $_->{id} eq $transaction{payee_id}
            } @{ $budget{payees} };
        }
        if ($transaction{category_id}) {
            ($transaction{category_name}) = map {
                $_->{name}
            } grep {
                $_->{id} eq $transaction{category_id}
            } @{ $budget{categories} };
        }
        $transaction{subtransactions} = [
            map {
                $self->model_from_data('WWW::YNAB::ScheduledSubTransaction', $_)
            } grep {
                $_->{scheduled_transaction_id} eq $transaction{id}
            } @{ $budget{scheduled_subtransactions} }
        ];
        $self->model_from_data('WWW::YNAB::ScheduledTransaction', \%transaction)
    } @{ $budget{scheduled_transactions} };
    $budget{scheduled_transactions} = \@scheduled_transactions;

    $self->model_from_data(
        'WWW::YNAB::Budget',
        \%budget,
        $data->{data}{server_knowledge},
    );
}

=method rate_limit

Returns the number of requests in the current rate limit bucket.

=cut

sub rate_limit {
    my $self = shift;

    $self->_ua->rate_limit
}

=method knows_rate_limit

Returns true if the current rate limit is known. This will only be true after a
request has already been made (since the API currently doesn't provide a way to
just request the current rate limit).

=cut

sub knows_rate_limit {
    my $self = shift;

    $self->_ua->knows_rate_limit
}

=method total_rate_limit

Returns the total number of requests that will be allowed in the current rate
limit bucket.

=cut

sub total_rate_limit {
    my $self = shift;

    $self->_ua->total_rate_limit
}

=method knows_total_rate_limit

Returns true if the current total rate limit is known. This will only be true
after a request has already been made (since the API currently doesn't provide
a way to just request the current rate limit).

=cut

sub knows_total_rate_limit {
    my $self = shift;

    $self->_ua->knows_total_rate_limit
}

__PACKAGE__->meta->make_immutable;
no Moose;

=head1 BUGS/LIMITATIONS

No known bugs.

Please report any bugs to GitHub Issues at
L<https://github.com/doy/www-ynab/issues>.

Not all of the API is exposed by this wrapper yet. In particular, these things
are missing:

=over 4

=item All modification endpoints (this module currently only exposes read-only operations)

=item The payee location API

=item OAuth authentication

=back

Patches are greatly appreciated if you are interested in this functionality.

=head1 SEE ALSO

L<https://api.youneedabudget.com/>

=head1 SUPPORT

You can find this documentation for this module with the perldoc command.

    perldoc WWW::YNAB

You can also look for information at:

=over 4

=item * MetaCPAN

L<https://metacpan.org/release/WWW-YNAB>

=item * Github

L<https://github.com/doy/www-ynab>

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-YNAB>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/WWW-YNAB>

=back

=cut

1;