File Coverage

blib/lib/Mobile/Ads.pm
Criterion Covered Total %
statement 21 79 26.5
branch 0 28 0.0
condition 0 9 0.0
subroutine 7 11 63.6
pod 3 4 75.0
total 31 131 23.6


line stmt bran cond sub pod time code
1             # Mobile::Ads.pm version 0.0.2
2             #
3             # Copyright (c) 2008 Thanos Chatziathanassioy . All rights reserved.
4             # This program is free software; you can redistribute it and/or
5             # modify it under the same terms as Perl itself.
6              
7             package Mobile::Ads;
8             local $^W;
9             require 'Exporter.pm';
10 1     1   36232 use vars qw(@ISA @EXPORT @EXPORT_OK);
  1         2  
  1         116  
11             @ISA = (Exporter);
12             @EXPORT = qw(); #&new);
13             @EXPORT_OK = qw();
14              
15             $Mobile::Ads::VERSION='0.0.2';
16             $Mobile::Ads::ver=$Mobile::Ads::VERSION;
17              
18 1     1   6 use strict 'vars';
  1         2  
  1         38  
19 1     1   3 use warnings;
  1         12  
  1         35  
20 1     1   1371 use diagnostics;
  1         222533  
  1         10  
21 1     1   512 use Carp();
  1         2  
  1         18  
22 1     1   1291 use LWP::UserAgent();
  1         50709  
  1         24  
23 1     1   7158 use HTTP::Request::Common();
  1         2297  
  1         859  
24              
25             =head1 NAME
26              
27             Mobile::Ads - base class for Mobile Ads
28              
29             Version 0.0.2
30              
31             =head1 SYNOPSIS
32              
33             use Mobile::Ads::Admob;
34            
35             =head1 DESCRIPTION
36              
37             C provides an object oriented interface to serve advertisements
38             It does nothing by itself and you should probably use one of
39             C (old AdMob implementation, lacks graphical ads)
40             C (newer AdMob, support image ads)
41             C
42             C (Google AdSense for Mobile)
43             C (ads.gr mobile ads)
44             C
45             C
46             C
47             C
48             C
49             C
50              
51             Refer to their man pages for help (?)
52              
53             =head1 new Mobile::Ads
54              
55             =cut
56              
57             sub new {
58 0     0 0   my $this = shift;
59 0   0       my $class = ref($this) || $this;
60 0           my $self = {};
61 0           bless $self, $class;
62              
63             #defaults
64 0           $self->{'timeout'} = 2;
65            
66 0           $self->{'ua'} = new LWP::UserAgent;
67 0           $self->{'ua'}->timeout($self->{'timeout'});
68 0           $self->{'ua'}->agent("Mobile::Ads/$Mobile::Ads::VERSION/".$self->{'ua'}->_agent);
69             #development aids
70 0           $self->{'DEBUG'} = 0;
71            
72 0           return $self;
73             }
74              
75             sub get_ad {
76 0     0 1   my $self = shift;
77            
78 0           my ($url,$method,$params) = ('','','');
79 0 0         if (ref $_[0] eq 'HASH') {
80 0           $url = $_[0]->{'url'};
81 0           $method = $_[0]->{'method'};
82 0           $params = $_[0]->{'params'};
83             }
84             else {
85 0           ($url,$method,$params) = @_;
86             }
87            
88             #test $uri is valid...
89 0 0         $url =~ m|^https?://| or Carp::croak("Ads.pm get_ad(): invalid URL $url\n");
90            
91 0           my $res;
92             # fetch data
93 0 0         if ($method eq 'POST') {
94 0 0 0       if ($params && ref($params) eq 'HASH') {
95 0 0         $self->{'DEBUG'} and Carp::cluck("POST to $url with $params\n");
96 0           $res = $self->{'ua'}->request(HTTP::Request::Common::POST $url, $params);
97             }
98             else {
99 0 0         $self->{'DEBUG'} and Carp::cluck("POST to $url without params\n");
100             #perhaps no need for $params, but one should still POST to this URL
101 0           $res = $self->{'ua'}->request(HTTP::Request::Common::POST $url);
102             }
103             }
104             else {
105 0 0 0       if ($params && ref($params) eq 'HASH') {
106             #add $params to the Query_String (remember to URLEncode them, btw)
107 0 0         if ($url =~ m|\?|) {
108 0           Carp::croak("Ads.pm get_ad() : either construct the QUERY_STRING for $url yourself, or give arguments in \$params, but not both\n");
109             }
110             else {
111             #first add the ``?'' to the URL (making it a URI :)
112 0           my $uri = $url . "?";
113            
114 0           my $last = 0; #useful to figure out when to stop adding ``&''s
115            
116 0           foreach (keys(%$params)) {
117 0 0         if ($last) {
118 0           $last = 0;
119 0           $uri .= "&";
120             }
121            
122 0           $uri .= $self->URLEncode($_);
123 0 0         if ($params->{$_}) {
124 0           $uri .= "=".$self->URLEncode($params->{$_});
125             }
126            
127 0           $last = 1;
128             }
129 0 0         $self->{'DEBUG'} and Carp::cluck("GET to $url with $params -> $uri\n");
130 0           $res = $self->{'ua'}->request(HTTP::Request::Common::GET $uri);
131             }
132             }
133             else {
134 0 0         $self->{'DEBUG'} and Carp::cluck("GET to $url without params\n");
135 0           $res = $self->{'ua'}->request(HTTP::Request::Common::GET $url);
136             }
137             }
138            
139 0 0         if ($res->is_success()) {
140 0 0         $self->{'DEBUG'} and Carp::cluck($res->as_string." is_success\n");
141 0           return($res->content());
142             }
143             else {
144 0           Carp::croak("HTTP Request failed with ".$res->as_string."\n");
145             }
146             }
147              
148             sub URLEncode {
149 0     0 1   my $self = shift;
150            
151 0           my $toencode = shift;
152            
153 0           $toencode =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/esg;
  0            
154 0           return $toencode;
155             }
156              
157             sub XMLEncode {
158 0     0 1   my $self = shift;
159            
160 0           my $toencode = shift;
161 0           $toencode =~ s/\&(?!amp\;)/\&/sg;
162 0           $toencode =~ s|\>|\>|sg;
163 0           $toencode =~ s|\<|\<|sg;
164             #only for the sake of completeness...
165 0           $toencode =~ s|\"|\"|sg;
166 0           return $toencode;
167             }
168              
169             =pod
170              
171             =head2 Methods
172              
173             =over 4
174              
175             =item get_ad
176              
177             C<>=> Does the actual HTTP.
178             url is obviously the ad serving site URL,
179             method is either ``POST'' or anything else (in which case a GET is performed)
180             and params is a hash reference with key/value pairs. The module will take care to URLEncode
181             as neccessary or set Content-length and Content-type if POST.
182             Note that you can either construct a GET URI yourself (taking care of encoding and stuff or
183             pass the arguments in params, but not both).
184              
185             Example:
186             $response = $ad->get_ad (
187             {
188             url => 'http://ad.serving.site/ad.php',
189             method => 'GET',
190             params => {
191             'some' => 'params'
192             'can' => 'go here'
193             }
194             });
195              
196             will result in ``http://ad.serving.site/ad.php?some=params&can=go%20here'' being actually sent
197             to the server.
198              
199             Will happily croak() if server is unreachable or not return 200, so eval() as neccessary.
200             Will NOT apply any kind of translation to the returned content. For this, each module should
201             make provisions for itself.
202            
203             =item URLEncode
204              
205             C<>=> Shamelessly plugged from Apache::ASP::Server::URLEncode
206              
207             =item XMLEncode
208              
209             C<>=> Just escapes ``&'' where neccessary in its input to make it XML safe.
210             Proably of use to everyone, so put here.
211              
212             =back
213              
214             =cut
215              
216              
217             =head1 Revision History
218              
219             0.0.1
220             Initial Release
221             0.0.2
222             First CPAN released version and the addition of $self->timeout to easily set
223             LWP::UserAgent timeout
224            
225             =head1 BUGS
226              
227             Thoughtlessly crafted to avoid having the same piece of code in several places.
228             Could use lots of enhancements.
229              
230             =head1 DISCLAIMER
231              
232             This module borrowed its OO interface from Mail::Sender.pm Version : 0.8.00
233             which is available on CPAN.
234              
235             =head1 AUTHOR
236              
237             Thanos Chatziathanassiou
238             http://www.arx.net
239              
240             =head1 COPYRIGHT
241              
242             Copyright (c) 2008 arx.net - Thanos Chatziathanassiou . All rights reserved.
243              
244             This program is free software; you can redistribute it and/or
245             modify it under the same terms as Perl itself.
246              
247             =cut
248              
249             1;