Last active
November 22, 2022 08:12
-
-
Save redperadot/9788400 to your computer and use it in GitHub Desktop.
Encode and decode URI strings with Ruby.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run with: