Plagger/Plugin/Publish/HatenaBookmark.pm untested

filter: Statementstmt 0.0% 0 / 47 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 70 CC 4 SCAR 20.2
CC
4
Cov
0%
add_entry
20.0
register
2.0
rule_hook
2.0
CRAP 7.5
1 package Plagger::Plugin::Publish::HatenaBookmark;
2 use strict;
3 use base qw( Plagger::Plugin );
4  
5 use Encode;
6 use Time::HiRes qw(sleep);
7 use XML::Atom::Client;
8 use XML::Atom::Entry;
9  
10 sub register {
11 my($self, $context) = @_;
12 $context->register_hook(
13 $self,
14 'plugin.init' => \&initialize,
15 'publish.entry' => \&add_entry,
16 );
17 }
18  
19 sub rule_hook { 'publish.entry' }
20  
21 sub initialize {
22 my ($self, $context, $args) = @_;
23 $self->{client} = XML::Atom::Client->new;
24 $self->{client}->username($self->conf->{username});
25 $self->{client}->password($self->conf->{password});
26 }
27  
28 sub add_entry {
29 my ($self, $context, $args) = @_;
30  
31 my @tags = @{$args->{entry}->tags};
32 my $tag_string = @tags ? join('', map "[$_]", @tags) : '';
33  
34 my $entry = XML::Atom::Entry->new;
35 $entry->title(encode('utf-8', $args->{entry}->title));
36  
37 my $link = XML::Atom::Link->new;
38 $link->rel('related');
39 $link->type('text/html');
40 $link->href($args->{entry}->link);
41 $entry->add_link($link);
42  
43 if ($self->conf->{post_body}) {
44 $entry->summary( encode('utf-8', $tag_string . $args->{entry}->body_text) ); # xxx should be summary
45 } else {
46 $entry->summary( encode('utf-8', $tag_string) );
47 }
48  
49 my $loc = $self->{client}->createEntry('http://b.hatena.ne.jp/atom/post', $entry);
50 unless ($loc) {
51 $context->log(error => $self->{client}->errstr);
52 return;
53 }
54  
55 my $sleeping_time = $self->conf->{interval} || 3;
56 $context->log(info => "Post entry success. sleep $sleeping_time.");
57 sleep( $sleeping_time );
58 }
59  
60 1;
61  
62 __END__