Skip to content

Instantly share code, notes, and snippets.

@srinivasmohan
Created August 2, 2013 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srinivasmohan/6143115 to your computer and use it in GitHub Desktop.
Save srinivasmohan/6143115 to your computer and use it in GitHub Desktop.
Add to unsafe characters to force them to be encoded - Ruby 1.9.x URI.
[smohan@dhcpa-111 ~]$ irb
irb(main):001:0> require 'uri'
=> true
irb(main):002:0> p URI::Parser.new.regexp[:UNSAFE]
/[^\-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]]/
=> /[^\-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]]/
irb(main):003:0> p Regexp.union([URI::Parser.new.regexp[:UNSAFE],'~','@'])
/(?-mix:[^\-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]])|~|@/
=> /(?-mix:[^\-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]])|~|@/
irb(main):004:0>
irb(main):005:0*
irb(main):006:0*
irb(main):007:0*
irb(main):008:0* puts URI::encode("/touch/123~ipad.png")
/touch/123~ipad.png
=> nil
irb(main):009:0> puts URI::encode("/touch/123~ipad.png", Regexp.union([URI::Parser.new.regexp[:UNSAFE],'~','@']))
/touch/123%7Eipad.png
=> nil
irb(main):010:0> puts URI::encode("/touch/123@ipad.png")
/touch/123@ipad.png
=> nil
irb(main):011:0> puts URI::encode("/touch/123@ipad.png", Regexp.union([URI::Parser.new.regexp[:UNSAFE],'~','@']))
/touch/123%40ipad.png
=> nil
irb(main):012:0>
[smohan@dhcpa-111 ~]$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment