File Coverage

lib/WWW/Instapaper/Client.pm
Criterion Covered Total %
statement 37 58 63.7
branch 14 32 43.7
condition n/a
subroutine 8 10 80.0
pod 3 3 100.0
total 62 103 60.1


line stmt bran cond sub pod time code
1             package WWW::Instapaper::Client;
2 1     1   876 use strict; use warnings;
  1     1   2  
  1         42  
  1         5  
  1         2  
  1         42  
3 1     1   17 use constant PACKAGE_VERSION => '0.1';
  1         1  
  1         85  
4 1     1   5 use constant DEBUG => 1;
  1         1  
  1         46  
5            
6 1     1   6 use base 'Class::Base';
  1         2  
  1         1042  
7 1     1   2208 use LWP::UserAgent;
  1         54237  
  1         44  
8 1     1   1208 use HTTP::Request::Common 'POST';
  1         2507  
  1         701  
9             # local %ENV;
10            
11             our $VERSION = '0.901';
12            
13             sub init {
14 8     8 1 5226 my ($self, $config) = @_;
15 8         16 my %default;
16             # my $package = shift;
17             # my %config = @_;
18             # my ($self, %default);
19            
20 8         64 %default = (
21             agent_string => 'WWW-Instapaper-Client/'.PACKAGE_VERSION,
22             api_url => 'https://www.instapaper.com/api',
23             http_proxy => $ENV{HTTP_PROXY},
24             http_proxyuser => $ENV{HTTP_PROXY_USERNAME},
25             http_proxypass => $ENV{HTTP_PROXY_PASSWORD},
26             username => $ENV{instapaper_user},
27             password => $ENV{instapaper_pass},
28             );
29            
30             # $self = bless \%default, $package;
31             ## make sure defaults are loaded
32 8         29 for (keys %default) { $self->{$_} = $default{$_} }
  56         107  
33            
34 8         24 for (keys %$config) {
35 8 100       40 die "Invalid parameter '$_'" unless exists $self->{$_};
36 7         17 $self->{$_} = $config->{$_};
37             }
38            
39            
40            
41 7         39 $self->{_ua} = LWP::UserAgent->new(
42             agent => $self->{agent_string},
43             );
44            
45             ## remember, this is the LOCALIZED %ENV variable - won't change live environment.
46 7 100       218246 $ENV{HTTPS_PROXY} = (defined $self->{http_proxy} ? $self->{http_proxy} : $ENV{HTTP_PROXY});
47 7 100       32 $ENV{HTTPS_PROXY_USERNAME} = (defined $self->{http_proxyuser} ? $self->{http_proxyuser} : $ENV{HTTP_PROXY_USERNAME});
48 7 100       26 $ENV{HTTPS_PROXY_PASSWORD} = (defined $self->{http_proxypass} ? $self->{http_proxypass} : $ENV{HTTP_PROXY_PASSWORD});
49 7 100       25 $ENV{HTTP_PROXY} = (defined $self->{http_proxy} ? $self->{http_proxy} : $ENV{HTTP_PROXY});
50 7 100       35 $ENV{HTTP_PROXY_USERNAME} = (defined $self->{http_proxyuser} ? $self->{http_proxyuser} : $ENV{HTTP_PROXY_USERNAME});
51 7 100       21 $ENV{HTTP_PROXY_PASSWORD} = (defined $self->{http_proxypass} ? $self->{http_proxypass} : $ENV{HTTP_PROXY_PASSWORD});
52            
53             # if (defined $self->{http_proxy)) {
54             # my $proxy_url = $self->{http_proxy};
55             # if (defined $self->{http_proxyuser}) {
56             # my $authstr = $self->{http_proxyuser};
57             # $authstr .= ":".$self->{http_proxypass} if defined $self->{http_proxypass};
58             # $proxy_url =~ s{(\w+?:)//}{$1//$authstr\@};
59             # warn "Updated Proxy URL to '$proxy_url'" if DEBUG;
60             # }
61             # $self->{_ua}->proxy(['http','https'], $proxy_url);
62             # }
63            
64 7         38 return $self;
65             }
66            
67             sub authenticate {
68 0     0 1   my $self = shift;
69 0           my $req = POST
70             $self->{api_url}.'/authenticate',
71             ['username'=>$self->{username}, 'password'=>$self->{password}];
72            
73 0           my $response = $self->{_ua}->request($req);
74            
75 0 0         return 1 if ($response->is_success);
76            
77 0 0         return $self->error('Bad username or password') if ($response->code == 403);
78 0 0         return $self->error('Error with service or proxy') if ($response->code == 500);
79            
80 0           return $self->error("Unknown error condition: ".$response->code); ## undefine error
81             }
82            
83             sub add {
84 0     0 1   my $self = shift;
85 0           my %param = @_;
86            
87 0 0         unless (exists $param{title}) { $param{'auto-title'} = 1 }
  0            
88 0 0         return $self->error("No URL provided") unless (defined $param{url});
89            
90 0           $param{username} = $self->{username};
91 0           $param{password} = $self->{password};
92            
93 0           my $req = POST $self->{api_url}.'/add', [ %param ];
94 0           my $response = $self->{_ua}->request($req);
95            
96 0 0         return [$response->header('Content-Location'), $response->header('X-Instapaper-Title')]
97             if ($response->is_success);
98            
99 0 0         return $self->error('Bad username or password') if ($response->code == 403);
100 0 0         return $self->error('Error with service or proxy') if ($response->code == 500);
101 0 0         return $self->error('Malformed request') if ($response->code == 400);
102            
103 0           return $self->error("Unknown error condition: ".$response->code); ## undefine error
104             }
105            
106             1;
107            
108             __END__