File Coverage

blib/lib/Finance/BlockIO.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package Finance::BlockIO;
2              
3 1     1   26097 use 5.006;
  1         4  
  1         55  
4 1     1   6 use strict;
  1         2  
  1         46  
5 1     1   8 use warnings FATAL => 'all';
  1         6  
  1         61  
6              
7 1     1   6 use Carp;
  1         1  
  1         77  
8 1     1   581 use WWW::Curl::Simple;
  0            
  0            
9             use JSON;
10             use Exporter 'import';
11             our @EXPORT_OK = qw(get_new_address
12             get_my_addresses
13             get_address_by_label
14             get_balance
15             get_address_balance
16             get_address_received
17             withdraw
18             create_user
19             get_users
20             get_user_balance
21             get_user_address
22             get_user_received
23             withdraw_from_user
24             get_current_price);
25              
26             =head1 NAME
27              
28             Finance::BlockIO - Perl wrapper to JSON-based Block.io API
29              
30             =head1 VERSION
31              
32             Version 0.01
33              
34             =cut
35              
36             our $VERSION = '0.02';
37              
38             =head1 SYNOPSIS
39              
40             use Finance::BlockIO;
41            
42             my $api_key = "gobbledegook";
43            
44             my $response = get_new_address($api_key, "foo");
45              
46             =head1 DESCRIPTION
47              
48             Finance::BlockIO provides a bare interface to the Block.io JSON-based API,
49             using WWW::Curl::Simple to make requests and returning a Perl data structure
50             created from any JSON response received.
51              
52             You'll probably need an API key (and therefore an account) at Block.io in order
53             to actually use this.
54              
55             This documentation is not a replacement for the actual API documentation, which
56             can be found at L<https://block.io/api/detailed/curl>.
57              
58             =head1 SUBROUTINES
59              
60             =cut
61              
62             sub _fetch_response {
63             my ($url) = @_;
64             my $curl = WWW::Curl::Simple->new();
65             my $json = $curl->get($url);
66             return decode_json($json);
67             }
68              
69             =head2 get_new_address
70              
71             my $response = get_new_address(
72             $api_key, # REQUIRED
73             $label); # Optional
74              
75             Returns a brand new address. Can optionally specify a label for this address.
76              
77             =cut
78              
79             sub get_new_address {
80             my ($api_key, $label) = @_;
81            
82             unless ($api_key) {
83             carp "No API key specified!";
84             return;
85             }
86            
87             my $url = "https://block.io/api/v1/get_new_address/?api_key=$api_key";
88             $url = $url . "&label=$label" if $label;
89            
90             return _fetch_response($url);
91             }
92              
93             =head2 get_my_addresses
94              
95             my $response = get_my_addresses(
96             $api_key); # REQUIRED
97              
98             Returns all addresses associated with $api_key.
99              
100             =cut
101              
102             sub get_my_addresses {
103             my ($api_key) = @_;
104            
105             unless ($api_key) {
106             carp "No API key specified!";
107             return;
108             }
109            
110             my $url = "https://block.io/api/v1/get_my_addresses/?api_key=$api_key";
111            
112             return _fetch_response($url);
113             }
114              
115             =head2 get_address_by_label
116              
117             my $response = get_address_by_label(
118             $api_key, # REQUIRED
119             $label); # REQUIRED
120              
121             Returns an address with a label $label associated with $api_key.
122              
123             =cut
124              
125             sub get_address_by_label {
126             my ($api_key, $label) = @_;
127            
128             unless ($api_key) {
129             carp "No API key specified!";
130             return;
131             }
132            
133             unless ($label) {
134             carp "No label specified!";
135             return;
136             }
137            
138             my $url = "https://block.io/api/v1/get_address_by_label/?api_key=$api_key&label=$label";
139            
140             return _fetch_response($url);
141             }
142              
143             =head2 get_balance
144              
145             my $response = get_balance( $api_key); # REQUIRED
146              
147             Returns the combined balance of all addresses associated with $api_key.
148              
149             =cut
150              
151             sub get_balance {
152             my ($api_key) = @_;
153            
154             unless ($api_key) {
155             carp "No API key specified!";
156             return;
157             }
158            
159             my $url = "https://block.io/api/v1/get_balance/?api_key=$api_key";
160            
161             return _fetch_response($url);
162             }
163              
164             =head2 get_address_balance
165              
166             my $response = get_balance( $api_key, # REQUIRED
167             $address, # REQUIRED unless $label
168             $label); # REQUIRED unless $address
169              
170             Returns the balance of an address associated with $api_key, the address being
171             specified by either $address or $label.
172              
173             =cut
174              
175             sub get_address_balance {
176             my ($api_key, $address, $label) = @_;
177            
178             unless ($api_key) {
179             carp "No API key specified!";
180             return;
181             }
182            
183             unless ($address or $label) {
184             carp "No address or label specified!";
185             return;
186             }
187            
188             my $url = "https://block.io/api/v1/get_address_balance/?api_key=$api_key";
189             $url = $url . "&address=$address" if $address;
190             $url = $url . "&label=$label" if $label;
191            
192             return _fetch_response($url);
193             }
194              
195             =head2 get_address_received
196              
197             my $response = get_balance( $api_key, # REQUIRED
198             $address, # REQUIRED unless $label
199             $label); # REQUIRED unless $address
200              
201             Returns the amount received by an address associated with $api_key, the address
202             being specified by either $address or $label.
203              
204             =cut
205              
206             sub get_address_received {
207             my ($api_key, $address, $label) = @_;
208            
209             unless ($api_key) {
210             carp "No API key specified!";
211             return;
212             }
213            
214             unless ($address or $label) {
215             carp "No address or label specified!";
216             return;
217             }
218            
219             my $url = "https://block.io/api/v1/get_address_received/?api_key=$api_key";
220             $url = $url . "&address=$address" if $address;
221             $url = $url . "&label=$label" if $label;
222            
223             return _fetch_response($url);
224             }
225              
226             =head2 withdraw
227              
228             my $response = withdraw($api_key, # REQUIRED
229             $amount, # REQUIRED
230             $pin, # REQUIRED
231             $payment_address, # REQUIRED unless $to_user_ids
232             $to_user_id, # REQUIRED unless $payment_address
233             $from_user_ids); # Optional
234              
235             Requests payment to be sent from the account associated with $api_key to either
236             an address specified by $payment_address or a Block.io user specified by
237             $to_user_id, in an amount specified by $amount and (optionally) from user IDs
238             associated with $api_key specified in a string $from_user_ids.
239              
240             =cut
241              
242             sub withdraw {
243             my ($api_key,
244             $amount,
245             $pin,
246             $payment_address,
247             $to_user_id,
248             $from_user_ids) = @_;
249            
250             unless ($api_key) {
251             carp "No API key specified!";
252             return;
253             }
254            
255             unless ($amount) {
256             carp "No amount specified!";
257             return;
258             }
259            
260             unless ($pin) {
261             carp "No PIN specified!";
262             return;
263             }
264            
265             unless ($payment_address or $to_user_id) {
266             carp "No payment address or user ID specified";
267             return;
268             }
269            
270             my $url = "https://block.io/api/v1/withdraw/?api_key=$api_key&amount=$amount&pin=$pin";
271             $url = $url . "&payment_address=$payment_address" if $payment_address;
272             $url = $url . "&to_user_id=$to_user_id" if $to_user_id;
273             $url = $url . "&from_user_ids=$from_user_ids" if $from_user_ids;
274            
275             return _fetch_response($url);
276             }
277              
278             =head2 create_user
279              
280             my $response = create_user( $api_key, # REQUIRED
281             $label); # Optional
282              
283             Creates a new user associated with $api_key, optionally with a label specified
284             by $label.
285              
286             =cut
287              
288             sub create_user {
289             my ($api_key, $label) = @_;
290            
291             unless ($api_key) {
292             carp "No API key specified!";
293             return;
294             }
295            
296             my $url = "https://block.io/api/v1/create_user/?api_key=$api_key";
297             $url = $url . "&label=$label" if $label;
298            
299             return _fetch_response($url);
300             }
301              
302             =head2 get_users
303              
304             my $response = get_users( $api_key); # REQUIRED
305              
306             Returns a list of users associated with $api_key.
307              
308             =cut
309              
310             sub get_users {
311             my ($api_key) = @_;
312            
313             unless ($api_key) {
314             carp "No API key specified!";
315             return;
316             }
317            
318             my $url = "https://block.io/api/v1/get_users/?api_key=$api_key";
319            
320             return _fetch_response($url);
321             }
322              
323             =head2 get_user_balance
324              
325             my $response = get_user_balance(
326             $api_key, # REQUIRED
327             $user_id); # REQUIRED
328              
329             Get the balance of $user_id associated with $api_key.
330              
331             =cut
332              
333             sub get_user_balance {
334             my ($api_key, $user_id) = @_;
335            
336             unless ($api_key) {
337             carp "No API key specified!";
338             return;
339             }
340            
341             unless ($user_id) {
342             carp "No user ID specified!";
343             return;
344             }
345            
346             my $url = "https://block.io/api/v1/get_user_balance/?api_key=$api_key&user_id=$user_id";
347            
348             return _fetch_response($url);
349             }
350              
351             =head2 get_user_address
352              
353             my $response = get_user_address(
354             $api_key, # REQUIRED
355             $user_id); # REQUIRED
356              
357             Get the address of $user_id associated with $api_key.
358              
359             =cut
360              
361             sub get_user_address {
362             my ($api_key, $user_id) = @_;
363            
364             unless ($api_key) {
365             carp "No API key specified!";
366             return;
367             }
368            
369             unless ($user_id) {
370             carp "No user ID specified!";
371             return;
372             }
373            
374             my $url = "https://block.io/api/v1/get_user_address/?api_key=$api_key&user_id=$user_id";
375            
376             return _fetch_response($url);
377             }
378              
379             =head2 get_user_received
380              
381             my $response = get_user_received(
382             $api_key, # REQUIRED
383             $user_id); # REQUIRED
384              
385             Get the amount received by $user_id associated with $api_key.
386              
387             =cut
388              
389             sub get_user_received {
390             my ($api_key, $user_id) = @_;
391            
392             unless ($api_key) {
393             carp "No API key specified!";
394             return;
395             }
396            
397             unless ($user_id) {
398             carp "No user ID specified!";
399             return;
400             }
401            
402             my $url = "https://block.io/api/v1/get_user_received/?api_key=$api_key&user_id=$user_id";
403            
404             return _fetch_response($url);
405             }
406              
407             =head2 withdraw_from_user
408              
409             See withdraw; as far as I can tell, this is just another name for it.
410              
411             =cut
412              
413             sub withdraw_from_user {
414             return withdraw(@_);
415             }
416              
417             =head2 get_current_price
418              
419             =cut
420              
421             sub get_current_price {
422             my ($api_key, $price_base) = @_;
423            
424             unless ($api_key) {
425             carp "No API key specified!";
426             return;
427             }
428            
429             my $url = "https://block.io/api/v1/get_current_price/?api_key=$api_key";
430             $url = $url . "&price_base=$price_base" if $price_base;
431            
432             return _fetch_response($url);
433             }
434              
435             =head1 AUTHOR
436              
437             Ryan Northrup, C<< <northrup at cpan.org> >>
438              
439             =head1 BUGS
440              
441             Please report any bugs or feature requests to C<bug-finance-blockio at rt.cpan.org>, or through
442             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Finance-BlockIO>. I will be notified, and then you'll
443             automatically be notified of progress on your bug as I make changes.
444              
445              
446              
447              
448             =head1 SUPPORT
449              
450             You can find documentation for this module with the perldoc command.
451              
452             perldoc Finance::BlockIO
453              
454              
455             You can also look for information at:
456              
457             =over 4
458              
459             =item * RT: CPAN's request tracker (report bugs here)
460              
461             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Finance-BlockIO>
462              
463             =item * AnnoCPAN: Annotated CPAN documentation
464              
465             L<http://annocpan.org/dist/Finance-BlockIO>
466              
467             =item * CPAN Ratings
468              
469             L<http://cpanratings.perl.org/d/Finance-BlockIO>
470              
471             =item * Search CPAN
472              
473             L<http://search.cpan.org/dist/Finance-BlockIO/>
474              
475             =back
476              
477              
478             =head1 ACKNOWLEDGEMENTS
479              
480              
481             =head1 LICENSE AND COPYRIGHT
482              
483             Copyright 2014 Ryan Northrup.
484              
485             This program is free software; you can redistribute it and/or modify it
486             under the terms of the the Artistic License (2.0). You may obtain a
487             copy of the full license at:
488              
489             L<http://www.perlfoundation.org/artistic_license_2_0>
490              
491             Any use, modification, and distribution of the Standard or Modified
492             Versions is governed by this Artistic License. By using, modifying or
493             distributing the Package, you accept this license. Do not use, modify,
494             or distribute the Package, if you do not accept this license.
495              
496             If your Modified Version has been derived from a Modified Version made
497             by someone other than you, you are nevertheless required to ensure that
498             your Modified Version complies with the requirements of this license.
499              
500             This license does not grant you the right to use any trademark, service
501             mark, tradename, or logo of the Copyright Holder.
502              
503             This license includes the non-exclusive, worldwide, free-of-charge
504             patent license to make, have made, use, offer to sell, sell, import and
505             otherwise transfer the Package with respect to any patent claims
506             licensable by the Copyright Holder that are necessarily infringed by the
507             Package. If you institute patent litigation (including a cross-claim or
508             counterclaim) against any party alleging that the Package constitutes
509             direct or contributory patent infringement, then this Artistic License
510             to you shall terminate on the date that such litigation is filed.
511              
512             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
513             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
514             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
515             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
516             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
517             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
518             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
519             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
520              
521              
522             =cut
523              
524             1; # End of Finance::BlockIO