lib/rmobio/ads/smaato.rb in rmobio-1.1.30 vs lib/rmobio/ads/smaato.rb in rmobio-1.1.31

- old
+ new

@@ -14,92 +14,128 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # require 'collections/sequenced_hash' +require 'open-uri' +require 'rexml/document' +require 'xmlsimple' module Rmobio module Ads module Smaato - @@adServer="http://dev.soma.smaato.com:8080/wap/reqAd.jsp" - - def getAdByPage(adClient,ip=request.remote_ip,url=request.request_uri,useragent=request.user_agent) - getAd(nil,adClient,ip,url,useragent) + @@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) - logger.info "Smaato ad retrieval" -=begin -Example: -http://dev.soma.smaato.com:8080/wap/reqAd.jsp?pub=3517&adspace=137 -&format=JPG&numads=10&offline=true&user=&response=XML -&width=176&height=208&device=Sony&devip=123.45.67.89&carrier=o2 -&lang=de -=end - @@userId=request.env['HTTP_USERID'] - + 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 # replace with real publisher ID - adArgs["adspace"]="137" # what is the ad space ID that we need here?! + 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["width"]="176" # get these from device capabilities service instead! - adArgs["height"]="208" # get these from device capabilities service instead adArgs["response"]="XML" - adArgs["user"]=@@userId - if (keywords) - adArgs["kws"]=CGI::escape(keywords) - end - # now built the URL to call out to based upon the base URL and ad hash + 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] + 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 -CALL: -http://dev.soma.smaato.com:8080/wap/reqAd.jsp? -pub=3517&adspace=0&format=JPG&numads=1&offline=false&width=100&height=100&response=XML&user=101&kws=India - -=end - # ok, now call google's mobile adSense service and get back the full ad - @adxml=open(adURL).read -=begin RESULT: -<?xml version="1.0"?><response xmlns="http://dev.soma.smaato.com:8080/wap/" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" -xsi:schemaLocation="http://dev.soma.smaato.com:8080/wap/ http://dev.soma.smaato.com:8080/wap/smaatoapi.xsd"> -<event-response><status>error</status><error>375</error><desc>No request data found.</desc></event-response> -<status>success</status><user><id>101</id></user> -<ads> -<ad id="1358" type="JPEG" width="112" height="20" refresh="1206058763039" caching="false" priority="0"> -<log-id>77bbb740be3c54debc5076f2be48df41</log-id><valid start="1204849163039" end="1212707963039" max="999999" /> -<link>http://dev.soma.smaato.com:8080/wap/getAd.jsp?id=1358&amp;l=77bbb740be3c54debc5076f2be48df41&amp;o=false</link><action target="http://dev.soma.smaato.com:8080/wap/lp.jsp?id=1358&amp;l=77bbb740be3c54debc5076f2be48df41&amp;o=false&amp;a=6&amp;t=http://www.smaato.mobi" type="LINK" acc="server" /> -<adtext /> +− + <response xsi:schemaLocation="http://soma.smaato.com/oapi/ http://soma.smaato.com/oapi/smaatoapi.xsd"> +− + <event-response> +<status>error</status> +<error>375</error> +<desc>No request data found.</desc> +</event-response> +<status>success</status> +− + <user> +<id>265563</id> +</user> +− + <ads> +− + <ad id="1000253060" type="JPEG" width="167" height="30" refresh="1215816516646" caching="true" priority="0"> +<log-id>8c7871a957ad65cc7e3c3047c5ade89c</log-id> +<valid start="1214606916646" end="1222382916646" max="999999"/> +− + <link> +http://soma.smaato.com/oapi/getAd.jsp?id=1000253060&l=8c7871a957ad65cc7e3c3047c5ade89c&o=true&t=ext +</link> +<action target="http://soma.smaato.com/oapi/lp.jsp?id=1000253060&l=8c7871a957ad65cc7e3c3047c5ade89c&o=false&a=6&t=ext" type="LINK" acc="server"/> +<adtext/> </ad> </ads> </response> =end - if (@adxml) - adDoc = REXML::Document.new @adxml - @adelement = adDoc.elements["//ad"] - if (@adelement) - @adurl=@adelement.elements["link"].text - @urltext=@adelement.elements["adtext"].text - @urltext||="LINK TO AD" + 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 - 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