File Coverage

blib/lib/Vonage/Click2Call.pm
Criterion Covered Total %
statement 15 53 28.3
branch 0 12 0.0
condition 0 8 0.0
subroutine 5 10 50.0
pod 0 4 0.0
total 20 87 22.9


line stmt bran cond sub pod time code
1             package Vonage::Click2Call;
2            
3 1     1   6528 use 5.00503;
  1         4  
  1         41  
4 1     1   6 use strict;
  1         1  
  1         32  
5 1     1   1255 use LWP::UserAgent;
  1         63492  
  1         37  
6 1     1   11 use Carp;
  1         2  
  1         92  
7 1     1   5 use vars qw($VERSION);
  1         2  
  1         746  
8            
9             $VERSION = '0.11';
10            
11             sub new {
12 0     0 0   my ($class,%args) = @_;
13 0 0 0       if (! exists($args{login}) || ! exists($args{password}) ) {
14 0           croak("Failed to provide login and/or password to constructor.");
15             }
16 0           my %hash = (
17             baseURL => 'https://secure.click2callu.com',
18             user => $args{login},
19             pass => $args{password},
20             error => undef,
21             skipHttpsCheck => $args{no_https_check},
22             );
23 0           $hash{_userAgent} = LWP::UserAgent->new;
24            
25             # Do we have HTTPS support ?
26 0 0         if (! $hash{skipHttpsCheck}) {
27 0           my $req = HTTP::Request->new(GET => $hash{baseURL});
28 0           my $res = $hash{_userAgent}->request($req);
29 0 0 0       if (! $res->is_success && $res->code == 501) {
30 0           $Vonage::Click2Call::errstr = "Error while testing HTTPS : " . $res->status_line;
31             }
32             }
33            
34 0   0       return(bless(\%hash,$class||__PACKAGE__));
35             }
36            
37             sub fromNumbers {
38 0     0 0   my ($self) = @_;
39 0           my $uri = sprintf("%s/tpcc/getnumbers?username=%s&password=%s",
40             $self->{baseURL},$self->{user},$self->{pass});
41 0           my $req = HTTP::Request->new(GET => $uri);
42 0           my $res = $self->{_userAgent}->request($req);
43 0 0         if ($res->is_success) {
44 0           return(split(/,/,$res->as_string));
45             } else {
46 0           $self->{error} = "Failed to GET '$uri': " . $res->status_line;
47 0           carp("Failed to GET '$uri': " . $res->status_line);
48 0           return();
49             }
50             }
51            
52             sub errstr {
53 0     0 0   my ($self) = @_;
54 0           return($self->{error});
55             }
56            
57             # return undef on failure. true otherwise.
58             sub call {
59 0     0 0   my ($self,$from,$to) = @_;
60 0           my $uri = sprintf("%s/tpcc/makecall?username=%s&password=%s&fromnumber=%s&tonumber=%s",
61             $self->{baseURL},$self->{user},$self->{pass},_validPhone($from),_validPhone($to));
62 0           my $req = HTTP::Request->new(GET => $uri);
63 0           my $res = $self->{_userAgent}->request($req);
64 0 0         if ($res->is_success) {
65 0           my ($res_code,$message) = split(/,/,$res->as_string);
66 0 0         if ($res_code == 0) {
67             # success.
68 0           return(1);
69             } else {
70 0           $self->{error} = sprintf("Error %03d : %s",$res_code,$message);
71 0           return();
72             }
73             } else {
74 0           $self->{error} = "Failed to GET '$uri': " . $res->status_line;
75 0           carp("Failed to GET '$uri': " . $res->status_line);
76 0           return();
77             }
78             }
79            
80             # reformat to all numbers.
81             sub _validPhone {
82 0     0     my ($phone) = @_;
83 0           $phone =~ s/\D//g;
84 0           return($phone);
85             }
86            
87             1;
88             __END__