SlideShare.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« SlideShare/Object.pm ↑ Summary
1 package WWW::SlideShare;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Digest::SHA1 qw(sha1_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use WWW::SlideShare::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use XML::Parser;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use Carp qw(confess);
9  
10 our $AUTOLOAD;
11 our $VERSION='1.01';
12  
13 use constant {
14 TIMEOUT => 10,
15 BASE_URL => 'http://www.slideshare.net/api/2/',
16 URL => 0,
17 OBJECT_TYPE => 1,
18 };
19  
20 {
21 my $ua = LWP::UserAgent->new;
22 $ua->timeout(TIMEOUT);
23 $ua->env_proxy;
24  
25 my %conf = (
26 '_get_slideshow' => [ 'get_slideshow', 'Slideshow' ],
27 'get_slideshows_by_tag' => [ 'get_slideshows_by_tag', 'Slideshow' ],
28 'get_slideshows_by_group' => [ 'get_slideshows_by_group', 'Slideshow' ],
29 'get_slideshows_by_user' => [ 'get_slideshows_by_user', 'Slideshow' ],
30 'search_slideshows' => [ 'search_slideshows', 'Slideshow' ],
31 'get_user_groups' => [ 'get_user_groups', 'Group' ],
32 'get_user_contacts' => [ 'get_user_contacts', 'Contact' ],
33 'get_user_tags' => [ 'get_user_tags', 'Tag' ],
34 );
35  
36 sub new {
37 my ($class, %params) = @_;
38  
39 my $self = {
40 '_objs' => [],
41 '_objtype' => '',
42 };
43  
44 $self->{'_api_key'} = $params{'api_key'};
45 $self->{'_ts'} = time;
46 $self->{'_hash'} = lc(sha1_hex($params{'secret'},$self->{'_ts'}));
47 $self->{'_params'} = "?api_key=$self->{'_api_key'}&ts=$self->{'_ts'}&hash=$self->{'_hash'}";
48  
49 bless $self, $class;
50 }
51  
52 sub AUTOLOAD {
53 my ($self, $params) = @_;
54  
55 my $name = $AUTOLOAD;
56 $name =~ s/.*://;
57  
58 confess "Method $name not found" unless $conf{$name};
59
60 my $query_string = join('&', map { "$_=$params->{$_}" } keys %$params);
61  
62 my $url = BASE_URL.$conf{$name}->[URL].$self->{'_params'}.'&'.$query_string;
63 my $response = $ua->get($url);
64  
65 if ($response->is_success) {
66 my $content = $response->content;
67 my $ref = $self->_parseResponse($content, $conf{$name}->[OBJECT_TYPE]);
68 return $ref;
69 }
70 else {
71 confess "Web Service Error for $url";
72 }
73 }
74  
75 sub get_slideshow {
76 my ($self, $params) = @_;
77
78 my $ref = $self->_get_slideshow($params);
79 return $ref->[0];
80 }
81  
82 sub _parseResponse {
83 my ($self, $xml, $objtype) = @_;
84  
85 if ($xml =~ /serviceerror/i) {
86 confess "SlideShare Service Error $xml";
87 }
88  
89 $self->{'_objs'} = [];
90 $self->{'_objtype'} = $objtype;
91  
92 my %fields = ();
93 my $last_tag = undef;
94  
95 my $st_sub_ref = sub {
96 my ($obj, $tag) = @_;
97  
98 if ($tag eq $self->{'_objtype'}) {
99 %fields = ();
100 }
101
102 $last_tag = $tag;
103 };
104  
105 my $text_handler_ref = sub {
106 $fields{$last_tag} .= $_[1];
107 };
108  
109 my $end_sub_ref = sub {
110 my ($obj, $tag) = @_;
111
112 $fields{$last_tag} =~ s/^\s+//g;
113 delete $fields{''};
114  
115 while (my ($k, $v) = each %fields) {
116 delete $fields{$k} if ($v eq '');
117 }
118  
119 if ($tag eq $self->{'_objtype'}) {
120 push @{$self->{'_objs'}}, WWW::SlideShare::Object->new({ %fields });
121 }
122 };
123  
124 my $p2 = new XML::Parser(Handlers => {
125 Start => $st_sub_ref,
126 Char => $text_handler_ref,
127 End => $end_sub_ref,
128 });
129 $p2->parse($xml);
130  
131 return $self->{'_objs'};
132 }
133  
134 sub DESTROY { }
135 }
136  
137 1;
138  
139 __END__
« SlideShare/Object.pm ↑ Summary