Encoding only spaces and commas in URL - ruby-on-rails

I am trying to encode a url in rails for an image attachment, but using CGI::escape or URI.escape seems to encode everything. I just need the commas and spaces encoded, nothing else. How would I go about doing that in rails? I did gsub, but could only replace commas or spaces, not both. Is there a way to do both?
http://"URL"?operation=getfieldclip&outlinePoints=600%2C600%2C400%2CTest Area%2C44.982643%2C-94.696723%2C44.982343%2C-94.696723%2C44.982293%2C-94.697170%2C44.982293%2C-94.697555%2C44.982313%2C-94.697740%2C44.982333%2C-94.697987%2C44.982363%2C-94.698110%2C44.982403%2C-94.698233%2C44.982453%2C-94.698341%2C44.982493%2C-94.698511%2C44.982553
I can get the commas changed, but not sure how to get the space changed at the same time as well.

You should use CGI.escape. Don't use it for the entire URL, but only for the values of it.
require "cgi"
"http://URL?" + params.map{|k, v| "#{k}=#{CGI.escape(v)}"}.join("&")

Related

How to rewrite URLs split by hyphens?

I am getting confused while writing URLs with hyphens. It is conflicting with GET parameters.
For instance, I have a long book name in URL, with spaces replaced by hyphens, like the-famous-world-records-of-athletics. After this I am getting error in pagination also separated with hyphens.
Please suggest how I can write URLs in given stage:
example.com/vc.php?book=the-famous-world-records-of-athletics
example.com/vc.php?book=the-famous-world-records-of-athletics&page=1
example.com/vc.php?book=the-famous-world-records-of-athleticstopic=jumping-and-racing&page=2
Wishing to write as:
example.com/the-famous-world-records-of-athletics.html
example.com/the-famous-world-records-of-athletics-1.html
example.com/the-famous-world-records-of-athletics-jumping-and-racing-2.html
A minus is perfectly valid in an URL, it is a so-called 'unreserved' character.
https://en.wikipedia.org/wiki/Percent-encoding
If you really need to replace them, I'd replace them with %2D, just like you would replace a space with %20.

Clean up & style characters from text

I am getting text from a feed that has alot of characters like:
Insignia™ 2.0 Stereo Computer Speaker System (2-Piece) - Black
4th-Generation AppleĀ® iPodĀ® touch
Is there an easy way to get rid of these, or do I have to anticipate which characters I want to delete and use the delete method to remove them? Also, when I try to remove
&
with
str.delete("&")
It leaves behind "amp;" Is there a better way to delete this type of character? Do I need to re-encode the text?
String#delete is certainly not what you want, as it works on characters, not the string as a whole.
Try
str.gsub /&/, ""
You may also want to try replacing the & with a literal ampersand, such as:
str.gsub /&/, "&"
If this is closer to what you really want, you may get the best results unescaping the HTML string. If so try this:
CGI::unescapeHTML(str)
Details of the unescapeHTML method are here.
If you are getting data from a 'feed', aka RSS XML, then you should be using an XML parser like Nokogiri to process the XML. This will automatically unescape HTML entities and allow you to get the proper string representation directly.
For removing try to use gsub method, something like this:
text = "foo&bar"
text.gsub /\b&\b/, "" #=> foobar

how to pass URL with whitespace in Chrome and IE or include whitespace without encode?

I have a url to pass on my website that have whitespace. what thing i should done that chrome and IE never encode them. suppose
Mywebsite.com/search/ASP.NET MVC 2
IE and chrome fill whitespace with %20 how i can stop them to do this type of things.
You can replace the whitespace with "_" - this is a pretty normal case. But you will probably not be able to keep your spaces.
Whitespaces are not allowed in URLs. That is why they can automatically encoded by some browser if you call that page directly.
What's the problem with that encoding on the receiver side? Just decode the data or (if you are sure there were only whitespaces) just replace all %20 with a whitespace.

Passing fullstops (periods) and forward slashes in a GET request?

I have built a form that submits values to Wufoo as a GET request in the URL. I cannot get it to work if any of the values (in a textarea) contain a line-break or a forward slash. Is there a way to encode these in a URL?
This is being done in Rails.
I thought Rails would do that for you. But if you need to do it manually, you can use CGI::escape, e.g.
> require 'cgi'
...
> CGI.escape("hello%there\nworld")
=> "hello%25there%0Aworld"
EDIT:
Actually, CGI does not seem to escape a dot. URI can be used instead, it takes an extra parameter that lets you list extra characters you want escaped:
URI.escape("hello.there%world", ".")
http://en.wikipedia.org/wiki/Percent-encoding

slashes in url variables

I have set up my coldfusion application to have dynamic urls on the page, such as
www.musicExplained/index.cfm/artist/:VariableName
However my variable names will sometimes contain slashes, such as
www.musicExplained/index.cfm/artist/GZA/Genius
This is causing a problem, because my application presumes that the slash in the variable name represents a different section of the website, the artists albums. So the URL will fail.
I am wondering if there is anyway to prevent this from happening? Do I need to use a function that replaces slashes in the variable names with another character?
You need to escape the slashes as %2F.
You could easily replace the forward slashes / with something like an underscore _ such as Wikipedia uses for spaces. Replacing special characters with underscores, etc., is common practice.
You need to escape those but don't just replace it by %2F manually. You can use URLEncoder for this.
Eg URLEncoder.encode(url, "UTF-8")
Then you can say
yourUrl = "www.musicExplained/index.cfm/artist/" + URLEncoder.encode(VariableName, "UTF-8")
Check out this w3schools page about "HTML URL Encoding Reference":
https://www.w3schools.com/tags/ref_urlencode.asp
for / you would escape with %2F

Resources