views:

56

answers:

2

Hi,

I'm accessing the Amazon AWS API using the ruby-aaws gem, but without going to much into details of the API or the gem, I think my problem is more of a general nature.

When I query the API I will end up with "object array", let's call it item, containing the API response. I can easily access the data in the array, e.g. puts item.item_attributes.artist.to_s

Now the API returns attributes whose identifier are reserved words in Rails, e.g. format or binding.

So doing this:
puts item.item_attributes.format.to_s will return method not found

while
puts item.item_attributes.binding.to_s will return some object hash like #<Binding:0xb70478e4>.

I can see that there are values under that name when doing
puts item.item_attributes.to_yaml

Snippet from the resulting yaml show artist and binding:
--- !seq:Amazon::AWS::AWSArray
- !ruby/object:Amazon::AWS::AWSObject::ItemAttributes
__val__:
artist: !seq:Amazon::AWS::AWSArray
- !ruby/object:Amazon::AWS::AWSObject::Artist
__val__: Summerbirds in the Cellar
binding: !seq:Amazon::AWS::AWSArray
- !ruby/object:Amazon::AWS::AWSObject::Binding
__val__: Vinyl

This was probably a very detailed explanation with a very simple solution, but I can't seem to find the solution.

edit
Finally found it. I guess it is because it is an array of objects, duh... puts item.item_attributes[0].binding.to_s

A: 

You may be able to access the individual attributes by using [] instead of the method name (which is probably provided using method_missing anyway).

So, item.item_attributes[:artist].to_s may return what you want. If it doesn't it would be worth trying 'artist' as the key instead.

Shadwell
Doesn't work error message is: `[]': Symbol as array index (TypeError)
capsized
Ah, looks like item['artist'] may do what you want. Let me know and I'll update my answer.
Shadwell
No avail: `[]': can't convert String into Integer (TypeError)
capsized
A: 

Finally found it. I guess it is because it is an array of objects, duh...
puts item.item_attributes[0].binding.to_s

capsized