root/lang/perl/Net-CIDR-MobileJP/tags/0.10/lib/Net/CIDR/MobileJP.pm @ 4919

Revision 4919, 2.5 kB (checked in by yappo, 5 years ago)

revert revision 4905, 4906, 4907, 4908, 4909

Line 
1package Net::CIDR::MobileJP;
2use strict;
3use warnings;
4use Carp;
5use YAML;
6use Net::CIDR::Lite;
7use File::ShareDir ();
8our $VERSION = '0.10';
9
10sub new {
11    my ($class, $stuff) = @_;
12
13    return bless {spanner => $class->_create_spanner($stuff)}, $class;
14}
15
16sub _create_spanner {
17    my ($class, $stuff) = @_;
18
19    my @cidrs;
20    my %cidr_for = %{$class->_load_config($stuff)};
21    my $spanner = Net::CIDR::Lite->new->spanner;
22    while (my ($carrier, $ip_ranges) = each %cidr_for) {
23        my $cidr = Net::CIDR::Lite->new;
24        for my $ip_range (@$ip_ranges) {
25            $cidr->add($ip_range);
26        }
27        $spanner->add($cidr, $carrier);
28    }
29    return $spanner;
30}
31
32sub _load_config {
33    my ($self, $stuff) = @_;
34
35    my $data;
36    if (defined $stuff && -f $stuff && -r _) {
37        # load yaml from file
38        $data = YAML::LoadFile($stuff);
39    } elsif ($stuff) {
40        # raw data
41        $data = $stuff;
42    } else {
43        # generated file
44        $data = YAML::LoadFile(File::ShareDir::module_file('Net::CIDR::MobileJP', 'cidr.yaml'));
45    }
46    return $data;
47}
48
49sub get_carrier {
50    my ($self, $ip) = @_;
51
52    my ($carrier,) =  map { keys %$_ } values %{$self->{spanner}->find($ip)};
53    return $carrier || 'N';
54}
55
56
571;
58__END__
59
60=head1 NAME
61
62Net::CIDR::MobileJP - mobile ip address in Japan
63
64=head1 SYNOPSIS
65
66    use Net::CIDR::MobileJP;
67    my $cidr = Net::CIDR::MobileJP->new('net-cidr-mobile-jp.yaml');
68    $cidr->get_carrier('222.7.56.248');
69    # => 'E'
70
71=head1 DESCRIPTION
72
73Net::CIDR::MobileJP is an utility to detect an ip address is mobile (cellular) ip address or not.
74
75=head1 METHODS
76
77=head2 new
78
79    my $cidr = Net::CIDR::MobileJP->new('net-cidr-mobile-jp.yaml');  # from yaml
80    my $cidr = Net::CIDR::MobileJP->new({E => ['59.135.38.128/25'], ...});
81
82create new instance.
83
84The argument is 'path to yaml' or 'raw data'.
85
86=head2 get_carrier
87
88    $cidr->get_carrier('222.7.56.248');
89
90Get the career name from IP address.
91
92Carrier name is compatible with L<HTTP::MobileAgent>.
93
94=head1 AUTHORS
95
96  Tokuhiro Matsuno  C<< <tokuhiro __at__ mobilefactory.jp> >>
97  Jiro Nishiguchi
98
99=head1 THANKS TO
100
101  Tatsuhiko Miyagawa
102  Masayoshi Sekimura
103  HIROSE, Masaaki
104
105=head1 SEE ALSO
106
107L<http://d.hatena.ne.jp/spiritloose/20061010/1160471510>
108
109=head1 LICENCE AND COPYRIGHT
110
111Copyright (c) 2006, Tokuhiro Matsuno C<< <tokuhiro __at__ mobilefactory.jp> >>. All rights reserved.
112
113This module is free software; you can redistribute it and/or
114modify it under the same terms as Perl itself. See L<perlartistic>.
115
Note: See TracBrowser for help on using the browser.