# URL Splitting

**URL:** <https://www.ruby-forum.com/t/url-splitting/92629>\
**Category:** Ruby\
**Created:** [April 21, 2007, 8:44pm UTC](https://www.ruby-forum.com/t/url-splitting/92629 "2007-04-21T20:44:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 21, 2007, 8:44pm UTC](https://www.ruby-forum.com/t/url-splitting/92629/1 "2007-04-21T20:44:23Z")

</div>

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](http://www.mob.com/add.asp?username=hussain&password=hussain)
2. [http://www.zad.com/list.asp?username=hussain](http://www.zad.com/list.asp?username=hussain)
3. [http://www.mob.com/add.asp?username=hussain&password=hussain&code=xxx](http://www.mob.com/add.asp?username=hussain&password=hussain&code=xxx)

I have stored this data “[http://www.mob.com/add.asp](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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 24, 2007, 12:07pm UTC](https://www.ruby-forum.com/t/url-splitting/92629/2 "2007-04-24T12:07:06Z")

</div>

Hi,

On 4/21/07, hussain [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 25, 2007, 5:59am UTC](https://www.ruby-forum.com/t/url-splitting/92629/3 "2007-04-25T05:59:11Z")

</div>

Hi Dude  
Thank you for your reply

Actually  
i have one table called “urls”

id url params

1 [http://www.mob.com/add.asp](http://www.mob.com/add.asp) username;password;  
2 [http://www.zad.com/add.asp](http://www.zad.com/add.asp) username;password;code;  
3 [http://www.mob.com/add.asp](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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 25, 2007, 9:36am UTC](https://www.ruby-forum.com/t/url-splitting/92629/4 "2007-04-25T09:36:46Z")

</div>

On Behalf Of Mohamed H.:

# 1 [http://www.mob.com/add.asp](http://www.mob.com/add.asp) username;password;

# 2 [http://www.zad.com/add.asp](http://www.zad.com/add.asp) username;password;code;

# 3 [http://www.mob.com/add.asp](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](http://www.mob.com/add.asp) hussain;hussain”,  
“[http://www.zad.com/list.asp](http://www.zad.com/list.asp) hussain”,  
“[http://www.mob.com/add.asp](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.mob.com/add.asp?username=hussain&password=hussain)  
[http://www.zad.com/list.asp?username=hussain](http://www.zad.com/list.asp?username=hussain)  
[http://www.mob.com/add.asp?username=hussain&password=hussain&code=xxx](http://www.mob.com/add.asp?username=hussain&password=hussain&code=xxx)

C:\family\ruby\>

hth.  
kind regards -botp

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 25, 2007, 10:26am UTC](https://www.ruby-forum.com/t/url-splitting/92629/5 "2007-04-25T10:26:09Z")

</div>

Hi,

On 4/25/07, Mohamed H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 3 [http://www.mob.com/add.asp](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](http://www.url.com)’), { “user” =\>  
“hussain”, “pass” =\> “hussain” })

Or:

Net::HTTP.post\_form(URI.parse(‘[http://www.url.com?user=hussain&pass=hussain](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](http://www.url.com)”)  
url.query = query

p url.to\_s  
end

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 25, 2007, 9:25am UTC](https://www.ruby-forum.com/t/url-splitting/92629/6 "2007-04-25T09:25:45Z")

</div>

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?
