File Coverage

blib/lib/Posterous.pm
Criterion Covered Total %
statement 53 102 51.9
branch 0 10 0.0
condition 0 14 0.0
subroutine 19 29 65.5
pod 0 2 0.0
total 72 157 45.8


line stmt bran cond sub pod time code
1             package Posterous;
2              
3 4     4   67381 use 5.010;
  4         16  
  4         153  
4 4     4   20 use strict;
  4         8  
  4         113  
5 4     4   19 use warnings;
  4         11  
  4         187  
6              
7             our $VERSION = '0.03';
8              
9 4     4   1466033 use LWP::UserAgent;
  4         1975936  
  4         166  
10 4     4   62 use HTTP::Request;
  4         8  
  4         99  
11 4     4   6880 use MIME::Base64;
  4         3788  
  4         428  
12 4     4   3906 use Rubyish::Attribute;
  4         24726  
  4         28  
13 4     4   18701 use Data::Dumper;
  4         39123  
  4         367  
14 4     4   3794 use Attribute::Protected;
  4         54452  
  4         538  
15              
16              
17             our $DOMAIN = "http://posterous.com";
18              
19             our $AUTH_PATH = $DOMAIN."/api/getsites";
20             our $NEWPOST_PATH = $DOMAIN."/api/newpost";
21             our $COMMMENT_PATH = $DOMAIN."/api/newcomment";
22             our $READPOST_PATH = $DOMAIN."/api/readposts";
23              
24             our $UA = LWP::UserAgent->new();
25              
26             BEGIN {
27 4     4   60 attr_accessor "user", "pass", "site_id";
28             }
29              
30             sub new {
31 0     0     my ($class, $user, $pass, $site_id) = @_;
32 0 0 0       die "didn\'t give user\' email or password" unless defined($user) && defined($pass);
33 0           my $self = bless {}, $class;
34 0           __user__ = $user;
35 0           __pass__ = $pass;
36 0 0         __site_id__ = $site_id if $site_id;
37 0           $self;
38             }
39              
40             sub auth_key : Public {
41 0     0   0 my ($self) = @_;
42 0         0 state $auth_key;
43 0   0     0 $auth_key //= encode_base64($self->user.":".$self->pass);
44 0         0 $auth_key;
45 4     4   2166 }
  4         7  
  4         20  
46              
47             sub account_info : Public {
48 0     0   0 my ($self) = @_;
49 0         0 state $account_info;
50 0   0     0 $account_info //= HTTP::Request->new( GET => $AUTH_PATH )
51             ->basic_auth($self->auth_key)
52             ->submit_by($UA)
53             ->xmlized_content;
54 0         0 $account_info;
55 4     4   1092 }
  4         5  
  4         21  
56              
57             sub read_posts : Public {
58 0     0   0 my ($self, %options) = @_;
59 0         0 HTTP::Request->new( GET => $READPOST_PATH . "?" . options2query(%options) )
60             ->basic_auth($self->auth_key)
61             ->submit_by($UA)
62             ->xmlized_content;
63 4     4   3157 }
  4         7  
  4         18  
64              
65             sub read_public_posts : Public {
66 0     0   0 my ($self, %options) = @_;
67 0 0       0 $options{site_id} = $self->site_id unless exists($options{site_id});
68 0 0 0     0 die "no site_id or hostname is given" unless exists($options{site_id}) or exists($options{hostname});
69 0         0 HTTP::Request->new( GET => $READPOST_PATH . "?" . options2query(%options) )
70             ->submit_by($UA)
71             ->xmlized_content;
72 4     4   1330 }
  4         9  
  4         44  
73              
74             sub primary_site : Public {
75 0     0   0 my ($self) = @_;
76 0         0 state $primary_site;
77 0         0 while ( my ($key, $value) = each %{ $self->account_info->{site} } ) {
  0         0  
78 0 0       0 $primary_site = $value->{id} if $value->{primary} eq "true"
79             }
80 0         0 $primary_site;
81 4     4   1186 }
  4         7  
  4         16  
82              
83             sub add_post : Public {
84 0     0   0 my ($self, %options) = @_;
85 0         0 my $request = HTTP::Request->new( POST => $NEWPOST_PATH )->basic_auth($self->auth_key);
86 0         0 $request->content(options2query(%options));
87 0         0 $UA->request($request)->xmlized_content;
88 4     4   1237 }
  4         7  
  4         19  
89              
90             sub add_comment :Public {
91 0     0   0 my ($self, %options) = @_;
92 0         0 my $request = HTTP::Request->new( POST => $COMMMENT_PATH )->basic_auth($self->auth_key);
93 0         0 $request->content(options2query(%options));
94 0         0 $UA->request($request)->xmlized_content;
95 4     4   1056 }
  4         9  
  4         15  
96              
97             sub options2query : Protected {
98 0         0 my (%options) = @_;
99 0         0 my $query;
100 0         0 while ( my ($key,$value) = each %options) {
101 0         0 $query .= "$key=$value&";
102             }
103 0   0     0 $query //= " "; # avoid warning $query was not initialized
104 0         0 $query =~ s/&$//g;
105 0         0 $query;
106 4     4   1106 }
  4         13  
  4         18  
107              
108             package HTTP::Request;
109              
110             # authorization_basic($user, $pass) is another way,
111             # but basic_auth($auth_key) reutrn request object itself.
112             sub basic_auth {
113 0     0 0   my ($self, $key) = @_;
114 0           $self->header(Authorization => "Basic $key");
115 0           $self;
116             }
117              
118             sub submit_by {
119 0     0 0   my ($self, $ua) = @_;
120 0           $ua->request($self);
121             }
122              
123             package HTTP::Response;
124 4     4   3743 use XML::Simple;
  0            
  0            
125              
126             sub xmlized_content {
127             my ($self) = @_;
128             XMLin($self->content);
129             }
130              
131             1;
132             __END__