如何在Grape中定义哈希数组?

时间:2016-06-08 20:11:08

标签: ruby api rest grape-api

我使用Ember作为我的前端和Grape API来提供我的API。

前端发送类似:

{
  "service"=>{
    "name"=>"Name",
    "duration"=>"30",
    "user"=>nil,
    "organization"=>"org",
    "category"=>nil,
    "description"=>"description",
    "disabled"=>true,
    "color"=>nil,
    "availabilities"=>[
      {
        "day"=>"Saturday",
        "enabled"=>false,
        "timeSlots"=>[
          {
            "startAt"=>"09:00 AM",
            "endAt"=>"05:00 PM"
          }
        ]
      },
      {
        "day"=>"Sunday",
        "enabled"=>false,
        "timeSlots"=>[
          {
            "startAt"=>"09:00 AM",
            "endAt"=>"05:00 PM"
          }
        ]
      },
      {
        "day"=>"Monday",
        "enabled"=>true,
        "timeSlots"=>[
          {
            "startAt"=>"09:00 AM",
            "endAt"=>"05:00 PM"
          },
          {
            "startAt"=>"05:00 AM",
            "endAt"=>"09:00 PM"
          },
          {
            "startAt"=>"05:00 AM",
            "endAt"=>"09:00 PM"
          }
        ]
      },
      {
        "day"=>"Tuesday",
        "enabled"=>true,
        "timeSlots"=>[
          {
            "startAt"=>"09:00 AM",
            "endAt"=>"05:00 PM"
          }
        ]
      },
      {
        "day"=>"Wednesday",
        "enabled"=>true,
        "timeSlots"=>[
          {
            "startAt"=>"09:00 AM",
            "endAt"=>"05:00 PM"
          }
        ]
      },
      {
        "day"=>"Thursday",
        "enabled"=>true,
        "timeSlots"=>[
          {
            "startAt"=>"09:00 AM",
            "endAt"=>"05:00 PM"
          }
        ]
      },
      {
        "day"=>"Friday",
        "enabled"=>true,
        "timeSlots"=>[
          {
            "startAt"=>"09:00 AM",
            "endAt"=>"05:00 PM"
          }
        ]
      }
    ]
  }
}

我正在努力实现Grape api接口以接收有效负载,我最终得到了类似的内容:

  resource :services do
    params do
      requires :service, type: Hash do
        requires :name, type: String
        requires :organization, type: String
        requires :duration, type: Integer
        requires :category, type: String
        optional :color, type: String
        optional :description, type: String
        optional :disabled, type: Boolean
        requires :availabilities, type: Array do
          requires :day, type: String
          optional :enabled, type: Boolean
          optional :timeSlots, type: Array do
            requires :startAt, type: Time
            requires :endtAt, type: Time
          end
        end
      end
    end
    post do
      puts params
    end
  end

但是没有运气,因为我收到了以下错误:

ERROR -- service[availabilities][0][timeSlots][0][endtAt] is missing

2 个答案:

答案 0 :(得分:7)

您可以尝试使用“group”来指示数组元素的类型。

示例(更新):

params do
  group :services, type: Array, desc: "An array of services" do
    requires :name, type: String
    requires :organization, type: String
    requires :duration, type: Integer
    ...
  end
end
put '/test' do
  s = []
  params[:services].each do |service|
    s << service
  end
  return s
end

答案 1 :(得分:1)

通过@massinissa的回答,我能够完成以下所有工作:

params do
  group :rules, type: Array do
    requires :rule_id, type: String
    requires :rule_content, type: String
  end
end

params do
  requires :rules, type: Array do
    requires :rule_id, type: String
    requires :rule_content, type: String
  end
end

params do
  requires :rules, type: Array[Hash]
end

但是,默认情况下,JQuery ajax函数以Grape无法理解的格式将数据作为查询字符串发送。所以我必须设置contentType = 'application/json'并使用

`data = JSON.stringify({rules: [{rule_id: '1', rule_content: 'blah'}]})`