Reputation:
I have a Java REST API that looks like this:
GET /dashboard/group/{group}/class/{class}/view/{view_by}/utilizations
I want to write a client for it in Rails and also in Controller to access the values of its parameters, etc
So for example in /dashboard/group/group_elite/class/middle_class/view/all/utilizations
the values for parametrs group, class and view
are group_elite, middle_class and all
My question is how can I access those paramters and their values?
Upvotes: 0
Views: 44
Reputation: 54882
Okay let's say you do your thing in the controller to construct the URL to query, then you can use this to "parse and get the params":
url = '/dashboard/group/{group}/class/{class}/view/{view_by}/utilizations'
ar = url.split('/')
params_to_check = [ :group, :class, :view ]
params_h = {}
params_to_check.each do |param|
value_index = ar.index(param.to_s) + 1
params_h[param] = ar[value_index] if ar[value_index].present?
end
There is actually no need for a Regexp here. (You can do a copy-paste in your console and run puts params_h
, it should outputs what you want (hopefully!)
As you can see, this code implies that the parameter follows the attribute name: /group/{value}
, not like /group/smthing/{group}
Upvotes: 2