CheckPad.pm

filter: Statementstmt 100% 23 / 23 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 100% 31 / 31 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« CheckPad/Base.pm ↑ Summary
1 package WWW::CheckPad;
2  
3 1 ▶use 5.008006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 __PACKAGE__->mk_accessors(qw(ua
13 base_url
14 has_logged_in
15 ));
16  
17 our $VERSION = '0.035';
18  
19 our $connection;
20  
21  
22 BEGIN {
23 # use Jcode::convert if we have Jcode.pm.
24 # otherwise, don't do anything.
25 # TODO: We should be able to use other
26 # character converter(ex, jcode.pl, Encode).
27 my $have_jcode;
28 eval { require Jcode; $have_jcode = 1 };
29  
30 if ($have_jcode) {
31 *_jconvert = sub {
32 Jcode::convert($_[1], $_[2]?$_[2]:'euc-jp')
33 }
34 }
35 else {
36 carp "Couldn't use Jcode.pm. WWW::CheckPad won't convert Japanese " .
37 "character. We prefer to include Jcode.pm into your path.";
38 *_jconvert = sub { $_[1] };
39 }
40 }
41  
42  
43 sub connect {
44 my ($class, %connect_info) = @_;
45  
46 $connection = $class->_new(
47 user => {
48 email => $connect_info{email},
49 password => $connect_info{password},
50 },
51 );
52 return $connection;
53 }
54  
55  
56 sub disconnect {
57 my ($class) = @_;
58  
59 return if not defined $connection or not $connection->has_logged_in();
60  
61 $connection->_logout();
62 $connection->has_logged_in(0);
63 }
64  
65 ###########################################################################
66  
67 ## DO NOT USE THIS FROM OUTSIDE OF THIS MODULE
68 sub _new {
69 my ($class, %params) = @_;
70 my $self = {};
71 bless $self, $class;
72 $self->base_url( ($params{base_url} ?
73 $params{base_url} :
74 'http://www.checkpad.jp/'));
75  
76 ## If user specified login info, save it.
77 $self->user(%{$params{user}}) if $params{user};
78  
79 ## Set up LWP::UserAgent
80 $self->ua(new LWP::UserAgent());
81 $self->ua->cookie_jar(HTTP::Cookies->new(file => 'cookie.jar',
82 autosave => 1));
83  
84 ## Login if user info was specified.
85 if ($self->user->{email} and $self->user->{password}) {
86 $self->_login();
87 }
88  
89 return $self;
90 }
91  
92  
93 # returns domain of the base_url.
94 # TODO: It might be better to use URI::URL(cpan) module to
95 # control URL <-> Domain.
96 sub _domain {
97 my ($self) = @_;
98 $self->base_url =~ /http:\/\/(.*)\//;
99 return $1;
100 }
101  
102  
103 # user info looks like this.
104 # {email => foo, password => hoge}
105 sub user {
106 my ($self, %user_info) = @_;
107 $self->{_user} = {} if not defined $self->{_user};
108 $self->{_user}->{email} = $user_info{email} if $user_info{email};
109 $self->{_user}->{password} = $user_info{password} if $user_info{password};
110 return $self->{_user};
111 }
112  
113  
114 sub _login {
115 my ($self) = @_;
116 if (not $self->user->{email} or not $self->user->{password}) {
117 croak('Have to specified email and password to CheckPad::user()');
118 return undef;
119 }
120  
121 my %form = (
122 login_email => $self->user->{email},
123 login_pwd => $self->user->{password},
124 mode => 'sys',
125 act => 'login'
126 );
127 my $response = $self->ua->request(POST $self->base_url(), \%form);
128  
129 ## Check the cookies to find out the login has succeed or not.
130 $self->ua->cookie_jar->scan(
131 sub {
132 my ($domain, $key, $value) = @_[4, 1, 2];
133 if ($domain eq $self->_domain and
134 $key eq 'kj_my_id' and
135 $value ne '') {
136 $self->has_logged_in(1);
137 }
138 });
139 return $self->has_logged_in();
140 }
141  
142  
143 sub _logout {
144 my ($self) = @_;
145  
146 return if not $self->has_logged_in();
147  
148 my %form = (
149 mode => 'sys',
150 act => 'logout',
151 );
152  
153 $self->_request(\%form);
154 }
155  
156  
157 sub _urldecode {
158 my ($self, $data) = @_;
159 $data =~ s/%([0-9a-f][0-9a-f])/pack("C",hex($1))/egi;
160 return $data;
161 }
162  
163  
164 # Just simply access to server and returns response from the server.
165 sub _request {
166 my ($self, $info) = @_;
167  
168 $info->{form} ||= {};
169 $info->{path} ||= '';
170  
171 # Convert all form values to specified character encoding.
172 foreach my $key (keys %{$info->{form}}) {
173 my $value = $info->{form}->{$key};
174 $info->{form}->{$key} = $self->_jconvert($value, $info->{encoding});
175 }
176  
177 my $url = $self->base_url . $info->{path};
178 my $res = $self->ua->request(POST $url, $info->{form});
179  
180 # print "*******************************************************************\n";
181 # $self->dumper($res);
182  
183 croak "There was an error during accessing to the chech*pad:\n",
184 $res->as_string if $res->code =~ /^[45]\d\d$/;
185  
186 return $res;
187 }
188  
189  
190 sub dumper {
191 my ($self, $table, $indent) = @_;
192  
193 foreach my $key (keys %{$table}) {
194 my $value = $table->{$key};
195 if (ref $value eq 'HASH' or (ref $value) =~ /HTTP/) {
196 $self->dumper($value, $indent + 4);
197 }
198 else {
199 printf "%s%s = %s(%s)\n", ' ' x $indent, $key, $value, ref $value;
200 }
201 }
202 }
203  
204  
205  
206 sub _get_cookie_of {
207 my ($self, $search_key) = @_;
208 my $result = undef;
209
210 $self->ua->cookie_jar->scan(
211 sub {
212 my ($domain, $key, $value) = @_[4, 1, 2];
213 return unless $domain eq $self->_domain;
214  
215 if ($key eq $search_key) {
216 $result = $self->_urldecode($value);
217 }
218 }
219 );
220 return $result;
221 }
222  
223 sub _clear_cookie_of {
224 my ($self, $search_key) = @_;
225 $self->ua->cookie_jar->clear($self->_domain, '/', $search_key);
226 }
227  
228  
229  
230  
231 ##############################################################################
232 1;
233 __END__
« CheckPad/Base.pm ↑ Summary