# Ruby/Amazon nil Search Result

**URL:** <https://www.ruby-forum.com/t/ruby-amazon-nil-search-result/72775>\
**Category:** Rails\
**Created:** [October 18, 2006, 8:55pm UTC](https://www.ruby-forum.com/t/ruby-amazon-nil-search-result/72775 "2006-10-18T20:55:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [October 18, 2006, 8:55pm UTC](https://www.ruby-forum.com/t/ruby-amazon-nil-search-result/72775/1 "2006-10-18T20:55:31Z")

</div>

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:in`parse’  
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:in`search’  
C:/InstantRails/ruby/lib/ruby/site\_ruby/1.8/amazon/search.rb:648:in  
`asin\_search’

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [October 18, 2006, 9:37pm UTC](https://www.ruby-forum.com/t/ruby-amazon-nil-search-result/72775/2 "2006-10-18T21:37:06Z")

</div>

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](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](http://jystewart.net/process/)  
Play : [http://james.anthropiccollective.org](http://james.anthropiccollective.org)

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [October 18, 2006, 9:49pm UTC](https://www.ruby-forum.com/t/ruby-amazon-nil-search-result/72775/3 "2006-10-18T21:49:48Z")

</div>

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](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](http://jystewart.net/process/)  
> Play : [http://james.anthropiccollective.org](http://james.anthropiccollective.org)

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [October 18, 2006, 10:37pm UTC](https://www.ruby-forum.com/t/ruby-amazon-nil-search-result/72775/4 "2006-10-18T22:37:30Z")

</div>

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:

> **[loads\_from\_amazon (Ruby on Rails plugin)](https://jystewart.net/2006/07/20/loads_from_amazon-ruby-on-rails-plugin/)**
>
> UPDATE (10/2008): This plugin is no longer maintained. I’d recommend looking at one of the newer alternatives such as acts\_as\_amazon\_product if you need this functionality. I’ve been pr…

on-rails-plugin/

James.
