sub AREA_NAMES (); ## no critic (ProhibitSubroutinePrototypes)
-our $VERSION = '0.001';
+our $VERSION = '0.002';
our %cache;
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',
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:
=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>
--- /dev/null
+#!/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)";