我正在使用Google的宝石google-adwords-api
和ads-common
(利用savon
)在Ruby上开发AdWords API客户端。
如果违反某些AdWords政策,Savon会在其message
中引发包含违规说明的例外情况。例如,违反商标政策:
[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}]}]
为了清晰起见,我对其进行了格式化,但最初在“目标”之后只有两个换行符,其余的是一行没有断行,只有逗号后面的空格,用“@”和自然语言文本。
如何以最简单的方式用Ruby解析这种消息?我希望这是一种标记语言,它有一个宝石。所以如果有更正确的方法,我不想使用正则表达式。
答案 0 :(得分:0)
找到答案。它在Google的宝石google-adwords-api
中撒谎。引发的例外是AdwordsApi::V201309::AdGroupAdService::ApiException
类。除message
属性外,它还具有errors
属性,该属性包含结构形式的相同消息。所以不需要解析奇怪的标记语法。
> 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"}]
一个很好的哈希,我可以从中提取policy_name
,violating_text
和其他详细信息。