root/lang/perl/Net-CIDR-MobileJP/trunk/lib/Net/CIDR/MobileJP.pm @ 17944

Revision 17944, 2.6 kB (checked in by tokuhirom, 5 years ago)

refactoring

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