| 1 | package HTTP::MobileAgent::Plugin::Locator; |
|---|
| 2 | |
|---|
| 3 | use warnings; |
|---|
| 4 | use strict; |
|---|
| 5 | use HTTP::MobileAgent; |
|---|
| 6 | use Carp; |
|---|
| 7 | use UNIVERSAL::require; |
|---|
| 8 | use UNIVERSAL::can; |
|---|
| 9 | |
|---|
| 10 | our $VERSION = '0.01'; |
|---|
| 11 | |
|---|
| 12 | our $DOCOMO_GPS_COMPLIANT_MODELS = qr/(?:903i(?!TV|X)|(?:90[45]|SA[78]0[02])i)/; |
|---|
| 13 | |
|---|
| 14 | sub import { |
|---|
| 15 | my $class = shift; |
|---|
| 16 | no strict 'refs'; |
|---|
| 17 | *{"HTTP\::MobileAgent\::gps_compliant"} = \&_gps_compliant; |
|---|
| 18 | *{"HTTP\::MobileAgent\::locator"} = sub { $class->new( @_ ) }; |
|---|
| 19 | *{"HTTP\::MobileAgent\::get_location"} = sub { |
|---|
| 20 | my ( $self, $stuff ) = @_; |
|---|
| 21 | my $params = _prepare_params( $stuff ); |
|---|
| 22 | $self->locator( $params )->get_location( _prepare_params( $params ) ); |
|---|
| 23 | }; |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | sub _gps_compliant { |
|---|
| 27 | my $self = shift; |
|---|
| 28 | if ( $self->is_docomo ) { |
|---|
| 29 | return $self->model =~ $DOCOMO_GPS_COMPLIANT_MODELS; |
|---|
| 30 | } elsif ( $self->is_ezweb ) { |
|---|
| 31 | my @specs = split //, $ENV{ HTTP_X_UP_DEVCAP_MULTIMEDIA } || ''; |
|---|
| 32 | return defined $specs[ 1 ] && $specs[ 1 ] =~ /^[23]$/; |
|---|
| 33 | } elsif ( $self->is_softbank ) { |
|---|
| 34 | return $self->is_type_3gc; |
|---|
| 35 | } |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | sub new { |
|---|
| 39 | my ( $class, $agent, $params ) = @_; |
|---|
| 40 | |
|---|
| 41 | my $sub; |
|---|
| 42 | if ( $agent->is_docomo ) { |
|---|
| 43 | # BasicLocation send back $params->{AREACODE} |
|---|
| 44 | $sub = ( !defined $params->{AREACODE} ) ? 'DoCoMo::GPS' |
|---|
| 45 | : 'DoCoMo::BasicLocation'; |
|---|
| 46 | } |
|---|
| 47 | elsif ( $agent->is_ezweb ) { |
|---|
| 48 | # GPS lat format is +35.39.01.22 |
|---|
| 49 | # Basic lat format is 35.39.01.22 |
|---|
| 50 | $sub = ( $params->{lat} =~ /^[-+]/ ) ? 'EZweb::GPS' |
|---|
| 51 | : 'EZweb::BasicLocation'; |
|---|
| 52 | } |
|---|
| 53 | elsif ( $agent->is_softbank ) { |
|---|
| 54 | $sub = ( defined $params->{pos} ) ? 'SoftBank::GPS' |
|---|
| 55 | : 'SoftBank::BasicLocation'; |
|---|
| 56 | } |
|---|
| 57 | elsif ( $agent->is_airh_phone ) { |
|---|
| 58 | $sub = 'Willcom::BasicLocation'; |
|---|
| 59 | } |
|---|
| 60 | else { |
|---|
| 61 | croak( "Invalid mobile user agent: " . $agent->user_agent ); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | my $locator_class = "HTTP::MobileAgent::Plugin::Locator\::$sub"; |
|---|
| 65 | $locator_class->require or die $!; |
|---|
| 66 | return bless {}, $locator_class; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | sub get_location { die "ABSTRACT METHOD" } |
|---|
| 70 | |
|---|
| 71 | sub _prepare_params { |
|---|
| 72 | my $stuff = shift; |
|---|
| 73 | if ( ref $stuff && eval { $stuff->can( 'param' ) } ) { |
|---|
| 74 | return +{ map { $_ => $stuff->param( $_ ) } $stuff->param }; |
|---|
| 75 | } |
|---|
| 76 | else { |
|---|
| 77 | return $stuff; |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | 1; |
|---|
| 82 | __END__ |
|---|
| 83 | |
|---|
| 84 | =head1 NAME |
|---|
| 85 | |
|---|
| 86 | HTTP::MobileAgent::Plugin::Locator - Handling mobile location information plugin for HTTP::MobileAgent |
|---|
| 87 | |
|---|
| 88 | =head1 SYNOPSIS |
|---|
| 89 | |
|---|
| 90 | use CGI; |
|---|
| 91 | use HTTP::MobileAgent; |
|---|
| 92 | use HTTP::MobileAgent::Plugin::Locator; |
|---|
| 93 | |
|---|
| 94 | # get location is Geo::Coordinates::Converter::Point instance formatted wgs84 |
|---|
| 95 | my $q = CGI->new; |
|---|
| 96 | my $agent = HTTP::MobileAgent->new; |
|---|
| 97 | my $location = $agent->get_location( $q ); |
|---|
| 98 | |
|---|
| 99 | print "lat is " . $location->lat; |
|---|
| 100 | print "lng is " . $location->lng; |
|---|
| 101 | |
|---|
| 102 | =head1 METHODS |
|---|
| 103 | |
|---|
| 104 | =over |
|---|
| 105 | |
|---|
| 106 | =item get_location([params]); |
|---|
| 107 | |
|---|
| 108 | return Geo::Coordinates::Converter::Point instance formatted if specify gps or basic location parameters sent from carrier. The parameters are different by each carrier. |
|---|
| 109 | |
|---|
| 110 | This method accept a Apache instance, CGI instance or hashref of query parameters. |
|---|
| 111 | |
|---|
| 112 | =item gps_compliant() |
|---|
| 113 | |
|---|
| 114 | returns if the agent is GPS compliant. |
|---|
| 115 | |
|---|
| 116 | =back |
|---|
| 117 | |
|---|
| 118 | =head1 CLASSES |
|---|
| 119 | |
|---|
| 120 | =over |
|---|
| 121 | |
|---|
| 122 | =item HTTP::MobileAgent::Plugin::Locator::DoCoMo::BasicLocation |
|---|
| 123 | |
|---|
| 124 | for iArea data support. |
|---|
| 125 | |
|---|
| 126 | =item HTTP::MobileAgent::Plugin::Locator::DoCoMo::GPS |
|---|
| 127 | |
|---|
| 128 | for GPS data support. |
|---|
| 129 | |
|---|
| 130 | =item HTTP::MobileAgent::Plugin::Locator::EZweb::BasicLocation |
|---|
| 131 | |
|---|
| 132 | for basic location information data support. |
|---|
| 133 | |
|---|
| 134 | =item HTTP::MobileAgent::Plugin::Locator::EZweb::GPS |
|---|
| 135 | |
|---|
| 136 | for EZnavi data support. |
|---|
| 137 | |
|---|
| 138 | =item HTTP::MobileAgent::Plugin::Locator::SoftBank::BasicLocation |
|---|
| 139 | |
|---|
| 140 | for basic location information data support. |
|---|
| 141 | |
|---|
| 142 | =item HTTP::MobileAgent::Plugin::Locator::SoftBank::GPS |
|---|
| 143 | |
|---|
| 144 | for GPS data support. |
|---|
| 145 | |
|---|
| 146 | =item HTTP::MobileAgent::Plugin::Locator::Willcom::BasicLocation |
|---|
| 147 | |
|---|
| 148 | for basic location information data support. |
|---|
| 149 | |
|---|
| 150 | =back |
|---|
| 151 | |
|---|
| 152 | =head1 EXAMPLES |
|---|
| 153 | |
|---|
| 154 | There is request template using C<Template> in eg directory and mod_rewrite configuration for ezweb extraordinary parameter handling. |
|---|
| 155 | |
|---|
| 156 | =head1 AUTHOR |
|---|
| 157 | |
|---|
| 158 | Yoshiki Kurihara E<lt>kurihara __at__ cpan.orgE<gt> with many feedbacks and changes from: |
|---|
| 159 | |
|---|
| 160 | Tokuhiro Matsuno E<lt>tokuhiro __at__ mobilefactory.jpE<gt> |
|---|
| 161 | |
|---|
| 162 | =head1 SEE ALSO |
|---|
| 163 | |
|---|
| 164 | C<HTTP::MobileAgent>, C<Geo::Coordinates::Converter>, C<Geo::Coordinates::Converter::Point>, C<Geo::Coordinates::Converter::iArea>, C<http://coderepos.org/share/log/lang/perl/HTTP-MobileAgent-Plugin-Locator/> |
|---|
| 165 | |
|---|
| 166 | =head1 LICENCE AND COPYRIGHT |
|---|
| 167 | |
|---|
| 168 | Copyright (c) 2008, Yoshiki Kurihara E<lt>kurihara __at__ cpan.orgE<gt>. All rights reserved. |
|---|
| 169 | |
|---|
| 170 | This module is free software; you can redistribute it and/or |
|---|
| 171 | modify it under the same terms as Perl itself. See L<perlartistic>. |
|---|
| 172 | |
|---|
| 173 | =head1 DISCLAIMER OF WARRANTY |
|---|
| 174 | |
|---|
| 175 | BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY |
|---|
| 176 | FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN |
|---|
| 177 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES |
|---|
| 178 | PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER |
|---|
| 179 | EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|---|
| 180 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE |
|---|
| 181 | ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH |
|---|
| 182 | YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL |
|---|
| 183 | NECESSARY SERVICING, REPAIR, OR CORRECTION. |
|---|
| 184 | |
|---|
| 185 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING |
|---|
| 186 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR |
|---|
| 187 | REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE |
|---|
| 188 | LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, |
|---|
| 189 | OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE |
|---|
| 190 | THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING |
|---|
| 191 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A |
|---|
| 192 | FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF |
|---|
| 193 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF |
|---|
| 194 | SUCH DAMAGES. |
|---|