summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2014-04-12 01:23:42 -0400
committerJesse Luehrs <doy@tozt.net>2014-04-12 01:23:42 -0400
commit8be20837c5ccad127f35e5dd038e8140540686e3 (patch)
tree37f15637c4cb15ed3573170d9286006fa97fe0bd /lib
parent9bcd372e9d29c75351772f10596b6c1b68a91a8f (diff)
downloadwww-pinboard-8be20837c5ccad127f35e5dd038e8140540686e3.tar.gz
www-pinboard-8be20837c5ccad127f35e5dd038e8140540686e3.zip
initial implementation
Diffstat (limited to 'lib')
-rw-r--r--lib/WWW/Pinboard.pm69
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/WWW/Pinboard.pm b/lib/WWW/Pinboard.pm
index e69de29..39e1c63 100644
--- a/lib/WWW/Pinboard.pm
+++ b/lib/WWW/Pinboard.pm
@@ -0,0 +1,69 @@
+package WWW::Pinboard;
+use Moose;
+
+use HTTP::Tiny;
+use JSON::PP;
+use URI;
+
+has token => (
+ is => 'ro',
+ isa => 'Str',
+);
+
+has _endpoint => (
+ is => 'ro',
+ isa => 'Str',
+ init_arg => 'endpoint',
+ default => 'https://api.pinboard.in/v1/',
+);
+
+has endpoint => (
+ is => 'ro',
+ isa => 'URI',
+ lazy => 1,
+ default => sub {
+ my $self = shift;
+ my $uri = URI->new($self->_endpoint);
+ $uri->query_form(auth_token => $self->token, format => 'json');
+ return $uri;
+ },
+);
+
+has ua => (
+ is => 'ro',
+ isa => 'HTTP::Tiny',
+ lazy => 1,
+ default => sub { HTTP::Tiny->new },
+);
+
+has json => (
+ is => 'ro',
+ isa => 'JSON::PP',
+ lazy => 1,
+ default => sub { JSON::PP->new },
+);
+
+for my $method (qw(update add delete get recent dates all suggest)) {
+ __PACKAGE__->meta->add_method($method => sub {
+ my $self = shift;
+ my (%args) = @_;
+
+ my $progress = delete $args{progress};
+
+ my $uri = $self->endpoint->clone;
+ # XXX eventually support other parts of the api
+ $uri->path($uri->path . 'posts/' . $method);
+ $uri->query_form($uri->query_form, %args);
+
+ my $res = $self->ua->get(
+ $uri, { $progress ? (data_callback => $progress) : () }
+ );
+ die $res->{content} unless $res->{success};
+ return $self->json->decode($res->{content});
+ });
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;