2016-03-28 21 views
0

Bu yüzden Amazon ürün reklamı API'sini kullanıyorum. Aramaların çoğunluğunun sonuçlarını araştırıp görüntüledim, ancak API çağrısı sonuçlarının bazı alanlarda sıfır değerine sahip olması durumunda çöküyor gibi görünüyor.API çağrısı = undefined method nil için: NilClass

undefined method `[]' for nil:NilClass 

Aşağıda geçerli:

Ben üçlü operatörünü kullanarak bunun üstesinden gelmek için çalıştık, ancak bu hala, aşağıdaki hata mesajında ​​veri bileşenleri sonuçlarını eksik API çağrısı çalışmak görünmüyor benim denetleyicisi alakalı kodu:

Orjinal Denemesi: Kullanımda

@hashed_products['ItemSearchResponse']['Items']['Item'].each do |item| 
    product = OpenStruct.new 
    product.Title = item['ItemAttributes']['Title'] 
    product.image = item['MediumImage']['URL'] 
    product.ASIN = item['ASIN'] 
    product.URL = item['DetailPageURL'] 
    product.Feature = item['ItemAttributes']['Feature'] 
    product.Price = item['OfferSummary']['LowestNewPrice']['FormattedPrice'] 
    @products << product 
end 

Üçlü Operatör:

@hashed_products['ItemSearchResponse']['Items']['Item'].each do |item| 
    product = OpenStruct.new 
    product.Title = item['ItemAttributes']['Title'] ? item['ItemAttributes']['Title'] : nil 
    product.image = item['MediumImage']['URL'] ? item['MediumImage']['URL'] : nil 
    product.ASIN = item['ASIN'] ? item['ASIN'] : nil 
    product.URL = item['DetailPageURL'] ? item['DetailPageURL'] : nil 
    product.Feature = item['ItemAttributes']['Feature'] ? item['ItemAttributes']['Feature'] : nil 
    product.Price = item['OfferSummary']['LowestNewPrice']['FormattedPrice'] ? item['OfferSummary']['LowestNewPrice']['FormattedPrice'] : nil 

    if product.Title || product.image || product.ASIN || product.URL || product.Feature || product.Price === nil 
     @products << product 
    end 
end 

cevap

0

Hash ['key']. üçlü işlemde test ederken.

Bunun yerine aşağıdakilerden kodunuzu:

@hashed_products['ItemSearchResponse']['Items']['Item'].each do |item| 
    product = OpenStruct.new 
    product.Title = item['ItemAttributes']['Title'] ? item['ItemAttributes']['Title'] : nil 
    product.image = item['MediumImage']['URL'] ? item['MediumImage']['URL'] : nil 
    product.ASIN = item['ASIN'] ? item['ASIN'] : nil 
    product.URL = item['DetailPageURL'] ? item['DetailPageURL'] : nil 
    product.Feature = item['ItemAttributes']['Feature'] ? item['ItemAttributes']['Feature'] : nil 
    product.Price = item['OfferSummary']['LowestNewPrice']['FormattedPrice'] ? item['OfferSummary']['LowestNewPrice']['FormattedPrice'] : nil 

    if product.Title || product.image || product.ASIN || product.URL || product.Feature || product.Price === nil 
     @products << product 
    end 
end 

şöyle olmalıdır:

@hashed_products['ItemSearchResponse']['Items']['Item'].each do |item| 
    product = OpenStruct.new 
    product.Title = item['ItemAttributes']['Title'].present? ? item['ItemAttributes']['Title'] : nil 
    product.image = item['MediumImage']['URL'].present? ? item['MediumImage']['URL'] : nil 
    product.ASIN = item['ASIN'].present? ? item['ASIN'] : nil 
    product.URL = item['DetailPageURL'].present? ? item['DetailPageURL'] : nil 
    product.Feature = item['ItemAttributes']['Feature'].present? ? item['ItemAttributes']['Feature'] : nil 
    product.Price = item['OfferSummary']['LowestNewPrice']['FormattedPrice'].present? ? item['OfferSummary']['LowestNewPrice']['FormattedPrice'] : nil 

    if product.Title || product.image || product.ASIN || product.URL || product.Feature || product.Price === nil 
     @products << product 
    end 
end 

Ama oldukça burada ulaşmaya çalıştıkları anlayamadım:

if product.Title || product.image || product.ASIN || product.URL || product.Feature || product.Price === nil 
    @products << product 
end 

product, @products dizisine eklemeye çalışıyorsunuz Title, image, ASIN, URL, Feature veya Price'dan biri varsa n0 var mı? Öyleyse, kodunuz çalışmıyor. Aşağıdaki olarak güncellemeye olmalıdır:

if product.Title.present? || product.image.present? || product.ASIN.present? || product.URL.present? || product.Feature.present? || product.Price.present? 
    @products << product 
end 

GÜNCELLEME: item herhangi birini veya tümünü nitelikler isteğe bağlı olduğunu, bunun yerine üçlü operasyon, aşağıdaki yöntemi uygulayabilirsiniz olası olduğundan :

@hashed_products['ItemSearchResponse']['Items']['Item'].each do |item| 
    product = OpenStruct.new 
    product.Title = item.try(:[], 'ItemAttributes').try(:[], 'Title') 
    product.image = item.try(:[], 'MediumImage').try(:[], 'URL') 
    product.ASIN = item.try(:[], 'ASIN') 
    product.URL = item.try(:[], 'DetailPageURL') 
    product.Feature = item.try(:[], 'ItemAttributes').try(:[], 'Feature') 
    product.Price = item.try(:[], 'OfferSummary').try(:[], 'LowestNewPrice').try(:[], 'FormattedPrice') 

    if product.Title.present? || product.image.present? || product.ASIN.present? || product.URL.present? || product.Feature.present? || product.Price.present? 
    @products << product 
    end 
end 
+0

Merhaba @Dharam, Üçlü operatörün aslında en iyi yaklaşım olup olmadığından emin değilim. Çözümünüzü denedim, ekledim. Ancak, bazı ölçütlerin NIL olduğu XML veri kümeleri üzerinde çöküyor. Aklında başka yaklaşımlar var mı? –

+0

Başarısız olduğunu görmek için stacktrace'i sağlayın. – Dharam

+0

Hata iletisi, ürünün product.image üzerinde başarısız olduğunu gösteriyor. Dolayısıyla, tüm öğelerin görüntülere veya orta boyutlu görüntülere sahip olmadığını varsayalım. –

İlgili konular