File Coverage

blib/lib/Business/Mondo/Utils.pm
Criterion Covered Total %
statement 28 28 100.0
branch 8 8 100.0
condition 2 3 66.6
subroutine 6 6 100.0
pod 1 1 100.0
total 45 46 97.8


line stmt bran cond sub pod time code
1             package Business::Mondo::Utils;
2              
3             =head1 NAME
4              
5             Business::Mondo::Utils
6              
7             =head1 DESCRIPTION
8              
9             A role containing Mondo utilities.
10              
11             =cut
12              
13 12     12   149683 use strict;
  12         14  
  12         265  
14 12     12   35 use warnings;
  12         11  
  12         208  
15              
16 12     12   404 use Moo::Role;
  12         9689  
  12         61  
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             Mondo 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             sub normalize_params {
32 5     5 1 7294 my ( $self,$params,$rfc_encode ) = @_;
33              
34 5 100 66     21 return '' if ( ! $params || ! keys( %{ $params } ) );
  2         7  
35              
36             return join( '&',
37 10         22 map { $_->[0] . '=' . $_->[1] }
38             map { $rfc_encode
39             ? [ _rfc5849_encode( $_ ),_rfc5849_encode( $params->{$_} ) ]
40 10 100       17 : [ $_,$params->{$_} ]
41             }
42 22         17 sort { $a cmp $b }
43 2         4 keys( %{ $params } )
  2         7  
44             );
45             }
46              
47             sub _rfc5849_encode {
48 18     18   14 my ( $str ) = @_;
49              
50 18 100       23 if ( ref( $str ) eq 'DateTime' ) {
51 1         3 $str = $str->iso8601;
52             }
53              
54 18         41 $str =~ s#([^-.~_a-z0-9])#sprintf('%%%02X', ord($1))#gei;
  11         26  
55 18         30 return $str;
56             }
57              
58             sub _params_as_array_string {
59 3     3   6 my ( $self,$key,$params ) = @_;
60              
61             my %modded_params = map {
62             +"$key\[$_\]" => ref( $params->{$_} ) eq 'DateTime'
63 17 100       82 ? $params->{$_}->iso8601 : $params->{$_}
64 3         3 } keys %{ $params };
  3         12  
65              
66 3         26 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-mondo
78              
79             =cut
80              
81             1;
82              
83             # vim: ts=4:sw=4:et