Query portabilitate.ro for operator information
authorMarius Gavrilescu <marius@ieval.ro>
Sat, 14 May 2016 22:59:55 +0000 (23:59 +0100)
committerMarius Gavrilescu <marius@ieval.ro>
Sat, 14 May 2016 22:59:55 +0000 (23:59 +0100)
Changes
MANIFEST
README
lib/Number/Phone/RO.pm
t/network.t [new file with mode: 0644]

diff --git a/Changes b/Changes
index 50721e7bf3feacfd58712c1d0d69a7b3062fb396..376d815961a641865a58b283730e1fedd9acfb10 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,4 +1,7 @@
 Revision history for Perl extension Number::Phone::RO.
 
+0.002 2015-11-28T16:33+01:00
+ - Query http://portabilitate.ro/ for operator information and expose it.
+
 0.001 2015-05-30T23:30+03:00
  - Initial release
index 2d4b4454616dcc0c2e83991161192037442e8f85..fdfecc35ab5dac6b6168129cc22dfa93b9c6fd8a 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -3,6 +3,7 @@ Makefile.PL
 MANIFEST
 README
 t/Number-Phone-RO.t
+t/network.t
 t/perlcritic.t
 t/perlcriticrc
 lib/Number/Phone/RO.pm
diff --git a/README b/README
index 65c6d6d7e30361fd08d224823f35485b12ee888e..8e95954cf1c080cb8d03f775d6e18d17db4a25a8 100644 (file)
--- a/README
+++ b/README
@@ -1,4 +1,4 @@
-Number-Phone-RO version 0.001
+Number-Phone-RO version 0.002
 =============================
 
 Number::Phone::RO is a Number::Phone extension for Romanian phone
index d2d94d5f4d4d37ed975d1ca8fe6c14bd5bf94bb8..adf4e2d19a7952dc4fa56e82ec18ff99199a4ba5 100644 (file)
@@ -9,7 +9,7 @@ use re '/s';
 
 sub AREA_NAMES (); ## no critic (ProhibitSubroutinePrototypes)
 
-our $VERSION = '0.001';
+our $VERSION = '0.002';
 
 our %cache;
 
@@ -78,6 +78,34 @@ sub format { ## no critic (ProhibitBuiltinHomonyms)
 
 sub intra_country_dial_to { "0${$_[0]}" }
 
+use HTTP::Tiny;
+
+my $ht = HTTP::Tiny->new(agent => "Number-Phone-RO/$VERSION ");
+
+sub query_portabilitate {
+       my ($self) = @_;
+       $self->_info->{portabilitate_queried} = 1;
+       my $req = $ht->get("http://portabilitate.ro/ro-no-0$$self");
+       return unless $req->{success};
+       my ($initial_operator) = $req->{content} =~ /lnkOperatorInitial">([^<]*)</;
+       my ($current_operator) = $req->{content} =~ /lnkOperator">([^<]*)</;
+       $initial_operator //= $current_operator;
+       $self->_info->{initial_operator} = $initial_operator;
+       $self->_info->{current_operator} = $current_operator;
+}
+
+sub operator {
+       my ($self) = @_;
+       $self->query_portabilitate unless $self->_info->{portabilitate_queried};
+       $self->_info->{initial_operator}
+}
+
+sub operator_ported {
+       my ($self) = @_;
+       $self->query_portabilitate unless $self->_info->{portabilitate_queried};
+       $self->_info->{current_operator}
+}
+
 use constant AREA_NAMES => {
        1  => 'București',
        30 => 'Suceava',
@@ -135,20 +163,20 @@ Number::Phone::RO - Phone number information for Romania (+40)
 
   use Number::Phone::RO;
   my $nr = Number::Phone::RO->new('+40250123456');
-  say $nr->is_geographic;  # 1
-  say $nr->is_fixed_line;  # 1
-  say $nr->is_mobile;      # 0
-  say $nr->is_tollfree;    # 0
-  say $nr->is_specialrate; # 0
-  say $nr->areacode;       # 250
-  say $nr->areaname;       # Vâlcea
-  say $nr->subscriber;     # 123456
-  say $nr->format;         # +40 250 123 456
+  say $nr->is_geographic;   # 1
+  say $nr->is_fixed_line;   # 1
+  say $nr->is_mobile;       # 0
+  say $nr->is_tollfree;     # 0
+  say $nr->is_specialrate;  # 0
+  say $nr->areacode;        # 250
+  say $nr->areaname;        # Vâlcea
+  say $nr->subscriber;      # 123456
+  say $nr->operator;        # (the name of this number's original operator)
+  say $nr->operator_ported; # (the name of this number's current operator)
+  say $nr->format;          # +40 250 123 456
 
 =head1 DESCRIPTION
 
-This module is a work in progress. See the L<TODO> section below.
-
 See the L<Number::Phone> documentation for usage information. The
 following methods from L<Number::Phone> are overridden:
 
@@ -176,18 +204,40 @@ Returns the name and URL of the regulator, ANCOM.
 
 =item B<areaname>
 
+=item B<operator>
+
+=item B<operator_ported>
+
 =item B<subscriber>
 
 =item B<format>
 
 =back
 
+Other methods:
+
+=over 4
+
+=item B<query_portabilitate>
+
+Queries L<http://portabilitate.ro> to get the information for the
+B<operator> and B<operator_ported> methods. The result is cached.
+Note that failures (such as number invalid, no internet connection)
+are also cached. Also note that the service rate limits
+requests. Going over the (unspecified) rate limit causes the service
+to ask for a captcha (which is interpreted as a failure by this
+function).
+
+This method is automatically called by B<operator> and
+B<operator_ported> the first time they are called. A possible reason
+for calling it explicitly is refreshing the cache.
+
+=back
+
 =head1 TODO
 
 Only long (10 digits) numbers are supported.
 
-Should query L<http://portabilitate.ro/> to implement B<operator> and B<operator_ported>.
-
 =head1 AUTHOR
 
 Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
diff --git a/t/network.t b/t/network.t
new file mode 100644 (file)
index 0000000..9431290
--- /dev/null
@@ -0,0 +1,22 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+use Test::More;
+BEGIN { plan skip_all => '$ENV{RELEASE_TESTING} is false' unless $ENV{RELEASE_TESTING} }
+BEGIN { plan tests => 4 }
+
+use Number::Phone::RO;
+
+my $nr = Number::Phone::RO->new("0767254626");
+
+like $nr->operator, qr/COSMOTE/, 'operator (ported number)';
+like $nr->operator_ported, qr/ROMTELECOM/, 'operator_ported (ported number)';
+
+$nr = Number::Phone::RO->new("0773928513");
+
+is $nr->operator, $nr->operator_ported, 'operator eq operator_ported (unported number)';
+
+$nr = Number::Phone::RO->new("0111111111");
+
+ok !$nr->operator, "operator (invalid number)";
This page took 0.015143 seconds and 4 git commands to generate.