blib/lib/Net/BobrDobr.pm untested

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 183 CC 15 SCAR 28.4
CC
15
Cov
0%
new
42.0
call
20.0
plaincall
20.0
CRAP 17.1
1 package Net::BobrDobr;
2  
3 ## Project: BobrDobr.ru
4 ## File BobrDobr.pm
5 ## Creator: Artur Penttinen <artur.penttinen@scandicom.fi>
6 ## Creation date: <Friday 06-June-2008 08:58 || Artur Penttinen>
7 ## Last modified: <Friday 06-June-2008 12:28 || Artur Penttinen>
8 ##
9 ## Copyright (C) 2008 Artur Penttinen
10 ##
11 ## $Id:$
12 ##
13  
14 use 5.006;
15 use strict;
16 use warnings;
17  
18 use Digest::MD5 qw(md5_hex);
19 use LWP::UserAgent;
20 use URI::Escape;
21 use IO::File;
22 use XML::Simple;
23  
24 our $VERSION = (qw$Revision: $)[1] || "0.01";
25  
26 my $agent = "NetBobrDobr/$VERSION (perl-agent)";
27 my $authurl = "http://bobrdobr.ru/";
28 my $requrl = "http://bobrdobr.ru/services/rest/" .
29 "?method=%s&api_key=%s&%s&api_sig=%s";
30 my %commonhdr = ( 'accept' => "text/html, text/plain, text/css, */*;q=0.01",
31 'accept-encoding' => "gzip, bzip2",
32 'accept-language' => "ru, en",
33 'pragma' => "no-cache",
34 'cache-control' => "no-cache",
35 'accept-charset' => "utf8, iso-8859-1;q=0.01," );
36 my $error;
37  
38 sub new ($%) {
39 my ($class,%opt) = @_;
40 my $self = {};
41  
42 $self = read_file ($opt{'api'}) if (exists $opt{'api'});
43  
44 $self->{'.api-key'} = $opt{'api-key'}
45 if (exists $opt{'api-key'});
46 $self->{'.api-secret'} = $opt{'api-secret'}
47 if (exists $opt{'api-secret'});
48 $self->{'.debug'} = exists $opt{'debug'} && $opt{'debug'} ? 1 : 0;
49  
50 unless (exists ($self->{'.api-key'}) ||
51 exists ($self->{'.api-secret'})) {
52 $error = "not supplied api-key or api-secret";
53 # return;
54 }
55  
56 $self->{'.ua'} = new LWP::UserAgent ('agent' => $opt{'agent'} || $agent,
57 'timeout' => $opt{'timeout'} || 60,
58 'cookie_jar' => $opt{'cookie'} || {});
59 $self->{'.ua'}->env_proxy;
60 return bless $self,$class;
61 }
62  
63 sub connect ($$$) {
64 my ($self,$login,$password) = @_;
65  
66 unless (defined ($login) || defined ($password)) {
67 $error = "not supplied login or password";
68 return;
69 }
70  
71 $self->{'.ua'}->get ($authurl)->is_success or return;
72  
73 my $auth = { 'username' => $login,
74 'password' => $password,
75 'remember_user' => "on",
76 'next' => "/" };
77 my $ret = $self->{'.ua'}->post ($authurl . "login/",
78 %commonhdr,
79 'referer' => $authurl,
80 'content' => $auth);
81  
82 if ($ret->is_success || $ret->is_redirect) {
83 return $self;
84 }
85 else {
86 $error = $ret->status_line;
87 return;
88 }
89 }
90  
91 ## Call BD-method
92 sub call ($$;%) {
93 my ($self,$method,%args) = @_;
94  
95 my $secret = join "",$self->{'.api-secret'},"api_key",$self->{'.api-key'},
96 map { $_ eq "method" ? "method$method" : "$_$args{$_}" }
97 sort "method",keys %args;
98 my $md5secret = md5_hex ($secret);
99 my $args = join "&",map { join "=",uri_escape ($_),uri_escape ($args{$_}) }
100 keys %args;
101 my $url = sprintf $requrl,$method,$self->{'.api-key'},$args,$md5secret;
102 $url =~ s#&&+#&#g;
103  
104 return $url if ($self->{'.debug'});
105  
106 my $ret = $self->{'.ua'}->get ($url);
107  
108 if ($ret->is_success) {
109 return XMLin ($ret->content);
110 }
111 else {
112 $error = $ret->status_line;
113 return;
114 }
115 }
116  
117 ## Call BD-method, return plain answer
118 sub plaincall ($$;%) {
119 my ($self,$method,%args) = @_;
120  
121 my $secret = join "",$self->{'.api-secret'},"api_key",$self->{'.api-key'},
122 map { $_ eq "method" ? "method$method" : "$_$args{$_}" }
123 sort "method",keys %args;
124 my $md5secret = md5_hex ($secret);
125 my $args = join "&",map { join "=",uri_escape ($_),uri_escape ($args{$_}) }
126 keys %args;
127 my $url = sprintf $requrl,$method,$self->{'.api-key'},$args,$md5secret;
128 $url =~ s#&&+#&#g;
129  
130 return $url if ($self->{'.debug'});
131  
132 my $ret = $self->{'.ua'}->get ($url);
133  
134 if ($ret->is_success) {
135 return $ret->content;
136 }
137 else {
138 $error = $ret->status_line;
139 return;
140 }
141 }
142  
143 sub read_file ($) {
144 my ($file) = @_;
145 my %ret;
146  
147 my $io = new IO::File $file or return;
148 while (<$io>) {
149 chomp;
150 my ($a,$b) = split ":\\s*",$_,2;
151 $ret{".$a"} = $b if ($a eq "api-key" || $a eq "api-secret");
152 }
153 $io->close;
154  
155 return \%ret;
156 }
157  
158 sub error ($) {
159 return $error;
160 }
161  
162 1;
163  
164 __END__