Ruby/Amazon nil Search Result

Hi.

In the Amazon/Ruby module. when someone searches for an item, and it’s
not found on amazon’s side, there’s a nasty error page that is
displayed. I’d simply like to test if there are no results, to redirect
to a controller/action inside my rails app.

In the search.rb amazon file, in the RDoc, it says that if a block is
available, it will throw it all into @products, but if not it seems to
go here within the search.rb file:
class SearchError < StandardError; end

or would it help if i put the ruby/amazon module inside my rails app?

Thank you very much.

PS: the error:

Amazon::Search::Request::SearchError (Invalid Asin : 0234526345):
C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/amazon/search.rb:1173:in
get_args' C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/amazon/search.rb:1224:inparse’
C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/amazon/search.rb:1143:in
initialize' C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/amazon/search.rb:1036:insearch’
C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/amazon/search.rb:648:in
`asin_search’

On Oct 18, 2006, at 2:55 PM, Dominic S. wrote:

In the Amazon/Ruby module. when someone searches for an item, and it’s
not found on amazon’s side, there’s a nasty error page that is
displayed. I’d simply like to test if there are no results, to
redirect
to a controller/action inside my rails app.

That’s amazon/ruby throwing an exception, just like ActiveRecord will
if you call find with an invalid ID. There’s more about exceptions at:

http://www.rubycentral.com/book/tut_exceptions.html

What you want to do is to put your request to amazon/ruby inside a
begin/rescue block. eg.

begin
result = req.asin_search(asin_value.to_s)
create_instance_from_amazon(result.products[0])
rescue
# Handle the error, redirect, or whatever
end

Handle the result, continue execution

James.


James S. : Freelance Web D.
Work : Processing Greenbelt 2009 – James Stewart
Play : http://james.anthropiccollective.org

Thank you Mr James. Now for the re-directing. I guess I should put the
amazon/ruby files in my rails app’s public html to get it to re-drect
properly to one of my controller/actions now.

James S. wrote:

On Oct 18, 2006, at 2:55 PM, Dominic S. wrote:

In the Amazon/Ruby module. when someone searches for an item, and it’s
not found on amazon’s side, there’s a nasty error page that is
displayed. I’d simply like to test if there are no results, to
redirect
to a controller/action inside my rails app.

That’s amazon/ruby throwing an exception, just like ActiveRecord will
if you call find with an invalid ID. There’s more about exceptions at:

http://www.rubycentral.com/book/tut_exceptions.html

What you want to do is to put your request to amazon/ruby inside a
begin/rescue block. eg.

begin
result = req.asin_search(asin_value.to_s)
create_instance_from_amazon(result.products[0])
rescue
# Handle the error, redirect, or whatever
end

Handle the result, continue execution

James.


James S. : Freelance Web D.
Work : Processing Greenbelt 2009 – James Stewart
Play : http://james.anthropiccollective.org

On Oct 18, 2006, at 3:49 PM, Dominic S. wrote:

Thank you Mr James. Now for the re-directing. I guess I should put the
amazon/ruby files in my rails app’s public html to get it to re-drect
properly to one of my controller/actions now.

You’re probably best off putting the amazon/ruby files under
RAILS_ROOT/lib/ and then calling them from a controller or model
(depending on how you’re using them). For example:

require ‘amazon/search.rb’

class MyController < ApplicationController
include Amazon::Search

def amazon_search
	req = Request.new(dev_key, associates_id)
	result = req.asin_search(asin_value.to_s)
	# do something with your result
rescue
	redirect_to :action => 'error_page'
end

def error_page
end

end

You might want to take a look at my loads_from_amazon plugin:

on-rails-plugin/

James.