<% require 'cgi' require 'net/http' # Replace the publisher identifier line with your publisher ID, available on line 4 of the PHP script # on https://www.google.com/adsense/mobile-settings#section=2. client_id = "pub-REPLACEME" def fetchurl(url_str, user_agent_param = nil, limit=10) raise "Too many redirects" if limit <= 0 user_agent = user_agent_param || "" uri = URI.parse(url_str) http = Net::HTTP.new(uri.host,uri.port) http.open_timeout = 3 http.read_timeout = 3 response = http.get("#{uri.path}?#{uri.query}", {"User-Agent"=>user_agent}) case response when Net::HTTPSuccess then return response.body when Net::HTTPRedirection then return fetchurl(response['location'], user_agent, limit - 1) else return response.error! end end google_dt = Time.now.to_i google_scheme = request.ssl? ? "https://" : "http://" google_host = CGI.escape(google_scheme + request.host) # allow a user agent to be passed in on the query string for # testing purposes user_agent = (request.params['user_agent'][0] rescue nil) || request.user_agent google_ad_url = "http://pagead2.googlesyndication.com/pagead/ads?" + "ad_type=text" + "&channel=" + "&client=ca-mb-#{client_id}" + "&dt=#{google_dt}" + "&format=mobile_single" + "&host=#{google_host}" + "&ip="+ CGI.escape(request.remote_ip.to_s) + "&markup=xhtml" + "&output=xhtml" + "&ref="+ CGI.escape(headers['HTTP_REFERER'] || '') + "&url="+google_host + CGI.escape(request.request_uri) + "&useragent="+ CGI.escape(user_agent); google_ad_output = fetchurl(google_ad_url) %> <%= google_ad_output %>