- Timestamp:
- 10/17/08 20:01:36 (5 years ago)
- Location:
- lang/perl/HTTP-MobileAgent-Plugin-Locator/trunk
- Files:
-
- 2 added
- 4 modified
-
.shipit (added)
-
lib/HTTP/MobileAgent/Plugin/Locator.pm (modified) (11 diffs)
-
t/01.get_location.t (modified) (4 diffs)
-
t/02.gps_comliant.t (modified) (1 diff)
-
t/07.gps_parameter.t (modified) (2 diffs)
-
t/09.get_carrier_locator.t (added)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/HTTP-MobileAgent-Plugin-Locator/trunk/lib/HTTP/MobileAgent/Plugin/Locator.pm
r11677 r21515 12 12 our %EXPORT_TAGS = (locator => [@EXPORT_OK]); 13 13 14 our $VERSION = '0.0 1';15 16 our $DOCOMO_GPS_COMPLIANT_MODELS = qr/(?:903i(?!TV|X)|(?:90[4 5]|SA[78]0[02])i)/;14 our $VERSION = '0.02'; 15 16 our $DOCOMO_GPS_COMPLIANT_MODELS = qr/(?:903i(?!TV|X)|(?:90[4-6]|SA[78]0[02])i)/; 17 17 18 18 our $LOCATOR_AUTO_FROM_COMPLIANT = 1; … … 25 25 my ( $class ) = @_; 26 26 no strict 'refs'; 27 *{"HTTP\::MobileAgent\::gps_compliant"} = \&_gps_compliant; 28 *{"HTTP\::MobileAgent\::gps_parameter"} = \&_gps_parameter; 29 *{"HTTP\::MobileAgent\::locator"} = sub { $class->new( @_ ) }; 30 *{"HTTP\::MobileAgent\::get_location"} = sub { 27 *HTTP::MobileAgent::gps_compliant = \&_gps_compliant; 28 *HTTP::MobileAgent::locator = sub { $class->new( @_ ) }; 29 *HTTP::MobileAgent::get_location = sub { 31 30 my ( $self, $stuff, $option_ref ) = @_; 32 31 my $params = _prepare_params( $stuff ); 33 32 $self->locator( $params, $option_ref )->get_location( $params ); 34 33 }; 35 36 $class->export_to_level(1, @_); 37 } 34 $class->export_to_level( 1, @_ ); 35 } 36 37 sub new { 38 my ( $class, $agent, $params, $option_ref ) = @_; 39 my $carrier_locator = _get_carrier_locator( $agent, $params, $option_ref ); 40 my $locator_class = "HTTP::MobileAgent::Plugin::Locator::$carrier_locator"; 41 $locator_class->require or die $!; 42 return bless {}, $locator_class; 43 } 44 45 sub get_location { die "ABSTRACT METHOD" } 38 46 39 47 sub _gps_compliant { … … 41 49 if ( $self->is_docomo ) { 42 50 return $self->model =~ $DOCOMO_GPS_COMPLIANT_MODELS; 43 } elsif ( $self->is_ezweb ) { 51 } 52 elsif ( $self->is_ezweb ) { 44 53 my @specs = split //, $ENV{ HTTP_X_UP_DEVCAP_MULTIMEDIA } || ''; 45 54 return defined $specs[ 1 ] && $specs[ 1 ] =~ /^[23]$/; 46 } elsif ( $self->is_softbank ) { 55 } 56 elsif ( $self->is_softbank ) { 47 57 return $self->is_type_3gc; 48 58 } 49 59 } 50 60 51 52 sub _gps_parameter { 53 my ( $self, $stuff ) = @_; 54 55 my $params = _prepare_params( $stuff ); 56 57 if ( $self->is_docomo ) { 58 return (!defined $params->{AREACODE}) ? 1 : 0; 59 } 60 elsif ( $self->is_ezweb ) { 61 return ( $params->{datum} =~ /^\d+$/ ) ? 1 : 0; 62 } 63 elsif ( $self->is_softbank ) { 64 return ( defined $params->{pos} ) ? 1 : 0; 65 } 66 elsif ( $self->is_airh_phone ) { 67 return 0; 68 } 69 else { 70 croak( "Invalid mobile user agent: " . $self->user_agent ); 71 } 72 } 73 74 75 sub new { 76 my ( $class, $agent, $params, $option_ref ) = @_; 77 78 79 my $sub_locator = _get_sub_locator($agent, $params, $option_ref); 80 81 my $locator_class = "HTTP::MobileAgent::Plugin::Locator\::$sub_locator"; 82 $locator_class->require or die $!; 83 return bless {}, $locator_class; 84 } 85 86 sub get_location { die "ABSTRACT METHOD" } 87 88 sub _get_sub_locator { 61 sub _get_carrier_locator { 89 62 my ( $agent, $params, $option_ref ) = @_; 90 63 91 my $carrier = ( $agent->is_docomo ) ? 'DoCoMo' : 92 ( $agent->is_ezweb ) ? 'EZweb' : 93 ( $agent->is_softbank ) ? 'SoftBank' : 94 ( $agent->is_airh_phone ) ? 'Willcom' : undef 95 ; 96 if ( !$carrier ) { 97 croak( "Invalid mobile user agent: " . $agent->user_agent ); 98 } 64 my $carrier = $agent->is_docomo ? 'DoCoMo' 65 : $agent->is_ezweb ? 'EZweb' 66 : $agent->is_softbank ? 'SoftBank' 67 : $agent->is_airh_phone ? 'Willcom' 68 : undef; 69 croak( "Invalid mobile user agent: " . $agent->user_agent ) if !$carrier; 99 70 100 71 my $locator; 101 if ( !defined $option_ref 102 || !defined $option_ref->{locator} 103 || $option_ref->{locator} eq $LOCATOR_AUTO_FROM_COMPLIANT ) 104 { 105 $locator = ( $agent->gps_compliant ) ? 'GPS' : 'BasicLocation'; 72 if ( !defined $option_ref 73 || !defined $option_ref->{locator} 74 || $option_ref->{locator} eq $LOCATOR_AUTO_FROM_COMPLIANT ) { 75 $locator = $agent->gps_compliant ? 'GPS' : 'BasicLocation'; 106 76 } 107 77 elsif ( $option_ref->{locator} eq $LOCATOR_AUTO ) { 108 $locator = ( $agent->gps_parameter( $params )) ? 'GPS' : 'BasicLocation';78 $locator = _is_gps_parameter( $agent, $params ) ? 'GPS' : 'BasicLocation'; 109 79 } 110 80 elsif ( $option_ref->{locator} eq $LOCATOR_GPS ) { 111 $locator = 'GPS';81 $locator = 'GPS'; 112 82 } 113 83 elsif ( $option_ref->{locator} eq $LOCATOR_BASIC ) { … … 119 89 120 90 return $carrier . '::' . $locator; 91 } 92 93 # to check whether parameter is gps or basic 94 sub _is_gps_parameter { 95 my ( $agent, $stuff ) = @_; 96 my $params = _prepare_params( $stuff ); 97 if ( $agent->is_docomo ) { 98 return !defined $params->{ AREACODE }; 99 } 100 elsif ( $agent->is_ezweb ) { 101 return defined $params->{ datum } && $params->{ datum } =~ /^\d+$/ 102 } 103 elsif ( $agent->is_softbank ) { 104 return defined $params->{ pos }; 105 } 106 elsif ( $agent->is_airh_phone ) { 107 return; 108 } 109 else { 110 croak( "Invalid mobile user agent: " . $agent->user_agent ); 111 } 121 112 } 122 113 … … 124 115 my $stuff = shift; 125 116 if ( ref $stuff && eval { $stuff->can( 'param' ) } ) { 126 return +{ map { $_ => (scalar(@{[$stuff->param($_)]}) > 1) ? [$stuff->param( $_ )] : $stuff->param($_) } $stuff->param }; 117 return +{ map { 118 $_ => ( scalar(@{[$stuff->param($_)]}) > 1 ) ? [ $stuff->param( $_ ) ] 119 : $stuff->param( $_ ) 120 } $stuff->param }; 127 121 } 128 122 else { … … 144 138 use HTTP::MobileAgent::Plugin::Locator; 145 139 140 $q = CGI->new; 141 $agent = HTTP::MobileAgent->new; 142 146 143 # get location is Geo::Coordinates::Converter::Point instance formatted wgs84 147 my $q = CGI->new; 148 my $agent = HTTP::MobileAgent->new; 149 my $location = $agent->get_location( $q ); 150 144 # ./t/* has many examples. 145 $location = $agent->get_location( $q ); 146 # or 147 $location = $agent->get_location( { lat => '35.21.03.342', 148 lon => '138.34.45.725', 149 geo => 'wgs84' } ); 150 # or 151 $location = $agent->get_location( $q, { locator => $LOCATOR_GPS } ); 152 153 # get latitude and longitude 151 154 print "lat is " . $location->lat; 152 155 print "lng is " . $location->lng; … … 154 157 =head1 METHODS 155 158 156 157 159 =head2 get_location([params], $option_ref); 158 160 … … 163 165 =over 164 166 165 =item $option_ref {locator}167 =item $option_ref->{locator} 166 168 167 169 select locator class algorithm option. 168 170 169 171 $LOCATOR_AUTO_FROM_COMPLIANT 170 auto detect locator from gps compliant. this is I<default>.172 auto detect locator from gps compliant. This is I<default>. 171 173 172 174 $LOCATOR_AUTO … … 181 183 =back 182 184 183 184 185 185 =head2 gps_compliant() 186 186 187 187 returns if the agent is GPS compliant. 188 188 189 190 191 =head2 gps_parameter([params])192 193 returns if the params is GPS request.194 195 196 189 =head1 CLASSES 197 190 … … 237 230 238 231 Tokuhiro Matsuno E<lt>tokuhiro __at__ mobilefactory.jpE<gt> 232 Masahiro Chiba E<lt>chiba __at__ geminium.comE<gt> 239 233 240 234 =head1 SEE ALSO … … 242 236 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/> 243 237 244 =head1 LICENCE AND COPYRIGHT 245 246 Copyright (c) 2008, Yoshiki Kurihara E<lt>kurihara __at__ cpan.orgE<gt>. All rights reserved. 238 =head1 LICENCE 247 239 248 240 This module is free software; you can redistribute it and/or 249 241 modify it under the same terms as Perl itself. See L<perlartistic>. 250 242 251 =head1 DISCLAIMER OF WARRANTY 252 253 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 254 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 255 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 256 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER 257 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 258 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE 259 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH 260 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL 261 NECESSARY SERVICING, REPAIR, OR CORRECTION. 262 263 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 264 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 265 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE 266 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, 267 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE 268 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 269 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 270 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 271 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 272 SUCH DAMAGES. 243 =cut -
lang/perl/HTTP-MobileAgent-Plugin-Locator/trunk/t/01.get_location.t
r7422 r21515 50 50 $for_basic_on_gps_option_refs, 51 51 { AREACODE => '05902' }, 52 { lat => '35.39. 43.535', lng => '139.44.06.233' }52 { lat => '35.39.52.909', lng => '139.43.52.172' } 53 53 ); 54 55 54 } 56 55 … … 66 65 $for_basic_option_refs, 67 66 { AREACODE => '05902' }, 68 { lat => '35.39. 43.535', lng => '139.44.06.233' }67 { lat => '35.39.52.909', lng => '139.43.52.172' } 69 68 ); 70 69 } … … 74 73 local $ENV{HTTP_USER_AGENT} = 'KDDI-CA31 UP.Browser/6.2.0.7.3.129 (GUI) MMP/2.0'; 75 74 local $ENV{HTTP_X_UP_DEVCAP_MULTIMEDIA} = '0200000000000000'; 76 75 77 76 locator_test( 78 77 $for_gps_option_refs, … … 105 104 { lat => '35.21.03.342', lng => '138.34.45.725' } 106 105 ); 107 106 108 107 local $ENV{ HTTP_X_JPHONE_GEOCODE } = '352051%1a1383456%1afoo'; 109 108 locator_test( -
lang/perl/HTTP-MobileAgent-Plugin-Locator/trunk/t/02.gps_comliant.t
r4371 r21515 3 3 use HTTP::MobileAgent; 4 4 use HTTP::MobileAgent::Plugin::Locator; 5 6 { 7 local $ENV{HTTP_USER_AGENT} = 'DoCoMo/2.0 P906i(c100;TB;W24H15)'; 8 my $agent = HTTP::MobileAgent->new; 9 ok $agent->gps_compliant, "docomo gps"; 10 } 5 11 6 12 { -
lang/perl/HTTP-MobileAgent-Plugin-Locator/trunk/t/07.gps_parameter.t
r7422 r21515 10 10 11 11 # GPS 12 is $agent->gps_parameter({12 is HTTP::MobileAgent::Plugin::Locator::_is_gps_parameter( $agent, { 13 13 lat => '35.21.03.342', lon => '138.34.45.725', geo => 'wgs84' 14 } ) => 1;14 } ) => 1; 15 15 16 16 # Basic 17 is $agent->gps_parameter({18 AREACODE => '05902', LAT => '+35.39.43.538', LON => '+139.44.06.232', GEO => 'wgs84', XACC => 1 19 }) => 0;17 is HTTP::MobileAgent::Plugin::Locator::_is_gps_parameter( $agent, { 18 AREACODE => '05902', LAT => '+35.39.43.538', LON => '+139.44.06.232', GEO => 'wgs84', XACC => 1 19 }) => ''; 20 20 21 is $agent->gps_parameter({21 is HTTP::MobileAgent::Plugin::Locator::_is_gps_parameter( $agent, { 22 22 AREACODE => '05902' 23 }) => 0;23 }) => ''; 24 24 } 25 25 … … 31 31 32 32 # GPS 33 is $agent->gps_parameter({33 is HTTP::MobileAgent::Plugin::Locator::_is_gps_parameter( $agent, { 34 34 lat => '+35.21.03.342', lon => '+138.34.45.725', datum => '0' 35 35 }) => 1; 36 36 37 37 # Basic 38 is $agent->gps_parameter({38 is HTTP::MobileAgent::Plugin::Locator::_is_gps_parameter( $agent, { 39 39 lat => '35.21.03.342', lon => '138.34.45.725', datum => 'wgs84' 40 }) => 0; 41 40 }) => ''; 42 41 } 43 42 44 43 45 44 { # SoftBank 46 local $ENV{HTTP_USER_AGENT} = 'SoftBank/1.0/911T/TJ001 Browser/NetFront/3.3 Profile/MIDP-2.0 Configuration/CLDC-1.1'; 45 local $ENV{HTTP_USER_AGENT} = 46 'SoftBank/1.0/911T/TJ001 Browser/NetFront/3.3 Profile/MIDP-2.0 Configuration/CLDC-1.1'; 47 47 my $agent = HTTP::MobileAgent->new; 48 48 49 49 # GPS 50 is $agent->gps_parameter({51 pos => 'N35.21.03.342E138.34.45.725' 50 is HTTP::MobileAgent::Plugin::Locator::_is_gps_parameter( $agent, { 51 pos => 'N35.21.03.342E138.34.45.725' 52 52 }) => 1; 53 53 54 54 # Basic 55 55 local $ENV{ HTTP_X_JPHONE_GEOCODE } = '352051%1a1383456%1afoo'; 56 is $agent->gps_parameter({ 57 }) => 0; 58 56 is HTTP::MobileAgent::Plugin::Locator::_is_gps_parameter( $agent, { 57 }) => ''; 59 58 } 60 59
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)