File Coverage

blib/lib/WebService/Instapaper.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


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