Skip to content

Instantly share code, notes, and snippets.

@redperadot
Last active November 22, 2022 08:12
Show Gist options
  • Save redperadot/9788400 to your computer and use it in GitHub Desktop.
Save redperadot/9788400 to your computer and use it in GitHub Desktop.
Encode and decode URI strings with Ruby.
#!/usr/bin/env ruby
require 'uri'
pb = ( (/darwin/ =~ RUBY_PLATFORM) != nil ? true : false )
## Get Options
if ARGV[0] == "-h"
puts "#{$0} -e [STRING] (Encode String)"
puts "#{$0} -d [STRING] (Decode String)"
exit 0
end
decode = ( ARGV[0] == "-d" ? true : false )
encode = ( ARGV[0] == "-e" ? true : false )
encode = true if encode == false && decode == false
## Get String
string = ARGV[1]
string ||= `pbpaste` if pb
string ||= (print "Srting to manipulate: "; gets.chomp)
## Manipulate String
result = URI.decode_www_form_component(string).strip if decode
result = URI.encode_www_form_component(string).gsub("+", "%20").strip if encode
## Print Results To Screen And Pastboard
puts result
exec("echo '#{result}' | pbcopy") if pb
@redperadot
Copy link
Author

Run with:

ruby <( curl -fsSkL https://gist.github.com/redperadot/9788400/raw/uri.rb )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment