File Coverage

lib/WordPress/Post.pm
Criterion Covered Total %
statement 15 70 21.4
branch 0 42 0.0
condition 0 2 0.0
subroutine 5 10 50.0
pod 2 2 100.0
total 22 126 17.4


line stmt bran cond sub pod time code
1             package WordPress::Post;
2 2     2   85682 use base 'WordPress::Base';
  2         5  
  2         834  
3 2     2   10 use strict;
  2         3  
  2         49  
4 2     2   8 use warnings;
  2         5  
  2         38  
5 2     2   9 use Carp;
  2         3  
  2         113  
6 2     2   11 use vars qw($VERSION);
  2         2  
  2         2050  
7             $VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)/g;
8              
9              
10             my @struct_possible_keys = qw(title
11             description
12             mt_excerpt
13             mt_text_more
14             mt_allow_comments
15             mt_allow_pings
16             mt_tb_ping_urls
17             dateCreated
18             categories);
19              
20              
21             sub _is_in_arrayref {
22 0     0     my ($string,$arrayref) = @_;
23 0           for my $element (@$arrayref) {
24 0 0         return 1 if ($element eq $string);
25             }
26 0           return 0;
27             }
28              
29             sub post {
30 0     0 1   my $self = shift;
31 0           my $struct = shift;
32 0 0         ref $struct eq 'HASH' or die('arg must be hash');
33              
34             ### $struct
35              
36             # MAKE SURE WE HAVE A VALID STRUCT
37            
38 0           for my $key ( keys %$struct){
39 0 0         _is_in_arrayref($key, \@struct_possible_keys)
40             or croak("element $key is invalid, valid are: [@struct_possible_keys]");
41             }
42              
43              
44              
45              
46              
47             # CHECK MIN REQIREMENTS
48            
49 0 0         $struct->{title} or die('missing title');
50 0 0         $struct->{description} or die('missing description');
51              
52              
53              
54              
55              
56             # IF CATEGORY IS PRESENT, MAKE SURE IT IS A VALID ONE
57              
58 0 0         if ( defined $struct->{categories} ){
59             ### had categories..
60 0           for my $category (@{$struct->{categories}}){
  0            
61 0 0         _is_in_arrayref($category, $self->categories)
62             or croak("category $category is not one of the categories of the wordpress blog");
63             }
64             }
65             else {
66 0           $struct->{categories} = [];
67             }
68              
69              
70              
71              
72             # CHECK VALIDITY OF DATE IF PRESENT
73              
74 0 0         if( defined $struct->{dateCreated} ){
75             # is it a valid date?
76             #require Date::Simple;
77              
78             #my $date = Date::Simple->new($struct->{dateCreated})
79             # or croak("dateCreated value $$struct{dateCreated} is not a valid date");
80              
81             # date MUST be iso compliant - need to change the format to include time ?
82             #$struct->{dateCreated} = ($date->as_iso . ' 12:00:00');
83             #
84 0           warn('dateCreated not implemented.');
85 0           delete $struct->{dateCreated};
86             }
87              
88              
89             ### $struct
90            
91              
92              
93              
94              
95             # MAKE THE CALL
96              
97 0           my $call = $self->server->call(
98             'metaWeblog.newPost',
99             1, # blogid, idnored
100             $self->username,
101             $self->password,
102             $struct,
103             1, # 1 is publish
104             );
105              
106 0 0         if( _call_has_fault($call) ){
107 0           return 0;
108             }
109              
110              
111 0 0         if( my $result = $call->result ){
112             ### $result
113             }
114              
115 0           my $result =$call->result;
116 0 0         defined $result or warn('result should return id');
117              
118 0           return $result;
119             }
120              
121             sub _call_has_fault {
122 0     0     my $call = shift;
123 0 0         my $err = $call->fault or return 0;
124            
125 0           for( keys %$err ){
126 0           print STDERR "ERROR:$_ $$err{$_}\n";
127             }
128 0           return 1;
129             }
130              
131              
132             sub _file_bits {
133 0     0     my $abs_path = shift;
134             # from http://search.cpan.org/~gaas/MIME-Base64-3.07/Base64.pm
135 0           require MIME::Base64;
136              
137 0 0         open(FILE, $abs_path) or die($!);
138 0           my $bits;
139             my $buffer;
140 0           while( read(FILE, $buffer, (60*57)) ) {
141 0           $bits.= $buffer;
142             }
143              
144 0           return $bits;
145             }
146              
147              
148              
149             sub post_file {
150 0     0 1   my ($self,$_abs_path) = @_;
151 0 0         defined $_abs_path or croak('missing file path arg');
152 0           require Cwd;
153 0 0         my $abs_path = Cwd::abs_path($_abs_path) or die("cant resolve $_abs_path");
154 0 0         -f $abs_path or die("$abs_path not on disk");
155              
156 0 0         $abs_path=~/([^\/]+)$/ or die;
157 0           my $filename = $1;
158            
159 0           require File::Type;
160 0           my $ft = new File::Type;
161 0 0         my $type = $ft->mime_type($abs_path) or die('missing mime');
162              
163             ### $type
164             ### $abs_path
165             ### $filename
166              
167 0           my $struct ={
168             name => $filename,
169             type => $type,
170             bits => _file_bits($abs_path),
171             };
172              
173 0           my $call = $self->server->call(
174             'metaWeblog.newMediaObject',
175             1, # blogid ignored
176             $self->username,
177             $self->password,
178             $struct,
179             );
180              
181 0 0         if( _call_has_fault($call) ){
182 0           return;
183             }
184              
185 0 0         my $result = $call->result or die('no result returned');
186              
187 0 0 0       my $url = $result->{url} or warn('nothing in result->{url}') and return;
188             ### $url
189            
190 0           return $url;
191              
192             }
193              
194              
195             1;
196              
197              
198             __END__