From 910b926d93ef91e4c40d4c4c7b7ac071540bfdd7 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Sat, 14 May 2016 23:59:55 +0100 Subject: [PATCH] Query portabilitate.ro for operator information --- Changes | 3 ++ MANIFEST | 1 + README | 2 +- lib/Number/Phone/RO.pm | 78 ++++++++++++++++++++++++++++++++++-------- t/network.t | 22 ++++++++++++ 5 files changed, 91 insertions(+), 15 deletions(-) create mode 100644 t/network.t diff --git a/Changes b/Changes index 50721e7..376d815 100644 --- 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 diff --git a/MANIFEST b/MANIFEST index 2d4b445..fdfecc3 100644 --- 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 65c6d6d..8e95954 100644 --- 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 diff --git a/lib/Number/Phone/RO.pm b/lib/Number/Phone/RO.pm index d2d94d5..adf4e2d 100644 --- a/lib/Number/Phone/RO.pm +++ b/lib/Number/Phone/RO.pm @@ -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">([^<]*){content} =~ /lnkOperator">([^<]*)_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 section below. - See the L documentation for usage information. The following methods from L are overridden: @@ -176,18 +204,40 @@ Returns the name and URL of the regulator, ANCOM. =item B +=item B + +=item B + =item B =item B =back +Other methods: + +=over 4 + +=item B + +Queries L to get the information for the +B and B 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 and +B 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 to implement B and B. - =head1 AUTHOR Marius Gavrilescu, Emarius@ieval.roE diff --git a/t/network.t b/t/network.t new file mode 100644 index 0000000..9431290 --- /dev/null +++ b/t/network.t @@ -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)"; -- 2.39.2