File Coverage

blib/lib/Web/Sitemap/Url.pm
Criterion Covered Total %
statement 43 45 95.5
branch 17 24 70.8
condition 2 3 66.6
subroutine 7 7 100.0
pod 0 2 0.0
total 69 81 85.1


",
line stmt bran cond sub pod time code
1             package Web::Sitemap::Url;
2              
3             our $VERSION = '0.903';
4              
5 4     4   27 use strict;
  4         10  
  4         129  
6 4     4   22 use warnings;
  4         7  
  4         93  
7 4     4   25 use utf8;
  4         7  
  4         27  
8              
9 4     4   86 use Carp;
  4         8  
  4         2381  
10              
11             sub new
12             {
13 50044     50044 0 143115 my ($class, $data, %p) = @_;
14              
15 50044         85181 my %allowed_keys = map { $_ => 1 } qw(
  100088         203499  
16             mobile loc_prefix
17             );
18              
19 50044         106438 my @bad_keys = grep { !exists $allowed_keys{$_} } keys %p;
  100088         202005  
20 50044 50       120098 croak "Unknown parameters: @bad_keys" if @bad_keys;
21              
22 50044         140752 my $self = {
23             mobile => 0,
24             loc_prefix => '',
25             %p, # actual input values
26             };
27              
28 50044 100       95537 if (not ref $data) {
    50          
29 50041 50       89576 croak 'Web::Sitemap::Url first argument must be defined'
30             unless defined $data;
31 50041         76327 $self->{loc} = $data;
32             }
33             elsif (ref $data eq 'HASH') {
34 3 50       9 unless (defined $data->{loc}) {
35 0         0 croak 'Web::Sitemap::Url first argument hash must have `loc` key defined';
36             }
37 3         18 $self = {%$self, %$data};
38             }
39             else {
40 0         0 croak 'Web::Sitemap::Url first argument must be a string or a hash reference';
41             }
42              
43 50044         148160 return bless $self, $class;
44             }
45              
46             sub to_xml_string
47             {
48 50044     50044 0 85289 my ($self, %p) = @_;
49              
50             return sprintf(
51             "\n%s%s%s%s%s",
52             $self->{loc_prefix},
53             $self->{loc},
54             $self->{changefreq} ? sprintf('%s', $self->{changefreq}) : '',
55 50044 50       128234 $self->{mobile} ? '' : '',
    50          
56             $self->_images_xml_string
57             );
58             }
59              
60             sub _images_xml_string
61             {
62 50044     50044   71318 my ($self) = @_;
63              
64 50044         64972 my $result = '';
65              
66 50044 100       95243 if (defined $self->{images}) {
67 3         4 my $i = 1;
68 3         6 for my $image (@{$self->{images}{loc_list}}) {
  3         9  
69 8 100       21 my $loc = ref $image eq 'HASH' ? $image->{loc} : $image;
70              
71 8         13 my $caption = '';
72 8 100 66     33 if (ref $image eq 'HASH' and defined $image->{caption}) {
    100          
    50          
73 4         7 $caption = $image->{caption};
74             }
75             elsif (defined $self->{images}{caption_format_simple}) {
76 2         7 $caption = $self->{images}{caption_format_simple} . " $i";
77             }
78             elsif (defined $self->{images}{caption_format}) {
79 2         4 $caption = &{$self->{images}{caption_format}}($i);
  2         11  
80             }
81              
82 8         42 $result .= sprintf(
83             "\n%s
84             $loc, $caption
85             );
86              
87 8         17 $i++;
88             }
89             }
90              
91 50044         191396 return $result;
92             }
93              
94             1;
95              
96             __END__