有没有更好的方法来处理数组响应Rails 4

时间:2016-01-27 18:18:32

标签: ruby-on-rails api httparty

我正在与Scalable Press API整合。我正在使用HTTparty来处理api响应。

示例请求:

curl "https://api.scalablepress.com/v2/products/gildan-sweatshirt-crew"

回复示例:

{
  "comments": "Generous fit. Soft, sturdy, easy to move around in, all the while looking good.",
  "description": "Air Jet Spun Yarn. Double-needle stitching. Set-in sleeves. 1x1 Athletic Rib with Lycra(R). Quarter-turned to 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"
}

这个反应并不算太糟糕,但有些产品有50种以上的颜色,每种颜色都有4张左右的颜色。

问题: 我希望使我的代码更好,更容易访问图像。有没有更好的方法来处理数组?

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

0 个答案:

没有答案