Buxfer.pm untested

filter: Statementstmt 0.0% 0 / 116 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 214 CC 8 SCAR 14.5
CC
8
Cov
0%
accounts
12.0
add_transactions
6.0
_login
6.0
CRAP 4.3
1 package WebService::Buxfer;
2  
3 use Moose;
4 use URI;
5 use LWP::UserAgent;
6 use HTTP::Request::Common;
7 use WebService::Buxfer::Response;
8 use WebService::Buxfer::Utils;
9 use Carp qw(croak);
10  
11 our $VERSION = '0.01';
12 use 5.008;
13  
14 ################################################################################
15 ##### Options
16 has 'preload_accounts' => ( is => 'ro', isa => 'Bool', default => 1 );
17 has 'inject_account_name' => ( is => 'rw', isa => 'Bool', default => 1 );
18 has 'data_format' => ( is => 'ro', isa => 'Str', default => 'json' );
19 has 'debug' => ( is => 'rw', isa => 'Bool', default => 0 );
20 has 'userid' => ( is => 'ro', isa => 'Str', required => 1);
21 has 'password' => ( is => 'ro', isa => 'Str', required => 1);
22 has 'url' => (
23 is => 'ro',
24 isa => 'URI',
25 default => sub { URI->new('https://www.buxfer.com/api') },
26 );
27  
28 ################################################################################
29 ##### Internal Accessors
30 has '_ua' => (
31 is => 'ro',
32 isa => 'Object',
33 default => sub { LWP::UserAgent->new },
34 );
35 has '_response' => ( is => 'rw', isa => 'Maybe[Object]',);
36 has '_accounts' => ( is => 'rw', isa => 'Maybe[HashRef]',);
37 has '_token' => ( is => 'rw', isa => 'Maybe[Str]',);
38 has '_uid' => ( is => 'rw', isa => 'Maybe[Str]',);
39  
40 ################################################################################
41 ##### GET Methods
42 sub transactions {
43 return shift->_do_get('transactions', @_);
44 }
45 sub analysis {
46 my $results = shift->_do_get('analysis', @_);
47 $results->{buxferImageURL} and $results->{buxferImageURL} =~ s/&/&/g;
48 $results->{imageURL} and $results->{imageURL} =~ s/&/&/g;
49 return $results;
50 }
51 sub accounts {
52 my $self = shift;
53  
54 unless ( $self->_accounts ) {
55 # Accounts are more useful when keyed on accountId
56 my %hash;
57 map {
58 $hash{$_->{'id'}} = $_;
59 } $self->_do_get('accounts', @_);
60
61 $self->_accounts(\%hash);
62 }
63  
64 return wantarray ? values(%{$self->_accounts}) : $self->_accounts;
65 }
66 sub impacts {
67 return shift->_do_get('impacts', @_);
68 }
69 sub tags {
70 return shift->_do_get('tags', @_);
71 }
72 sub budgets {
73 return shift->_do_get('budgets', @_);
74 }
75 sub groups {
76 return shift->_do_get('groups', @_);
77 }
78 sub contacts {
79 return shift->_do_get('contacts', @_);
80 }
81  
82 ################################################################################
83 ##### POST Methods
84 sub add_transactions {
85 my ($self, $raw_txns, $params) = @_;
86 my @responses = ();
87  
88 my $format = $params->{format} || 'sms';
89 WebService::Buxfer::Utils->max_transactions_per_submit(
90 $params->{max_transactions_per_submit} || 1000
91 );
92  
93 foreach my $text ( @{ &make_transactions($raw_txns) } ) {
94 my $txn = {
95 format => $format,
96 text => $text,
97 };
98 $self->_debug("Adding transaction(s):\n$text");
99  
100 $self->_do_post('add_transaction', $txn);
101 push @responses, $self->_response;
102 }
103  
104 return wantarray ? @responses : \@responses;
105 }
106  
107 ################################################################################
108 ##### Internal Methods
109 sub _login {
110 my ( $self ) = @_;
111  
112 # Return if we already have a token
113 return if $self->_token;
114  
115 my $params = {
116 userid => $self->userid,
117 password => $self->password,
118 };
119  
120 my $response = WebService::Buxfer::Response->new(
121 $self->_ua->request( GET $self->_build_url('login', $params) )
122 );
123  
124 !$response->ok and
125 croak("Login failed: ".$response->buxfer_status);
126  
127 $self->_token($response->content->{response}->{token});
128 $self->_uid($response->content->{response}->{uid});
129  
130 $self->_debug("Authorization token is '".$self->_token."'");
131  
132 $self->preload_accounts and $self->accounts;
133  
134 return $self->_response($response);
135 }
136  
137 sub _do_get {
138 my $self = shift;
139 my ( $buxfer_method, $get_params ) = @_;
140  
141 $self->_call_api('GET', $buxfer_method, $get_params, {});
142  
143 my $results = $self->_response->content->{response}->{$buxfer_method};
144  
145 # Some API calls return results with a Buxfer internal accountId field.
146 # For convenience, this method looks up the actual account name for you.
147 $self->inject_account_name && $buxfer_method ne 'accounts' and
148 &inject_accountName($self->accounts, $results);
149  
150 return wantarray ? @{$results} : $results;
151 }
152  
153 sub _do_post {
154 my $self = shift;
155 my ( $buxfer_method, $post_params ) = @_;
156  
157 $self->_call_api('POST', $buxfer_method, {}, $post_params);
158  
159 my $results = $self->_response->content->{response};#->{$buxfer_method};
160  
161 return wantarray ? @{$results} : $results;
162 }
163  
164 sub _call_api {
165 my ( $self, $method, $buxfer_method, $get_params, $post_params ) = @_;
166  
167 !$buxfer_method and croak("buxfer_method is undef");
168  
169 $self->_login;
170  
171 $get_params->{token} = $self->_token || 'undef';
172 $post_params->{token} = $get_params->{token};
173  
174 no strict 'refs';
175 my $response = WebService::Buxfer::Response->new(
176 $self->_ua->request(
177 &{"HTTP::Request::Common::".$method}($self->_build_url($buxfer_method, $get_params), $post_params)
178 )
179 );
180  
181 $self->_debug(
182 "_call_api, status: '".$response->buxfer_status."'");
183  
184 !$response->ok and
185 croak("Error calling '$buxfer_method': ".$response->buxfer_status);
186  
187 return $self->_response($response);
188 }
189  
190 sub _build_url {
191 my ( $self, $buxfer_method, $params ) = @_;
192 $params ||= {};
193  
194 my $url = $self->url->clone;
195 $url->path( $url->path . "/$buxfer_method.".$self->data_format );
196 $url->query_form( { %$params } );
197  
198 $self->_debug("_build_url, '$buxfer_method' URL: '$url'");
199  
200 return $url;
201 }
202  
203 sub _debug {
204 my $self = shift;
205 print ref($self).": ".shift()."\n" if $self->debug;
206 }
207  
208 1;
209  
210 __END__