0

I have link like this:

http://localhost:3000/sms/receive/sms-id=7bb28e244189f2cf36cbebb9d1d4d02001da53ab&operator-%20id=1&from=37126300682&to=371144&text=RV9+c+Dace+Reituma+0580913

I want to extract all diferent variable values from this link. For example sms-id,operator,from, to and text.

So far I have like this:

routes.rb

get 'sms/receive/:params', to: 'sms#receive'

SMS#RECEIVE controller

def receive

     query = params[:params]

      sms_id=     query[/["="].+?[&]/]   

      flash[:notice] = sms_id

end

This gives me : =7bb28e244189f2cf36cbebb9d1d4d02001da53ab& but I need without the first = and last characher & .

If I try to add strings like :query[/["sms-id"].+?[&operator]/] that could allow me to extract all variables smoothly, it gives me error : empty range in char class: /["sms-id"].+?[&operator]/

But I believe there is other way to extract all these variable values in different way?

Thanks in advance!

Edgars
  • 913
  • 1
  • 19
  • 53

3 Answers3

1

You need

get 'sms/receive/', to: 'sms#receive' 

path in routes.rb and get params in the controller

Sergey Sokolov
  • 994
  • 7
  • 6
  • This gives me `No route matches [GET] "/sms/receive/sms-id=7bb28e244189f2cf36cbebb9d1d4d02001da53ab&operator-%20id=1&from=37126300682&to=371144&text=RV9+c+Dace+Reituma+0580913"` I tried this before... – Edgars Dec 14 '14 at 12:51
  • 1
    Can you change url to correct syntax: "/sms/receive?sms-id=7bb28e244189f2cf36cbebb9d1d4d02001da53ab&operator-%20id=1&f‌​rom=37126300682&to=371144&text=RV9+c+Dace+Reituma+0580913" (add ? instead of / before params)? – Sergey Sokolov Dec 14 '14 at 12:57
  • Nice one, I understand my problem now. I can access all params except sms-id and operator-id, because that line in midle should be in underscore ? – Edgars Dec 14 '14 at 13:07
  • Should probabluly note that query string (stuff after `?`) is **not** part of the route and should not be mentioned there. – D-side Dec 14 '14 at 13:10
1

The error in your regular expression is because the - is a reserved character when in-between square brackets. In this context, it must be escaped with a backslash: \-.

To parse your query string, you can do this:

sms_id = params[:params].match(/sms-id=([^&]*)/)[1]

or parse it with the more generic method:

parsed_query = Rack::Utils.parse_nested_query(params[:params])
sms_id = parsed_query['sms-id']

(quoted from this answer)

If you have control over the initial URL, change the last / for a ? for an even easier solution:

http://localhost:3000/sms/receive?sms-id=7bb28e244189f2cf36cbebb9d1d4d02001da53ab&operator-%20id=1&from=37126300682&to=371144&text=RV9+c+Dace+Reituma+0580913

and you will have sms-id in params:

sms_id = params['sms-id']
Community
  • 1
  • 1
Tiago
  • 365
  • 1
  • 8
0

Try this

matches = params[:params].scan(/(?:=)([\w\+]+)(?:\&)?/)
# this will make matches = [[first_match], [second_match], ..., [nth_match]]

# now you can read all matches

sms_id = matches[0][0]
operator_id = matches[1][0]
from = matches[2][0]
to = matches[3][0]
text = matches[4][0]
# and it will not contatin = or &

I suggest for you to make method in model or helper, and not to write whole code in controller.

Nermin
  • 6,118
  • 13
  • 23