Url parsing in ruby

Hello,

I use ruby to make some scriptings inside an html page.
I made a form to pass variable in an URL.
I need this variable in my ruby code, how can i get this variable ?

my URL looks like http://localhost/mypage.rbx?var=nas002001

I need the value after var=

Thanks for your help.

Ydil

2010/3/3 John Y. [email protected]:

Thanks for your help.
Here’s one way to do it:

$ irb19 -r uri
irb(main):001:0> u = URI.parse
http://localhost/mypage.rbx?var=nas002001
=> #<URI::HTTP:0x1029adb0 URL:http://localhost/mypage.rbx?var=nas002001>
irb(main):002:0> v = u.query[/(?<=\A|&)var=([^&]*)/, 1]
=> “nas002001”

Note, error checking is missing from this example and it’s probably
also not very robust.

Cheers

robert

John Y. wrote:

my URL looks like http://localhost/mypage.rbx?var=nas002001

require ‘rubygems’
=> true

require ‘rack’
=> true

require ‘uri’
=> false

u = URI.parse(“http://localhost/mypage.rbx?var=nas002001”)
=> #<URI::HTTP:0x7ff33783be90
URL:http://localhost/mypage.rbx?var=nas002001>

Rack::Utils.parse_query(u.query)
=> {“var”=>“nas002001”}

Brian C. wrote:

John Y. wrote:

my URL looks like http://localhost/mypage.rbx?var=nas002001

require ‘rubygems’
=> true

require ‘rack’
=> true

require ‘uri’
=> false

u = URI.parse(“http://localhost/mypage.rbx?var=nas002001”)
=> #<URI::HTTP:0x7ff33783be90
URL:http://localhost/mypage.rbx?var=nas002001>

Rack::Utils.parse_query(u.query)
=> {“var”=>“nas002001”}

I knew URI.parse but to use it, you need to know the URL.
In my case, i need to parse the URL in the web browser,in the address
bar, not a given URL.
Or maybe there’s something a don’t understand about how to use it.

2010/3/4 John Y. [email protected]:

u = URI.parse(“http://localhost/mypage.rbx?var=nas002001”)
=> #<URI::HTTP:0x7ff33783be90
URL:http://localhost/mypage.rbx?var=nas002001>

Rack::Utils.parse_query(u.query)
=> {“var”=>“nas002001”}

I knew URI.parse but to use it, you need to know the URL.
In my case, i need to parse the URL in the web browser,in the address
bar, not a given URL.
Or maybe there’s something a don’t understand about how to use it.

Maybe we should start over with you describing what you are trying to
do. Your initial posting read like “I have an URL and need to extract
a particular part of it”. That’s what Brian and I have answered. I’m
afraid, you’ll probably have to provide more context or explain where
exactly you are stuck.

Kind regards

robert

2010/3/4 John Y. [email protected]:

Maybe we should start over with you describing what you are trying to
do. Your initial posting read like “I have an URL and need to extract
a particular part of it”. That’s what Brian and I have answered. I’m
afraid, you’ll probably have to provide more context or explain where
exactly you are stuck.

Ok,I’m coding a web page with Ruby.

And how do you interface with the web server? If you are using Cgi you
can use
http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/index.html

Kind regards

robert

Maybe we should start over with you describing what you are trying to
do. Your initial posting read like “I have an URL and need to extract
a particular part of it”. That’s what Brian and I have answered. I’m
afraid, you’ll probably have to provide more context or explain where
exactly you are stuck.

Kind regards

robert

Ok,I’m coding a web page with Ruby.
On the first page I have a form which send var to another page. This var
is send in the URL. (http://localhost/mypage.rbx?itf_name=nas001001)

So on my second page i need to check the URL of this page to retrieve
the variable in order to use it in my script. The var in my exemple is
nas001001.

Sorry if i wasn’t clear, i hope it’s better now.

Thanks again

2010/3/4 John Y. [email protected]:

And how do you interface with the web server? Â If you are using Cgi you
can use
http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/index.html

I think this lib is for generate html, I didn’t see method to get the
URL

This is not true. Please read the documentation I referred you to.

How it works :
I told my web server (apache) to use ruby (.rbx extension)

Barring any other information I have to assume that this is the CGI
interface.

HOST = ‘192.168.0.121’

Maybe I don’t have the good way of doing this but at least, it works, my
web pages are displayed.

Class CGI helps with creating valid HTML as well so I suggest you use
it. It’s just the proper tool for the job.

Kind regards

robert

Robert K. wrote:

2010/3/4 John Y. [email protected]:

And how do you interface with the web server? Â If you are using Cgi you
can use
http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/index.html

I think this lib is for generate html, I didn’t see method to get the
URL

This is not true. Please read the documentation I referred you to.

How it works :
I told my web server (apache) to use ruby (.rbx extension)

Barring any other information I have to assume that this is the CGI
interface.

HOST = ‘192.168.0.121’

Maybe I don’t have the good way of doing this but at least, it works, my
web pages are displayed.

Class CGI helps with creating valid HTML as well so I suggest you use
it. It’s just the proper tool for the job.

Kind regards

robert

I did it !

require “cgi”

cgi_request = CGI::new(“html4”)

puts “Content-Type: text/html; charset=UTF-8”
puts

itf = cgi_request[‘itf_name’]
puts itf

Thanks for your help

Regards

John

Robert K. wrote:

2010/3/4 John Y. [email protected]:

Maybe we should start over with you describing what you are trying to
do. �Your initial posting read like “I have an URL and need to extract
a particular part of it”. �That’s what Brian and I have answered. �I’m
afraid, you’ll probably have to provide more context or explain where
exactly you are stuck.

Ok,I’m coding a web page with Ruby.

And how do you interface with the web server? If you are using Cgi you
can use
http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/index.html

Kind regards

robert

I think this lib is for generate html, I didn’t see method to get the
URL

How it works :
I told my web server (apache) to use ruby (.rbx extension)
In my web page, I use ruby to write html which is interpreted by apache.
It looks like :
mywebpage.rbx

#!/usr/bin/env ruby

print “Content-type: text/html\r\n\r\n”
require ‘rubygems’
require ‘net/ssh’

HOST = ‘192.168.0.121’
USER = ‘root’
PASS = ‘xxxx’

Net::SSH.start( HOST, USER, :password => PASS ) do|ssh|
result = ssh.exec!(’/root/checknas.rb --list’)
print " Info "
print “{html stuff}”
print “”
end

Maybe I don’t have the good way of doing this but at least, it works, my
web pages are displayed.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs