Ads/Decktrade.pm untested

filter: Statementstmt 0.0% 0 / 70 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 60 MC/DCmcdc 0.0% 0 / 33 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 194 CC 11 SCAR 32.7
CC
11
Cov
0%
get_decktrade_ad
56.0
new
12.0
parse
12.0
CRAP 26.3
1 # Mobile::Ads::Decktrade.pm version 0.1.0
2 #
3 # Copyright (c) 2008 Thanos Chatziathanassioy <tchatzi@arx.net>. 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::Decktrade;
8 local $^W;
9 require 'Exporter.pm';
10 use vars qw(@ISA @EXPORT @EXPORT_OK);
11 @ISA = (Exporter);
12 @EXPORT = qw(); #&new);
13 @EXPORT_OK = qw();
14  
15 $Mobile::Ads::Decktrade::VERSION='0.1.0';
16 $Mobile::Ads::Decktrade::ver=$Mobile::Ads::Decktrade::VERSION;
17  
18 use strict 'vars';
19 use Carp();
20 use Mobile::Ads();
21  
22 =head1 NAME
23  
24 Mobile::Ads::Decktrade - module to serve Decktrade ads
25  
26 Version 0.1.0
27  
28 =head1 SYNOPSIS
29  
30 use Mobile::Ads::Decktrade;
31 $ad = new Mobile::Ads::Decktrade
32 ($text,$link,$image) = $ad->get_Decktrade_ad({
33 site => 'Ads.gr site code',
34 remote => $ENV{'HTTP_USER_AGENT'},
35 address => $ENV{'REMOTE_ADDR'},
36 text => 'default ad text',
37 link => 'default ad link',
38 });
39
40 =head1 DESCRIPTION
41  
42 C<Mobile::Ads::Decktrade> provides an object oriented interface to serve advertisements
43 from Decktrade.com in mobile sites.
44  
45 =head1 new Mobile::Ads::Decktrade
46  
47 =over 4
48  
49 =item [$parent]
50  
51 To reuse Mobile::Ads in multiple (subsequent) ad requests, you can pass a C<Mobile::Ads>
52 reference here. Instead of creating a new Mobile::Ads object, we will use the one you
53 pass instead. This might save a little C<LWP::UserAgent> creation/destruction time.
54  
55 =head2 Parameters/Properties
56  
57 =over 4
58  
59 =item site
60  
61 C<>=> Decktrade site code, delivered by them. Something in the form off ``1234'' (they call it ``dt_siteid'')
62  
63 =item remote
64  
65 C<>=> Remote User Agent ($ENV{'HTTP_USER_AGENT'}). In fact $ENV{'HTTP_USER_AGENT'} will be used
66 if not supplied.
67  
68 =item address
69  
70 C<>=> $ENV{'REMOTE_ADDR'}. All things about HTTP_USER_AGENT also apply here.
71  
72 =item text
73  
74 C<>=> Should we fail to retrieve a real ad, this is the text of the ad displayed instead
75  
76 =item link
77  
78 C<>=> Same with text, but for the ad's link.
79  
80 =back
81  
82 =cut
83  
84 sub new {
85 my $this = shift;
86 my $class = ref($this) || $this;
87 my $self = {};
88 bless $self, $class;
89  
90 my $parent = shift;
91
92 if ($parent && ref($parent) && ref($$parent) && ref($$parent) eq "Mobile::Ads") {
93 $self->{'parent'} = $$parent;
94 }
95 elsif ($parent && ref($parent) && ref($parent) eq "Mobile::Ads") {
96 $self->{'parent'} = $parent;
97 }
98 else {
99 $self->{'parent'} = new Mobile::Ads;
100 }
101
102 return $self;
103 }
104  
105 *get_ad = \&get_decktrade_ad;
106  
107 sub get_decktrade_ad {
108 my $self = shift;
109
110 my ($site,$remote,$address,$text,$link) = ('','','','','','');
111 if (ref $_[0] eq 'HASH') {
112 $site = $_[0]->{'site'} || $self->{'site'};
113 $remote = $_[0]->{'remote'};
114 $address = $_[0]->{'address'};
115 $text = $_[0]->{'text'};
116 $link = $_[0]->{'link'};
117 }
118 else {
119 ($site,$remote,$address,$text,$link) = @_;
120 }
121
122 $site ||= $self->{'site'};
123 $remote ||= $ENV{'HTTP_USER_AGENT'};
124 $address ||= $ENV{'REMOTE_ADDR'};
125 $text ||= $self->{'text'};
126 $link ||= $self->{'link'};
127
128 my $auid = $ENV{'REMOTE_ADDR'};
129 $ENV{'HTTP_X_UP_SUBNO'} and $auid = $ENV{'HTTP_X_UP_SUBNO'} or
130 $ENV{'HTTP_XID'} and $auid = $ENV{'HTTP_XID'} or
131 $ENV{'HTTP_CLIENTID'} and $auid = $ENV{'HTTP_CLIENTID'};
132
133 Carp::croak("cant serve ads without site\n") unless ($site);
134 Carp::croak("cant serve ads without remote user agent\n") unless ($remote);
135 Carp::croak("cant serve ads without remote address\n") unless ($address);
136
137 # fetch data
138 my $res;
139 my $params = {
140 asid => $site,
141 ua => $remote,
142 auid => $auid,
143 uip => $address,
144 mode => 'test'
145 };
146
147 eval q[$res = $self->{'parent'}->get_ad({
148 url => 'http://ads.decktrade.com/getAd.php5',
149 method => 'GET',
150 params => $params
151 });];
152 if ($@) {
153 return ($text,$link);
154 }
155 else {
156 my $ret = $self->parse($res,$text,$link);
157 if (wantarray) {
158 return ($ret->{'text'},$ret->{'link'},$ret->{'image'});
159 }
160 else {
161 return $ret;
162 }
163 }
164 }
165  
166 sub parse {
167 my $self = shift;
168
169 my ($toparse,$text,$link) = @_;
170 my $ret = { };
171
172 if ($toparse =~ m|\<a\s+href=\"([^\"]+)\".*?\<img\s+src=\"([^\"]+)\".*?alt=\"([^\"]+)\"|s) {
173 #an ad with both text and image (and link of course)
174 (
175 $ret->{'link'} = $1,
176 $ret->{'text'} = $3,
177 $ret->{'image'} = $2
178 );
179 }
180 elsif ($toparse =~ m|^<a\s+href=\"([^\"]+)\".*?\>(.+?)\</a\>|s) {
181 #an ad with only text and link
182 (
183 $ret->{'link'} = $1,
184 $ret->{'text'} = $2
185 );
186 }
187
188 defined($ret->{'link'}) and $ret->{'link'} = $self->{'parent'}->XMLEncode($ret->{'link'});
189 defined($ret->{'text'}) and $ret->{'text'} = $self->{'parent'}->XMLEncode($ret->{'text'});
190 defined($ret->{'image'}) and $ret->{'image'} = $self->{'parent'}->XMLEncode($ret->{'image'});
191
192 return $ret;
193 }
194  
195 =pod
196  
197 =head2 Methods
198  
199 =over 4
200  
201 =item get_Decktrade_ad
202  
203 C<>=> Does the actual fetching of the ad for the site given. Refer to new for details
204 Returns a list ($text_for_ad,$link_for_ad,$ad_image) value.
205  
206 =back
207  
208 =cut
209  
210  
211 =head1 Revision History
212  
213 0.0.1
214 Initial Release
215 0.0.2
216 Fixed stupid typo
217 0.0.3
218 Didn't preserve default values on failure
219 0.0.4
220 $ua timeout set to 20 sec
221 0.0.5
222 All ua stuff put in Mobile::Ads
223 0.0.6
224 Initial Release for Decktrade
225 0.0.7
226 Aliased get_ad to get_decktrade_ad
227 0.0.8
228 Option to reuse parent Mobile::Ads instead of creating anew
229 0.0.9
230 Skipped this to have same verion number in all modules
231 0.1.0
232 One could also use a reference to the parent... :)
233  
234 =head1 BUGS
235  
236 Thoughtlessly crafted to avoid having the same piece of code in several places.
237 Could use lots of enhancements.
238  
239 =head1 DISCLAIMER
240  
241 This module borrowed its OO interface from Mail::Sender.pm Version : 0.8.00
242 which is available on CPAN.
243  
244 =head1 AUTHOR
245  
246 Thanos Chatziathanassiou <tchatzi@arx.net>
247 http://www.arx.net
248  
249 =head1 COPYRIGHT
250  
251 Copyright (c) 2008 arx.net - Thanos Chatziathanassiou . All rights reserved.
252  
253 This program is free software; you can redistribute it and/or
254 modify it under the same terms as Perl itself.
255  
256 =cut
257  
258 1;