| 1 | # Mobile::Ads::Buzzcity.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::Buzzcity; | ||
| 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::Buzzcity::VERSION='0.1.0'; | ||
| 16 | $Mobile::Ads::Buzzcity::ver=$Mobile::Ads::Buzzcity::VERSION; | ||
| 17 | |||
| 18 | use strict 'vars'; | ||
| 19 | use Carp(); | ||
| 20 | use Mobile::Ads(); | ||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | Mobile::Ads::Buzzcity - module to serve buzzcity ads | ||
| 25 | |||
| 26 | Version 0.1.0 | ||
| 27 | |||
| 28 | =head1 SYNOPSIS | ||
| 29 | |||
| 30 | use Mobile::Ads::Buzzcity; | ||
| 31 | $ad = new Mobile::Ads::Buzzcity | ||
| 32 | ($text,$link,$image) = $ad->get_buzzcity_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::Buzzcity> provides an object oriented interface to serve advertisements | ||
| 43 | from buzzcity.com in mobile sites. | ||
| 44 | |||
| 45 | =head1 new Mobile::Ads::Buzzcity | ||
| 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<>=> Buzzcity site code, delivered by them. Something in the form off ``123456'' (they call it ``partnerid'') | ||
| 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_buzzcity_ad; | ||
| 106 | |||
| 107 | sub get_buzzcity_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 | Carp::croak("cant serve ads without site\n") unless ($site); | ||
| 129 | Carp::croak("cant serve ads without remote user agent\n") unless ($remote); | ||
| 130 | Carp::croak("cant serve ads without remote address\n") unless ($address); | ||
| 131 | |||
| 132 | # fetch data | ||
| 133 | my $res; | ||
| 134 | my $params = { | ||
| 135 | partnerid => $site, | ||
| 136 | a => $remote, | ||
| 137 | i => $address, | ||
| 138 | get => 1 | ||
| 139 | }; | ||
| 140 | |||
| 141 | eval q[$res = $self->{'parent'}->get_ad({ | ||
| 142 | url => 'http://ads.buzzcity.com/show.php', | ||
| 143 | method => 'GET', | ||
| 144 | params => $params | ||
| 145 | });]; | ||
| 146 | if ($@) { | ||
| 147 | return ($text,$link); | ||
| 148 | } | ||
| 149 | else { | ||
| 150 | my $ret = $self->parse($res,$site,$text,$link); | ||
| 151 | if (wantarray) { | ||
| 152 | return ($ret->{'text'},$ret->{'link'},$ret->{'image'}); | ||
| 153 | } | ||
| 154 | else { | ||
| 155 | return $ret; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | sub parse { | ||
| 161 | my $self = shift; | ||
| 162 | |||
| 163 | my ($toparse,$pid,$text,$link) = @_; | ||
| 164 | my $ret = { }; | ||
| 165 | |||
| 166 | if ($toparse =~ m|^(.+?)\r?\n(.+?)\r?\n(.+?)$|s) { | ||
| 167 | #an ad with both text and image (and link of course) | ||
| 168 | ( | ||
| 169 | $ret->{'link'} = $3, | ||
| 170 | $ret->{'text'} = $1, | ||
| 171 | $ret->{'image'} = $2 | ||
| 172 | ); | ||
| 173 | } | ||
| 174 | elsif ($toparse =~ m|^(.+?)\r?\n(.+?)$|s) { | ||
| 175 | #an ad with only text and link | ||
| 176 | ( | ||
| 177 | $ret->{'link'} = 'http://click.buzzcity.com/click.php?partnerid='.$pid.'&cid='.$2, | ||
| 178 | $ret->{'text'} = $1 | ||
| 179 | ); | ||
| 180 | } | ||
| 181 | |||
| 182 | defined($ret->{'link'}) and $ret->{'link'} = $self->{'parent'}->XMLEncode($ret->{'link'}); | ||
| 183 | defined($ret->{'text'}) and $ret->{'text'} = $self->{'parent'}->XMLEncode($ret->{'text'}); | ||
| 184 | defined($ret->{'image'}) and $ret->{'image'} = $self->{'parent'}->XMLEncode($ret->{'image'}); | ||
| 185 | |||
| 186 | return $ret; | ||
| 187 | } | ||
| 188 | |||
| 189 | =pod | ||
| 190 | |||
| 191 | =head2 Methods | ||
| 192 | |||
| 193 | =over 4 | ||
| 194 | |||
| 195 | =item get_buzzcity_ad | ||
| 196 | |||
| 197 | C<>=> Does the actual fetching of the ad for the site given. Refer to new for details | ||
| 198 | Returns a list ($text_for_ad,$link_for_ad,$ad_image) value. | ||
| 199 | |||
| 200 | =back | ||
| 201 | |||
| 202 | =cut | ||
| 203 | |||
| 204 | |||
| 205 | =head1 Revision History | ||
| 206 | |||
| 207 | 0.0.1 | ||
| 208 | Initial Release | ||
| 209 | 0.0.2 | ||
| 210 | Fixed stupid typo | ||
| 211 | 0.0.3 | ||
| 212 | Didn't preserve default values on failure | ||
| 213 | 0.0.4 | ||
| 214 | $ua timeout set to 20 sec | ||
| 215 | 0.0.5 | ||
| 216 | All ua stuff put in Mobile::Ads | ||
| 217 | 0.0.6 | ||
| 218 | Initial Release for Buzzcity, can only deal with text ads for now | ||
| 219 | 0.0.7 | ||
| 220 | Aliased get_ad to get_buzzcity_ad | ||
| 221 | 0.0.8 | ||
| 222 | Option to reuse parent Mobile::Ads instead of creating anew | ||
| 223 | 0.0.9 | ||
| 224 | Skipped this to have same verion number in all modules | ||
| 225 | 0.1.0 | ||
| 226 | One could also use a reference to the parent... :) | ||
| 227 | |||
| 228 | =head1 BUGS | ||
| 229 | |||
| 230 | Thoughtlessly crafted to avoid having the same piece of code in several places. | ||
| 231 | Could use lots of enhancements. | ||
| 232 | |||
| 233 | =head1 DISCLAIMER | ||
| 234 | |||
| 235 | This module borrowed its OO interface from Mail::Sender.pm Version : 0.8.00 | ||
| 236 | which is available on CPAN. | ||
| 237 | |||
| 238 | =head1 AUTHOR | ||
| 239 | |||
| 240 | Thanos Chatziathanassiou <tchatzi@arx.net> | ||
| 241 | http://www.arx.net | ||
| 242 | |||
| 243 | =head1 COPYRIGHT | ||
| 244 | |||
| 245 | Copyright (c) 2008 arx.net - Thanos Chatziathanassiou . All rights reserved. | ||
| 246 | |||
| 247 | This program is free software; you can redistribute it and/or | ||
| 248 | modify it under the same terms as Perl itself. | ||
| 249 | |||
| 250 | =cut | ||
| 251 | |||
| 252 | 1; |