Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby/rails equivalent to javascript decodeURIComponent?

I have some content (html) that is being encoded as a result of this javascript (from this page) and sent to my rails application:

function encode_utf8_b64(string) {
return window.btoa(unescape(encodeURIComponent(string)));
}

The correspond js code to get it back to original is this:

function decode_utf8_b64(string) {
return decodeURIComponent(escape(window.atob(string)));
}

My question is, is there an equivalent in ruby of decodeURIComponent()? So far I have this that gets it part of the way out, but I'm missing the last step of decodeURIComponent:

CGI::escape(Base64.decode64(string))
like image 413
bobfet1 Avatar asked Jun 23 '11 17:06

bobfet1


People also ask

What is difference between decodeURI and decodeURIComponent?

decodeURI(): It takes encodeURI(url) string as parameter and returns the decoded string. decodeURIComponent(): It takes encodeURIComponent(url) string as parameter and returns the decoded string.

What is Javascript decodeURIComponent?

The decodeURIComponent() function decodes a Uniform Resource Identifier (URI) component previously created by encodeURIComponent or by a similar routine.


1 Answers

URI.unescape could probably help:

def decode_utf8_b64(string)
  URI.unescape(CGI::escape(Base64.decode64(string)))
end

you have to add the necessary rubygem too:

require 'uri'

I've tested this on ruby 1.9.2.

like image 141
olistik Avatar answered Nov 15 '22 16:11

olistik