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))
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.
The decodeURIComponent() function decodes a Uniform Resource Identifier (URI) component previously created by encodeURIComponent or by a similar routine.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With