blib/lib/Webservice/OVH/Helper.pm untested

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 75 SCAR 34.0
CC
5
Cov
0%
construct_filter
30.0
parse_datetime
2.0
parse_date
2.0
CRAP 30.0
1 package Webservice::OVH::Helper;
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 Webservice::OVH::Helper
8  
9 =head1 DESCRIPTION
10  
11 Some Helper Methods
12  
13 =head1 METHODS
14  
15 =cut
16  
17 use strict;
18 use warnings;
19 use Carp qw{ carp croak };
20  
21 use DateTime::Format::Strptime;
22  
23 =head2 construct_filter
24  
25 Helper method to construct uri parameter
26  
27 =over
28  
29 =item * Parameter: key => value
30  
31 =item * Return: VALUE
32  
33 =item * Synopsis: Webservice::OVH::Helper->construct_filter();
34  
35 =back
36  
37 =cut
38  
39 sub construct_filter {
40  
41 my ( $class, %params ) = @_;
42  
43 my @params = keys %params;
44 my @values = values %params;
45 my $filter = scalar @values ? '?' : "";
46  
47 my $pairs = [];
48  
49 foreach my $param (@params) {
50  
51 my $value = $params{$param};
52 next unless $value;
53  
54 $value = $value eq '_empty_' ? "" : $value;
55 push @$pairs, sprintf( "%s=%s", $param, $value );
56  
57 }
58
59 my $str = join('&', @$pairs);
60  
61 return $str ? "?" . $str : "";
62 }
63  
64 =head2 construct_filter
65  
66 Returns a DateTime Object.
67 Methods uses special pattern to match ovhs DT format.
68  
69 =over
70  
71 =item * Parameter: $str_datetime - datetime string, $locale - locale like 'en_EN', $timezone - timezone
72  
73 =item * Return: DateTime
74  
75 =item * Synopsis: Webservice::OVH::Helper->parse_datetime("2016-05-15T19:30:23", 'de_DE', 'Europe/Berlin');
76  
77 =back
78  
79 =cut
80  
81 sub parse_datetime {
82  
83 my ( $class, $str_datetime, $locale, $timezone ) = @_;
84  
85 my $strp = DateTime::Format::Strptime->new(
86 pattern => '%FT%T',
87 locale => ( $locale || 'de_DE' ),
88 time_zone => ( $timezone || 'Europe/Berlin' ),
89 on_error => 'croak',
90 );
91  
92 return $strp->parse_datetime($str_datetime);
93 }
94  
95 sub parse_date {
96  
97 my ( $class, $str_datetime, $locale, $timezone ) = @_;
98  
99 my $strp = DateTime::Format::Strptime->new(
100 pattern => '%F',
101 locale => ( $locale || 'de_DE' ),
102 time_zone => ( $timezone || 'Europe/Berlin' ),
103 on_error => 'croak',
104 );
105  
106 return $strp->parse_datetime($str_datetime);
107 }
108  
109 =head2 format_datetime
110  
111 Returns a date time string fitting ovhs requirements
112  
113 =over
114  
115 =item * Parameter: $dt - DateTime object
116  
117 =item * Return: VALUE
118  
119 =item * Synopsis: my $dt_str = Webservice::OVH::Helper->format_datetime(DateTime->today());
120  
121 =back
122  
123 =cut
124  
125 sub format_datetime {
126  
127 my ( $class, $dt ) = @_;
128  
129 return $dt->strftime('%FT%T%z');
130 }
131  
132 sub trim {
133  
134 my ( $class, $string ) = @_;
135  
136 $string =~ s/^\s+|\s+$//g;
137  
138 return $string;
139 }
140  
141 sub rtrim {
142  
143 my ( $class, $string ) = @_;
144  
145 $string =~ s/\s+$//;
146  
147 return $string;
148 }
149  
150 sub ltrim {
151  
152 my ( $class, $string ) = @_;
153  
154 $string =~ s/^\s+//;
155  
156 return $string;
157 }
158  
159 1;