2013-11-20 40 views
0

我正在使用Google的寶石google-adwords-apiads-common(利用savon)在Ruby上開發AdWords API客戶端。解析AdWords API響應中的faultString

當違反了某些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}]}] 

我格式化它的清晰度,但最初,只有兩個行breakes後「有的放矢」,剩下的就是一條線不breakes與空間只有逗號後,使用「@」和自然語言文本。

如何以最簡單的方式用Ruby解析這種消息?我希望這是一些標記語言,它有一個寶石。所以如果有更正確的方法,我不想使用正則表達式。

+0

它是一個字符串還是一個結構?如果它是一個字符串,我會使用正則表達式。你需要提取哪些數據? –

+0

一個字符串。 RegEx是最好的答案,可能我會用它。我只是希望有人能夠識別標記語言並告訴我要使用什麼寶石。 – Hnatt

回答

0

找到了答案。它騙了谷歌的創業板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_nameviolating_text和其他細節。