Class: AdwordsApi::BatchJobUtils
- Inherits:
-
Object
- Object
- AdwordsApi::BatchJobUtils
- Defined in:
- lib/adwords_api/batch_job_utils.rb
Instance Method Summary collapse
-
#get_job_results(batch_job_url) ⇒ Object
Downloads the results of a batch job from the specified URL.
-
#initialize(api, version) ⇒ BatchJobUtils
constructor
Default constructor.
-
#put_incremental_operations(operations, batch_job_url, total_content_length = 0, is_last_request = false) ⇒ Object
Puts the provided operations to the provided URL, allowing for incremental followup puts.
-
#start_incremental_upload(batch_job_url, uploaded_bytes = 0) ⇒ Object
Provides a helper to manage incremental uploads.
-
#upload_operations(operations, batch_job_url) ⇒ Object
Uploads the given operations for a batch job to the provided URL.
Constructor Details
#initialize(api, version) ⇒ BatchJobUtils
Default constructor.
Args:
-
api: AdwordsApi object
-
version: API version to use
36 37 38 |
# File 'lib/adwords_api/batch_job_utils.rb', line 36 def initialize(api, version) @api, @version = api, version end |
Instance Method Details
#get_job_results(batch_job_url) ⇒ Object
Downloads the results of a batch job from the specified URL.
Args:
-
batch_job_url: The URL provided by BatchJobService to fetch the results from
Returns:
-
the results of the batch job, as a ruby hash, or nil if none yet exist
144 145 146 147 148 149 150 151 152 |
# File 'lib/adwords_api/batch_job_utils.rb', line 144 def get_job_results(batch_job_url) xml_response = AdsCommon::Http.get_response(batch_job_url, @api.config) begin return sanitize_result( get_nori().parse(xml_response.body)[:mutate_response][:rval]) rescue return nil end end |
#put_incremental_operations(operations, batch_job_url, total_content_length = 0, is_last_request = false) ⇒ Object
Puts the provided operations to the provided URL, allowing for incremental followup puts.
Args:
-
soap_operations: An array including SOAP operations provided by generate_soap_operations
-
batch_job_url: The URL provided by BatchJobService to post the provided operations to
-
total_content_length: The total number of bytes already uploaded incrementally. Set this to 0 the first time you call the method.
-
is_last_request: Whether or not this set of uploads will conclude the full request.
Returns:
-
total content length, including what was just uploaded. Pass this back into this method on subsequent calls.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/adwords_api/batch_job_utils.rb', line 93 def put_incremental_operations( operations, batch_job_url, total_content_length = 0, is_last_request = false) headers = DEFAULT_HEADERS soap_operations = generate_soap_operations(operations) request_body = soap_operations.join is_first_request = (total_content_length == 0) if is_first_request request_body = (UPLOAD_XML_PREFIX % [@version]) + request_body end if is_last_request request_body += UPLOAD_XML_SUFFIX end request_body = add_padding(request_body) content_length = request_body.size headers['Content-Length'] = content_length.to_s lower_bound = total_content_length upper_bound = total_content_length + content_length - 1 total_bytes = is_last_request ? upper_bound + 1 : '*' content_range = "bytes %d-%d/%s" % [lower_bound, upper_bound, total_bytes] headers['Content-Range'] = content_range log_request(batch_job_url, headers, request_body) # The HTTPI library fails to handle the response when uploading # incremental requests. We're not interested in the response, so just # ignore the error. begin AdsCommon::Http.put_response( batch_job_url, request_body, @api.config, headers) rescue ArgumentError end total_content_length += content_length return total_content_length end |
#start_incremental_upload(batch_job_url, uploaded_bytes = 0) ⇒ Object
Provides a helper to manage incremental uploads.
Args:
-
batch_job_url: The URL provided by BatchJobService to put the provided
operations to.
-
uploaded_bytes: The number of bytes already uploaded for this
incremental batch job. Can be retrieved from the IncrementalUploadHelper using uploaded_bytes.
Returns:
-
an IncrementalUploadHelper that will accept operations and put them,
keeping track of uploaded bytes automatically.
72 73 74 75 |
# File 'lib/adwords_api/batch_job_utils.rb', line 72 def start_incremental_upload(batch_job_url, uploaded_bytes = 0) return AdwordsApi::IncrementalUploadHelper.new( self, uploaded_bytes, batch_job_url) end |
#upload_operations(operations, batch_job_url) ⇒ Object
Uploads the given operations for a batch job to the provided URL.
Args:
-
hash_operations: An array of ruby has operations to execute by posting them to the provided URL
-
service_name: The name of the AdwordsApi service as a symbol that would normally make this request
-
batch_job_url: The URL provided by BatchjobService to post the provided operations to
Raises:
-
InvalidBatchJobOperationError: If there is a problem converting the
given operations to SOAP.
54 55 56 57 |
# File 'lib/adwords_api/batch_job_utils.rb', line 54 def upload_operations(operations, batch_job_url) soap_operations = generate_soap_operations(operations) post_soap_operations(soap_operations, batch_job_url) end |