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

···

--
Posted via http://www.ruby-forum.com/.

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&gt;
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

···

2010/3/3 John Ydil <john.gendrot@cnsi.fr>:

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.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

John Ydil 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&quot;\)

=> #<URI::HTTP:0x7ff33783be90
URL:http://localhost/mypage.rbx?var=nas002001&gt;

Rack::Utils.parse_query(u.query)

=> {"var"=>"nas002001"}

···

--
Posted via http://www.ruby-forum.com/\.

Brian Candler wrote:

John Ydil 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&quot;\)

=> #<URI::HTTP:0x7ff33783be90
URL:http://localhost/mypage.rbx?var=nas002001&gt;

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.

···

--
Posted via http://www.ruby-forum.com/\.

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 Ydil <john.gendrot@cnsi.fr>:

Brian Candler wrote:

John Ydil 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&quot;\)

=> #<URI::HTTP:0x7ff33783be90
URL:http://localhost/mypage.rbx?var=nas002001&gt;

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.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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

···

--
Posted via http://www.ruby-forum.com/\.

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

···

2010/3/4 John Ydil <john.gendrot@cnsi.fr>:

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.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert Klemme wrote:

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 "<html><body><title> Info </title>"
print "{html stuff}"
print "</body></html>"
end

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

···

2010/3/4 John Ydil <john.gendrot@cnsi.fr>:

--
Posted via http://www.ruby-forum.com/\.

Robert Klemme wrote:

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

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.

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 "<html><body><title> Info </title>"
print "{html stuff}"
print "</body></html>"
end

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

···

2010/3/4 John Ydil <john.gendrot@cnsi.fr>:

2010/3/4 John Ydil <john.gendrot@cnsi.fr>:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert Klemme wrote:

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

···

2010/3/4 John Ydil <john.gendrot@cnsi.fr>:

--
Posted via http://www.ruby-forum.com/\.