理系学生日記

おまえはいつまで学生気分なのか

忍者TOOLS

WebService::ReadItLater 書いた

薄い。

package WebService::ReadItLater;
use base 'WebService::Simple';
use WebService::Simple::Parser::JSON;

use strict;
use warnings;
use Carp;
use JSON;
our $VERSION = '0.01';

__PACKAGE__->config(
    base_url        => 'https://readitlaterlist.com/v2/',
    response_parser => WebService::Simple::Parser::JSON->new,
);

sub new {
    my $class  = shift;
    my $args   = shift; 

    my $apikey = delete $args->{apikey} or croak "apikey must be specified.";
    my $user   = delete $args->{user}   or croak "user must be specified.";
    my $pass   = delete $args->{pass}   or croak "pass must be specified.";

    my $self = $class->SUPER::new(
        params => { 
            apikey   => $apikey,
            username => $user,
            password => $pass,
        },
        %$args,
    );
}

sub add_page {
    my $self = shift;
    my ($url, $title) = @_;
    $self->get( 'add' => { url => $url, title => $title } );
}

sub add_pages {
    my ($self, $pages) = @_;
    my @pages = map { +{
        url   => $_[0],
        title => $_[1]
    }} @$pages;
    
    my $json = $self->__format( $pages );
    $self->post( 'send' => { new => $json } );
}

sub read_pages {
    my ($self, $pages) = @_;
    my @pages = map { +{
        url => $_
    }} @$pages;
    my $json = $self->__format( \@pages );

    print $json;
    $self->post( 'send' => { read => $json } );
}

sub stats {
    $_[0]->post( 'stats' => { format => 'json' } );
}

sub get_lists {
    my ($self, $href) = @_;
    my %params;

    $params{state} = $href->{read}  if exists $href->{read};
    $params{since} = $href->{since} if exists $href->{since};
    $params{count} = $href->{count} if exists $href->{count};
    $params{page}  = $href->{page}  if exists $href->{page};

    $self->post( 'get' => { 
        format => 'json',
        %params
    });
}

sub authenticate {
    $_[0]->post( 'auth' );
}

sub api_status {
    $_[0]->post( 'api' );
}

sub __format {
    my ($self, $aref) = @_;
    my $cnt = 0;

    my $format = { map { $cnt++ => $_} @$aref };
    to_json( $format );
}


1;
__END__