1

I had searched around but couldn't find something for it specifically. I was looking for a way to find the content of a URL (In this case these are URIs in a rest API)

A few examples of these look like: /currency/{currency-id} Or /contact-group/{ID}/member/{CONTACT-ID}

The parameters always can be different, however they always are between {}, in different forms within the string. I know how I can replace these when there is only one in the URI without issue, but at runtime the programmer won't know these, and I'm trying to prevent having to define them and because of this when URIs contain multiple parameters I'm not sure how to obtain each case of them.

Happy for any ideas on how to get around this!

2 Answers2

1

Seems like you're looking for a basic example of routing:

# in config/routes.rb
get "/:param_1/:param_2", to: "MyController#some_action"

Then in the controller you'd be able to get params[:param_1] and such.

You can see Rails' routing guide for more info

Maybe I'm not totally understanding your question, though. If you're looking to be able to capture a variable number of params, there's a special syntax for passing arrays in the query param.

See this: Passing array of parameters through get in rails

Community
  • 1
  • 1
max pleaner
  • 26,189
  • 9
  • 66
  • 118
  • Thanks for the reply! This would work but this is currently a ruby exclusive application. I may at some point use rails however at the moment I'm looking for a way to do this with just ruby – Rob Nicholas Nov 23 '16 at 05:43
  • @RobertNicholas It seems I wrongly assumed you'd be using Rails. Sinatra is more 'lightweight' framework that you can basically use this exact same syntax in. It's hard to find a more minimalist way to do it in Ruby. – max pleaner Nov 23 '16 at 05:45
  • Cheers! I'll give this a look, still open to any other suggestions if anyone does know of a ruby way with no libraries – Rob Nicholas Nov 23 '16 at 06:02
0

The answer to this was here

Basically using parameterset = url.scan(/{.+?}/) (replace url with your string name), and what's in scan with your parameter list, I can use this to do parameterset.each { |x| x.... etc}