OFX/UserAgent.pm

filter: Statementstmt 32.1% 26 / 81 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 42.1% 8 / 19 Podpod 72.7% 8 / 11 totaltotal 32.3% 42 / 130 CC 10 SCAR 20.4
CC
10
Cov
27%
new
20.0
statement
20.0
institution
6.0
CRAP 7.7
« OFX/Institution.pm ↑ Summary OFX/Response.pm »
1 # Filename: UserAgent.pm
2 #
3 # OFX Interface for interacting with a Financial Institution server
4 #
5 # Created January 30, 2008 Brandon Fosdick <bfoz@bfoz.net>
6 #
7 # Copyright 2008 Brandon Fosdick <bfoz@bfoz.net> (BSD License)
8 #
9 # $Id: UserAgent.pm,v 1.2 2008/03/04 04:22:27 bfoz Exp $
10  
11 package Finance::OFX::UserAgent;
12  
13 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 our $VERSION = '2';
17  
18 2 ▶use Finance::OFX::Parse;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Finance::OFX::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Data::GUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use LWP;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 2 ▶use constant DEFAULT_OPTIONS =>
Subroutine BEGIN: called CC=1 SCAR=0.0
25 {
26 'ofxVersion' => '100',
27 'ofxAppID' => 'QWIN',
28 'ofxAppVer' => '0900',
29 'userID' => 'anonymous00000000000000000000000',
30 'userPass' => 'anonymous00000000000000000000000',
31 2 };
32  
33 sub new
34 0 ▶{
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
35 0 my ($this, %options) = @_;
36 0 ▶ my $class = ref($this) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
37 0 my $self = {};
38 0 bless $self, $class;
39  
40 # Initialization
41 0 $self->{Institution} = delete $options{Institution};
42 0 $self->{ofxVersion} = delete $options{ofxVersion};
43 0 $self->{ofxAppID} = delete $options{ofxAppID};
44 0 $self->{ofxAppVer} = delete $options{ofxAppVer};
45 0 $self->{userID} = delete $options{userID};
46 0 $self->{userPass} = delete $options{userPass};
47  
48 # Defaults
49 0 for( keys %{DEFAULT_OPTIONS()} )
50 {
51 0 ▶ $self->{$_} = DEFAULT_OPTIONS()->{$_} unless defined $self->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$_} 0 0 0
52 }
53  
54 0 $self->{ua} = LWP::UserAgent->new(%options);
55  
56 0 return $self;
57 }
58  
59 sub account_info
60 0 ▶{
Subroutine account_info: not called CC=1 SCAR=6.9
Pod documented
61 0 my $s = shift;
62  
63 0 my $r = $s->request('
64 <SIGNONMSGSRQV1>
65 '.$s->sonrq.'
66 </SIGNONMSGSRQV1>
67 <SIGNUPMSGSRQV1>
68 <ACCTINFOTRNRQ>
69 <TRNUID>'.(Data::GUID->new()->as_string).'
70 <ACCTINFORQ>
71 <DTACCTUP>19700101
72 </ACCTINFORQ>
73 </ACCTINFOTRNRQ>
74 </SIGNUPMSGSRQV1>');
75 0 return $s->{response} = Finance::OFX::Response->from_http_response($r);
76 }
77  
78 sub statement
79 0 ▶{
Subroutine statement: not called CC=4 SCAR=30.0
Pod documented
80 0 my ($s, $acct, %options) = @_;
81 0 my $start = delete $options{start};
82 0 my $end = delete $options{end};
83 0 my $transactions = delete $options{transactions};
84  
85 0 ▶ my $r = $s->request('
Branch: 3 branches
BranchTrueFalseTotal
$start ? : 0 0 0
$end ? : 0 0 0
$transactions ? : 0 0 0
86 <SIGNONMSGSRQV1>'.$s->sonrq.'
87 </SIGNONMSGSRQV1>
88 <BANKMSGSRQV1>
89 <STMTTRNRQ>
90 <TRNUID>'.(Data::GUID->new()->as_string).'
91 <STMTRQ>'.$acct->bankacctfrom().'
92 <INCTRAN>'.
93 ($start ?
94 '<DTSTART>'.strftime('%Y%m%d%H%M%S', gmtime($start))."[0:UTC]\n" : '').
95 ($end ?
96 '<DTEND>'.strftime('%Y%m%d%H%M%S', gmtime($end))."[0:UTC]\n" : '').'
97 <INCLUDE>'.($transactions ? 'Y' : 'N').'
98 </INCTRAN>
99 </STMTRQ>
100 </STMTTRNRQ>
101 </BANKMSGSRQV1>');
102  
103 0 return $s->{response} = Finance::OFX::Response->from_http_response($r);
104 }
105  
106 sub profile
107 0 ▶{
Subroutine profile: not called CC=1 SCAR=6.9
Pod documented
108 0 my $s = shift;
109  
110 0 my $request = HTTP::Request->new(POST => ${$s->{Institution}}->url);
111 0 $request->content_type('application/x-ofx');
112 $request->content($s->header().
113 '<OFX>
114 <SIGNONMSGSRQV1>
115 '.$s->sonrq.'
116 </SIGNONMSGSRQV1>
117 <PROFMSGSRQV1>
118 <PROFTRNRQ>
119 <TRNUID>'.(Data::GUID->new()->as_string).'
120 <PROFRQ>
121 <CLIENTROUTING>NONE
122 0 <DTPROFUP>'.${$s->{Institution}}->date.'
123 </PROFRQ>
124 </PROFTRNRQ>
125 </PROFMSGSRQV1>
126 </OFX>');
127  
128 0 print $request->as_string;
129 0 my $r = $s->request($request);
130 0 return $s->{response} = Finance::OFX::Response->from_http_response($r);
131 }
132  
133 # --- Getters and Setters ---
134  
135 sub institution
136 0 ▶{
Subroutine institution: not called CC=2 SCAR=17.9
Pod documented
137 0 my $s = shift;
138 0 ▶ $s->{Institution} = shift if scalar @_;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ 0 0 0
139 0 $s->{Institution};
140 }
141  
142 sub response
143 0 ▶{
Subroutine response: not called CC=1 SCAR=6.9
Pod documented
144 0 my $s = shift;
145 0 $s->{response};
146 }
147  
148 sub user_id
149 0 ▶{
Subroutine user_id: not called CC=2 SCAR=17.9
Pod documented
150 0 my $s = shift;
151 0 ▶ $s->{userID} = shift if scalar @_;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ 0 0 0
152 0 $s->{userID};
153 }
154  
155 sub user_pass
156 0 ▶{
Subroutine user_pass: not called CC=2 SCAR=17.9
Pod documented
157 0 my $s = shift;
158 0 ▶ $s->{userPass} = shift if scalar @_;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ 0 0 0
159 0 $s->{userPass};
160 }
161  
162 # --- Internal use only ---
163  
164 # Returns an OFX header appropriate for the protocol version in use
165 sub header
166 0 ▶{
Subroutine header: not called CC=1 SCAR=6.9
Pod undocumented
167 0 my $s = shift;
168  
169 0 'OFXHEADER:100
170 DATA:OFXSGML
171 VERSION:102
172 SECURITY:NONE
173 ENCODING:USASCII
174 CHARSET:1252
175 COMPRESSION:NONE
176 OLDFILEUID:NONE
177 NEWFILEUID:NONE'."\n\n"
178 }
179  
180 sub request
181 0 ▶{
Subroutine request: not called CC=1 SCAR=6.9
Pod undocumented
182 0 my ($s, $content) = @_;
183 0 my $r = HTTP::Request->new(POST => $s->{Institution}->url);
184 0 $r->content_type('application/x-ofx');
185 0 $r->content($s->header().'<OFX>'.$content.'</OFX>');
186 # print "request: ", $r->as_string, "\n\n---\n\n";
187 0 return $s->{ua}->request($r);
188 }
189  
190 # Returns a SONRQ block
191 sub sonrq
192 0 ▶{
Subroutine sonrq: not called CC=1 SCAR=6.9
Pod undocumented
193 0 my $s = shift;
194 0 my $t = strftime('%Y%m%d%H%M%S[%z:%Z]', localtime);
195 0 $t =~ s/00:/:/; # Fix the %z format
196  
197 '<SONRQ>
198 <DTCLIENT>'.$t.'
199 <USERID>'.$s->{userID}.'
200 <USERPASS>'.$s->{userPass}.'
201 <LANGUAGE>'.$s->{Institution}->language.'
202 <FI>
203 <ORG>'.$s->{Institution}->org.'
204 <FID>'.$s->{Institution}->fid.'
205 </FI>
206 <APPID>'.$s->{ofxAppID}.'
207 0 <APPVER>'.$s->{ofxAppVer}.'
208 </SONRQ>'
209 }
210  
211 2 1;
212  
213 __END__
« OFX/Institution.pm ↑ Summary OFX/Response.pm »