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
Extension method — Gets the quota usage per child of the entire account tree below the root user. That is, for each child that is a client manager, all units below that client manager are summed upwards. The result is very useful for invoicing sub-MCCs that may have many clients that units may be spent on.
Note: unit data is not available in real time.
Args:
- wrapper: the service wrapper object for any API methods that need to be called
- start_date: starting date for unit spend count (as a Date)
- end_date: starting date for unit spend count (as a Date)
Returns:
- Hash of account to unit usage,
{ 'account1@domain.tld' => 10, 'account2@domain.tld' => 0, ...}
- List of double counted children (account emails)
Source: show
# File lib/adwords4r/apiextensions.rb, line 209 def self.getClientUnitsUsage(wrapper, start_date, end_date) # Create a new AdWords::API object to ensure thread-safety (we'll need to # change the clientEmail) adwords = AdWords::API.new(wrapper.api.credentials.dup) adwords.credentials.set_header('clientEmail', '') # Call unit_adder on the main user unit_map = client_unit_adder(adwords, start_date, end_date) # Pass back the spent unit information to the main AdWords::API object wrapper.api.mutex.synchronize do wrapper.api.last_units = adwords.total_units wrapper.api.total_units += adwords.total_units end return unit_map end
Extension method — Get a mapping between API methods and the number of units used through them for a given amount of time.
Running this helper method will consume 71 units.
Note: unit data is not available in real time.
Args:
- wrapper: the service wrapper object for any API methods that need to be called
- start_date: starting date for unit spend count (as a Date)
- end_date: starting date for unit spend count (as a Date)
Returns: Hash of service.method to the number of units used, e.g.,
{ 'AccountService.getAccountInfo' => 10, 'AccountService.getClientAccountInfos' => 0, ...}
Source: show
# File lib/adwords4r/apiextensions.rb, line 176 def self.getMethodUsage(wrapper, start_date, end_date) op_rates = AdWords::Utils.get_operation_rates usage = {} op_rates.each do |op| service, method = op usage[service + '.' + method] = wrapper.getUnitCountForMethod(service, method, start_date, end_date).getUnitCountForMethodReturn end return usage end
Return the parameter list for every extension method.
Source: show
# File lib/adwords4r/apiextensions.rb, line 48 def self.methods return @@methods end