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 118 def self.downloadCsvReport(wrapper, job_id, report_xml=nil) # Get XML report data. report_xml = downloadXmlReport(wrapper, job_id) if report_xml.nil? begin # Construct DOM object. doc = REXML::Document.new(report_xml) # Get data columns. columns = [] doc.elements.each('report/table/columns/column') do |column_elem| name = column_elem.attributes['name'] columns << name unless name.nil? end # Get data rows. rows = [] doc.elements.each('report/table/rows/row') do |row_elem| rows << row_elem.attributes unless row_elem.attributes.nil? end # Build CSV csv = '' CSV::Writer.generate(csv) do |writer| writer << columns rows.each do |row| row_values = [] columns.each { |column| row_values << row[column] } writer << row_values end end return csv rescue REXML::ParseException => e # Error parsing XML raise AdWords::Error::Error, "Error parsing report XML: %s\nSource: %s" % [e, e.backtrace.first] end 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 71 def self.downloadXmlReport(wrapper, job_id) sleep_interval = 30 # Repeatedly check the report status until it is finished. # 'Pending' and 'InProgress' statuses indicate the job is still being run. status = wrapper.getReportJobStatus(job_id).getReportJobStatusReturn while status != 'Completed' && status != 'Failed' sleep(sleep_interval) status = wrapper.getReportJobStatus(job_id).getReportJobStatusReturn end if status == 'Completed' report_url = wrapper.getReportDownloadUrl(job_id).getReportDownloadUrlReturn # Download the report via the HTTPClient library and return its # contents. The report is an XML document; the actual element names vary # depending on the type of report run and columns requested. begin client = HTTPClient.new return client.get_content(report_url) rescue Errno::ECONNRESET, SOAP::HTTPStreamError, SocketError => e # This exception indicates a connection-level error. # In general, it is likely to be transitory. raise AdWords::Error::Error, "Connection Error: %s\nSource: %s" % [e, e.backtrace.first] end else # Reports that pass validation will normally not fail, but if there is # an error in the report generation service it can sometimes happen. raise AdWords::Error::Error, 'Report generation failed.' end end
Return list of all extension methods, indexed by version and service.
Source: show
# File lib/adwords4r/apiextensions.rb, line 43 def self.extensions return @@extensions end
Return the parameter list for every extension method.
Source: show
# File lib/adwords4r/apiextensions.rb, line 48 def self.methods return @@methods end