I am trying to request information from Amazon using the vacuum gem (v. 2.0.2). However, I am not sure how I am supposed to return the results I get. Currently, I have made this code for my method:
def self.isbn_lookup(val)
request = Vacuum.new('US')
request.configure(
aws_access_key_id: 'access_key_goes_here',
aws_secret_access_key: 'secret_key_goes_here',
associate_tag: 'associate_tag_goes_here'
)
response = request.item_lookup(
query: {
'ItemId' => val,
'SearchIndex' => 'Books',
'IdType' => 'ISBN'
},
persistent: true
)
fr = response.to_h #returns complete hash
if fr["ItemLookupResponse"]["Items"]["Item"]["ItemAttributes"]["Author"]
@author = fr.dig("ItemLookupResponse","Items","Item","ItemAttributes","Author")
end
if fr["ItemLookupResponse"]["Items"]["Item"]["ItemAttributes"]["Author"]
@title = fr.dig("ItemLookupResponse","Items","Item","ItemAttributes","Title")
end
if fr["ItemLookupResponse"]["Items"]["Item"]["ItemAttributes"]["Manufacturer"]
@manufacturer = fr.dig("ItemLookupResponse","Items","Item","ItemAttributes","Manufacturer")
end
if fr["ItemLookupResponse"]["Items"]["Item"]["ItemAttributes"][6]["URL"]
@url = fr.dig("ItemLookupResponse","Items","Item","ItemLinks","ItemLink",6,"URL")
end
end
I would like to call be able to use the variables created in this method in my controller. How can I access the author, title, manufacturer, and url instance variables in my controller? I want to make it so that when users type in their ISBN, it will send an AJAX request to the server to request the relevant information (author, title, etc.). Currently, this is what my controller looks like:
def create
@listing = Listing.new(listing_params)
@listing.user = current_user
if @listing.save
flash[:success] = "Your listing was successfully saved."
redirect_to listing_path(@listing)
else
render 'new'
end
end
def edit
@isbn = Listing.isbn_lookup(1285741552)
end