PHP $ _GET Ruby equivalent

What is the minimum minimum that I need in Ruby (not Rails or any other framework) to easily get request data like GET?

I would like to avoid using rubies on rails or any other structure to get this data, so the ideal answer will not be structure dependent.

My current setup has a ruby ​​file ( script ) in cgi-bin on apache ( site.com/script )

 #!/usr/bin/env ruby # bare minimum to output content puts "Content-type: text/html" puts puts "it works!" #works fine. # how would I obtain request data like GET parameters here # for a url like site.com/script?hi=there # in PHP, I would $_GET['hi'] 
+4
source share
3 answers

And, I solved my problem using the CGI class.

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/cgi/rdoc/CGI.html

 require 'cgi' cgi = CGI.new cgi.params.each do |key, val| puts key puts val end 

therefore site.com/script?hi=there displays hi\nthere

+5
source

Please do not do this without a frame. There are several super-light ones, such as Sinatra , that do as little as possible to provide you with the support you need to get it right. CGI died in the 1990s and does not need to be returned.

To write a Sinatra application, you basically define a method and then deploy it to your server. Of course, you can grumble about how much work is available, but if you do not have the correct deployment procedure, you have problems before you even begin.

Ruby has a lot of infrastructure built around things like Rack that connect to Apache through modules like Passenger , which do much more than cgi-bin ever did without all the risks involved.

A typical Sinatra application looks like this:

 get '/example/:id' do "Example #{params[:id]}!" end 

Nothing really happens there, and the result is a lot less work than the old CGI method.

+5
source

If your web server calls the script as CGI , then the content will be in the environment QUERY_STRING accessible through the ENV object. Without a framework, you will have to parse this into separate name-value pairs to get something like PHP $_GET .

Since Ruby is a general-purpose programming language, it does not have basic classes and methods for handling HTTP requests or CGI. That is why it is recommended to use a framework such as Ruby on Rails to handle parsing and tuning at the protocol level. There are too many places to make mistakes, and you should not reinvent the wheel.

+2
source

Source: https://habr.com/ru/post/1439921/


All Articles