Extension method — Download and return report data in CSV format.
Warning: this method is blocking for the calling thread.
Args:
wrapper: the service wrapper object for any API methods that need to be called
job_id: the job id for the report to be downloaded
xml: optional parameter used for testing and debugging
Returns: The CSV data for the report (as a string)
Source: show
# File lib/adwords4r/apiextensions.rb, line 121 121: def self.downloadCsvReport(wrapper, job_id, report_xml=nil) 122: # Get XML report data. 123: report_xml = downloadXmlReport(wrapper, job_id) if report_xml.nil? 124: 125: begin 126: # Construct DOM object. 127: doc = REXML::Document.new(report_xml) 128: 129: # Get data columns. 130: columns = [] 131: doc.elements.each('report/table/columns/column') do |column_elem| 132: name = column_elem.attributes['name'] 133: columns << name unless name.nil? 134: end 135: 136: # Get data rows. 137: rows = [] 138: doc.elements.each('report/table/rows/row') do |row_elem| 139: rows << row_elem.attributes unless row_elem.attributes.nil? 140: end 141: 142: # Build CSV 143: csv = '' 144: CSV::Writer.generate(csv) do |writer| 145: writer << columns 146: rows.each do |row| 147: row_values = [] 148: columns.each { |column| row_values << row[column] } 149: writer << row_values 150: end 151: end 152: 153: return csv 154: rescue REXML::ParseException => e 155: # Error parsing XML 156: raise AdWords::Error::Error, 157: "Error parsing report XML: %s\nSource: %s" % [e, e.backtrace.first] 158: end 159: end
Extension method — Download and return a v20xx report.
Warning: this method is blocking for the calling thread.
Args:
wrapper: the service wrapper object for any API methods that need to be called
report_definition_id: the id for the report definition
Returns: The data for the report (as a string)
Source: show
# File lib/adwords4r/apiextensions.rb, line 197 197: def self.downloadReport(wrapper, report_definition_id) 198: # Set URL parameters. 199: parameters = {'__rd' => report_definition_id.to_s} 200: 201: # Set HTTP headers. 202: headers = {} 203: headers['Authorization'] = 'GoogleLogin auth=%s' % 204: wrapper.api.credentials.auth_token 205: creds = wrapper.api.credentials.credentials 206: if creds['clientEmail'] 207: headers['clientEmail'] = creds['clientEmail'] 208: elsif creds['clientCustomerId'] 209: headers['clientCustomerId'] = creds['clientCustomerId'] 210: end 211: 212: # Get download URL. 213: url = AdWords::Service.report_download_url( 214: wrapper.api.credentials.environment, 201003) 215: 216: # Download report data. 217: client = HTTPClient.new 218: report_data = client.get_content(url, parameters, headers) 219: 220: return report_data 221: end
Extension method — Download and return a v20xx report into a file.
Warning: this method is blocking for the calling thread.
Args:
wrapper: the service wrapper object for any API methods that need to be called
report_definition_id: the id for the report definition
path: the file where the data should be saved
Returns: nil
Source: show
# File lib/adwords4r/apiextensions.rb, line 174 174: def self.downloadReportAsFile(wrapper, report_definition_id, path) 175: report_data = downloadReport(wrapper, report_definition_id) 176: 177: # Write to file (if provided) 178: if path 179: open(path, 'w') { |file| file.puts(report_data) } 180: end 181: 182: return nil 183: end
Extension method — Download and return report data in XML format.
Warning: this method is blocking for the calling thread.
Args:
wrapper: the service wrapper object for any API methods that need to be called
job_id: the job id for the report to be downloaded
Returns: The xml for the report (as a string)
Source: show
# File lib/adwords4r/apiextensions.rb, line 74 74: def self.downloadXmlReport(wrapper, job_id) 75: sleep_interval = 30 76: 77: # Repeatedly check the report status until it is finished. 78: # 'Pending' and 'InProgress' statuses indicate the job is still being run. 79: status = wrapper.getReportJobStatus(job_id).getReportJobStatusReturn 80: while status != 'Completed' && status != 'Failed' 81: sleep(sleep_interval) 82: status = wrapper.getReportJobStatus(job_id).getReportJobStatusReturn 83: end 84: 85: if status == 'Completed' 86: report_url = 87: wrapper.getReportDownloadUrl(job_id).getReportDownloadUrlReturn 88: 89: # Download the report via the HTTPClient library and return its 90: # contents. The report is an XML document; the actual element names vary 91: # depending on the type of report run and columns requested. 92: begin 93: client = HTTPClient.new 94: return client.get_content(report_url) 95: rescue Errno::ECONNRESET, SOAP::HTTPStreamError, SocketError => e 96: # This exception indicates a connection-level error. 97: # In general, it is likely to be transitory. 98: raise AdWords::Error::Error, "Connection Error: %s\nSource: %s" % 99: [e, e.backtrace.first] 100: end 101: else 102: # Reports that pass validation will normally not fail, but if there is 103: # an error in the report generation service it can sometimes happen. 104: raise AdWords::Error::Error, 'Report generation failed.' 105: end 106: end
Return list of all extension methods, indexed by version and service.
Source: show
# File lib/adwords4r/apiextensions.rb, line 46 46: def self.extensions 47: return @@extensions 48: end
Return the parameter list for every extension method.
Source: show
# File lib/adwords4r/apiextensions.rb, line 51 51: def self.methods 52: return @@methods 53: end