google-ads-common 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,3 +1,8 @@
1
+ 0.5.2:
2
+ - Additional information such as headers are now available via user-specified
3
+ block. See API-specific README for more details.
4
+ - Fixed xsi:types were not returned in responses (issue #10).
5
+
1
6
  0.5.1:
2
7
  - Now passing response header information as :header field in response.
3
8
  - Major performance improvements.
@@ -91,7 +91,7 @@ module AdsCommon
91
91
  # Check if the specified version has the requested service.
92
92
  if !api_config.version_has_service(version, name)
93
93
  raise AdsCommon::Errors::Error,
94
- "Version '%s' does not contain service '%s'", [version, name]
94
+ "Version '%s' does not contain service '%s'" % [version, name]
95
95
  end
96
96
 
97
97
  # Try to re-use the service for this version if it was requested before.
@@ -26,7 +26,7 @@ module AdsCommon
26
26
  # Contains helper methods for loading and managing the available services.
27
27
  # This module is meant to be imported into API-specific modules.
28
28
  module ApiConfig
29
- ADS_COMMON_VERSION = '0.5.1'
29
+ ADS_COMMON_VERSION = '0.5.2'
30
30
 
31
31
  # Get the available API versions.
32
32
  #
@@ -48,8 +48,8 @@ module AdsCommon
48
48
  end
49
49
  <% @actions.each do |action| %>
50
50
 
51
- def <%= action %>(*args)
52
- return execute_action('<%= action %>', args)
51
+ def <%= action %>(*args, &block)
52
+ return execute_action('<%= action %>', args, &block)
53
53
  end
54
54
  <% end %>
55
55
  <% @extensions.each do |extention| %>
@@ -88,7 +88,7 @@ module AdsCommon
88
88
  app_name = credentials[:user_agent]
89
89
  # We don't know the library version here. A breaking change needs to be
90
90
  # introduced. This is scheduled for 0.6.0, using Common version for now.
91
- lib_version = '0.5.1'
91
+ lib_version = '0.5.2'
92
92
  soap_user_agent = "Common-Ruby-%s; %s" % [lib_version, app_name]
93
93
  return "Savon/%s (%s)" % [Savon::Version, soap_user_agent]
94
94
  end
@@ -80,11 +80,11 @@ module AdsCommon
80
80
  end
81
81
 
82
82
  # Executes SOAP action specified as a string with given arguments.
83
- def execute_action(action_name, args)
83
+ def execute_action(action_name, args, &block)
84
84
  args = validate_args(action_name, args)
85
85
  response = execute_soap_request(action_name.to_sym, args)
86
86
  handle_errors(response)
87
- return extract_result(response, action_name)
87
+ return extract_result(response, action_name, &block)
88
88
  end
89
89
 
90
90
  # Executes the SOAP request with original SOAP name.
@@ -101,7 +101,7 @@ module AdsCommon
101
101
  # Validates input parameters to:
102
102
  # - add parameter names;
103
103
  # - resolve xsi:type where required;
104
- # - convert some native types to xml.
104
+ # - convert some native types to XML.
105
105
  def validate_args(action_name, args)
106
106
  validated_args = {}
107
107
  in_params = get_service_registry.get_method_signature(action_name)[:input]
@@ -110,6 +110,9 @@ module AdsCommon
110
110
  value = deep_copy(args[index])
111
111
  validated_args[key] = (value.nil?) ?
112
112
  nil : validate_arg(value, validated_args, key, in_param[:type])
113
+ # Adding :order! key to keep correct order in SOAP elements.
114
+ validated_args[:order!] =
115
+ generate_order_for_args(validated_args, in_params)
113
116
  end
114
117
  return validated_args
115
118
  end
@@ -143,7 +146,8 @@ module AdsCommon
143
146
  # Non-default namespace should be used, overriding default.
144
147
  if field_type and field_type.include?(:ns)
145
148
  namespace = get_service_registry.get_namespace(field_type[:ns])
146
- add_attribute(parent, prefix_key(key), 'xmlns', namespace)
149
+ key = prefix_key(key)
150
+ add_attribute(parent, key, 'xmlns', namespace)
147
151
  end
148
152
 
149
153
  # Handling custom xsi:type.
@@ -274,26 +278,38 @@ module AdsCommon
274
278
 
275
279
  # Extracts the finest results possible for the given result. Returns the
276
280
  # response itself in worst case (contents unknown).
277
- def extract_result(response, action_name)
281
+ def extract_result(response, action_name, &block)
278
282
  method = get_service_registry.get_method_signature(action_name)
279
283
  action = method[:output][:name].to_sym
280
284
  result = response.to_hash
281
285
  result = result[action] if result.include?(action)
282
286
  result = normalize_output(result, method)
283
- result[:header] = extract_header_data(response) if result.kind_of?(Hash)
287
+ run_user_block(response, result, &block) if block_given?
284
288
  return result
285
289
  end
286
290
 
291
+ # Yields to user-specified block with additional information such as
292
+ # headers.
293
+ def run_user_block(response, body, &block)
294
+ header = extract_header_data(response)
295
+ case block.arity
296
+ when 1: yield(header)
297
+ when 2: yield(header, body)
298
+ else
299
+ raise AdsCommon::Errors::ApiException,
300
+ "Wrong number of block parameters: %d" % block.arity
301
+ end
302
+ return nil
303
+ end
304
+
287
305
  # Extracts misc data from response header.
288
306
  def extract_header_data(response)
289
307
  header_type = get_full_type_signature(:SoapResponseHeader)
290
308
  headers = response.header[:response_header].dup
309
+ process_attributes(headers, false)
291
310
  result = headers.inject({}) do |result, (key, v)|
292
- # Attributes start with '@' and are not included in type definition.
293
- if !(key.to_s.start_with?('@'))
294
- normalize_output_field(headers, header_type[:fields], key)
295
- result[key] = headers[key]
296
- end
311
+ normalize_output_field(headers, header_type[:fields], key)
312
+ result[key] = headers[key]
297
313
  result
298
314
  end
299
315
  return result
@@ -313,6 +329,9 @@ module AdsCommon
313
329
  # - field_name: specifies field name to normalize
314
330
  def normalize_output_field(output_data, fields_list, field_name)
315
331
  return nil if output_data.nil?
332
+
333
+ process_attributes(output_data, true)
334
+
316
335
  field_definition = get_field_by_name(fields_list, field_name)
317
336
  if field_definition.nil?
318
337
  @api.logger.warn("Can not determine type for field: %s" % field_name)
@@ -366,7 +385,7 @@ module AdsCommon
366
385
  return (item.nil?) ? item :
367
386
  case type_name
368
387
  when 'long', 'int' then Integer(item)
369
- when 'double' then Float(item)
388
+ when 'double', 'float' then Float(item)
370
389
  when 'boolean' then item.kind_of?(String) ?
371
390
  item.casecmp('true') == 0 : item
372
391
  else item
@@ -423,5 +442,16 @@ module AdsCommon
423
442
  end
424
443
  return result
425
444
  end
445
+
446
+ # Handles attributes received from Savon.
447
+ def process_attributes(data, keep_xsi_type = false)
448
+ if data.kind_of?(Hash)
449
+ if keep_xsi_type
450
+ xsi_type = data.delete(:"@xsi:type")
451
+ data[:xsi_type] = xsi_type if xsi_type
452
+ end
453
+ data.reject! {|key, value| key.to_s.start_with?('@')}
454
+ end
455
+ end
426
456
  end
427
457
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-ads-common
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 5
9
- - 1
10
- version: 0.5.1
9
+ - 2
10
+ version: 0.5.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sergio Gomes
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-09-21 00:00:00 +04:00
19
+ date: 2011-10-20 00:00:00 +04:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -94,25 +94,25 @@ extra_rdoc_files:
94
94
  - COPYING
95
95
  - ChangeLog
96
96
  files:
97
- - lib/ads_common/config.rb
98
- - lib/ads_common/build/savon_abstract_generator.rb
99
- - lib/ads_common/build/savon_service_generator.rb
97
+ - lib/ads_common/auth/client_login_handler.rb
98
+ - lib/ads_common/auth/base_handler.rb
99
+ - lib/ads_common/auth/oauth_handler.rb
100
+ - lib/ads_common/credential_handler.rb
100
101
  - lib/ads_common/build/savon_generator.rb
101
102
  - lib/ads_common/build/savon_registry_generator.rb
102
103
  - lib/ads_common/build/savon_registry.rb
104
+ - lib/ads_common/build/savon_service_generator.rb
105
+ - lib/ads_common/build/savon_abstract_generator.rb
106
+ - lib/ads_common/api_config.rb
107
+ - lib/ads_common/config.rb
108
+ - lib/ads_common/api.rb
103
109
  - lib/ads_common/savon_headers/base_header_handler.rb
104
110
  - lib/ads_common/savon_headers/simple_header_handler.rb
105
- - lib/ads_common/savon_headers/httpi_request_proxy.rb
106
111
  - lib/ads_common/savon_headers/oauth_header_handler.rb
112
+ - lib/ads_common/savon_headers/httpi_request_proxy.rb
107
113
  - lib/ads_common/savon_service.rb
108
- - lib/ads_common/api_config.rb
109
114
  - lib/ads_common/http.rb
110
- - lib/ads_common/api.rb
111
- - lib/ads_common/auth/oauth_handler.rb
112
- - lib/ads_common/auth/base_handler.rb
113
- - lib/ads_common/auth/client_login_handler.rb
114
115
  - lib/ads_common/errors.rb
115
- - lib/ads_common/credential_handler.rb
116
116
  - Rakefile
117
117
  - test/test_config.rb
118
118
  - test/test_savon_service.rb