URL Splitting

Hai
I am new to Ruby
now i have one situation
that,I need to get the url from database dependding upon user id
i have stored the url in database and also stored the parameter keys in
database
but my problem is ,i can get the url and parameter from database, but i
want to post the url from ruby Net/http api.i am using
Net::HTTP.post_form() method to post the Url,but my question is ,if the
url having the same type of parameter then there is no problem,if the
Url is not having the same type of parameter then how can i post the Url
dynamically?

Example:

  1. http://www.mob.com/add.asp?username=hussain&password=hussain
  2. http://www.zad.com/list.asp?username=hussain
  3. http://www.mob.com/add.asp?username=hussain&password=hussain&code=xxx

I have stored this data “http://www.mob.com/add.asp” in url field in DB
I have stored this data “username;password” in parameter field in DB
i want to post these three url dynamically from Ruby

can anybody tell me the answer?

Bye
By
P.S.Hussain

Hi,

On 4/21/07, hussain [email protected] wrote:

Url is not having the same type of parameter then how can i post the Url
i want to post these three url dynamically from Ruby

can anybody tell me the answer?

I don’t understand what you want.

Can you please rephrase or give some concrete example?

Hi Dude
Thank you for your reply

Actually
i have one table called “urls”

id url params

1 http://www.mob.com/add.asp username;password;
2 http://www.zad.com/add.asp username;password;code;
3 http://www.mob.com/add.asp username;

These are my table records

I want to get these url and parameters dynamically depending upon id

after i got the value
i will post the url by Net::HTTP.post_form() method

i can get the record from table but i cannot post the url dynamically
i want to post the url in a single block.

the username ,password and code are set dynamically

i will call only one function like
g.send(1,user,pass)
g.send(2,user,pass,code)
g.send(3,user)

these three function should call single method,on that method ,we should
post the url

this is my problem
can anybody tell me the solution?

ok
Bye
By
P.S.Hussain

On Behalf Of Mohamed H.:

1 http://www.mob.com/add.asp username;password;

2 http://www.zad.com/add.asp username;password;code;

3 http://www.mob.com/add.asp username;

something like this perhaps?

C:\family\ruby>cat test.rb

def build url,username,password=nil,code=nil
post = “#{url}?username=#{username}”
post << “&password=#{password}” if not (password.nil? or
password.empty?)
post << “&code=#{code}” if not (code.nil? or code.empty?)
post
end

a_test = [
http://www.mob.com/add.asp hussain;hussain”,
http://www.zad.com/list.asp hussain”,
http://www.mob.com/add.asp hussain;hussain;xxx”,
]

a_test.each do |e|
url,user_pass_code = e.split
user,pass,code = user_pass_code.split “;”
puts build(url,user,pass,code)
end

C:\family\ruby>ruby test.rb
http://www.mob.com/add.asp?username=hussain&password=hussain
http://www.zad.com/list.asp?username=hussain
http://www.mob.com/add.asp?username=hussain&password=hussain&code=xxx

C:\family\ruby>

hth.
kind regards -botp

Hi,

On 4/25/07, Mohamed H. [email protected] wrote:

3 http://www.mob.com/add.asp username;
i want to post the url in a single block.

this is my problem
can anybody tell me the solution?

This is still not clear.

Are you going to use post_form like this:

Net::HTTP.post_form(URI.parse(‘http://www.url.com’), { “user” =>
“hussain”, “pass” => “hussain” })

Or:

Net::HTTP.post_form(URI.parse(‘http://www.url.com?user=hussain&pass=hussain’))

Besides, if you are going to fetch the parameters dynamically (I’m not
sure what you mean by that) from the database, then shouldn’t the
g.send function receive only the id?

So it should be either

g.send(1)

or

g.send(url, user, pass)

Anyway, check this code:

require ‘uri’

def send(id, user, pass = nil, code = nil)
params = {}
params[“user”] = user
params[“pass”] = pass if pass
params[“code”] = code if code

query = params.map { |k,v| “%s=%s” % [URI.encode(k), URI.encode(v)]
}.join(“&”)

url = URI.parse(“http://www.url.com”)
url.query = query

p url.to_s
end

send(1, “foo”, “bar”)
send(2, “foo”, “bar”, “300”)
send(3, “foo”)

class G
def send( url, user, pass, code = nil )
begin
str = Url.find( url ).url + ‘?username=’ + user + ‘&password=’ +
pass.to_s
str += ‘&code=’ + code.to_s if code
rescue => e
if e.is_kind_of RecordNotFoundError

end
end
end
end

Is this?