summaryrefslogtreecommitdiffstats
path: root/lib/WWW/Pocket/Script.pm
blob: 119894a2e8f18ce850232d6914832168ef3cb925 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package WWW::Pocket::Script;
use Moose;

with 'MooseX::Getopt';

use JSON::PP;
use Path::Class;

use WWW::Pocket;

has consumer_key => (
    is      => 'ro',
    isa     => 'Str',
    lazy    => 1,
    default => sub { die "consumer_key is required to authenticate" },
);

has redirect_uri => (
    is      => 'ro',
    isa     => 'Str',
    default => 'https://getpocket.com/',
);

has credentials_file => (
    is      => 'ro',
    isa     => 'Str',
    default => "$ENV{HOME}/.pocket",
);

has pocket => (
    traits  => ['NoGetopt'],
    is      => 'ro',
    isa     => 'WWW::Pocket',
    lazy    => 1,
    default => sub {
        my $self = shift;

        my $pocket = WWW::Pocket->new;
        my $credentials_file = file($self->credentials_file);
        if (-e $credentials_file) {
            $self->_apply_credentials($pocket, $credentials_file);
        }
        else {
            $self->_authenticate($pocket);
        }

        $pocket
    },
);

sub run {
    my $self = shift;
    my @args = @{ $self->extra_argv };

    my $method = shift @args;
    if ($self->can($method)) {
        return $self->$method(@args);
    }
    else {
        $self->print_usage_text($self->usage);
    }
}

sub authenticate {
    my $self = shift;
    $self->pocket;
}

sub add {
    my $self = shift;
    my @args = @_;

    $self->_pretty_print($self->pocket->add(@args));
}

sub modify {
    my $self = shift;
    my @args = @_;

    $self->_pretty_print($self->pocket->modify(@args));
}

sub retrieve {
    my $self = shift;
    my @args = @_;

    $self->_pretty_print($self->pocket->retrieve(@args));
}

sub _apply_credentials {
    my $self = shift;
    my ($pocket, $file) = @_;

    my ($consumer_key, $access_token, $username) = $file->slurp(chomp => 1);
    $pocket->consumer_key($consumer_key);
    $pocket->access_token($access_token);
    $pocket->username($username);
}

sub _authenticate {
    my $self = shift;
    my ($pocket) = @_;

    my $consumer_key = $self->consumer_key;
    my $redirect_uri = $self->redirect_uri;
    my $code = $pocket->start_authentication($consumer_key, $redirect_uri);

    print "Visit https://getpocket.com/auth/authorize?request_token=${code}&redirect_uri=${redirect_uri} and log in. When you're done, press enter to continue.\n";
    <STDIN>;

    $pocket->finish_authentication($consumer_key, $code);

    my $fh = file($self->credentials_file)->openw;
    $fh->write($pocket->consumer_key . "\n");
    $fh->write($pocket->access_token . "\n");
    $fh->write($pocket->username . "\n");
    $fh->close;
}

sub _pretty_print {
    my $self = shift;
    my ($data) = @_;

    print JSON::PP->new->utf8->pretty->canonical->encode($data), "\n";
}

__PACKAGE__->meta->make_immutable;
no Moose;

1;