File Coverage

blib/lib/WebService/StreetMapLink.pm
Criterion Covered Total %
statement 71 73 97.2
branch 14 20 70.0
condition 3 5 60.0
subroutine 19 19 100.0
pod 4 5 80.0
total 111 122 90.9


line stmt bran cond sub pod time code
1             package WebService::StreetMapLink;
2              
3 4     4   94261 use strict;
  4         9  
  4         188  
4              
5 4     4   24 use vars qw($VERSION);
  4         9  
  4         255  
6              
7             $VERSION = 0.21;
8              
9 4     4   17112 use Class::Factory::Util;
  4         2869  
  4         29  
10 4     4   4753 use URI;
  4         43694  
  4         168  
11              
12 4     4   4786 use Params::Validate qw( validate_with SCALAR UNDEF );
  4         57876  
  4         918  
13              
14              
15             my %CountryToClass;
16              
17             sub RegisterSubclass
18             {
19 8     8 0 18 my $class = shift;
20 8   100     48 my $priority = shift || 100;
21              
22 8         42 foreach my $country ( map { lc } $class->Countries )
  72         293  
23             {
24 72         75 push @{ $CountryToClass{$country} },
  72         288  
25             { class => $class,
26             priority => $priority,
27             };
28             }
29             }
30              
31             BEGIN
32             {
33 4     4   35 for my $class ( map { __PACKAGE__ . '::' . $_ } __PACKAGE__->subclasses )
  8         2406  
34             {
35 8     4   580 eval "use $class";
  4     4   2836  
  4         13  
  4         80  
  4         2118  
  4         11  
  4         61  
36 8 50       541 die $@ if $@;
37             }
38             }
39              
40 4         3521 use constant NEW_SPEC => { country => { type => SCALAR },
41             address => { type => UNDEF | SCALAR, optional => 1 },
42             city => { type => UNDEF | SCALAR, optional => 1 },
43             state => { type => UNDEF | SCALAR, optional => 1 },
44             postal_code => { type => UNDEF | SCALAR, optional => 1 },
45             subclass => { type => UNDEF | SCALAR, optional => 1 },
46 4     4   25 };
  4         8  
47              
48             sub new
49             {
50 9     9 1 24445 my $class = shift;
51 9         546 my %p = validate_with( params => \@_,
52             spec => NEW_SPEC,
53             allow_extra => 1,
54             );
55              
56 9         74 my $subclass;
57 9 100       45 if ( $p{subclass} )
58             {
59 4 50       23 if ( $p{subclass} =~ /::/ )
60             {
61 0         0 $subclass = $p{subclass};
62             }
63             else
64             {
65 4         13 $subclass = __PACKAGE__ . '::' . $p{subclass};
66             }
67              
68 4         38 delete $p{subclass};
69             }
70              
71 9 100       33 unless ($subclass)
72             {
73 5         22 my $country = $class->_sanitize_country( $p{country} );
74              
75 5 50       17 return unless exists $CountryToClass{$country};
76              
77 0         0 my $entry =
78 5         10 (sort { $a->{priority} <=> $b->{priority} } @{ $CountryToClass{$country} })[0];
  5         17  
79 5         13 $subclass = $entry->{class};
80              
81 5         10 $p{country} = $country;
82             }
83              
84 9         75 return $subclass->new(%p);
85             }
86              
87             sub _sanitize_country
88             {
89 11     11   2367 shift;
90 11         19 my $country = shift;
91              
92 11 100       82 if ( $country =~ /united\s+states|u\.s\.(?:a\.)?/i )
93             {
94 4         10 $country = 'usa';
95             }
96              
97 11 100       51 if ( $country =~ /united\s+kingdom|u\.k\./i )
98             {
99 2         5 $country = 'uk';
100             }
101              
102 11         59 return lc $country;
103             }
104              
105             sub uri_object
106             {
107 18     18 1 20982 my $self = shift;
108              
109 18         96 my $uri = URI->new( '' );
110              
111 18         19471 foreach my $k ( qw( scheme host path ) )
112             {
113 54         11664 my $m = "_$k";
114 54 50       347 $uri->$k( $self->$m() )
115             if $self->can($m);
116             }
117              
118 18 50       577 $uri->query_form( $self->_query )
119             if $self->can('_query');
120              
121 18         2368 return $uri->canonical;
122             }
123              
124             sub uri
125             {
126 9     9 1 66 $_[0]->uri_object->as_string;
127             }
128              
129 18 50   18   141 sub _scheme { $_[0]->{scheme} ? $_[0]->{scheme} : 'http' }
130              
131 18     18   79 sub _host { $_[0]->{host} }
132 18     18   84 sub _path { $_[0]->{path} }
133 18     18   87 sub _query { $_[0]->{query} }
134              
135             sub service_name
136             {
137 1     1 1 13 my $self = shift;
138              
139 1   33     5 my $class = ref $self || $self;
140              
141 1         15 return ( split /::/, $class )[-1];
142             }
143              
144              
145             1;
146              
147             __END__