File Coverage

blib/lib/SMS/Send/TW/PChome.pm
Criterion Covered Total %
statement 16 46 34.7
branch 0 8 0.0
condition n/a
subroutine 6 10 60.0
pod 2 2 100.0
total 24 66 36.3


line stmt bran cond sub pod time code
1             package SMS::Send::TW::PChome;
2              
3 1     1   20192 use strict;
  1         1  
  1         42  
4 1     1   5 use base 'SMS::Send::Driver';
  1         2  
  1         959  
5 1     1   1410 use WWW::Mechanize;
  1         177709  
  1         46  
6 1     1   995 use Text::Iconv;
  1         3428  
  1         70  
7              
8 1     1   7 use vars qw{$VERSION};
  1         2  
  1         48  
9             BEGIN {
10 1     1   463 $VERSION = '0.03';
11             }
12              
13             sub new {
14 0     0 1   my ($class, %params) = @_;
15              
16 0           foreach(qw/username password authcode/) {
17 0 0         Carp::croak("No $_ specified") unless(defined $params{"_$_"});
18             }
19              
20 0           my $self = bless { %params }, $class;
21              
22 0           return $self;
23             }
24              
25             sub send_sms {
26 0     0 1   my $self = shift;
27 0           my %params = @_;
28              
29             # Get the message and destination
30 0           my $message = $self->_MESSAGE( $params{text} );
31 0           my $recipient = $self->_TO( delete $params{to} );
32            
33 0           my $ua = WWW::Mechanize->new(
34             agent => __PACKAGE__." v. $VERSION",
35             );
36              
37 0           $ua->agent_alias('Windows IE 6');
38            
39             # Should be ok now, right? Let's send it!
40             # Login
41 0           $ua->post('https://login.pchome.com.tw/adm/person_sell.htm',
42             [ 'mbrid' => $self->{"_username"},
43             'mbrpass' => $self->{"_password"},
44             'chan' => 'sms',
45             'record_ipw' => 'false',
46             'ltype' => 'checklogin',
47             'buyflag' => '', ]);
48            
49            
50             # Input SMS_Message, Recipients
51 0           $ua->get('http://sms.pchome.com.tw/quick_index.htm');
52 0           $ua->post('http://sms.pchome.com.tw/check_msg.htm',
53             [ 'encoding_type' => 'BIG5',
54             'msg_body' => $message,
55             'mobile_list' => $recipient,
56             'send_type' => 1, ]);
57              
58              
59             # Input Authcode
60 0           $ua->field('ezpay_key', $ua->value('ezpay_key')); # Forward Hidden field: ezpay_key
61 0           $ua->field('exh_no', $ua->value('exh_no')); # Forward Hidden field: exh_no
62 0           $ua->field('auth_code', $self->{"_authcode"}); # put Auth Code
63 0           $ua->current_form()->action('https://ezpay.pchome.com.tw/auth_access.htm');
64 0           $ua->submit();
65            
66 0           return $ua->content;
67             }
68              
69             sub _MESSAGE {
70              
71 0 0   0     my $class = ref $_[0] ? ref shift : shift;
72 0           my $message = shift;
73 0           my $converter = Text::Iconv->new("big5", "utf-8");
74 0 0         unless ( length($message) <= 160 ) {
75 0           Carp::croak("Message length limit is 160 characters");
76             }
77            
78 0           return $converter->convert($message);
79             }
80              
81             sub _TO {
82 0 0   0     my $class = ref $_[0] ? ref shift : shift;
83 0           my $to = shift;
84              
85             # International numbers need their + removed
86 0           $to =~ y/0123456789//cd;
87              
88 0           return $to;
89             }
90            
91              
92             1;
93             __END__