File Coverage

blib/lib/WebService/Instapaper.pm
Criterion Covered Total %
statement 18 47 38.3
branch 0 4 0.0
condition n/a
subroutine 6 15 40.0
pod 8 9 88.8
total 32 75 42.6


line stmt bran cond sub pod time code
1             package WebService::Instapaper;
2 1     1   505 use 5.008001;
  1         2  
  1         32  
3 1     1   4 use strict;
  1         1  
  1         26  
4 1     1   4 use warnings;
  1         7  
  1         24  
5              
6 1     1   517 use OAuth::Lite::Consumer;
  1         61259  
  1         8  
7 1     1   37 use JSON qw(decode_json);
  1         1  
  1         6  
8 1     1   118 use Carp qw(croak);
  1         1  
  1         427  
9              
10             our $VERSION = "0.02";
11              
12             my $endpoint = "https://www.instapaper.com/api/1.1";
13              
14             sub new {
15 0     0 1   my ($class, %args) = @_;
16 0           my $self = {%args};
17 0           $self->{consumer} = OAuth::Lite::Consumer->new(
18             consumer_key => $self->{consumer_key},
19             consumer_secret => $self->{consumer_secret}
20             );
21 0           bless $self, $class;
22             }
23              
24             sub auth {
25 0     0 1   my ($self, $username, $password) = @_;
26 0           my $res = $self->{consumer}->obtain_access_token(
27             url => $endpoint . '/oauth/access_token',
28             params => {
29             x_auth_username => $username,
30             x_auth_password => $password,
31             x_auth_mode => 'client_auth'
32             }
33             );
34 0 0         unless ($res) {
35 0           croak 'failed to obtain access token';
36             }
37 0           $self->{access_token} = $res->access_token;
38             }
39              
40             sub token {
41 0     0 1   my ($self, $access_token, $access_secret) = @_;
42 0           $self->{access_token} = OAuth::Lite::Token->new(token => $access_token, secret => $access_secret);
43             }
44              
45             sub request {
46 0     0 0   my ($self, $method, $path, $params) = @_;
47 0           my $res = $self->{consumer}->request(method => $method, url => $endpoint . $path, token => $self->{access_token}, params => $params);
48 0 0         unless ($res->is_success) {
49 0           croak "failed to ${method} ${path}";
50             }
51 0           $res;
52             }
53              
54             sub bookmarks {
55 0     0 1   my ($self, %params) = @_;
56 0           my $res = $self->request('POST', '/bookmarks/list', \%params);
57 0           @{decode_json($res->decoded_content)->{bookmarks}};
  0            
58             }
59              
60             sub add_bookmark {
61 0     0 1   my ($self, $url, %params) = @_;
62 0           $params{url} = $url;
63 0           $self->request('POST', '/bookmarks/add', \%params);
64             }
65              
66             sub delete_bookmark {
67 0     0 1   my ($self, $id) = @_;
68 0           $self->request('POST', '/bookmarks/delete', {bookmark_id => $id});
69             }
70              
71             sub archive_bookmark {
72 0     0 1   my ($self, $id) = @_;
73 0           $self->request('POST', '/bookmarks/archive', {bookmark_id => $id});
74             }
75              
76             sub unarchive_bookmark {
77 0     0 1   my ($self, $id) = @_;
78 0           $self->request('POST', '/bookmarks/unarchive', {bookmark_id => $id});
79             }
80              
81             1;
82             __END__