| 1 | package WebService::Buxfer::Utils; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use base qw/Exporter/; | ||
| 6 | |||
| 7 | our $VERSION = '0.01'; | ||
| 8 | our @EXPORT = qw/inject_accountName make_transactions/; | ||
| 9 | |||
| 10 | my $_max_transactions_per_submit = 1000; | ||
| 11 | sub max_transactions_per_submit { | ||
| 12 | my $val = shift; | ||
| 13 | $val && !ref($val) and $_max_transactions_per_submit = shift; | ||
| 14 | return $_max_transactions_per_submit; | ||
| 15 | } | ||
| 16 | |||
| 17 | sub inject_accountName { | ||
| 18 | my ($accounts, $results) = @_; | ||
| 19 | |||
| 20 | return unless ref $results eq 'ARRAY'; | ||
| 21 | foreach my $r ( @$results ) { | ||
| 22 | next unless defined $r->{accountId}; | ||
| 23 | $r->{accountName} = $accounts->{$r->{accountId}}->{name}; | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | sub make_transactions { | ||
| 28 | my ($items) = @_; | ||
| 29 | my @txns = (); | ||
| 30 | my @return = (); | ||
| 31 | |||
| 32 | ref($items) ne 'ARRAY' and $items = [$items]; | ||
| 33 | |||
| 34 | map { push @txns, &_to_string($_) } @$items; | ||
| 35 | |||
| 36 | while ( scalar @txns ) { | ||
| 37 | push @return, | ||
| 38 | join "\n", splice(@txns, 0, &max_transactions_per_submit()); | ||
| 39 | } | ||
| 40 | |||
| 41 | return wantarray ? @return : \@return; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub _to_string { | ||
| 45 | my ($item) = @_; | ||
| 46 | |||
| 47 | # Return pre-stringified items unmolested. | ||
| 48 | !ref($item) and return $item; | ||
| 49 | |||
| 50 | my %i = %$item; | ||
| 51 | my $str = ''; | ||
| 52 | |||
| 53 | $str .= $i{description} . " "; | ||
| 54 | |||
| 55 | $i{amount} !~ /^-/ and $i{amount} = "+".$i{amount}; | ||
| 56 | $i{amount} =~ s/^-//; | ||
| 57 | $str .= $i{amount} . " "; | ||
| 58 | |||
| 59 | $i{payer} and $str .= $i{payer} . " "; | ||
| 60 | |||
| 61 | if ( my $tags = $i{tags} ) { | ||
| 62 | $str .= "tags:"; | ||
| 63 | |||
| 64 | !ref($tags) and $tags = [$tags]; | ||
| 65 | |||
| 66 | foreach my $tag ( @$tags ) { | ||
| 67 | $tag =~ /\s/ && $tag !~ /^'.*'$/ and $tag = "'$tag'"; | ||
| 68 | $str .= $tag.","; | ||
| 69 | } | ||
| 70 | $str =~ s/,$/ /; | ||
| 71 | } | ||
| 72 | |||
| 73 | $i{account} and $str .= 'acct:' . $i{account} . " "; | ||
| 74 | $i{date} and $str .= 'date:' . $i{date} . " "; | ||
| 75 | $i{status} and $str .= 'status:' . $i{status} . " "; | ||
| 76 | |||
| 77 | if ( my $parts = $i{participants} ) { | ||
| 78 | $str .= "@@ "; | ||
| 79 | foreach my $part ( @$parts ) { | ||
| 80 | !ref($part) and $str .= $part . " ", next; | ||
| 81 | $str .= (join ' ', @$part) . " "; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | # `return ($str = $str) =~ s/ $/;/;` doesn't work. | ||
| 86 | $str =~ s/ $/;/; | ||
| 87 | return $str; | ||
| 88 | } | ||
| 89 | |||
| 90 | 1; | ||
| 91 | |||
| 92 | __END__ |