Query portabilitate.ro for operator information
[number-phone-ro.git] / lib / Number / Phone / RO.pm
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>
This page took 0.01131 seconds and 4 git commands to generate.