root/lang/perl/HTTP-MobileAgent-Plugin-Locator/branches/get_locator_from_params/lib/HTTP/MobileAgent/Plugin/Locator.pm @ 7408

Revision 7408, 7.2 kB (checked in by chiba, 5 years ago)

lang/perl/HTTP-MobileAgent?-Plugin-Locator: enable option_ref (include locator option)

Line 
1package HTTP::MobileAgent::Plugin::Locator;
2
3use warnings;
4use strict;
5use HTTP::MobileAgent;
6use Carp;
7use UNIVERSAL::require;
8use UNIVERSAL::can;
9
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
14our $VERSION = '0.01';
15
16our $DOCOMO_GPS_COMPLIANT_MODELS = qr/(?:903i(?!TV|X)|(?:90[45]|SA[78]0[02])i)/;
17
18our $LOCATOR_AUTO_FROM_COMPLIANT = 1;
19our $LOCATOR_AUTO                = 2;
20our $LOCATOR_GPS                 = 3;
21our $LOCATOR_BASIC               = 4;
22
23
24sub import {
25    my ( $class ) = @_;
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 {
31        my ( $self, $stuff, $option_ref ) = @_;
32        my $params = _prepare_params( $stuff );
33        $self->locator( $params, $option_ref )->get_location( $params );
34    };
35
36    $class->export_to_level(1, @_);
37}
38
39sub _gps_compliant {
40    my $self = shift;
41    if ( $self->is_docomo ) {
42        return $self->model =~ $DOCOMO_GPS_COMPLIANT_MODELS;
43    } elsif ( $self->is_ezweb ) {
44        my @specs = split //, $ENV{ HTTP_X_UP_DEVCAP_MULTIMEDIA } || '';
45        return defined $specs[ 1 ] && $specs[ 1 ] =~ /^[23]$/;
46    } elsif ( $self->is_softbank ) {
47        return $self->is_type_3gc;
48    }
49}
50
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
75sub 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
86sub 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}
122
123sub _prepare_params {
124    my $stuff = shift;
125    if ( ref $stuff && eval { $stuff->can( 'param' ) } ) {
126        return +{ map { $_ => $stuff->param( $_ ) } $stuff->param };
127    }
128    else {
129        return $stuff;
130    }
131}
132
1331;
134__END__
135
136=head1 NAME
137
138HTTP::MobileAgent::Plugin::Locator - Handling mobile location information plugin for HTTP::MobileAgent
139
140=head1 SYNOPSIS
141
142    use CGI;
143    use HTTP::MobileAgent;
144    use HTTP::MobileAgent::Plugin::Locator;
145
146    # 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
151    print "lat is " . $location->lat;
152    print "lng is " . $location->lng;
153
154=head1 METHODS
155
156=over
157
158=item get_location([params], $option_ref);
159
160return Geo::Coordinates::Converter::Point instance formatted if specify gps or basic location parameters sent from carrier. The parameters are different by each carrier.
161
162This method accept a Apache instance, CGI instance or hashref of query parameters.
163
164=item gps_compliant()
165
166returns if the agent is GPS compliant.
167
168=back
169
170=head1 CLASSES
171
172=over
173
174=item HTTP::MobileAgent::Plugin::Locator::DoCoMo::BasicLocation
175
176for iArea data support.
177
178=item HTTP::MobileAgent::Plugin::Locator::DoCoMo::GPS
179
180for GPS data support.
181
182=item HTTP::MobileAgent::Plugin::Locator::EZweb::BasicLocation
183
184for basic location information data support.
185
186=item HTTP::MobileAgent::Plugin::Locator::EZweb::GPS
187
188for EZnavi data support.
189
190=item HTTP::MobileAgent::Plugin::Locator::SoftBank::BasicLocation
191
192for basic location information data support.
193
194=item HTTP::MobileAgent::Plugin::Locator::SoftBank::GPS
195
196for GPS data support.
197
198=item HTTP::MobileAgent::Plugin::Locator::Willcom::BasicLocation
199
200for basic location information data support.
201
202=back
203
204=head1 EXAMPLES
205
206There is request template using C<Template> in eg directory and mod_rewrite configuration for ezweb extraordinary parameter handling.
207
208=head1 AUTHOR
209
210Yoshiki Kurihara  E<lt>kurihara __at__ cpan.orgE<gt> with many feedbacks and changes from:
211
212  Tokuhiro Matsuno E<lt>tokuhiro __at__ mobilefactory.jpE<gt>
213
214=head1 SEE ALSO
215
216C<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/>
217
218=head1 LICENCE AND COPYRIGHT
219
220Copyright (c) 2008, Yoshiki Kurihara E<lt>kurihara __at__ cpan.orgE<gt>. All rights reserved.
221
222This module is free software; you can redistribute it and/or
223modify it under the same terms as Perl itself. See L<perlartistic>.
224
225=head1 DISCLAIMER OF WARRANTY
226
227BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
228FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
229OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
230PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
231EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
232WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
233ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
234YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
235NECESSARY SERVICING, REPAIR, OR CORRECTION.
236
237IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
238WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
239REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
240LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
241OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
242THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
243RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
244FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
245SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
246SUCH DAMAGES.
Note: See TracBrowser for help on using the browser.