On Sun, Jun 10, 2012 at 12:24 AM, rubix Rubix <[email protected]> wrote:
> Hi,
> I have an url: http://foo.com/posts?id=30&limit=éé#time=1305298413
> I am using uri class to parse it: URI::split(url)
> the problem is I need to encode it: URI.encode(url) first, which it
> gives
> http://foo.com/posts?id=30&limit=%C3%A9%C3%A9%23time=1305298413
> but with encoding it the fragment is lost and I can't
> perform the split function
>
> Can anyone help me to solve this problem
> regards,

The issue is that your original URI is not a valid URI since it
contains characters which are not allowed in the URI.  Basically you
need to encode parameters _before_ you put them in the URI to make it
a valid URI and have URI.split work successfully:

irb(main):044:0> u = URI.parse 'http://foo.com/posts'
=> #<URI::HTTP:0x956a280 URL:http://foo.com/posts>
irb(main):045:0> u.query =
{id:30,limit:'éé'}.map{|k,v|"#{k}=#{URI.encode(v.to_s)}"}.join '&'
=> "id=30&limit=%C3%A9%C3%A9"
irb(main):046:0> u.fragment='time=1305298413'
=> "time=1305298413"
irb(main):047:0> u
=> #<URI::HTTP:0x956a280
URL:http://foo.com/posts?id=30&limit=%C3%A9%C3%A9#time=1305298413>
irb(main):048:0> puts u
http://foo.com/posts?id=30&limit=%C3%A9%C3%A9#time=1305298413
=> nil

Kind regards

robert

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

-- You received this message because you are subscribed to the Google Groups 
ruby-talk-google group. To post to this group, send email to 
[email protected]. To unsubscribe from this group, send email 
to [email protected]. For more options, visit this 
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en

Reply via email to