Alternate URI escaping mechanisms?

Discussion in 'Ruby' started by Davis Frank, Jan 16, 2007.

  1. Davis Frank

    Davis Frank Guest

    URI#escape escapes to UTF-8. I need to escape to Latin-1 for Amazon's
    REST API. Is there another library I can use? There doesn't seem to be
    a way to change what URI#escape uses (at least, not from the skeleton
    std API docs).

    I'm getting very little to go on via searching.

    --dwf

    --
    Posted via http://www.ruby-forum.com/.
     
    Davis Frank, Jan 16, 2007
    #1
    1. Advertising

  2. Davis Frank

    Alex Young Guest

    Davis Frank wrote:
    > URI#escape escapes to UTF-8. I need to escape to Latin-1 for Amazon's
    > REST API. Is there another library I can use? There doesn't seem to be
    > a way to change what URI#escape uses (at least, not from the skeleton
    > std API docs).
    >
    > I'm getting very little to go on via searching.


    Iconv may be what you're after...

    --
    Alex
     
    Alex Young, Jan 16, 2007
    #2
    1. Advertising

  3. Davis Frank

    Carlos Guest

    Davis Frank wrote:
    > URI#escape escapes to UTF-8. I need to escape to Latin-1 for Amazon's
    > REST API. Is there another library I can use? There doesn't seem to be
    > a way to change what URI#escape uses (at least, not from the skeleton
    > std API docs).
    >
    > I'm getting very little to go on via searching.


    URI#escape escapes to UTF-8 because your string is UTF-8-encoded. Use
    iconv, as Alex suggested, to re-encode it to Latin-1, or do it with
    unpack and pack
    str.unpack("U*").pack("C*")

    and then apply URI#escape.

    Of course, both methods will fail if any character in your string is
    outside the Latin-1 range.

    Good luck.
    --
     
    Carlos, Jan 16, 2007
    #3
  4. Davis Frank

    Carlos Guest

    Carlos wrote:
    >> URI#escape escapes to UTF-8. I need to escape to Latin-1 for Amazon's

    [...]
    > URI#escape escapes to UTF-8 because your string is UTF-8-encoded. Use

    [...]
    > and then apply URI#escape.


    Correction: it's not URI#escape but URI.escape.

    --
     
    Carlos, Jan 16, 2007
    #4
  5. Davis Frank

    Davis Frank Guest

    Carlos wrote:
    > Carlos wrote:
    >>> URI#escape escapes to UTF-8. I need to escape to Latin-1 for Amazon's

    > [...]
    >> URI#escape escapes to UTF-8 because your string is UTF-8-encoded. Use

    > [...]
    >> and then apply URI#escape.


    I found Iconv, but my head was bent the wrong way. I was escaping, then
    converting instead of converting, then escaping.

    The magic is here:
    URI.escape(Iconv.new('latin1', 'utf-8').iconv(value))

    Now everything is happy. Thanks for the quick response.

    --dwf


    --
    Posted via http://www.ruby-forum.com/.
     
    Davis Frank, Jan 17, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Harris
    Replies:
    0
    Views:
    6,489
    Simon Harris
    May 10, 2005
  2. Stanimir Stamenkov
    Replies:
    1
    Views:
    2,538
    Stanimir Stamenkov
    Aug 17, 2005
  3. Pavel
    Replies:
    2
    Views:
    1,732
    Peter Flynn
    Aug 4, 2004
  4. Chris
    Replies:
    2
    Views:
    800
    Chris
    Mar 4, 2007
  5. Replies:
    10
    Views:
    368
    Sean O'Halpin
    Jul 17, 2006
Loading...

Share This Page