1

I want to create a route for the Grape gem so that a route accepts either an array of strings or a single string with a specific pre-defined value. From the documentation it's not clear how to do that.

Your suggestions?

UPDATE:

I want status to be passed either a single value status1 or as a array where values can be arbitrary and unknown. I combine these?

params do
  requires :status, type: Symbol, values: [:status1]
  requires :status, type: Array[String]
end
  • 1
    Be more specific. It's not clear what you really want to do. Do you want to handle multiple routes in a single method? – Marlon Jun 29 '16 at 13:09
  • @Marlon, I don't know how to explain that even more simple. –  Jun 29 '16 at 13:40
  • Give us an example. Add in your question the code you've written and that isn't working. Or add a code illustrating what you want to do. – Marlon Jun 29 '16 at 14:02
  • @Marlon, I don't think it's needed. –  Jun 29 '16 at 14:07
  • Okay. It's up to you. The way you stated your question, I can't understand your needs, therefore I'm unable to help you. – Marlon Jun 29 '16 at 14:19

1 Answers1

1

A parameter must be declared only once in the params block. If you declare it twice then only one will be used by Grape. In your case, there're two options to solve your problem.

First option: declare two parameters and define them as mutually exclusive. It means that the user will be able to inform only one of them.

params do
  requires :status1, type: Symbol, values: [:status1]
  requires :status2, type: Array[String]
  mutually_exclusive :status1, :status2
end

Second option: declare only one parameter and set its type to Object. In the method's body, check if it's an Array or a String. If it's a String, verify if it has the correct values.

params do
  requires :status, type: Object
end
get 'testing' do
    if params[:status].class.name.eql? "Array" then
    elsif params[:status].class.name.eql? "String" then
    end
end
Marlon
  • 888
  • 6
  • 12