File Coverage

blib/lib/LendingClub/API.pm
Criterion Covered Total %
statement 24 54 44.4
branch 0 4 0.0
condition n/a
subroutine 8 18 44.4
pod 7 8 87.5
total 39 84 46.4


line stmt bran cond sub pod time code
1             #######################################
2             # LendingClub.com API Perl Module #
3             # version 0.2.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 1     1   17318 use strict;
  1         2  
  1         37  
43 1     1   3 use warnings;
  1         2  
  1         23  
44 1     1   3 use Exporter;
  1         7  
  1         41  
45 1     1   4 use Carp;
  1         1  
  1         69  
46              
47             my $modname="lendingclub-perl-module";
48             my $modver="0.2.0";
49              
50 1     1   4 use vars qw($VERSION);
  1         1  
  1         48  
51             $LendingClub::API::VERSION = '0.2.0';
52              
53 1     1   626 use JSON;
  1         14255  
  1         4  
54 1     1   592 use Hash::Flatten qw(:all);
  1         1771  
  1         113  
55 1     1   1781 use LWP::UserAgent;
  1         41111  
  1         655  
56              
57             my $ua = LWP::UserAgent->new();
58             $ua->agent("${modname}/${modver}");
59             $ua->timeout(1);
60             $ua->default_header('Accept' => "application/json");
61              
62             my %lcapi = ( version => "v1" );
63              
64             $lcapi{urls}{api_url} = "https://api.lendingclub.com/api/investor/" .$lcapi{version} ;
65              
66             # LendingClub API Resource URLs
67             $lcapi{urls}{api}{accounts_url} = $lcapi{urls}{api_url}. "/accounts";
68             $lcapi{urls}{api}{loans_url} = $lcapi{urls}{api_url}. "/loans";
69             $lcapi{urls}{api}{loans}{listing} = $lcapi{urls}{api}{loans_url}. "/listing";
70              
71             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             {
87 0     0 1   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 0         croak("api_key not defined") if ( !defined( $api_key) );
94 0 0         croak("investor_id not defined") if ( !defined( $investor_id) );
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             {
152 0     0 1   my ($self) = @_;
153 0           return $self->_json_get( $lcapi{urls}{api}{accounts}{availablecash} )->{availableCash};
154             }
155              
156             sub summary
157             {
158 0     0 1   my ($self) = @_;
159 0           return $o->unflatten( $self->_json_get( $lcapi{urls}{api}{accounts}{summary} ) );
160             }
161              
162             sub notes_owned
163             {
164 0     0 1   my ($self) = @_;
165 0           return $o->unflatten( $self->_json_get( $lcapi{urls}{api}{accounts}{notes} ) );
166             }
167              
168             sub detailed_notes_owned
169             {
170 0     0 1   my ($self) = @_;
171 0           return $o->unflatten( $self->_json_get( $lcapi{urls}{api}{accounts}{detailednotes} ) );
172             }
173              
174             sub portfolios_owned
175             {
176 0     0 1   my ($self) = @_;
177 0           return $o->unflatten( $self->_json_get( $lcapi{urls}{api}{accounts}{portfolios} ) );
178             }
179              
180             sub listed_loans
181             {
182 0     0 1   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             {
191 0     0     my ($self, $url) = @_;
192 0           return decode_json $ua->get( $url )->decoded_content();
193             }
194              
195             sub _json_post
196             {
197 0     0     my ($self, $url) = @_;
198 0           return decode_json $ua->post( $url, $self->{post_message} )->decoded_content();
199             }
200              
201 0     0 0   sub TRACE {}
202              
203             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 * Attempt 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.2.0 2014/06/08 09:08:00 CRYPTOGRA Exp $
242              
243              
244             =head1 AUTHOR
245              
246             Michael W. Renz, C<< >>