Ruby 从AdWords API的响应解析faultString
我正在使用谷歌的gemsRuby 从AdWords API的响应解析faultString,ruby,savon,google-ads-api,Ruby,Savon,Google Ads Api,我正在使用谷歌的gemsgoogleadwordsapi和adsconomon(利用savon)在Ruby上开发adwordsapi客户端 当某些AdWords策略被违反时,Savon会在其消息中引发一个异常,并在其中进行违反描述。例如,关于违反商标政策: [PolicyViolationError{ super=PolicyViolationError.POLICY_ERROR @ operations[0].operand.ad.headline, key=PolicyViolati
googleadwordsapi
和adsconomon
(利用savon
)在Ruby上开发adwordsapi客户端
当某些AdWords策略被违反时,Savon会在其消息中引发一个异常,并在其中进行违反描述。例如,关于违反商标政策:
[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}]}]
为了清晰起见,我对其进行了格式化,但最初,在“targeted”之后只有两个换行符,其余的换行符是一行,没有换行符,只有逗号、“@”周围和自然语言文本中的空格
如何用Ruby以最简单的方式解析此类消息?我希望这是一种标记语言,它有一颗宝石。因此,如果有更正确的方法,我不想使用正则表达式。找到了答案。它存在于谷歌的gemgoogleadwordsapi
。引发的异常属于类AdwordsApi::V201309::AdGroupAdService::ApiException
。除了消息
属性外,它还有错误
属性,以结构形式包含相同的消息。因此,无需解析奇怪的标记语法
> 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"}]
我可以从中提取策略\u名称
,文本
和其他详细信息。是字符串还是结构?如果是字符串,我会使用正则表达式。您需要提取哪些数据?字符串。正则表达式是Uticlimate的答案,我可能会使用它。我只是希望有人能认识到这种标记语言,并告诉我应该使用什么gem。