How to define array of hashes in Grape?
Asked Answered
L

2

8

I'm using Ember as my front-end and Grape API to serve my API.

The front-end send something like:

{
  "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"
          }
        ]
      }
    ]
  }
}

And I'm struggling to implement Grape api interface to receive the payload, I ended up with something like:

  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

But with no luck as I'm getting the following error:

ERROR -- service[availabilities][0][timeSlots][0][endtAt] is missing
Leibowitz answered 8/6, 2016 at 20:11 Comment(0)
Y
8

You can try to use "group" to indicate the type of your array elements.

Example (updated):

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
Yeasty answered 10/6, 2016 at 17:57 Comment(1)
and how to input in field say :name? how can I give it mutilple values?Zonnya
A
1

With the answer from @massinissa, I was able to get all of the following to work:

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

However, by default, the JQuery ajax function sends data as a query string in a format that Grape could not understand. So I had to set contentType = 'application/json' and use

`data = JSON.stringify({rules: [{rule_id: '1', rule_content: 'blah'}]})`
Arlenarlena answered 13/2, 2018 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.