Tuesday, 6 August 2013

Ruby/Rails - How to Combine Two JSON Responses

Ruby/Rails - How to Combine Two JSON Responses

When you perform an Item Search on Amazon's Product Advertising API, it
returns very little information for each item result, and the response
does not include the URLs of the images. The image URLs must be returned
in a separate API call just for images sigh. So, the only way I think can
get item search results with image URLs in a single JSON response through
to my Backbone front-end is to perform two calls to the Amazon API in the
same controller method. A plain Item Search to retrieve the item
attributes, and the same Item Search with the 'Images' set as the
'Response Group'.
From the Item Search Images API Call, all I need is three image URLs
(small-med-large) for each product to be added to the JSON response of the
Item Search. Below are what both responses look like. The results are
returned in the same order. How can I do this in Ruby/Rails?
Plain Item Search API Call
{
items: [
{
links: [],
attrs: {
author: "J.K. Rowling",
creator: "Mary GrandPré",
manufacturer: "Scholastic",
product_group: "Book",
title: "Harry Potter and the Sorcerer's Stone (Book 1)"
},
image_sets: { },
editorial_reviews: [ ],
offers: [ ],
asin: "059035342X",
parent_asin: null,
detail_page_url: "http://rads.stackoverflow.com/amzn/click/059035342X"
},
{},
{},
{},
{},
{},
{},
{},
{},
{}
],
total_results: 115351,
total_pages: 11536,
valid: true,
operation_request: {},
more_search_results_url:
"http://www.amazon.com/gp/redirect.html?camp=2025&creative=386001&location=http%3A%2F%2Fwww.amazon.com%2Fgp%2Fsearch%3Fkeywords%3Dharry%26url%3Dsearch-alias%253Dstripbooks&linkCode=xm2&tag=shopperater08-20&SubscriptionId=AKIAIR66KAMMGUKSAKGA"
}
Item Search for Images API Call
{
items: [
{
links: [ ],
attrs: { },
image_sets: {},
editorial_reviews: [ ],
offers: [ ],
asin: "059035342X",
parent_asin: null,
detail_page_url: null,
small_image: {
width: 51,
height: 75,
url: "http://ecx.images-amazon.com/images/I/51MU5VilKpL._SL75_.jpg"
},
medium_image: {
width: 109,
height: 160,
url: "http://ecx.images-amazon.com/images/I/51MU5VilKpL._SL160_.jpg"
},
large_image: {
width: 340,
height: 500,
url: "http://ecx.images-amazon.com/images/I/51MU5VilKpL.jpg"
}
},
{},
{},
{},
{},
{},
{},
{},
{},
{}
],
total_results: 115351,
total_pages: 11536,
valid: true,
operation_request: {},
more_search_results_url:
"http://www.amazon.com/gp/redirect.html?camp=2025&creative=386001&location=http%3A%2F%2Fwww.amazon.com%2Fgp%2Fsearch%3Fkeywords%3Dharry%26url%3Dsearch-alias%253Dstripbooks&linkCode=xm2&tag=shopperater08-20&SubscriptionId=AKIAIR66KAMMGUKSAKGA"
}

No comments:

Post a Comment