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

Revision 7392, 6.3 kB (checked in by chiba, 5 years ago)

lang/perl/HTTP-MobileAgent?-Plugin-Locator: implement gps_parameter

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