blib/lib/LendingClub/API.pm

filter: Statementstmt 56.7% 38 / 67 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 44.4% 8 / 18 Podpod 87.5% 7 / 8 totaltotal 54.6% 53 / 97 CC 3 SCAR 11.2
CC
3
Cov
54%
new
12.0
available_cash
2.0
summary
2.0
CRAP 3.0
↑ Summary
1 #######################################
2 # LendingClub.com API Perl Module #
3 # version 0.3.0 #
4 # #
5 # Author: Michael W. Renz #
6 # Created: 17-Oct-2014 #
7 #######################################
8  
9 =pod
10  
11 =head1 NAME
12  
13 LendingClub::Api - perl module interface to the LendingClub API
14  
15 =head1 SYNOPSIS
16  
17 use LendingClub::API;
18 use Data::Dumper;
19  
20 # public functions do not require any options
21 my $lcapi_object = new LendingClub::API( "this-is-not-a-real-investor-id", "this-is-not-a-real-key" );
22  
23 print Dumper( $lcapi_object->available_cash() ) ."\n";
24 print Dumper( $lcapi_object->summary() ) ."\n";
25  
26 print Dumper( $lcapi_object->notes_owned() ) ."\n";
27 print Dumper( $lcapi_object->detailed_notes_owned() ) ."\n";
28  
29 print Dumper( $lcapi_object->portfolios_owned() ) ."\n";
30 print Dumper( $lcapi_object->listed_loans() ) ."\n";
31  
32  
33 =head1 DESCRIPTION
34  
35 Implements the LendingClub API described at https://www.lendingclub.com/developers/lc-api.action as a perl module
36  
37 =cut
38  
39 package LendingClub::API;
40  
41 # standard includes
42 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 2 my $modname="lendingclub-perl-module";
48 2 my $modver="0.3.0";
49  
50 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
51 2 $LendingClub::API::VERSION = '0.3.0';
52  
53 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 2 ▶use Hash::Flatten qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
55 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 2 my $ua = LWP::UserAgent->new();
58 2 $ua->agent("${modname}/${modver}");
59 2 $ua->timeout(1);
60 2 $ua->default_header('Accept' => "application/json");
61  
62 2 my %lcapi = ( version => "v1" );
63  
64 2 $lcapi{urls}{api_url} = "https://api.lendingclub.com/api/investor/" .$lcapi{version} ;
65  
66 # LendingClub API Resource URLs
67 2 $lcapi{urls}{api}{accounts_url} = $lcapi{urls}{api_url}. "/accounts";
68 2 $lcapi{urls}{api}{loans_url} = $lcapi{urls}{api_url}. "/loans";
69 2 $lcapi{urls}{api}{loans}{listing} = $lcapi{urls}{api}{loans_url}. "/listing";
70  
71 2 my $o = new Hash::Flatten();
72  
73 =pod
74  
75 =over 4
76  
77 =item my $lcapi_object = new LendingClub::API( "this-is-not-a-real-investor-id", "this-is-not-a-real-key" );
78  
79 The LendingClub::API module needs the investor id and the LendingClub API key for all functions.
80  
81 =back
82  
83 =cut
84  
85 sub new
86 0 ▶{
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
87 0 my ($class, $investor_id, $api_key) = @_;
88 0 my $self = ( {} );
89  
90 # We only expect the following options:
91 # - api_key - LendingClub API Key
92 # - investor_id - LendingClub Investor ID (from Summary page)
93 0 ▶ croak("api_key not defined") if ( !defined( $api_key) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $api_key 0 0 0
94 0 ▶ croak("investor_id not defined") if ( !defined( $investor_id) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $investor_id 0 0 0
95  
96 0 $lcapi{urls}{api}{accounts}{investor_id} = $lcapi{urls}{api}{accounts_url}. "/" .$investor_id;
97 0 $lcapi{urls}{api}{accounts}{availablecash} = $lcapi{urls}{api}{accounts}{investor_id}. "/availablecash";
98 0 $lcapi{urls}{api}{accounts}{summary} = $lcapi{urls}{api}{accounts}{investor_id}. "/summary";
99 0 $lcapi{urls}{api}{accounts}{notes} = $lcapi{urls}{api}{accounts}{investor_id}. "/notes";
100 0 $lcapi{urls}{api}{accounts}{detailednotes} = $lcapi{urls}{api}{accounts}{investor_id}. "/detailednotes";
101 0 $lcapi{urls}{api}{accounts}{portfolios} = $lcapi{urls}{api}{accounts}{investor_id}. "/portfolios";
102 0 $lcapi{urls}{api}{accounts}{orders} = $lcapi{urls}{api}{accounts}{investor_id}. "/orders";
103  
104 0 $ua->default_header('Authorization' => $api_key );
105  
106 0 return bless($self, $class);
107 }
108  
109 =pod
110  
111 =head2 Account information functions
112  
113 =over 4
114  
115 =item my $available_cash = $lcapi_object->available_cash();
116  
117 Returns the available cash for account.
118  
119 =item my $summary = $lcapi_object->summary();
120  
121 Returns the summary for account
122  
123 =item my $notes = $lcapi_object->notes_owned();
124  
125 Returns the notes owned by account.
126  
127 =item my $detailed_notes = $lcapi_object->detailed_notes_owned();
128  
129 Returns the detailed notes owned by account.
130  
131 =item my $portfolios = $lcapi_object->portfolios_owned();
132  
133 Returns the portfolios owned by account.
134  
135 =back
136  
137 =head2 Loan information
138  
139 =over 4
140  
141 =item my $listed_loans = $lcapi_object->listed_loans();
142  
143 Returns the loans listed on Lending Club.
144  
145 =back
146  
147 =cut
148  
149 # GET information from LendingClub
150 sub available_cash
151 0 ▶{
Subroutine available_cash: not called CC=1 SCAR=6.9
Pod documented
152 0 my ($self) = @_;
153 0 return $self->_json_get( $lcapi{urls}{api}{accounts}{availablecash} )->{availableCash};
154 }
155  
156 sub summary
157 0 ▶{
Subroutine summary: not called CC=1 SCAR=6.9
Pod documented
158 0 my ($self) = @_;
159 0 return $o->unflatten( $self->_json_get( $lcapi{urls}{api}{accounts}{summary} ) );
160 }
161  
162 sub notes_owned
163 0 ▶{
Subroutine notes_owned: not called CC=1 SCAR=6.9
Pod documented
164 0 my ($self) = @_;
165 0 return $o->unflatten( $self->_json_get( $lcapi{urls}{api}{accounts}{notes} ) );
166 }
167  
168 sub detailed_notes_owned
169 0 ▶{
Subroutine detailed_notes_owned: not called CC=1 SCAR=6.9
Pod documented
170 0 my ($self) = @_;
171 0 return $o->unflatten( $self->_json_get( $lcapi{urls}{api}{accounts}{detailednotes} ) );
172 }
173  
174 sub portfolios_owned
175 0 ▶{
Subroutine portfolios_owned: not called CC=1 SCAR=6.9
Pod documented
176 0 my ($self) = @_;
177 0 return $o->unflatten( $self->_json_get( $lcapi{urls}{api}{accounts}{portfolios} ) );
178 }
179  
180 sub listed_loans
181 0 ▶{
Subroutine listed_loans: not called CC=1 SCAR=6.9
Pod documented
182 0 my ($self,$showAll) = @_;
183 0 return $o->unflatten( $self->_json_get( $lcapi{urls}{api}{loans}{listing} ) );
184 }
185  
186  
187  
188 # private module functions
189 sub _json_get
190 0 ▶{
Subroutine _json_get: not called CC=1 SCAR=6.9
191 0 my ($self, $url) = @_;
192 0 return decode_json $ua->get( $url )->decoded_content();
193 }
194  
195 sub _json_post
196 0 ▶{
Subroutine _json_post: not called CC=1 SCAR=6.9
197 0 my ($self, $url) = @_;
198 0 return decode_json $ua->post( $url, $self->{post_message} )->decoded_content();
199 }
200  
201 0 ▶sub TRACE {}
Subroutine TRACE: not called CC=1 SCAR=0.0
Pod undocumented
202  
203 2 1; # End of LendingClub::API
204  
205 =pod
206  
207 =head1 CHANGELOG
208  
209 =over 4
210  
211 =item * Documentation for 'new'
212  
213 =item * Attempts 1/2 to add dependencies
214  
215 =back
216  
217 =head1 TODO
218  
219 =over 4
220  
221 =item * Add POST operations for the LendingClub API
222  
223 =item * Add comprehensive unit tests to module distribution
224  
225 =item * Add client error handling
226  
227 =item * Fix any bugs that anybody reports
228  
229 =item * Write better documentation. Always write better documentation
230  
231 =back
232  
233  
234 =head1 SEE ALSO
235  
236 See https://www.lendingclub.com/developers/lc-api.action for the most updated API docs and more details on each of the functions listed here.
237  
238  
239 =head1 VERSION
240  
241 $Id: API.pm,v 0.3.0 2014/06/08 09:08:00 CRYPTOGRA Exp $
242  
243  
244 =head1 AUTHOR
245  
246 Michael W. Renz, C<< <cryptographrix+cpan at gmail.com> >>
↑ Summary