api - Is there a better way to handle an Array response Rails 4 -
i integrating scalable press api. using httparty handle api response.
example request:
curl "https://api.scalablepress.com/v2/products/gildan-sweatshirt-crew"
example response:
{ "comments": "generous fit. soft, sturdy, easy move around in, while looking good.", "description": "air jet spun yarn. double-needle stitching. set-in sleeves. 1x1 athletic rib lycra(r). quarter-turned eliminate center crease.", "name": "gildan sweatshirt - crew", "type": "garment", "properties": { "brand": "gildan", "material": "7.75 oz 50% cotton, 50% polyester.", "style": "18000" }, "colors": [{ "name": "kiwi", "hex": "88b95d", "images": [{ "url": "http://i1.ooshirts.com/images/lab_shirts/kiwi-5-r.jpg", "label": "right" }, { "url": "http://i1.ooshirts.com/images/lab_shirts/kiwi-5-l.jpg", "label": "left" }, { "url": "http://i1.ooshirts.com/images/lab_shirts/kiwi-5-f.jpg", "label": "front" }, { "url": "http://i1.ooshirts.com/images/lab_shirts/kiwi-5-b.jpg", "label": "back" }], "sizes": [ "sml", "med", "lrg", "xlg", "xxl", "xxxl", "xxxxl", "xxxxxl" ] }, { "name": "irish green", "hex": "3da858", "images": [{ "url": "http://i1.ooshirts.com/images/lab_shirts/irish-green-5-f.jpg", "label": "front" }, { "url": "http://i1.ooshirts.com/images/lab_shirts/irish-green-5-l.jpg", "label": "left" }, { "url": "http://i1.ooshirts.com/images/lab_shirts/irish-green-5-r.jpg", "label": "right" }, { "url": "http://i1.ooshirts.com/images/lab_shirts/irish-green-5-b.jpg", "label": "back" }], "sizes": [ "sml", "med", "lrg", "xlg", "xxl", "xxxl", "xxxxl", "xxxxxl" ] }], "additionalimages": [{ "label": "front", "url": "http://i1.ooshirts.com/products/5/front.jpg" }, { "label": "back", "url": "http://i1.ooshirts.com/products/5/back.jpg" }, { "label": "collar", "url": "http://i1.ooshirts.com/products/5/collar.jpg" }], "image": { "label": "catalog", "url": "http://www.ooshirts.com/products/5/catalog.jpg" }, "available": true, "url": "https://api.scalablepress.com/v2/products/gildan-sweatshirt-crew", "availabilityurl": "https://api.scalablepress.com/v2/products/gildan-sweatshirt-crew/availability", "productid": "gildan-sweatshirt-crew" }
this response isn't bad, products have upwards of 50 colors , each color has around 4 images.
question: looking make code little better , easier access images. there better way handle array?
view: <%= image_tag @product['colors'].to_a[0].to_a[2].to_a[1].to_a[1].to_a[0].to_a[1] %>
controller: def show_product @product = scalable_press.show_product(params[:product]) end private def scalable_press scalablepress.new end
class scalablepress include httparty base_uri 'https://api.scalablepress.com' def initialize @options = { basic_auth: { password: 'apikey' }, verify: false} end def products_in_category(category) response = self.class.get("https://api.scalablepress.com/v2/categories/#{category}", @options) response["products"] end def categories response = self.class.get("https://api.scalablepress.com/v2/categories", @options) end def show_product(product) response = self.class.get("https://api.scalablepress.com/v2/products/#{product}", @options) # response["products"] end end
Comments
Post a Comment