# # Copyright (C) 2007 Mobio Networks, Inc. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # require 'collections/sequenced_hash' require 'open-uri' require 'rexml/document' require 'xmlsimple' module Rmobio module Ads module Smaato @@adServer="http://soma.smaato.com/oapi/reqAd.jsp" def get_ad(keywords,ad_client,request,kw_type="broad") getAd(keywords,ad_client,ip=request.remote_ip,request.request_uri,request.user_agent,request.env["HTTP_USERID"],kw_type) end def getAdByPage(adClient,ip=request.remote_ip,url=request.request_uri,useragent=request.user_agent,userid=request.env["HTTP_USERID"],kw_type="broad") getAd(nil,adClient,ip,url,useragent,userid,kw_type) end # this returns an adfrom Google's mobile AdSense, given a supplied set of keywords # Smaato doesn't seem to use the IP, the URL or the useragent def getAd(keywords,adClient,ip=request.remote_ip,url=request.request_uri,useragent=request.user_agent,userid=request.env["HTTP_USERID"],kw_type="broad") # build up the various arguments in the adArgs hash adArgs= SequencedHash.new adArgs["pub"]=adClient adArgs["adspace"]=MOBIO_CONFIG['adspace'] # what is the ad space ID that we need here?! adArgs["format"]="JPG" adArgs["numads"]="1" adArgs["offline"]="true" adArgs["response"]="XML" adArgs["ownid"]=userid # supply device information from the Mobio device capability service based on the user agent adArgs["device"]=capability("name",useragent) adArgs["device"]||="SonyEricsson%20K750i" adArgs["width"]=capability("width",useragent) # get these from device capabilities service instead! adArgs["width"]||=(MOBIO_CONFIG['default_width'].to_s) adArgs["height"]=capability("height",useragent) # get these from device capabilities service instead adArgs["height"]||=(MOBIO_CONFIG['default_height'].to_s) adArgs["devip"]=ip if ip and ip.size>0 adArgs["kws"]=CGI::escape(keywords) if keywords and keywords.size>0 # now build the URL to call out to based upon the base URL and ad hash adURL=@@adServer + "?" first=1 # dont put ampersand on first one adArgs.each_key do |x| if adArgs[x] (adURL=adURL+"&") unless first first=nil # start putting in &s adURL = adURL + x + "=" + adArgs[x].to_s end end p "URL to retrieve ad: " +adURL # result will have content, linktext, linkurl and optionally calltext, callurl, moretext result={} result['content']=open(adURL).read =begin RESULT: − error 375 No request data found. success 265563 8c7871a957ad65cc7e3c3047c5ade89c http://soma.smaato.com/oapi/getAd.jsp?id=1000253060&l=8c7871a957ad65cc7e3c3047c5ade89c&o=true&t=ext =end p result['content'] if (result['content']) doc=XmlSimple.xml_in(result['content']) ad=doc["ads"][0]["ad"][0] if (ad) p ad result['linkurl']=ad["link"] result['linktext']=ad["adtext"] result['linktext']||="LINK TO AD" end end result end # get_ad private # return the specified device capability for the specified useragent def capability(name,useragent) useragent||="Mozilla" # some reasonable default @@devicecap_service="http://homer.qa.mobiolabs.com/hub/devicecap/devices" devicecap_url=@@devicecap_service+"/capability?useragent="+CGI::escape(useragent)+"&capability="+name p devicecap_url begin doc=XmlSimple.xml_in(open(devicecap_url).read) rescue doc=nil end if (doc and doc[name]) doc[name] else nil end end end end end