| 1 | package WWW::Contact::Base; | ||||||||||
| 2 | |||||||||||
| 3 | 7 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 7 | ▶ | use Moose::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 7 | ▶ | use Carp qw/croak/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 7 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 7 | our $VERSION = '0.16'; | |||||||||
| 9 | 7 | our $AUTHORITY = 'cpan:FAYLAND'; | |||||||||
| 10 | |||||||||||
| 11 | my $sub_verbose = sub { | ||||||||||
| 12 | 0 | ▶ | my $msg = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 13 | 0 | $msg =~ s/\s+$//; | |||||||||
| 14 | 0 | print STDERR "$msg\n"; | |||||||||
| 15 | 7 | }; | |||||||||
| 16 | subtype 'Verbose' | ||||||||||
| 17 | => as 'CodeRef' | ||||||||||
| 18 | 3 | ▶ | => where { 1; }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 19 | coerce 'Verbose' | ||||||||||
| 20 | => from 'Int' | ||||||||||
| 21 | => via { | ||||||||||
| 22 | 3 | ▶ | if ($_) { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=9.2
Branch: 1 branch
| |||||||||||
| 23 | 0 | return $sub_verbose; | |||||||||
| 24 | } else { | ||||||||||
| 25 | 3 | ▶ | return sub { 0 }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 26 | } | ||||||||||
| 27 | 7 | }; | |||||||||
| 28 | |||||||||||
| 29 | 7 | has 'verbose' => ( is => 'rw', isa => 'Verbose', coerce => 1, default => 0 ); | |||||||||
| 30 | 7 | has 'ua_class' => ( is => 'rw', isa => 'Str', default => 'WWW::Mechanize' ); | |||||||||
| 31 | has 'ua' => ( | ||||||||||
| 32 | is => 'rw', | ||||||||||
| 33 | isa => 'Object', | ||||||||||
| 34 | lazy => 1, | ||||||||||
| 35 | default => sub { | ||||||||||
| 36 | 0 | ▶ | my $class = (shift)->ua_class; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 37 | 0 | eval "use $class"; | |||||||||
| 38 | 0 | ▶ | croak $@ if ($@); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 0 | $class->new( | |||||||||
| 40 | agent => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)', | ||||||||||
| 41 | cookie_jar => {}, | ||||||||||
| 42 | stack_depth => 1, | ||||||||||
| 43 | timeout => 60, | ||||||||||
| 44 | autocheck => 0, | ||||||||||
| 45 | ); | ||||||||||
| 46 | } | ||||||||||
| 47 | 7 | ); | |||||||||
| 48 | |||||||||||
| 49 | 7 | has 'errstr' => ( is => 'rw', isa => 'Maybe[Str]' ); | |||||||||
| 50 | |||||||||||
| 51 | sub debug { | ||||||||||
| 52 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
debug: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 53 | |||||||||||
| 54 | 0 | ▶ | return unless $self->verbose; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 0 | $self->verbose->(@_); | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | sub debug_to_file { | ||||||||||
| 59 | 0 | ▶ | my ($self, $file) = @_; | ||||||||
|
Subroutine
debug_to_file: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 60 | |||||||||||
| 61 | 0 | ▶ | return unless $self->verbose; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | |||||||||||
| 63 | 0 | open(my $fh, '>', $file); | |||||||||
| 64 | 0 | print $fh Dumper(\$self->ua); | |||||||||
| 65 | 0 | close($fh); | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | sub get_contacts_from_outlook_csv { | ||||||||||
| 69 | 0 | ▶ | my ($self, $csv) = @_; | ||||||||
|
Subroutine
get_contacts_from_outlook_csv: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 70 | |||||||||||
| 71 | 0 | my @contacts; | |||||||||
| 72 | |||||||||||
| 73 | # Name,E-mail Address,Notes, | ||||||||||
| 74 | 0 | my @lines = split(/\n/, $csv); | |||||||||
| 75 | 0 | shift @lines; # skip the first line | |||||||||
| 76 | 0 | foreach my $line (@lines) { | |||||||||
| 77 | 0 | my @cols = split(',', $line); | |||||||||
| 78 | 0 | ▶ | next if ( $cols[1] !~ /\@/ ); # skip unknow lines | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 0 | push @contacts, { | |||||||||
| 80 | name => $cols[0], | ||||||||||
| 81 | email => $cols[1] | ||||||||||
| 82 | }; | ||||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 0 | ▶ | return wantarray ? @contacts : \@contacts; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | sub get { | ||||||||||
| 89 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
get: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 90 | |||||||||||
| 91 | 0 | my $resp = $self->ua->get(@_); | |||||||||
| 92 | 0 | ▶ | unless ( $resp->is_success ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 0 | $self->errstr( $resp->as_string() ); | |||||||||
| 94 | 0 | return; | |||||||||
| 95 | } | ||||||||||
| 96 | 0 | return 1; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | sub submit_form { | ||||||||||
| 100 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
submit_form: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 101 | |||||||||||
| 102 | 0 | my $resp = $self->ua->submit_form(@_); | |||||||||
| 103 | 0 | ▶ | unless ( $resp->is_success ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 0 | $self->errstr( $resp->as_string() ); | |||||||||
| 105 | 0 | return; | |||||||||
| 106 | } | ||||||||||
| 107 | 0 | return 1; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 7 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 111 | 7 | ▶ | no Moose::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 112 | |||||||||||
| 113 | 7 | __PACKAGE__->meta->make_immutable; | |||||||||
| 114 | |||||||||||
| 115 | 7 | 1; | |||||||||
| 116 | __END__ | ||||||||||
| 117 | |||||||||||
| 118 | =head1 NAME | ||||||||||
| 119 | |||||||||||
| 120 | WWW::Contact::Base - Base module for WWW::Contact::* | ||||||||||
| 121 | |||||||||||
| 122 | =head1 SYNOPSIS | ||||||||||
| 123 | |||||||||||
| 124 | use WWW::Contact::MyMail; | ||||||||||
| 125 | |||||||||||
| 126 | use Moose; | ||||||||||
| 127 | extends 'WWW::Contact::Base'; | ||||||||||
| 128 | |||||||||||
| 129 | sub get_contacts { | ||||||||||
| 130 | my ($self, $email, $password) = @_; | ||||||||||
| 131 | |||||||||||
| 132 | # reset | ||||||||||
| 133 | $self->errstr(undef); | ||||||||||
| 134 | my @contacts; | ||||||||||
| 135 | |||||||||||
| 136 | my $ua = $self->ua; | ||||||||||
| 137 | $self->debug("start get_contacts from MyMail"); | ||||||||||
| 138 | |||||||||||
| 139 | # get contacts | ||||||||||
| 140 | |||||||||||
| 141 | return wantarray ? @contacts : \@contacts; | ||||||||||
| 142 | } | ||||||||||
| 143 | |||||||||||
| 144 | =head1 DESCRIPTION | ||||||||||
| 145 | |||||||||||
| 146 | This module is mainly for you to write your own WWW::Contact::* (and used in my WWW::Contact::) | ||||||||||
| 147 | |||||||||||
| 148 | =head1 METHODS | ||||||||||
| 149 | |||||||||||
| 150 | =over 4 | ||||||||||
| 151 | |||||||||||
| 152 | =item ua | ||||||||||
| 153 | |||||||||||
| 154 | an instance of L<WWW::Mechanize> | ||||||||||
| 155 | |||||||||||
| 156 | $self->ua->get('http://www.google.com'); | ||||||||||
| 157 | |||||||||||
| 158 | If u want to use WWW::Mechanize::* instead of WWW::Mechanize, try | ||||||||||
| 159 | |||||||||||
| 160 | extends 'WWW::Contact::Base'; | ||||||||||
| 161 | has '+ua_class' => ( default => 'WWW::Mechanize::GZip' ); | ||||||||||
| 162 | |||||||||||
| 163 | =item verbose | ||||||||||
| 164 | |||||||||||
| 165 | turn on debug, default is off | ||||||||||
| 166 | |||||||||||
| 167 | $self->verbose(1); # turn on | ||||||||||
| 168 | $self->verbose(0); # turn off | ||||||||||
| 169 | |||||||||||
| 170 | =item debug | ||||||||||
| 171 | |||||||||||
| 172 | write debug info depends on $self->verbose | ||||||||||
| 173 | |||||||||||
| 174 | $self->debug("start get_contacts from MyMail"); | ||||||||||
| 175 | |||||||||||
| 176 | =item debug_to_file($file) | ||||||||||
| 177 | |||||||||||
| 178 | Dumper(\$self->ua) to $file | ||||||||||
| 179 | |||||||||||
| 180 | $self->debug_to_file($file) | ||||||||||
| 181 | |||||||||||
| 182 | =item get | ||||||||||
| 183 | |||||||||||
| 184 | a wrapper of $self->ua->get, with $resp->is_success check | ||||||||||
| 185 | |||||||||||
| 186 | $self->get('http://www.google.com'); | ||||||||||
| 187 | |||||||||||
| 188 | =item submit_form | ||||||||||
| 189 | |||||||||||
| 190 | a wrapper of $self->ua->submit_form, with $resp->is_success check | ||||||||||
| 191 | |||||||||||
| 192 | $self->submit_form( | ||||||||||
| 193 | form_number => 1, | ||||||||||
| 194 | fields => { | ||||||||||
| 195 | Email => $email, | ||||||||||
| 196 | Passwd => $password, | ||||||||||
| 197 | } | ||||||||||
| 198 | ); | ||||||||||
| 199 | |||||||||||
| 200 | =back | ||||||||||
| 201 | |||||||||||
| 202 | =head1 SEE ALSO | ||||||||||
| 203 | |||||||||||
| 204 | L<WWW::Contact>, L<WWW::Mechanize>, L<Moose> | ||||||||||
| 205 | |||||||||||
| 206 | =head1 AUTHOR | ||||||||||
| 207 | |||||||||||
| 208 | Fayland Lam, C<< <fayland at gmail.com> >> | ||||||||||
| 209 | |||||||||||
| 210 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 211 | |||||||||||
| 212 | Copyright 2008 Fayland Lam, all rights reserved. | ||||||||||
| 213 | |||||||||||
| 214 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 215 | under the same terms as Perl itself. | ||||||||||
| 216 | |||||||||||
| 217 | =cut | ||||||||||
| 218 | |||||||||||