line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::ErnestMarples; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
68612
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
61
|
|
4
|
2
|
|
|
2
|
|
11
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
65
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
14
|
use Carp; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
283
|
|
7
|
2
|
|
|
2
|
|
2637
|
use HTML::Tiny; |
|
2
|
|
|
|
|
7106
|
|
|
2
|
|
|
|
|
78
|
|
8
|
2
|
|
|
2
|
|
4391
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
151935
|
|
|
2
|
|
|
|
|
129
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
WWW::ErnestMarples - Interface to the ernestmarples.com UK postcode lookup API |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 VERSION |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
This document describes WWW::ErnestMarples version 0.01 |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=cut |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
21
|
|
|
|
|
|
|
|
22
|
2
|
|
|
2
|
|
25
|
use constant SERVICE => 'http://ernestmarples.com/'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1699
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 SYNOPSIS |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
use WWW::ErnestMarples; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
my $em = WWW::ErnestMarples->new; |
29
|
|
|
|
|
|
|
my ( $lat, $lon ) = $em->lookup('CA9 3NT'); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head1 INTERFACE |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head2 C<< new >> |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
Create a new C. Accepts named arguments. The only |
36
|
|
|
|
|
|
|
argument currently supported is C which gives the base URL of |
37
|
|
|
|
|
|
|
the lookup service. Defaults to C. |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
my $em = WWW::ErnestMarples->new( |
40
|
|
|
|
|
|
|
service => 'http://localhost/emtest.cgi' |
41
|
|
|
|
|
|
|
); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
For normal use pass no args: |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
my $em = WWW::ErnestMarples->new; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=cut |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub new { |
50
|
1
|
|
|
1
|
1
|
12
|
my $class = shift; |
51
|
1
|
50
|
|
|
|
6
|
croak "Expected a number of key => value pairs" if @_ % 1; |
52
|
1
|
|
|
|
|
2
|
my %args = @_; |
53
|
1
|
|
|
|
|
2
|
my $service = delete $args{service}; |
54
|
1
|
50
|
|
|
|
7
|
$service = $class->SERVICE unless defined $service; |
55
|
1
|
50
|
|
|
|
5
|
croak "Unknown options: ", join ', ', sort keys %args if keys %args; |
56
|
1
|
|
|
|
|
7
|
return bless { service => $service }, $class; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head2 C |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
Look up a UK postcode. The return value is a list containing latitude, |
62
|
|
|
|
|
|
|
longitude of the postcode. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
my ( $lat, $lon ) = $em->lookup( $my_postcode ); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=cut |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub lookup { |
69
|
1
|
|
|
1
|
1
|
5
|
my ( $self, $postcode ) = @_; |
70
|
|
|
|
|
|
|
|
71
|
1
|
|
|
|
|
5
|
my $resp |
72
|
|
|
|
|
|
|
= $self->_ua->get( $self->{service} . '?' |
73
|
|
|
|
|
|
|
. HTML::Tiny->new->query_encode( { p => $postcode, f => 'csv' } ) |
74
|
|
|
|
|
|
|
); |
75
|
|
|
|
|
|
|
|
76
|
1
|
50
|
|
|
|
157503
|
croak $resp->status_line if $resp->is_error; |
77
|
1
|
50
|
|
|
|
20
|
croak "Bad response from $self->{service}; is that a valid postcode?" |
78
|
|
|
|
|
|
|
unless $resp->content_type =~ m{^text/csv\b}; |
79
|
0
|
|
|
|
|
0
|
chomp( my $content = $resp->content ); |
80
|
0
|
|
|
|
|
0
|
my ( $pc, $lat, $lon ) = split /,/, $content; |
81
|
0
|
0
|
0
|
|
|
0
|
croak "Bad response from $self->{service}; could not parse response" |
|
|
|
0
|
|
|
|
|
82
|
|
|
|
|
|
|
unless defined $lon |
83
|
|
|
|
|
|
|
&& $lat =~ /^-?\d+(?:\.\d+)?$/ |
84
|
|
|
|
|
|
|
&& $lon =~ /^-?\d+(?:\.\d+)?$/; |
85
|
0
|
|
|
|
|
0
|
return $lat, $lon; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub _ua { |
89
|
1
|
|
|
1
|
|
2
|
my $self = shift; |
90
|
1
|
|
33
|
|
|
10
|
return $self->{_ua} ||= do { |
91
|
1
|
|
|
|
|
11
|
my $ua = LWP::UserAgent->new; |
92
|
1
|
|
|
|
|
3376
|
$ua->agent( sprintf( '%s %s', __PACKAGE__, $VERSION ) ); |
93
|
1
|
|
|
|
|
87
|
$ua; |
94
|
|
|
|
|
|
|
}; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
1; |
98
|
|
|
|
|
|
|
__END__ |