Monzo/Utils.pm

filter: Statementstmt 100% 29 / 29 Branchbran 100% 8 / 8 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 100% 49 / 49 CC 6 SCAR 9.0
CC
6
Cov
100%
normalize_params
4.0
_rfc5849_encode
2.0
_params_as_array_string
2.0
CRAP 2.5
« Monzo/Transaction.pm ↑ Summary Monzo/Account.pm »
1 package Business::Monzo::Utils;
2  
3 =head1 NAME
4  
5 Business::Monzo::Utils
6  
7 =head1 DESCRIPTION
8  
9 A role containing Monzo utilities.
10  
11 =cut
12  
13 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 12 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 METHODS
19  
20 =head2 normalize_params
21  
22 Normalizes the passed params hash into a string for use in queries to the
23 Monzo API. If a second true value is passed this will includes RFC5849
24 encoding and will convert DateTime objects into the corresponding ISO8601
25 string
26  
27 my $query_string = $self->normalize_params( \%params,$rfc_encode );
28  
29 =cut
30  
31 5 ▶sub normalize_params {
Subroutine normalize_params: called CC=4 SCAR=13.9
Pod documented
32 5 my ( $self,$params,$rfc_encode ) = @_;
33  
34 5 ▶ return '' if ( ! $params || ! keys( %{ $params } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $params and keys %{$params;} 3 2 5
Condition100%3/3
$params and keys %{$params;}
!ll&&!rl&&r
212
MC/DC100%2/2
$params and keys %{$params;}
$params keys %{$params;}
Truth table100%3/3
A and B
A= $paramsB= keys %{$params;}
ABresult
0X0
100
111
35  
36 return join( '&',
37 11 map { $_->[0] . '=' . $_->[1] }
38 map { $rfc_encode
39 ? [ _rfc5849_encode( $_ ),_rfc5849_encode( $params->{$_} ) ]
40 11 ▶ : [ $_,$params->{$_} ]
Branch: 1 branch
BranchTrueFalseTotal
$rfc_encode ? : 9 2 11
41 }
42 17 sort { $a cmp $b }
43 2 keys( %{ $params } )
44 );
45 }
46  
47 18 ▶sub _rfc5849_encode {
Subroutine _rfc5849_encode: called CC=2 SCAR=6.9
48 18 my ( $str ) = @_;
49  
50 18 ▶ if ( ref( $str ) eq 'DateTime' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $str eq "DateTime") 1 17 18
51 1 $str = $str->iso8601;
52 }
53  
54 18 $str =~ s#([^-.~_a-z0-9])#sprintf('%%%02X', ord($1))#gei;
55 18 return $str;
56 }
57  
58 3 ▶sub _params_as_array_string {
Subroutine _params_as_array_string: called CC=2 SCAR=6.9
59 3 my ( $self,$key,$params ) = @_;
60  
61 my %modded_params = map {
62 +"$key\[$_\]" => ref( $params->{$_} ) eq 'DateTime'
63 17 ▶ ? $params->{$_}->iso8601 : $params->{$_}
Branch: 1 branch
BranchTrueFalseTotal
ref $params->{$_} eq 'DateTime' ? : 1 16 17
64 3 } keys %{ $params };
65  
66 3 return %modded_params;
67 }
68  
69 =head1 AUTHOR
70  
71 Lee Johnson - C<leejo@cpan.org>
72  
73 This library is free software; you can redistribute it and/or modify it under
74 the same terms as Perl itself. If you would like to contribute documentation,
75 features, bug fixes, or anything else then please raise an issue / pull request:
76  
77 https://github.com/leejo/business-monzo
78  
79 =cut
80  
81 12 1;
82  
83 # vim: ts=4:sw=4:et
« Monzo/Transaction.pm ↑ Summary Monzo/Account.pm »