I\'m trying to gracefully handle bad JSON for the following, where Hash#fetch
doesn\'t seem to be an option (Handle bad JSON gracefully with Hash#fetch):
As per the clarification in the comments, I will also answer. My solution is much the same as Зелёный's, but it takes dynamic attribute setting into account
mashie.products.each do |product|
product.extend Hashie::Extensions::DeepFetch
i%[name sale_price currency].each do |attribute|
value = product.deep_fetch attribute { 'not found' }
product.send("#{attribute}=", value)
end
@products << product
end
I not sure about right syntax in product.price = product.deep_fetch(attribute, { 'not found' })
:
mashie.products.each do |product|
product.extend Hashie::Extensions::DeepFetch
i%[name sale_price currency].each do |attribute|
product.price = product.deep_fetch attribute { 'not found' }
end
@products << product
end