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

Revision 7333, 5.9 kB (checked in by chiba, 7 years ago)

lang/perl/HTTP-MobileAgent?-Plugin-Locator: implement get_locator from params

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\::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
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
38sub 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
69sub get_location { die "ABSTRACT METHOD" }
70
71sub _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
811;
82__END__
83
84=head1 NAME
85
86HTTP::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
108return Geo::Coordinates::Converter::Point instance formatted if specify gps or basic location parameters sent from carrier. The parameters are different by each carrier.
109
110This method accept a Apache instance, CGI instance or hashref of query parameters.
111
112=item gps_compliant()
113
114returns 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
124for iArea data support.
125
126=item HTTP::MobileAgent::Plugin::Locator::DoCoMo::GPS
127
128for GPS data support.
129
130=item HTTP::MobileAgent::Plugin::Locator::EZweb::BasicLocation
131
132for basic location information data support.
133
134=item HTTP::MobileAgent::Plugin::Locator::EZweb::GPS
135
136for EZnavi data support.
137
138=item HTTP::MobileAgent::Plugin::Locator::SoftBank::BasicLocation
139
140for basic location information data support.
141
142=item HTTP::MobileAgent::Plugin::Locator::SoftBank::GPS
143
144for GPS data support.
145
146=item HTTP::MobileAgent::Plugin::Locator::Willcom::BasicLocation
147
148for basic location information data support.
149
150=back
151
152=head1 EXAMPLES
153
154There is request template using C<Template> in eg directory and mod_rewrite configuration for ezweb extraordinary parameter handling.
155
156=head1 AUTHOR
157
158Yoshiki 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
164C<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
168Copyright (c) 2008, Yoshiki Kurihara E<lt>kurihara __at__ cpan.orgE<gt>. All rights reserved.
169
170This module is free software; you can redistribute it and/or
171modify it under the same terms as Perl itself. See L<perlartistic>.
172
173=head1 DISCLAIMER OF WARRANTY
174
175BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
176FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
177OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
178PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
179EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
180WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
181ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
182YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
183NECESSARY SERVICING, REPAIR, OR CORRECTION.
184
185IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
186WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
187REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
188LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
189OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
190THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
191RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
192FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
193SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
194SUCH DAMAGES.
Note: See TracBrowser for help on using the browser.