Dogecoin/API.pm

filter: Statementstmt 100% 88 / 88 Branchbran 100% 16 / 16 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 23 / 23 Podpod 80.0% 4 / 5 totaltotal 99.2% 136 / 137 CC 13 SCAR 8.4
CC
13
Cov
100%
withdraw
5.0
request
4.0
BEGIN
3.0
CRAP 2.3
↑ Summary Dogecoin.pm »
1 package Finance::Dogecoin::API;
2 3 $Finance::Dogecoin::API::VERSION = '1.20140201.1608';
3 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # limit the damage from Moo's use of strictures
7 # see: http://www.modernperlbooks.com/mt/2014/01/fatal-warnings-are-a-ticking-time-bomb.html
8 3 ▶use warnings NONFATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use LWP::Protocol::https;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 has 'api_key', is => 'ro', required => 1;
20 6 ▶has 'endpoint', is => 'ro', default => sub { 'https://www.dogeapi.com/wow/' };
Subroutine __ANON__: called CC=1 SCAR=0.0
21 6 ▶has 'json', is => 'ro', default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
22 6 my $j = JSON->new; $j->allow_nonref; $j
23 3 };
24  
25 4 ▶has 'ua', is => 'ro', default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
26 4 my $headers = HTTP::Headers->new;
27 4 $headers->header( 'Content-Type' => 'application/json' );
28 4 LWP::UserAgent->new(
29 ssl_opts => { verify_hostname => 1 },
30 default_headers => $headers,
31 );
32 3 };
33  
34 13 ▶sub request {
Subroutine request: called CC=4 SCAR=13.9
Pod undocumented
35 13 my ($self, $method, %params) = @_;
36  
37 # manually setting the 'a' parameter avoids a weird behavior in LWP::UA
38 # which uppercases 'a'--not what the API expects or wants
39 13 my $uri = URI->new( $self->endpoint );
40 13 $uri->query_param( a => $method );
41  
42 13 ▶ while (my ($key, $value) = each %params) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %params) 16 13 29
43 16 $uri->query_param( $key => $value );
44 }
45  
46 13 my $response = $self->ua->get( $uri );
47 13 my $result = $self->json->decode( $response->decoded_content );
48  
49 13 ▶ Carp::croak( "Bad API call from $method() call" ) if $result eq 'Bad Query';
Branch: 1 branch
BranchTrueFalseTotal
if $result eq "Bad Query" 1 12 13
50 13 ▶ Carp::croak( "Invalid API key '" . $self->api_key . "'" )
Branch: 1 branch
BranchTrueFalseTotal
if $result eq "Invalid API Key" 1 12 13
51 if $result eq 'Invalid API Key';
52  
53 13 return $result;
54 }
55  
56 3 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.0
57 3 for my $non_api (qw( get_difficulty get_current_block get_current_price )) {
58 9 ▶ my $method = sub { $_[0]->request( $non_api ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
59 9 do {
60 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
61 9 *{ $non_api } = $method;
62 };
63 }
64  
65 3 for my $api (qw( get_balance get_my_addresses )) {
66 6 ▶ my $method = sub { $_[0]->request( $api, api_key => $_[0]->api_key ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
67 6 do {
68 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
69 6 *{ $api } = $method;
70 };
71 }
72 }
73  
74 7 ▶sub withdraw {
Subroutine withdraw: called CC=5 SCAR=16.1
Pod documented
75 7 my ($self, %params) = @_;
76  
77 7 my @errors;
78  
79 7 for my $param (qw( payment_address amount )) {
80 14 ▶ push @errors, $param unless $params{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{$param} 6 8 14
81 }
82  
83 7 ▶ if (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 4 3 7
84 4 my $error = join ', ', @errors;
85 4 Carp::croak( "Must call withdraw() with $error params" );
86 }
87  
88 Carp::croak( 'Must call withdraw() with amount of at least 5 Doge' )
89 3 ▶ if $params{amount} < 5;
Branch: 1 branch
BranchTrueFalseTotal
if $params{"amount"} < 5 2 1 3
90  
91 1 $self->request( 'withdraw', api_key => $self->api_key, %params );
92 }
93  
94 1 ▶sub get_new_address {
Subroutine get_new_address: called CC=1 SCAR=0.0
Pod documented
95 1 my ($self, %params) = @_;
96 1 $self->request( 'get_new_address', api_key => $self->api_key, %params );
97 }
98  
99 3 ▶sub get_address_received {
Subroutine get_address_received: called CC=3 SCAR=11.0
Pod documented
100 3 my ($self, %params) = @_;
101 3 ▶ $params{payment_address} //= $params{address_label};
Condition100%3/3
$params{"payment_address"} //= $params{"address_label"}
l!l&&r!l&&!r
111
MC/DC100%2/2
$params{"payment_address"} //= $params{"address_label"}
$params{"payment_address"} $params{"address_label"}
Truth table100%3/3
A //= B
A= $params{"payment_address"}B= $params{"address_label"}
ABresult
1X1
011
000
102  
103 Carp::croak( 'Must call get_address_received() with payment_address param' )
104 3 ▶ unless $params{payment_address};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"payment_address"} 1 2 3
105  
106 2 $self->request( 'get_address_received',
107 api_key => $self->api_key, %params
108 );
109 }
110  
111 3 ▶sub get_address_by_label {
Subroutine get_address_by_label: called CC=2 SCAR=6.9
Pod documented
112 3 my ($self, %params) = @_;
113  
114 Carp::croak( 'Must call get_address_by_label() with address_label param' )
115 3 ▶ unless $params{address_label};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"address_label"} 2 1 3
116  
117 2 $self->request( 'get_address_by_label',
118 api_key => $self->api_key, %params
119 );
120 }
121  
122 3 'to the moon';
123 __END__
↑ Summary Dogecoin.pm »