views:

90

answers:

2

I am trying to build a free web application using ruby/rails It should be able to send sms through online forms of various mobile operators. (like this one (in russian)).

So, I need to

  • wait for the user, who wants to send an sms through my website.
  • establish connection to operator website. Probably, using Mechanize.
  • retrieve captcha
  • show captcha to the user
  • allow user to enter a message and captcha
  • submit form on operators website (with message, captcha, phone number)

The connection to the operator website should be alive during all this process (otherwise captcha will change). As far as I understand, I need to create a (sub)process each time sms is sent. Could you please advise what is the best way of handling this in rails\ruby?

I am still rather new to web-development... Should I use threads? forks? popen? using PTY? some external gem? How should I communicate with my process?

+2  A: 

Assuming there's nothing special about the operator's web site, no, you don't need to keep a connection alive during the whole process. Generally speaking, forms on web pages work like this: You visit the URL, your web browser downloads the page with the form on it. In your case, it will also have an <img> tag or similar to show the CAPTCHA. Once your browser has downloaded the page, the connection is severed. After you fill out the form and click on Submit, your web browser opens a new connection to the server and sends the data, and the server sends its response (whatever page is shown after you click Submit).

All your program has to do is emulate this experience. So: 1) Download the page with the form on it. Scrape the form fields (make sure you don't miss any hidden fields--with a CAPTCHA there will probably be some) and the CAPTCHA. 2) Build a page to show your user that includes the CAPTCHA and a form with all the fields they need to fill out. If there were hidden fields in the original form, make sure you include their values (as hidden fields in your form) as well, because when the user submits your form you'll need them. 3) Then, when the user submits your form, send the data, including the hidden values and what the user entered for the CAPTCHA, to the operator. 4) Finally, check if the operator indicated success, and build a page to tell your user.

If you're doing this in Rails, you'll probably have two methods in your controller: One called e.g. 'show' (steps 1 and 2 above) that will scrape the CAPTCHA and other info from the operator's site and show the user your form view, and one called e.g. 'send' (step 3 and 4 above) that the form will submit to, and which will take their data and send it to the operator's web site, collect the response and tell your user if it was successful or not.

Note: You'll want to read the operators' terms of service before you bother with any of this. I'm fairly certain that this kind of thing will be against their TOSes and if they notice your server sending a lot of requests their way they're going to block you pretty quick.

Jordan
Exactly. You don't need to keep the connection opened, and even if you did it wouldn't make any difference with the CAPTCHA. Just make sure to submit all of the hidden fields along with the form.
mtyaka
OMG!Jordan, mtyaka - thanks a lot! Obviously, I like to overcomplicate things...Jordan - thank you for such a great answer! I've just found out that I don't have enough reputation (<15), so I can not add my vote to your answer, sorry. (but I will, once I get 15 rep)Thanks again.
Konstantin
+1  A: 

To answer another question of yours, you can use DRb or background_job (aka BJ) to actually accomplish the sending in the background so that after your user submits the captcha they don't have to wait for the response. Or you could wrap this in ajax and have the DRb/BJ process notify you when the sms sending has happened so you can notify the user of success or any problems.

Typically opening threads in Ruby is something to avoid as there are so many great gems that do what we need. Not to say that you shouldn't use threads, just that for the most part it's probably already been done really well.

Chuck Vose
Thanks for your tips!I will definitely use one of this plugins.
Konstantin