문제

I am developing an AdWords API client on Ruby using Google's gems google-adwords-api and ads-common (which utilizes savon).

When some AdWords policy is violated, Savon raises an exception with violation description in its message. For example, on violating trademark policies:

[PolicyViolationError{
  super=PolicyViolationError.POLICY_ERROR @ operations[0].operand.ad.headline,
  key=PolicyViolationKey{policyName=trademark,violatingText=Xerox},
  externalPolicyName=Trademarked Term,
  externalPolicyUrl=,
  externalPolicyDescription=Due to trademark reasons, we do not allow advertisers to use 'Xerox' in their Google AdWords ads. This term may be trademarked either for a certain product or service category and may apply only in certain countries you have targeted.

  ,
  isExemtable=true,
  violatingParts=[Part{index=0, length=5}]}]

I formatted it for clarity, but originally, there are only two line breakes after "targeted.", the rest is one line without breakes and with spaces only after commas, aroung "@" and in natural language text.

How do I parse this kind of message with Ruby in most simple way? My hope is that this is some markup language and there is a gem for it. So I don't want to use regular expressions if there is a more right way.

도움이 되었습니까?

해결책

Found the answer. It lied in Google's gem google-adwords-api. The exception raised was of class AdwordsApi::V201309::AdGroupAdService::ApiException. Besides the message attribute it has errors attribute containing the same message in form of a structure. So no need to parse the strange markup syntax.

> pp e.errors
[{:field_path=>"operations[0].operand.ad.headline",
  :trigger=>nil,
  :error_string=>"PolicyViolationError.POLICY_ERROR",
  :api_error_type=>"PolicyViolationError",
  :key=>{:policy_name=>"trademark", :violating_text=>"Sony"},
  :external_policy_name=>"Trademarked Term",
  :external_policy_url=>nil,
  :external_policy_description=>
   "Due to trademark reasons, we do not allow advertisers to use 'Sony' in their Google AdWords ads. This term may be trademarked either for a certain product or service category and may apply only in certain countries you have targeted.\n\n",
  :is_exemptable=>true,
  :violating_parts=>[{:index=>0, :length=>4}],
  :xsi_type=>"PolicyViolationError"}]

A nice hash from which I can pull policy_name, violating_text and other details.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top