blib/lib/Posterous.pm

filter: Statementstmt 81.0% 90 / 111 Branchbran 28.5% 4 / 14 Conditioncond 28.5% 4 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 83.8% 26 / 31 Podpod 72.7% 8 / 11 totaltotal 69.4% 132 / 190 CC 13 SCAR 14.3
CC
13
Cov
71%
read_public_posts
20.0
primary_site
12.0
new
4.4
CRAP 4.2
↑ Summary
1 package Posterous;
2  
3 4 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 our $VERSION = '0.03';
8  
9 4 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Rubyish::Attribute;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Attribute::Protected;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 4 our $DOMAIN = "http://posterous.com";
18  
19 4 our $AUTH_PATH = $DOMAIN."/api/getsites";
20 4 our $NEWPOST_PATH = $DOMAIN."/api/newpost";
21 4 our $COMMMENT_PATH = $DOMAIN."/api/newcomment";
22 4 our $READPOST_PATH = $DOMAIN."/api/readposts";
23  
24 4 our $UA = LWP::UserAgent->new();
25  
26 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
27 4 attr_accessor "user", "pass", "site_id";
28 }
29  
30 2 ▶sub new {
Subroutine new: called CC=4 SCAR=14.8
Pod documented
31 2 my ($class, $user, $pass, $site_id) = @_;
32 2 ▶ die "didn\'t give user\' email or password" unless defined($user) && defined($pass);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $user and defined $pass 0 2 2
Condition33%1/3
defined $user and defined $pass
!ll&&!rl&&r
002
MC/DC0%0/2
defined $user and defined $pass
defined $user defined $pass
Truth table33%1/3
A and B
A= defined $userB= defined $pass
ABresult
0X0
100
111
33 2 my $self = bless {}, $class;
34 2 __user__ = $user;
35 2 __pass__ = $pass;
36 2 ▶ __site_id__ = $site_id if $site_id;
Branch: 1 branch
BranchTrueFalseTotal
if $site_id 0 2 2
37 2 $self;
38 }
39  
40 2 ▶sub auth_key : Public {
Subroutine auth_key: called CC=2 SCAR=7.4
Pod documented
41 2 my ($self) = @_;
42 2 state $auth_key;
43 2 ▶ $auth_key //= encode_base64($self->user.":".$self->pass);
Condition33%1/3
$auth_key //= &encode_base64($self->user . ':' . $self->pass)
l!l&&r!l&&!r
020
MC/DC0%0/2
$auth_key //= &encode_base64($self->user . ':' . $self->pass)
$auth_key &encode_base64($self->user . ':' . $self->pass)
Truth table33%1/3
A //= B
A= $auth_keyB= &encode_base64($self->user . ':' . $self->pass)
ABresult
1X1
011
000
44 2 $auth_key;
45 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 1 ▶sub account_info : Public {
Subroutine account_info: called CC=2 SCAR=8.4
Pod documented
48 1 my ($self) = @_;
49 1 state $account_info;
50 1 ▶ $account_info //= HTTP::Request->new( GET => $AUTH_PATH )
Condition33%1/3
$account_info //= "HTTP::Request"->new("GET", $Posterous::AUTH_PATH)->basic_auth($self->auth_key)->submit_by($Posterous::UA)->xmlized_content
l!l&&r!l&&!r
001
MC/DC0%0/2
$account_info //= "HTTP::Request"->new("GET", $Posterous::AUTH_PATH)->basic_auth($self->auth_key)->submit_by($Posterous::UA)->xmlized_content
$account_info "HTTP::Request"->new("GET", $Posterous::AUTH_PATH)->basic_auth($self->auth_key)->submit_by($Posterous::UA)->xmlized_content
Truth table33%1/3
A //= B
A= $account_infoB= "HTTP::Request"->new("GET", $Posterous::AUTH_PATH)->basic_auth($self->auth_key)->submit_by($Posterous::UA)->xmlized_content
ABresult
1X1
011
000
51 ->basic_auth($self->auth_key)
52 ->submit_by($UA)
53 ->xmlized_content;
54 0 $account_info;
55 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 0 ▶sub read_posts : Public {
Subroutine read_posts: not called CC=1 SCAR=6.9
Pod documented
58 0 my ($self, %options) = @_;
59 0 HTTP::Request->new( GET => $READPOST_PATH . "?" . options2query(%options) )
60 ->basic_auth($self->auth_key)
61 ->submit_by($UA)
62 ->xmlized_content;
63 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 0 ▶sub read_public_posts : Public {
Subroutine read_public_posts: not called CC=4 SCAR=30.0
Pod documented
66 0 my ($self, %options) = @_;
67 0 ▶ $options{site_id} = $self->site_id unless exists($options{site_id});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{"site_id"} 0 0 0
68 0 ▶ die "no site_id or hostname is given" unless exists($options{site_id}) or exists($options{hostname});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{"site_id"} or exists $options{"hostname"} 0 0 0
Condition0%0/3
exists $options{"site_id"} or exists $options{"hostname"}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $options{"site_id"} or exists $options{"hostname"}
exists $options{"site_id"} exists $options{"hostname"}
Truth table0%0/3
A or B
A= exists $options{"site_id"}B= exists $options{"hostname"}
ABresult
1X1
011
000
69 0 HTTP::Request->new( GET => $READPOST_PATH . "?" . options2query(%options) )
70 ->submit_by($UA)
71 ->xmlized_content;
72 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 0 ▶sub primary_site : Public {
Subroutine primary_site: not called CC=3 SCAR=24.8
Pod documented
75 0 my ($self) = @_;
76 0 state $primary_site;
77 0 ▶ while ( my ($key, $value) = each %{ $self->account_info->{site} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->account_info->{"site"};}) 0 0 0
78 0 ▶ $primary_site = $value->{id} if $value->{primary} eq "true"
Branch: 1 branch
BranchTrueFalseTotal
if $value->{"primary"} eq "true" 0 0 0
79 }
80 0 $primary_site;
81 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 0 ▶sub add_post : Public {
Subroutine add_post: not called CC=1 SCAR=6.9
Pod documented
84 0 my ($self, %options) = @_;
85 0 my $request = HTTP::Request->new( POST => $NEWPOST_PATH )->basic_auth($self->auth_key);
86 0 $request->content(options2query(%options));
87 0 $UA->request($request)->xmlized_content;
88 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
89  
90 0 ▶sub add_comment :Public {
Subroutine add_comment: not called CC=1 SCAR=6.9
Pod documented
91 0 my ($self, %options) = @_;
92 0 my $request = HTTP::Request->new( POST => $COMMMENT_PATH )->basic_auth($self->auth_key);
93 0 $request->content(options2query(%options));
94 0 $UA->request($request)->xmlized_content;
95 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
96  
97 1 ▶sub options2query : Protected {
Subroutine options2query: called CC=3 SCAR=11.0
98 1 my (%options) = @_;
99 1 my $query;
100 1 ▶ while ( my ($key,$value) = each %options) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %options) 2 1 3
101 2 $query .= "$key=$value&";
102 }
103 1 ▶ $query //= " "; # avoid warning $query was not initialized
Condition50%1/2
$query //= " "
l!l
10
MC/DC0%0/1
$query //= " "
$query
Truth table50%1/2
A
A= $query
Aresult
11
00
104 1 $query =~ s/&$//g;
105 1 $query;
106 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
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 1 ▶sub basic_auth {
Subroutine basic_auth: called CC=1 SCAR=0.0
Pod undocumented
113 1 my ($self, $key) = @_;
114 1 $self->header(Authorization => "Basic $key");
115 1 $self;
116 }
117  
118 1 ▶sub submit_by {
Subroutine submit_by: called CC=1 SCAR=0.0
Pod undocumented
119 1 my ($self, $ua) = @_;
120 1 $ua->request($self);
121 }
122  
123 package HTTP::Response;
124 4 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
125  
126 1 ▶sub xmlized_content {
Subroutine xmlized_content: called CC=1 SCAR=0.0
Pod undocumented
127 1 my ($self) = @_;
128 1 XMLin($self->content);
129 }
130  
131 4 1;
132 __END__
↑ Summary