Show
Ignore:
Timestamp:
03/03/08 22:06:57 (7 years ago)
Author:
chiba
Message:

lang/perl/HTTP-MobileAgent?-Plugin-Locator: merged from branches/get_locator_from_params r7332:r7419

Location:
lang/perl/HTTP-MobileAgent-Plugin-Locator/trunk
Files:
2 modified
1 copied

Legend:

Unmodified
Added
Removed
  • lang/perl/HTTP-MobileAgent-Plugin-Locator/trunk/lib/HTTP/MobileAgent/Plugin/Locator.pm

    r4616 r7422  
    88use UNIVERSAL::can; 
    99 
     10use base qw( Exporter ); 
     11our @EXPORT_OK = qw( $LOCATOR_AUTO_FROM_COMPLIANT $LOCATOR_AUTO $LOCATOR_GPS $LOCATOR_BASIC ); 
     12our %EXPORT_TAGS = (locator => [@EXPORT_OK]); 
     13 
    1014our $VERSION = '0.01'; 
    1115 
    1216our $DOCOMO_GPS_COMPLIANT_MODELS = qr/(?:903i(?!TV|X)|(?:90[45]|SA[78]0[02])i)/; 
    1317 
     18our $LOCATOR_AUTO_FROM_COMPLIANT = 1; 
     19our $LOCATOR_AUTO                = 2; 
     20our $LOCATOR_GPS                 = 3; 
     21our $LOCATOR_BASIC               = 4; 
     22 
     23 
    1424sub import { 
    15     my $class = shift; 
     25    my ( $class ) = @_; 
    1626    no strict 'refs'; 
    1727    *{"HTTP\::MobileAgent\::gps_compliant"} = \&_gps_compliant; 
    18     *{"HTTP\::MobileAgent\::locator"}       = sub { $class->new( shift ) }; 
     28    *{"HTTP\::MobileAgent\::gps_parameter"} = \&_gps_parameter; 
     29    *{"HTTP\::MobileAgent\::locator"}       = sub { $class->new( @_ ) }; 
    1930    *{"HTTP\::MobileAgent\::get_location"}  = sub { 
    20         my ( $self, $stuff ) = @_; 
    21         $self->locator->get_location( _prepare_params( $stuff ) ); 
     31        my ( $self, $stuff, $option_ref ) = @_; 
     32        my $params = _prepare_params( $stuff ); 
     33        $self->locator( $params, $option_ref )->get_location( $params ); 
    2234    }; 
     35 
     36    $class->export_to_level(1, @_); 
    2337} 
    2438 
     
    3549} 
    3650 
     51 
     52sub _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 
    3775sub new { 
    38     my ( $class, $agent ) = @_; 
    39  
    40     my $sub; 
    41     if ( $agent->is_docomo ) { 
    42         $sub = $agent->gps_compliant ? 'DoCoMo::GPS' 
    43                                      : 'DoCoMo::BasicLocation'; 
    44     } 
    45     elsif ( $agent->is_ezweb ) { 
    46         $sub = $agent->gps_compliant ? 'EZweb::GPS' 
    47                                      : 'EZweb::BasicLocation'; 
    48     } 
    49     elsif ( $agent->is_softbank ) { 
    50         $sub = $agent->gps_compliant ? 'SoftBank::GPS' 
    51                                      : 'SoftBank::BasicLocation'; 
    52     } 
    53     elsif ( $agent->is_airh_phone ) { 
    54         $sub = 'Willcom::BasicLocation'; 
    55     } 
    56     else { 
    57         croak( "Invalid mobile user agent: " . $agent->user_agent ); 
    58     } 
    59  
    60     my $locator_class = "HTTP::MobileAgent::Plugin::Locator\::$sub"; 
     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"; 
    6182    $locator_class->require or die $!; 
    6283    return bless {}, $locator_class; 
     
    6485 
    6586sub get_location { die "ABSTRACT METHOD" } 
     87 
     88sub _get_sub_locator { 
     89    my ( $agent, $params, $option_ref ) = @_; 
     90 
     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    } 
     99 
     100    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'; 
     106    } 
     107    elsif ( $option_ref->{locator} eq $LOCATOR_AUTO ) { 
     108        $locator = ( $agent->gps_parameter( $params ) ) ? 'GPS' : 'BasicLocation'; 
     109    } 
     110    elsif ( $option_ref->{locator} eq $LOCATOR_GPS ) { 
     111        $locator =  'GPS'; 
     112    } 
     113    elsif ( $option_ref->{locator} eq $LOCATOR_BASIC ) { 
     114        $locator = 'BasicLocation'; 
     115    } 
     116    else { 
     117        croak( "Invalid locator: " . $option_ref->{locator} ); 
     118    } 
     119 
     120    return $carrier . '::' . $locator; 
     121} 
    66122 
    67123sub _prepare_params { 
     
    98154=head1 METHODS 
    99155 
     156 
     157=head2 get_location([params], $option_ref); 
     158 
     159return Geo::Coordinates::Converter::Point instance formatted if specify gps or basic location parameters sent from carrier. The parameters are different by each carrier. 
     160 
     161This method accept a Apache instance, CGI instance or hashref of query parameters. 
     162 
    100163=over 
    101164 
    102 =item get_location([params]); 
    103  
    104 return Geo::Coordinates::Converter::Point instance formatted if specify gps or basic location parameters sent from carrier. The parameters are different by each carrier. 
    105  
    106 This method accept a Apache instance, CGI instance or hashref of query parameters. 
    107  
    108 =item gps_compliant() 
     165=item $option_ref{locator} 
     166 
     167select locator class algorithm option. 
     168 
     169$LOCATOR_AUTO_FROM_COMPLIANT 
     170 auto detect locator from gps compliant.this is I<default>. 
     171 
     172$LOCATOR_AUTO 
     173 auto detect locator class from params. 
     174 
     175$LOCATOR_GPS 
     176 select GPS class. 
     177 
     178$LOCATOR_BASIC 
     179 select BasicLocation class. 
     180 
     181=back 
     182 
     183 
     184 
     185=head2 gps_compliant() 
    109186 
    110187returns if the agent is GPS compliant. 
    111188 
    112 =back 
     189 
     190 
     191=head2 gps_parameter([params]) 
     192 
     193returns if the params is GPS request. 
     194 
    113195 
    114196=head1 CLASSES 
  • lang/perl/HTTP-MobileAgent-Plugin-Locator/trunk/t/01.get_location.t

    r4137 r7422  
    22 
    33use HTTP::MobileAgent; 
    4 use HTTP::MobileAgent::Plugin::Locator; 
     4use HTTP::MobileAgent::Plugin::Locator qw/:locator/; 
    55 
    6 { 
    7     local $ENV{HTTP_USER_AGENT} = 'DoCoMo/2.0 SH904i(c100;TB;W24H16)'; 
     6my $for_gps_option_refs = [ 
     7    undef, 
     8    { locator => $LOCATOR_AUTO_FROM_COMPLIANT }, 
     9    { locator => $LOCATOR_AUTO }, 
     10    { locator => $LOCATOR_GPS }, 
     11]; 
     12my $for_basic_option_refs = [ 
     13    undef, 
     14    { locator => $LOCATOR_AUTO_FROM_COMPLIANT }, 
     15    { locator => $LOCATOR_AUTO }, 
     16    { locator => $LOCATOR_BASIC }, 
     17]; 
     18my $for_basic_on_gps_option_refs = [ 
     19    { locator => $LOCATOR_AUTO }, 
     20    { locator => $LOCATOR_BASIC }, 
     21]; 
     22 
     23sub locator_test { 
     24    my ( $option_refs, $params, $expect ) = @_; 
     25 
    826    my $agent = HTTP::MobileAgent->new; 
    9     my $location = $agent->get_location( { 
    10         lat => '35.21.03.342', lon => '138.34.45.725', geo => 'wgs84' 
    11     } ); 
    12     is ref $location, 'Geo::Coordinates::Converter::Point'; 
    13     is_deeply( { lat => $location->lat, lng => $location->lng  }, 
    14                { lat => '35.21.03.342', lng => '138.34.45.725' } ); 
    15 } 
    1627 
    17 { 
    18     local $ENV{HTTP_USER_AGENT} = 'DoCoMo/1.0/P503i/c10'; 
    19     my $agent = HTTP::MobileAgent->new; 
    20     my $location = $agent->get_location( { AREACODE => '05902', LAT => '+35.39.43.538', LON => '+139.44.06.232', GEO => 'wgs84', XACC => 1 } ); 
    21     is ref $location, 'Geo::Coordinates::Converter::Point'; 
    22     is_deeply( { lat => $location->lat, lng => $location->lng  }, 
    23                { lat => '35.39.43.538', lng => '139.44.06.232' } ); 
    24 } 
    25  
    26 { 
    27     local $ENV{HTTP_USER_AGENT} = 'DoCoMo/1.0/P503i/c10'; 
    28     my $agent = HTTP::MobileAgent->new; 
    29     my $location = $agent->get_location( { AREACODE => '05902' } ); 
    30     is ref $location, 'Geo::Coordinates::Converter::Point'; 
    31     is_deeply( { lat => $location->lat, lng => $location->lng  }, 
    32                { lat => '35.39.43.535', lng => '139.44.06.233' } ); 
    33 } 
    34  
    35 { 
    36     local $ENV{HTTP_USER_AGENT} = 'KDDI-CA31 UP.Browser/6.2.0.7.3.129 (GUI) MMP/2.0'; 
    37     local $ENV{HTTP_X_UP_DEVCAP_MULTIMEDIA} = '0200000000000000'; 
    38     my $agent = HTTP::MobileAgent->new; 
    39     my $location = $agent->get_location( { 
    40         lat => '+35.21.03.342', lon => '+138.34.45.725', datum => '0' 
    41     } ); 
    42     is ref $location, 'Geo::Coordinates::Converter::Point'; 
    43     is_deeply( { lat => $location->lat, lng => $location->lng  }, 
    44                { lat => '35.21.03.342', lng => '138.34.45.725' } ); 
    45 } 
    46  
    47 { 
    48     local $ENV{HTTP_USER_AGENT} = 'KDDI-CA31 UP.Browser/6.2.0.7.3.129 (GUI) MMP/2.0'; 
    49     my $agent = HTTP::MobileAgent->new; 
    50     my $location = $agent->get_location( { lat => '+35.21.03.342', lon => '+138.34.45.725' } ); 
    51     is ref $location, 'Geo::Coordinates::Converter::Point'; 
    52     is_deeply( { lat => $location->lat, lng => $location->lng  }, 
    53                { lat => '35.21.03.342', lng => '138.34.45.725' } ); 
    54 } 
    55  
    56 { 
    57     local $ENV{HTTP_USER_AGENT} = 'SoftBank/1.0/911T/TJ001 Browser/NetFront/3.3 Profile/MIDP-2.0 Configuration/CLDC-1.1'; 
    58     my $agent = HTTP::MobileAgent->new; 
    59     my $location = $agent->get_location( { pos => 'N35.21.03.342E138.34.45.725' } ); 
    60     is ref $location, 'Geo::Coordinates::Converter::Point'; 
    61     is_deeply( { lat => $location->lat, lng => $location->lng  }, 
    62                { lat => '35.21.03.342', lng => '138.34.45.725' } ); 
    63 } 
    64  
    65 { 
    66     local $ENV{HTTP_USER_AGENT} = 'J-PHONE/2.0/J-DN02'; 
    67     local $ENV{ HTTP_X_JPHONE_GEOCODE } = '352051%1a1383456%1afoo'; 
    68     my $agent = HTTP::MobileAgent->new; 
    69     my $location = $agent->get_location(); 
    70     is ref $location, 'Geo::Coordinates::Converter::Point'; 
    71     is_deeply( { lat => $location->lat, lng => $location->lng  }, 
    72                { lat => '35.21.02.678', lng => '138.34.44.820' } ); 
    73 } 
    74  
    75 { 
    76     local $ENV{HTTP_USER_AGENT} = 'Mozilla/3.0(DDIPOCKET;JRC/AH-J3001V,AH-J3002V/1.0/0100/c50)CNF/2.0'; 
    77     my $agent = HTTP::MobileAgent->new; 
    78     my $location = $agent->get_location( { pos => 'N35.20.51.664E138.34.56.905' } ); 
    79     is ref $location, 'Geo::Coordinates::Converter::Point'; 
    80     is_deeply( { lat => $location->lat, lng => $location->lng  }, 
    81                { lat => '35.21.03.342', lng => '138.34.45.725' } ); 
     28    for my $option_ref (@{$option_refs}) { 
     29        my $location = $agent->get_location( $params, $option_ref ); 
     30        is ref $location, 'Geo::Coordinates::Converter::Point'; 
     31        is_deeply( { lat => $location->lat, lng => $location->lng  } => $expect ); 
     32   } 
    8233} 
    8334 
    8435 
     36{ # DoCoMo GPS device 
     37    local $ENV{HTTP_USER_AGENT} = 'DoCoMo/2.0 SH904i(c100;TB;W24H16)'; 
     38 
     39    locator_test( 
     40        $for_gps_option_refs, 
     41        { lat => '35.21.03.342', lon => '138.34.45.725', geo => 'wgs84' }, 
     42        { lat => '35.21.03.342', lng => '138.34.45.725'} 
     43    ); 
     44    locator_test( 
     45        $for_basic_on_gps_option_refs, 
     46        { AREACODE => '05902', LAT => '+35.39.43.538', LON => '+139.44.06.232', GEO => 'wgs84', XACC => 1 }, 
     47        { lat => '35.39.43.538', lng => '139.44.06.232' } 
     48    ); 
     49    locator_test( 
     50        $for_basic_on_gps_option_refs, 
     51        { AREACODE => '05902' }, 
     52        { lat => '35.39.43.535', lng => '139.44.06.233' } 
     53    ); 
     54     
     55} 
     56 
     57{ # DoCoMo Basic device 
     58    local $ENV{HTTP_USER_AGENT} = 'DoCoMo/1.0/P503i/c10'; 
     59 
     60    locator_test( 
     61        $for_basic_option_refs, 
     62        { AREACODE => '05902', LAT => '+35.39.43.538', LON => '+139.44.06.232', GEO => 'wgs84', XACC => 1 }, 
     63        { lat => '35.39.43.538', lng => '139.44.06.232' } 
     64    ); 
     65    locator_test( 
     66        $for_basic_option_refs, 
     67        { AREACODE => '05902' }, 
     68        { lat => '35.39.43.535', lng => '139.44.06.233' } 
     69    ); 
     70} 
     71 
     72 
     73{ # au GPS device 
     74    local $ENV{HTTP_USER_AGENT} = 'KDDI-CA31 UP.Browser/6.2.0.7.3.129 (GUI) MMP/2.0'; 
     75    local $ENV{HTTP_X_UP_DEVCAP_MULTIMEDIA} = '0200000000000000'; 
     76     
     77    locator_test( 
     78        $for_gps_option_refs, 
     79        { lat => '+35.21.03.342', lon => '+138.34.45.725', datum => '0' }, 
     80        { lat => '35.21.03.342', lng => '138.34.45.725' } 
     81    ); 
     82    locator_test( 
     83        $for_basic_on_gps_option_refs, 
     84        { lat => '35.21.03.342', lon => '138.34.45.725', datum => 'wgs84' }, 
     85        { lat => '35.21.03.342', lng => '138.34.45.725' } 
     86    ); 
     87} 
     88 
     89{ # au Basic device 
     90    local $ENV{HTTP_USER_AGENT} = 'KDDI-CA31 UP.Browser/6.2.0.7.3.129 (GUI) MMP/2.0'; 
     91 
     92    locator_test( 
     93        $for_basic_option_refs, 
     94        { lat => '35.21.03.342', lon => '138.34.45.725', datum => 'wgs84' }, 
     95        { lat => '35.21.03.342', lng => '138.34.45.725' } 
     96    ); 
     97} 
     98 
     99{ # SoftBank GPS device 
     100    local $ENV{HTTP_USER_AGENT} = 'SoftBank/1.0/911T/TJ001 Browser/NetFront/3.3 Profile/MIDP-2.0 Configuration/CLDC-1.1'; 
     101 
     102    locator_test( 
     103        $for_gps_option_refs, 
     104        { pos => 'N35.21.03.342E138.34.45.725' }, 
     105        { lat => '35.21.03.342', lng => '138.34.45.725' } 
     106    ); 
     107     
     108    local $ENV{ HTTP_X_JPHONE_GEOCODE } = '352051%1a1383456%1afoo'; 
     109    locator_test( 
     110        $for_basic_on_gps_option_refs, 
     111        undef, 
     112        { lat => '35.21.02.678', lng => '138.34.44.820' } 
     113    ); 
     114} 
     115 
     116{ # SoftBank Basic device 
     117    local $ENV{HTTP_USER_AGENT} = 'J-PHONE/2.0/J-DN02'; 
     118    local $ENV{ HTTP_X_JPHONE_GEOCODE } = '352051%1a1383456%1afoo'; 
     119 
     120    locator_test( 
     121        $for_basic_option_refs, 
     122        undef, 
     123        { lat => '35.21.02.678', lng => '138.34.44.820' } 
     124    ); 
     125} 
     126 
     127{ # Willcom 
     128    local $ENV{HTTP_USER_AGENT} = 'Mozilla/3.0(DDIPOCKET;JRC/AH-J3001V,AH-J3002V/1.0/0100/c50)CNF/2.0'; 
     129 
     130    locator_test( 
     131        $for_basic_option_refs, 
     132        { pos => 'N35.20.51.664E138.34.56.905' }, 
     133        { lat => '35.21.03.342', lng => '138.34.45.725' } 
     134    ); 
     135} 
     136 
     137