Rails如何在我的帮助器中构建自定义json请求

时间:2014-03-09 06:41:15

标签: ruby-on-rails ruby json post httparty

我正在使用API​​,我正在尝试在帮助程序中发布以下请求

module ShoppingCartHelper

def update_order
  HTTParty.post("APIURL",
            :body => {
            @all_lines.each do |line|
            :ProductCode => line.ProductCode,
            :Quantity => line.Quantity,
            :UnitPrice => line.UnitPrice,
            :Pages =>
            [
                {
                    :PageNumber => line.PageNumber,
                    :Assets =>
                    [
                        { 
                            :AssetNumber => line.AssetNumber,
                            :Name => line.Name,
                            :HiResImage => line.HiResImage, 
                            :CropMode => line.CropMode
                        }
                    ]
                }
            ]
        end
        }.to_json,
      :headers => { 'Content-Type' => 'application/json' })
end

end

当我这样做时,我会遇到各种语法错误和意外字符。 @all_lines是数据库中订单的所有行,我正在尝试为它找到的每一行构建json主体..请帮助!。

我没有使用像jbuilder这样的东西,因为我正在使用的所有字段都在一个表中......它们没有链接到我可以包含它们的地方。除非有其他方法来创建自定义标签

更新:我需要这个结构才能工作,但我得到语法错误:

  HTTParty.post("APIURL",
            :body => 
            "Lines" =>
            [
            @all_lines.map do |line|
             {
               :ProductCode => line.ProductCode,
               :Quantity => line.Quantity,
               :UnitPrice => line.UnitPrice,
               :Pages =>
               [
                  {
                    :PageNumber => line.PageNumber,
                    :Assets =>
                    [
                      { 
                        :AssetNumber => line.AssetNumber,
                        :Name => line.Name,
                        :HiResImage => line.HiResImage, 
                        :CropMode => line.CropMode
                      }
                    ]
                  }
               ]
            } 
          end.to_json],
      :headers => { "Content-Type" => "application/json"})

API的Request主体需要如下所示:

"Lines":
    [
        {
            "ProductCode":"5x7",
            "Quantity":2,
            "UnitPrice":1.25,
            "Pages":
            [
                {
                    "PageNumber":1,
                    "Assets":
                    [
                        {
                            "AssetNumber":1,
                            "Name":"000-R20110419153212.jpg",
                            "HiResImage":"http:\/\/www.google.com\/xyz.jpg",
                            "CropMode":"FILLIN"
                        }
                    ]
                }
            ]
        }
    ]

1 个答案:

答案 0 :(得分:2)

each API返回数组本身(不做修改)。您可能希望使用map,它返回块结果的数组:

[1, 2, 3].each { |x| x*2 }
# => [1, 2, 3]
[1, 2, 3].map { |x| x*2 }
# => [2, 4, 6]

所以尝试(你也错过了你的块中哈希结果的花括号的位置):

def update_order
  HTTParty.post("APIURL",
            :body => 
            @all_lines.map do |line|
             {
               :ProductCode => line.ProductCode,
               :Quantity => line.Quantity,
               :UnitPrice => line.UnitPrice,
               :Pages =>
               [
                  {
                    :PageNumber => line.PageNumber,
                    :Assets =>
                    [
                      { 
                        :AssetNumber => line.AssetNumber,
                        :Name => line.Name,
                        :HiResImage => line.HiResImage, 
                        :CropMode => line.CropMode
                      }
                    ]
                  }
               ]
            } 
          end.to_json,
      :headers => { 'Content-Type' => 'application/json' })
end

使用'Lines'包装器:

HTTParty.post("APIURL",
            :body => 
            {"Lines" =>
            [
            @all_lines.map do |line|
             {
               :ProductCode => line.ProductCode,
               :Quantity => line.Quantity,
               :UnitPrice => line.UnitPrice,
               :Pages =>
               [
                  {
                    :PageNumber => line.PageNumber,
                    :Assets =>
                    [
                      { 
                        :AssetNumber => line.AssetNumber,
                        :Name => line.Name,
                        :HiResImage => line.HiResImage, 
                        :CropMode => line.CropMode
                      }
                    ]
                  }
               ]
            } 
          end]}.to_json,
      :headers => { "Content-Type" => "application/json" })