Reputation: 1825
I'm using the Ruby-AWS gem to interact with Amazon. I was testing it out in my controller and it worked fine, but when I moved the code into my Model, it isn't correctly parsing the data back from amazon.
For instance, in my controller
@items[0].medium_image.url = "http://ecx.images-amazon.com/images/I/61YIGdgh86L._SL160_.jpg"
But in my model
items[0].medium_umage.url = '[#<Amazon::AWS::AWSObject::URL:0x1030ba758 value="http://ecx.images-amazon.com/images/I/61YIGdgh86L._SL160_.jpg">]'
Please help!
In both cases, my code is:
def add_amazon_links
require 'amazon/aws'
require 'amazon/aws/search'
query = self.name
#!/usr/bin/ruby -w
#
# $Id: item_search1,v 1.4 2008/04/11 19:24:24 ianmacd Exp $
is = ItemSearch.new( 'All', { 'Keywords' => '#{query}' })
rg = ResponseGroup.new( 'Medium', 'Reviews')
req = Request.new
req.locale = 'us'
resp = req.search( is, rg )
items = resp.item_search_response[0].items[0].item
@items = resp.item_search_response[0].items[0].item
unless @items[0].blank?
self.image_url = @items[0].medium_image.url
self.a_price = @items[0].item_attributes[0].list_price[0].formatted_price
self.title = @items[0].item_attributes[0].title
self.a_url = @items[0].detail_page_url
self.save!
end
end
Upvotes: 0
Views: 602
Reputation: 284
The AWS module gives responses back as objects that detail the relevant result items.
I call the .to_s
method on these objects to get the text representation. As Jordan mentioned above, you may need to get the first element of the array too, so that'd be @items[0].medium_image[0].url.to_s
, though I suspect you would just need @items[0].medium_image.url.to_s
.
I've not worked specifically with the images, but my code (which works) does this: item.detail_page_url.to_s
I'm not entirely sure why your controller is getting the text version, and the model code isn't, but it's probably to do with the way the result is being used in the end result. If you're displaying the result in your view, the view code is implicitly calling to_s on the result - <%= whatever %>
effectively means <%= whatever.to_s %>
Upvotes: 1
Reputation: 106017
I haven't used AWS, but it looks like in the second case it's returning an array with one element, an Amazon::AWS::AWSObject::URL
object. That is, the following string:
[#<Amazon::AWS::AWSObject::URL:0x1030ba758 value="http://ecx.images-amazon.com/images/I/61YIGdgh86L._SL160_.jpg">]
...is what I'd expect to get if items[0].medium_image.url
was an Array with an AWSObject::URL
object in it and I tried to convert the whole thing to a string. To get at the actual URL string I would call items[0].medium_image.url[0].value
(the value
attribute of the first element of the array).
I don't know why you're getting a different value back in your controller then in your model, but you should look closely and make sure you're really calling it the same way in both cases.
Upvotes: 0