File Coverage

blib/lib/Geo/Coder/All.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Geo::Coder::All;
2 5     5   114179 use Moose;
  0            
  0            
3             use namespace::autoclean;
4             use Module::Runtime qw(require_module);
5              
6             my %VALID_GEOCODER_LIST = map { $_ => 1} qw(
7             Google
8             OSM
9             TomTom
10             Ovi
11             Bing
12             );
13             has 'geocoder' => (is=>'rw',isa=>'Str',default=>'Google');
14             has 'key' => (is=>'rw',isa=>'Str',default=>'', reader=>'get_key');
15             has 'langauge' => (is=>'rw',isa=>'Str',default=>'en', reader=>'get_language', init_arg=>'language');
16             has 'google_client' => (is=>'rw',isa=>'Str',default=>'', reader=>'get_google_client', init_arg=>'client');
17             has 'google_apiver' => (is=>'rw',isa=>'Num',default=>3, reader=>'get_google_apiver', init_arg=>'apiver');
18             has 'google_encoding' => (is=>'rw',isa=>'Str',default=>'utf8',reader=>'get_google_encoding', init_arg=>'encoding');
19             has 'google_country_code' => (is=>'rw',isa=>'Str',default=>'', reader=>'get_google_country_code', init_arg=>'country_code');
20             has 'google_sensor' => (is=>'rw',isa=>'Str',default=>'', reader=>'get_google_sensor', init_arg=>'sensor');
21              
22             has 'geocoder_engine' => (
23             is => 'rw',
24             init_arg => undef,
25             lazy => 1,
26             isa => 'Object',
27             builder => '_build_geocoder_engine',
28             handles =>{
29             geocode => 'geocode_local',
30             reverse_geocode => 'reverse_geocode_local'
31             }
32             );
33              
34             sub _build_geocoder_engine {
35             my $self = shift;
36             my $geocoder = $self->geocoder;
37            
38             if(!$VALID_GEOCODER_LIST{$geocoder} && $geocoder !~ /::/ ){
39             $geocoder = 'Google';
40             $self->geocoder('Google');
41             }
42            
43             my $class = ($geocoder =~ /::/ ? $geocoder : 'Geo::Coder::All::'.$geocoder);
44             require_module($class);
45             return $class->new();
46             }
47              
48             around 'geocode' => sub{
49             my ($orig,$class,$rh_args) = @_;
50             return $class->$orig($class->_process_args($rh_args));
51             };
52              
53             around 'reverse_geocode' => sub{
54             my ($orig,$class,$rh_args) = @_;
55             return $class->$orig($class->_process_args($rh_args));
56             };
57             #process the args passed to create new Geo::Coder::Google
58             sub _process_args {
59             my ($self,$rh_args) =@_;
60             $rh_args->{key} ||= $self->get_key;
61             $rh_args->{language} ||= $self->get_language;
62             $rh_args->{google_apiver}= $self->get_google_apiver || $rh_args->{apiver} if($self->geocoder eq 'Gooole');
63             $rh_args->{google_client}= $self->get_google_client || $rh_args->{client} if($self->geocoder eq 'Google');
64             $rh_args->{google_encoding}= $self->get_google_encoding || $rh_args->{encoding} if($self->geocoder eq 'Google');
65             $rh_args->{google_country_code}= $self->get_google_country_code || $rh_args->{country_code} if($self->geocoder eq 'Google');
66             return $rh_args;
67             }
68              
69             =head1 NAME
70              
71             Geo::Coder::All - Geo::Coder::All
72              
73             =head1 VERSION
74              
75             Version 0.06
76              
77             =cut
78              
79             our $VERSION = '0.06';
80              
81              
82             =head1 DESCRIPTION
83              
84             Geo::Coder::All is wrapper for other geocoder cpan modules such as Geo::Coder::Google,Geo::Coder::Bing,Geo::Coder::Ovi,Geo::Coder::OSM and Geo::Coder::TomTom. Geo::Coder::All provides common geocode output format for all geocoder.
85              
86              
87             =head1 SYNOPSIS
88              
89             use Geo::Coder::All;
90             #For google geocoder
91             my $google_geocoder = Geo::Coder::All->new();#geocoder defaults to Geo::Coder::Google::V3
92             #You can also use optional params for google api
93             my $google_geocoder = Geo::Coder::All->new(key=>'GMAP_KEY',client=>'GMAP_CLIENT');
94              
95             #For Bing
96             my $bing_geocoder = Geo::Coder::All->new(geocoder=>'Bing',key=>'BING_API_KEY');
97              
98             #For Ovi
99             my $ovi_geocoder = Geo::Coder::All->new(geocoder=>'Ovi');
100              
101             #For OSM
102             my $osm_geocoder = Geo::Coder::All->new(geocoder=>'OSM');
103              
104             #For TomTom
105             my $tomtom_geocoder = Geo::Coder::All->new(geocoder=>'TomTom');
106              
107             #Currently supported geocoders are
108             Geo::Coder::Google
109             Geo::Coder::Bing
110             Geo::Coder::TomTom
111             Geo::Coder::Ovi
112             Geo::Coder::OSM
113             #only Geo::Coder::Google is installed by default if you need to use other then you should install them manually
114              
115             #IF you want use geocder that is not listed above then you can now specify fully qualified class wrapper name to add your own custom handling for response. Please have look at how Geo::Coder::All::Google is working.
116              
117             =head1 METHODS
118              
119             Geo::Coder::All offers geocode and reverse_geocode methods
120              
121             =over 2
122              
123             =item geocode
124              
125             For Google geocoder , we can directly set the different geocoding options when calling geocode and reverse_geocode methods. i.e If you use Geo::Coder::Google you will have to create new instance every single time you need to change geocoding options
126              
127             $rh_location = $google_geocoder->geocode({location => 'London'});
128             #above will return London from United Kingdom
129             #With geocoding options
130             #Following will return London from Canada as we used country_code is ca (country_code is ISO 3166-1 )
131             $rh_location = $google_geocoder->geocode({location => 'London',language=>'en',country_code=>'ca',encoding=>'utf8',sensor=>1});
132             #in spanish
133             $rh_location = $google_geocoder->geocode({location => 'London',language=>'es',country_code=>'ca',encoding=>'utf8',sensor=>1});
134             #default encodings is set to 'utf8' you can change to other such as 'latin1'
135             #You can also set DEGUB=>1 to dump raw response from the geocoder api
136              
137             You cal also set GMAP_KEY and GMAP_CLIENT directly from geocode/reverse_geocode method and it will just work
138              
139             =item reverse_geocode
140              
141             For Google reverse_geocoder
142              
143             $rh_location = $google_geocoder->reverse_geocode({latlng=>'51.508515,-0.1254872',language=>'en',encoding=>'utf8',sensor=>1})
144             #in spanish
145             $rh_location = $google_geocoder->reverse_geocode({latlng=>'51.508515,-0.1254872',language=>'es',encoding=>'utf8',sensor=>1})
146            
147             =back
148              
149             =head1 SEE ALSO
150              
151             L<Geo::Coder::Many>,
152             L<Geo::Coder::Google>,
153             L<Geo::Coder::Bing>,
154             L<Geo::Coder::Ovi>,
155             L<Geo::Coder::OSM> and
156             L<Geo::Coder::TomTom>.
157              
158             =head1 AUTHOR
159              
160             Rohit Deshmukh, C<< <raigad1630 at gmail.com> >>
161              
162             =head1 BUGS
163              
164             Please report any bugs or feature requests to C<bug-geo-coder-all at rt.cpan.org>, or through
165             the web interface at L<https://github.com/raigad/geo-coder-all/issues>. I will be notified, and then you'll
166             automatically be notified of progress on your bug as I make changes.
167              
168             =head1 SUPPORT
169              
170             You can find documentation for this module with the perldoc command.
171              
172             perldoc Geo::Coder::All
173              
174             =head1 ACKNOWLEDGEMENTS
175              
176             Peter Sergeant, C<< <sargie@cpan.org> >>
177              
178             Neil Bowers, C<< <neilb@cpan.org> >>
179              
180             =head1 LICENSE AND COPYRIGHT
181              
182             Copyright 2014 Rohit Deshmukh.
183              
184             =cut
185              
186             1; # End of Geo::Coder::All