| 1 | package Net::CIDR::MobileJP; |
|---|
| 2 | use strict; |
|---|
| 3 | use warnings; |
|---|
| 4 | use Carp; |
|---|
| 5 | use Net::CIDR::Lite; |
|---|
| 6 | use File::ShareDir (); |
|---|
| 7 | our $VERSION = '0.13'; |
|---|
| 8 | |
|---|
| 9 | our $yaml_loader; |
|---|
| 10 | BEGIN { |
|---|
| 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 | |
|---|
| 22 | sub new { |
|---|
| 23 | my ($class, $stuff) = @_; |
|---|
| 24 | |
|---|
| 25 | return bless {spanner => $class->_create_spanner($class->_load_config($stuff))}, $class; |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | sub _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 | |
|---|
| 44 | sub _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 | |
|---|
| 61 | sub get_carrier { |
|---|
| 62 | my ($self, $ip) = @_; |
|---|
| 63 | |
|---|
| 64 | my ($carrier,) = map { keys %$_ } values %{$self->{spanner}->find($ip)}; |
|---|
| 65 | return $carrier || 'N'; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | 1; |
|---|
| 70 | __END__ |
|---|
| 71 | |
|---|
| 72 | =head1 NAME |
|---|
| 73 | |
|---|
| 74 | Net::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 | |
|---|
| 85 | Net::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 | |
|---|
| 94 | create new instance. |
|---|
| 95 | |
|---|
| 96 | The argument is 'path to yaml' or 'raw data'. |
|---|
| 97 | |
|---|
| 98 | =head2 get_carrier |
|---|
| 99 | |
|---|
| 100 | $cidr->get_carrier('222.7.56.248'); |
|---|
| 101 | |
|---|
| 102 | Get the career name from IP address. |
|---|
| 103 | |
|---|
| 104 | Carrier 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 | |
|---|
| 119 | L<http://d.hatena.ne.jp/spiritloose/20061010/1160471510> |
|---|
| 120 | |
|---|
| 121 | =head1 COPYRIGHT |
|---|
| 122 | |
|---|
| 123 | This program is free software; you can redistribute |
|---|
| 124 | it and/or modify it under the same terms as Perl itself. |
|---|
| 125 | |
|---|
| 126 | The full text of the license can be found in the |
|---|
| 127 | LICENSE file included with this module. |
|---|