Skip to content

Commit

Permalink
Added rescue for failed lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
scottned committed Oct 10, 2008
1 parent 934e511 commit 8b8010b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
5 changes: 3 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ include FileUtils
NAME = "acts_as_amazon_product"
# REV = File.read(".svn/entries")[/committed-rev="(\d+)"/, 1] rescue nil
# VERS = ENV['VERSION'] || ("1.1" + (REV ? ".#{REV}" : ""))
VERS = ENV['VERSION'] || "1.3"
VERS = ENV['VERSION'] || "1.3.1"
CLEAN.include ['**/.*.sw?', '*.gem', '.config', 'test/test.log']

desc 'Default: run unit tests.'
Expand All @@ -34,10 +34,11 @@ spec = Gem::Specification.new do |s|
s.version = VERS
s.author = "Scott Nedderman"
s.email = "scott@netphase.com"
s.homepage = "http://netphase.com"
s.summary = "A package for simplifying use of the Amazon/ECS API"
s.files = FileList['lib/*.rb', 'test/*'].to_a.reject {|f| f.match /config\.yml/ }
s.require_path = "lib"
s.autorequire = "acts_as_amazon_product"
# s.autorequire = "acts_as_amazon_product"
s.test_files = Dir.glob('tests/*.rb')
s.has_rdoc = true
s.extra_rdoc_files = ["README"]
Expand Down
40 changes: 24 additions & 16 deletions lib/acts_as_amazon_product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,31 @@ def amazon
name = (self.respond_to?('amazon_name')) ? self.send(self.amazon_name) : nil
search_index = (self.respond_to?('amazon_search_index')) ? self.amazon_search_index : 'Books'

if !asin.nil? && asin.length > 0
# puts "Looking up #{asin}"
res = Amazon::Ecs.item_lookup(self.send(self.amazon_asin), :response_group => 'Medium')
begin
if !asin.blank?
# puts "Looking up #{asin}"
res = Amazon::Ecs.item_lookup(self.send(self.amazon_asin), :response_group => 'Medium')

self.amazon_product =
AmazonProduct.new(:xml => res.doc.to_html, :asin => res.doc.at('asin').inner_html)
self.amazon_product.save
elsif !name.nil? && name.length > 0
# puts "Searching for #{name}"
res = Amazon::Ecs.item_search(self.send(self.amazon_name), :search_index => self.amazon_search_index, :response_group => 'Medium') #, :sort => 'salesrank'
res = res.doc.at('items/item')
self.amazon_product =
AmazonProduct.new(:xml => res.to_html, :asin => (res.at('itemattributes/isbn').nil? ? res.at('asin').inner_html : res.at('itemattributes/isbn').inner_html))
self.amazon_product.save
else
logger.error "No known attributes to search by"
end
self.amazon_product =
AmazonProduct.new(:xml => res.doc.to_html, :asin => res.doc.at('asin').inner_html)
self.amazon_product.save
elsif !name.blank?
# puts "Searching for #{name}"
res = Amazon::Ecs.item_search(self.send(self.amazon_name),
:search_index => self.amazon_search_index, :response_group => 'Medium') #, :sort => 'salesrank'
res = res.doc.at('items/item')
self.amazon_product =
AmazonProduct.new(:xml => res.to_html,
:asin => (res.at('itemattributes/isbn').nil? ?
res.at('asin').inner_html : res.at('itemattributes/isbn').inner_html))
self.amazon_product.save
else
logger.error "No known attributes to search by"
end
rescue
puts "Amazon lookup failed: $!"
self.amazon_product = AmazonProduct.new(:xml => "")
end
end
self.amazon_product
end
Expand Down

0 comments on commit 8b8010b

Please sign in to comment.