File Coverage

blib/lib/WebService/Hatena/AsinCount.pm
Criterion Covered Total %
statement 27 31 87.1
branch 2 6 33.3
condition 4 18 22.2
subroutine 6 6 100.0
pod 2 2 100.0
total 41 63 65.0


line stmt bran cond sub pod time code
1             # ----------------------------------------------------------------
2             package WebService::Hatena::AsinCount;
3 1     1   73427 use strict;
  1         3  
  1         44  
4 1     1   7 use Carp;
  1         2  
  1         114  
5 1     1   1198 use XML::TreePP;
  1         11157  
  1         40  
6             # ----------------------------------------------------------------
7 1     1   12 use vars qw( $VERSION $XMLRPC_URL );
  1         2  
  1         433  
8             $VERSION = "0.02";
9             $XMLRPC_URL = 'http://b.hatena.ne.jp/xmlrpc';
10             # ----------------------------------------------------------------
11              
12             =head1 NAME
13              
14             WebService::Hatena::AsinCount -- Interface for Hatena::Bookmark:Asin's getCount XML-RPC API
15              
16             =head1 SYNOPSIS
17              
18             use WebService::Hatena::AsinCount;
19             my @list = (
20             '4774124966',
21             '4886487319'
22             );
23             my $hash = WebService::Hatena::AsinCount->getCount( @list );
24             foreach my $url ( @list ) {
25             printf( "%5d %s\n", $hash->{$url}, $url );
26             }
27              
28             =head1 DESCRIPTION
29              
30             WebService::Hatena::AsinCount is a interface for
31             "bookmark.getAsinCount" method provided by Hatena::Bookmark:Asin XML-RPC API.
32             This module follows WebService-Hatena-BookmarkCount module.
33             I respect the author very much and want to say 'thank you'.
34              
35             =head1 METHODS
36              
37             =head2 $bgc = WebService::Hatena::AsinCount->new();
38              
39             This constructor method creates a instance.
40              
41             =head3 $hash = $bgc->getCount( @list );
42              
43             This method make a call to "bookmark.getAsinCount" method of the Hatena Web
44             Services. The arguments is list of Asin code to get a number of registrations
45             in Hatena::Bookmark:Asin. This method returns a reference for a hash, which keys
46             are Asin code and which values are counts returned by the Hatena Web Services.
47              
48             =head3 $hash = WebService::Hatena::AsinCount->getCount( @list );
49              
50             You can call this method directly without creating a instance.
51              
52             =head1 MODULE DEPENDENCIES
53              
54             XML::TreePP LWP::UserAgent
55              
56             =head1 AUTHOR
57              
58             Makoto Tanaka
59              
60             =head1 COPYRIGHT AND LICENSE
61              
62             Copyright (c) 2006 Makoto Tanaka. All rights reserved. This program
63             is free software; you can redistribute it and/or modify it under the same
64             terms as Perl itself.
65              
66             =cut
67             # ----------------------------------------------------------------
68             my $TREEPP_OPTIONS = { force_array => [qw( member )] };
69             # ----------------------------------------------------------------
70             sub new {
71 1     1 1 3 my $package = shift;
72 1         2 my $self = {@_};
73 1         146 bless $self, $package;
74 1         12 $self->{treepp} = XML::TreePP->new( %$TREEPP_OPTIONS );
75 1         21 $self;
76             }
77             # ----------------------------------------------------------------
78             sub getCount {
79 1     1 1 15 my $self = shift;
80 1 50       9 $self = $self->new() unless ref $self;
81 1         3 my $list = \@_;
82 1         13 my $reqtree = {
83             methodCall => {
84             methodName => "bookmark.getAsinCount",
85             params => {
86 1         3 param => [ map { {value=>{string =>$_}}; } @$list ]
87             }
88             }
89             };
90 1         6 my $reqxml = $self->{treepp}->write( $reqtree );
91 1         70131 my( $restree, $resxml ) = $self->{treepp}->parsehttp( POST => $XMLRPC_URL, $reqxml );
92 1         1130062 my $outhash;
93 1 0 33     21 if ( ref $restree &&
      33        
      33        
      33        
      0        
      0        
94             ref $restree->{methodResponse} &&
95             ref $restree->{methodResponse}->{params} &&
96             ref $restree->{methodResponse}->{params}->{param} &&
97             ref $restree->{methodResponse}->{params}->{param}->{value} &&
98             ref $restree->{methodResponse}->{params}->{param}->{value}->{struct} &&
99             ref $restree->{methodResponse}->{params}->{param}->{value}->{struct}->{member} ) {
100 0         0 $outhash = {};
101 0         0 foreach my $member ( @{$restree->{methodResponse}->{params}->{param}->{value}->{struct}->{member}}) {
  0         0  
102 0         0 $outhash->{$member->{name}}= 0+$member->{value}->{int};
103             }
104             }
105 1 50       32 wantarray ? ( $outhash, $reqxml, $resxml ) : $outhash;
106             }
107             # ----------------------------------------------------------------
108             ;1;
109             # ----------------------------------------------------------------