When testing a method that change embedded soundcloud-track urls to an iframe. I got a bug when escaping the result.
Can someone explain why escaping the +token+ in CASE 2 breaks? Its okay for me to use the result as is and append to it.
The two cases are shown in detail below.
#ruby --version
# => ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin12.2.1]
CASE 1
require 'cgi'
class String
def embedda
compiled = self
compiled = soundcloud_replace(compiled)
return compiled
end
private
def soundcloud_replace(compiled)
r = /(https?:\/\/(?:www.)?soundcloud.com\/[A-Za-z0-9]+(?:[-_][A-Za-z0-9]+)*(?!\/sets(?:\/|$))(?:\/[A-Za-z0-9]+(?:[-_][A-Za-z0-9]+)*){1,2}\/?)/i
compiled.gsub!(r) { |match| soundcloud_player(match) }
return compiled
end
def soundcloud_player(token)
# return token # => +matched soundcloud url+ in pry
# puts token # => +matched soundcloud url+ in pry
url_encoded_string = CGI::escape(token)
# puts url_encoded_string # => +escaped matched soundcloud url+
# "<iframe width=\"100%\" height=\"166\" scrolling=\"no\" frameborder=\"no\" src=\"https://w.soundcloud.com/player/?url=#{token}\"></iframe>"
# => Correct interpolated string with non-encoded url
"<iframe width=\"100%\" height=\"166\" scrolling=\"no\" frameborder=\"no\" src=\"https://w.soundcloud.com/player/?url=#{url_encoded_string}\"></iframe>"
# => Correct interpolated string with encoded url
end
end
CASE 2
require 'cgi'
class String
def embedda
compiled = self
compiled = soundcloud_replace(compiled)
return compiled
end
private
def soundcloud_replace(compiled)
compiled.gsub!(/(https?:\/\/(?:www.)?soundcloud.com\/[A-Za-z0-9]+(?:[-_][A-Za-z0-9]+)*(?!\/sets(?:\/|$))(?:\/[A-Za-z0-9]+(?:[-_][A-Za-z0-9]+)*){1,2}\/?)/i, soundcloud_player("\\1"))
return compiled
end
def soundcloud_player(token)
# return token # => +matched soundcloud url+ in pry
# puts token # => /1 in pry
url_encoded_string = CGI::escape(token)
# puts url_encoded_string = CGI::escape(token) # => %5C1 in pry
# "<iframe width=\"100%\" height=\"166\" scrolling=\"no\" frameborder=\"no\" src=\"https://w.soundcloud.com/player/?url=#{token}\"></iframe>"
# => Correct interpolated string with non-encoded url
"<iframe width=\"100%\" height=\"166\" scrolling=\"no\" frameborder=\"no\" src=\"https://w.soundcloud.com/player/?url=#{url_encoded_string}\"></iframe>"
# => Interpolated string with ...?url=%5C1
end
end